From af9faf1a9c00eb0bc69fcb6b40cd913fb2a20cee Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Wed, 20 May 2015 12:30:48 +0200 Subject: [PATCH] +ExactSubtreeMatchingAutomaton --- .../exact/ExactSubtreeMatchingAutomaton.cpp | 64 +++++++++++++++++++ .../exact/ExactSubtreeMatchingAutomaton.h | 40 ++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 alib2algo/src/arbology/exact/ExactSubtreeMatchingAutomaton.cpp create mode 100644 alib2algo/src/arbology/exact/ExactSubtreeMatchingAutomaton.h diff --git a/alib2algo/src/arbology/exact/ExactSubtreeMatchingAutomaton.cpp b/alib2algo/src/arbology/exact/ExactSubtreeMatchingAutomaton.cpp new file mode 100644 index 0000000000..eab632d6db --- /dev/null +++ b/alib2algo/src/arbology/exact/ExactSubtreeMatchingAutomaton.cpp @@ -0,0 +1,64 @@ +/* + * ExactSubtreeMatchingAutomaton.cpp + * + * Created on: 9. 2. 2014 + * Author: Jan Travnicek + */ + +#include "ExactSubtreeMatchingAutomaton.h" +#include <exception/AlibException.h> +#include <tree/ranked/PrefixRankedNotation.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; +} + +automaton::InputDrivenNPDA ExactSubtreeMatchingAutomaton::construct(const tree::PrefixRankedNotation& 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"); +} + +void ExactSubtreeMatchingAutomaton::Visit(void* data, const tree::PrefixRankedNotation& pattern) const { + automaton::Automaton* & out = *((automaton::Automaton**) data); + out = new automaton::Automaton(this->construct(pattern)); +} + +void ExactSubtreeMatchingAutomaton::Visit(void*, const tree::UnrankedTree&) const { + throw exception::AlibException("Unsupported tree type UnrankedTree"); +} + +const ExactSubtreeMatchingAutomaton ExactSubtreeMatchingAutomaton::EXACT_SUBTREE_MATCHING_AUTOMATON; + +} /* namespace exact */ + +} /* namespace arbology */ diff --git a/alib2algo/src/arbology/exact/ExactSubtreeMatchingAutomaton.h b/alib2algo/src/arbology/exact/ExactSubtreeMatchingAutomaton.h new file mode 100644 index 0000000000..79d3904783 --- /dev/null +++ b/alib2algo/src/arbology/exact/ExactSubtreeMatchingAutomaton.h @@ -0,0 +1,40 @@ +/* + * ExactSubtreeMatchingAutomaton.h + * + * Created on: 9. 2. 2014 + * Author: Jan Travnicek + */ + +#ifndef _EXACT_SUBTREE_MATCHING_AUTOMATON_H__ +#define _EXACT_SUBTREE_MATCHING_AUTOMATON_H__ + +#include <automaton/Automaton.h> +#include <automaton/PDA/InputDrivenNPDA.h> +#include <tree/Tree.h> + +namespace arbology { + +namespace exact { + +class ExactSubtreeMatchingAutomaton : public tree::VisitableTreeBase::const_visitor_type { +public: + /** + * Performs conversion. + * @return left regular grammar equivalent to source automaton. + */ + static automaton::Automaton construct(const tree::Tree& pattern); + + static automaton::InputDrivenNPDA construct(const tree::PrefixRankedNotation& pattern); +private: + void Visit(void*, const tree::RankedTree& pattern) const; + void Visit(void*, const tree::PrefixRankedNotation& pattern) const; + void Visit(void*, const tree::UnrankedTree& pattern) const; + + static const ExactSubtreeMatchingAutomaton EXACT_SUBTREE_MATCHING_AUTOMATON; +}; + +} /* namespace exact */ + +} /* namespace arbology */ + +#endif /* _EXACT_SUBTREE_MATCHING_AUTOMATON_H__ */ -- GitLab