Skip to content
Snippets Groups Projects
ExactSubtreeMatchingAutomaton.cpp 3.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * ExactSubtreeMatchingAutomaton.cpp
     *
     *  Created on: 9. 2. 2014
     *      Author: Jan Travnicek
     */
    
    #include "ExactSubtreeMatchingAutomaton.h"
    #include <exception/AlibException.h>
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    #include <tree/ranked/PrefixRankedTree.h>
    
    
    #include <deque>
    
    namespace arbology {
    
    namespace exact {
    
    automaton::Automaton ExactSubtreeMatchingAutomaton::construct(const tree::Tree& pattern) {
    	automaton::Automaton* out = NULL;
    	pattern.getData().Accept((void*) &out, ExactSubtreeMatchingAutomaton::EXACT_SUBTREE_MATCHING_AUTOMATON);
    	automaton::Automaton res = std::move(*out);
    	delete out;
    	return res;
    }
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    automaton::InputDrivenNPDA ExactSubtreeMatchingAutomaton::construct(const tree::PrefixRankedTree& pattern) {
    
    	automaton::InputDrivenNPDA res(automaton::State(0), alphabet::symbolFrom('S'));
    
    	for(const alphabet::RankedSymbol& symbol : pattern.getAlphabet()) {
    		res.addInputSymbol(alphabet::Symbol{symbol});
    		res.setPushdownStoreOperation(alphabet::Symbol{symbol}, std::vector<alphabet::Symbol>{1, alphabet::symbolFrom('S')}, std::vector<alphabet::Symbol>{symbol.getRank().getData(), alphabet::symbolFrom('S')});
    	}
    
    	for(const alphabet::RankedSymbol& symbol : pattern.getAlphabet()) {
    		res.addTransition(automaton::State(0), alphabet::Symbol{symbol}, automaton::State(0));
    	}
    	int i = 1;
    	for(const alphabet::RankedSymbol& symbol : pattern.getContent()) {
    		res.addState(automaton::State(i));
    		res.addTransition(automaton::State(i-1), alphabet::Symbol{symbol}, automaton::State(i));
    		i++;
    	}
    	res.addFinalState(automaton::State(i-1));
    	return res;
    }
    
    void ExactSubtreeMatchingAutomaton::Visit(void*, const tree::RankedTree&) const {
    	throw exception::AlibException("Unsupported tree type RankedTree");
    }
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    void ExactSubtreeMatchingAutomaton::Visit(void* data, const tree::PrefixRankedTree& pattern) const {
    
    	automaton::Automaton* & out = *((automaton::Automaton**) data);
    	out = new automaton::Automaton(this->construct(pattern));
    }
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    void ExactSubtreeMatchingAutomaton::Visit(void*, const tree::PrefixRankedBarTree&) const {
    	throw exception::AlibException("Unsupported tree type PrefixRankedBarTree");
    }
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    void ExactSubtreeMatchingAutomaton::Visit(void*, const tree::RankedPattern&) const {
    	throw exception::AlibException("Unsupported tree type RankedPattern");
    }
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    void ExactSubtreeMatchingAutomaton::Visit(void*, const tree::PrefixRankedPattern&) const {
    	throw exception::AlibException("Unsupported tree type PrefixRankedPattern");
    }
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    void ExactSubtreeMatchingAutomaton::Visit(void*, const tree::PrefixRankedBarPattern&) const {
    	throw exception::AlibException("Unsupported tree type PrefixRankedBarPattern");
    }
    
    
    void ExactSubtreeMatchingAutomaton::Visit(void*, const tree::UnrankedTree&) const {
    	throw exception::AlibException("Unsupported tree type UnrankedTree");
    }
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    void ExactSubtreeMatchingAutomaton::Visit(void*, const tree::UnrankedPattern&) const {
    	throw exception::AlibException("Unsupported tree type UnrankedPattern");
    }
    
    
    const ExactSubtreeMatchingAutomaton ExactSubtreeMatchingAutomaton::EXACT_SUBTREE_MATCHING_AUTOMATON;
    
    } /* namespace exact */
    
    } /* namespace arbology */