From 8ee8189917ae6b026bbdcb92e8d7b8983c06118a Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Sat, 24 Oct 2015 13:19:08 +0200
Subject: [PATCH] +TreePatternAutomaton,TreePatternMatchingAutomaton

---
 aarbology2/src/aarbology.cpp                  | 28 ++++++++
 .../arbology/exact/ExactSubtreeAutomaton.cpp  | 53 +++++++++++++++
 .../arbology/exact/ExactSubtreeAutomaton.h    | 41 +++++++++++
 .../exact/ExactTreePatternAutomaton.cpp       | 68 +++++++++++++++++++
 .../exact/ExactTreePatternAutomaton.h         | 41 +++++++++++
 5 files changed, 231 insertions(+)
 create mode 100644 alib2algo/src/arbology/exact/ExactSubtreeAutomaton.cpp
 create mode 100644 alib2algo/src/arbology/exact/ExactSubtreeAutomaton.h
 create mode 100644 alib2algo/src/arbology/exact/ExactTreePatternAutomaton.cpp
 create mode 100644 alib2algo/src/arbology/exact/ExactTreePatternAutomaton.h

diff --git a/aarbology2/src/aarbology.cpp b/aarbology2/src/aarbology.cpp
index f5aec491a2..3202948a49 100644
--- a/aarbology2/src/aarbology.cpp
+++ b/aarbology2/src/aarbology.cpp
@@ -20,6 +20,8 @@
 #include <arbology/exact/KnuthMorrisPratt.h>
 #include <arbology/exact/ExactSubtreeMatchingAutomaton.h>
 #include <arbology/exact/ExactPatternMatchingAutomaton.h>
+#include <arbology/exact/ExactSubtreeAutomaton.h>
+#include <arbology/exact/ExactTreePatternAutomaton.h>
 #include <chrono>
 
 int main ( int argc, char * argv[] ) {
@@ -33,6 +35,8 @@ int main ( int argc, char * argv[] ) {
 		allowed.push_back ( "knuthMorrisPratt" );
 		allowed.push_back ( "exactSubtreeMatchingAutomaton" );
 		allowed.push_back ( "exactPatternMatchingAutomaton" );
+		allowed.push_back ( "exactSubtreeAutomaton" );
+		allowed.push_back ( "exactTreePatternAutomaton" );
 		TCLAP::ValuesConstraint < std::string > allowedVals ( allowed );
 
 		TCLAP::ValueArg < std::string > algorithm ( "a", "algorithm", "Execute algorithm", false, "exactSubtreeMatch", & allowedVals );
@@ -178,6 +182,30 @@ int main ( int argc, char * argv[] ) {
 			std::chrono::measurements::end ( );
 			std::chrono::measurements::start ( "Output write", std::chrono::measurements::Type::AUXILARY );
 
+			alib::XmlDataFactory::toStdout ( res );
+		} else if ( algorithm.getValue ( ) == "exactSubtreeAutomaton" ) {
+			tree::Tree subject = alib::XmlDataFactory::fromTokens < tree::Tree > ( subjectTokens.front ( ) );
+
+			std::chrono::measurements::end ( );
+			std::chrono::measurements::start ( "Algorithm", std::chrono::measurements::Type::MAIN );
+
+			automaton::Automaton res = arbology::exact::ExactSubtreeAutomaton::construct ( subject );
+
+			std::chrono::measurements::end ( );
+			std::chrono::measurements::start ( "Output write", std::chrono::measurements::Type::AUXILARY );
+
+			alib::XmlDataFactory::toStdout ( res );
+		} else if ( algorithm.getValue ( ) == "exactTreePatternAutomaton" ) {
+			tree::Tree subject = alib::XmlDataFactory::fromTokens < tree::Tree > ( subjectTokens.front ( ) );
+
+			std::chrono::measurements::end ( );
+			std::chrono::measurements::start ( "Algorithm", std::chrono::measurements::Type::MAIN );
+
+			automaton::Automaton res = arbology::exact::ExactTreePatternAutomaton::construct ( subject );
+
+			std::chrono::measurements::end ( );
+			std::chrono::measurements::start ( "Output write", std::chrono::measurements::Type::AUXILARY );
+
 			alib::XmlDataFactory::toStdout ( res );
 		} else {
 			throw exception::AlibException ( "Invalid algorithm" );
diff --git a/alib2algo/src/arbology/exact/ExactSubtreeAutomaton.cpp b/alib2algo/src/arbology/exact/ExactSubtreeAutomaton.cpp
new file mode 100644
index 0000000000..ac61147cb5
--- /dev/null
+++ b/alib2algo/src/arbology/exact/ExactSubtreeAutomaton.cpp
@@ -0,0 +1,53 @@
+/*
+ * ExactSubtreeAutomaton.cpp
+ *
+ *  Created on: 7. 4. 2015
+ *      Author: Jan Travnicek
+ */
+
+#include "ExactSubtreeAutomaton.h"
+#include <exception/AlibException.h>
+#include <tree/Tree.h>
+#include <tree/ranked/PrefixRankedTree.h>
+#include <automaton/Automaton.h>
+#include <automaton/PDA/InputDrivenNPDA.h>
+
+#include <deque>
+
+namespace arbology {
+
+namespace exact {
+
+automaton::Automaton ExactSubtreeAutomaton::construct ( const tree::Tree & text ) {
+	return getInstance ( ).dispatch ( text.getData ( ) );
+}
+
+automaton::InputDrivenNPDA ExactSubtreeAutomaton::construct ( const tree::PrefixRankedTree & tree ) {
+	alphabet::Symbol S = alphabet::symbolFrom ( 'S' );
+	automaton::InputDrivenNPDA res ( automaton::State ( 0 ), S );
+
+	for ( const alphabet::RankedSymbol & rankedSymbol : tree.getAlphabet ( ) ) {
+		alphabet::Symbol symbol ( rankedSymbol );
+		res.addInputSymbol ( symbol );
+		res.setPushdownStoreOperation ( symbol, std::vector < alphabet::Symbol > { 1, S }, std::vector < alphabet::Symbol > { rankedSymbol.getRank ( ).getData ( ), S } );
+	}
+
+	int i = 1;
+
+	for ( const alphabet::RankedSymbol & rankedSymbol : tree.getContent ( ) ) {
+		alphabet::Symbol symbol ( rankedSymbol );
+
+		res.addState ( automaton::State ( i ) );
+		res.addTransition ( automaton::State ( i - 1 ), symbol, automaton::State ( i ) );
+		res.addTransition ( automaton::State ( 0 ), std::move ( symbol ), automaton::State ( i ) );
+		i++;
+	}
+
+	return res;
+}
+
+auto ExactSubtreeAutomatonPrefixRankedTree = ExactSubtreeAutomaton::RegistratorWrapper < automaton::InputDrivenNPDA, tree::PrefixRankedTree > ( ExactSubtreeAutomaton::getInstance ( ), ExactSubtreeAutomaton::construct );
+
+} /* namespace exact */
+
+} /* namespace arbology */
diff --git a/alib2algo/src/arbology/exact/ExactSubtreeAutomaton.h b/alib2algo/src/arbology/exact/ExactSubtreeAutomaton.h
new file mode 100644
index 0000000000..c8df1ca7ba
--- /dev/null
+++ b/alib2algo/src/arbology/exact/ExactSubtreeAutomaton.h
@@ -0,0 +1,41 @@
+/*
+ * ExactSubtreeAutomaton.h
+ *
+ *  Created on: 7. 4. 2015
+ *      Author: Jan Travnicek
+ */
+
+#ifndef _EXACT_SUBTREE_AUTOMATON_H__
+#define _EXACT_SUBTREE_AUTOMATON_H__
+
+#include <automaton/AutomatonFeatures.h>
+#include <tree/TreeFeatures.h>
+#include <common/multipleDispatch.hpp>
+
+namespace arbology {
+
+namespace exact {
+
+class ExactSubtreeAutomaton : public std::SingleDispatch < automaton::Automaton, tree::TreeBase > {
+public:
+	/**
+	 * Performs conversion.
+	 * @return left regular grammar equivalent to source automaton.
+	 */
+	static automaton::Automaton construct ( const tree::Tree & tree );
+
+	static automaton::InputDrivenNPDA construct ( const tree::PrefixRankedTree & tree );
+
+	static ExactSubtreeAutomaton & getInstance ( ) {
+		static ExactSubtreeAutomaton res;
+
+		return res;
+	}
+
+};
+
+} /* namespace exact */
+
+} /* namespace arbology */
+
+#endif /* _EXACT_SUBTREE_AUTOMATON_H__ */
diff --git a/alib2algo/src/arbology/exact/ExactTreePatternAutomaton.cpp b/alib2algo/src/arbology/exact/ExactTreePatternAutomaton.cpp
new file mode 100644
index 0000000000..28c5f60226
--- /dev/null
+++ b/alib2algo/src/arbology/exact/ExactTreePatternAutomaton.cpp
@@ -0,0 +1,68 @@
+/*
+ * ExactTreePatternAutomaton.cpp
+ *
+ *  Created on: 7. 4. 2015
+ *      Author: Jan Travnicek
+ */
+
+#include "ExactTreePatternAutomaton.h"
+#include <exception/AlibException.h>
+#include <alphabet/SubtreeWildcardSymbol.h>
+#include <tree/Tree.h>
+#include <tree/ranked/PrefixRankedTree.h>
+#include <automaton/Automaton.h>
+#include <automaton/PDA/InputDrivenNPDA.h>
+
+#include <deque>
+
+namespace arbology {
+
+namespace exact {
+
+automaton::Automaton ExactTreePatternAutomaton::construct ( const tree::Tree & text ) {
+	return getInstance ( ).dispatch ( text.getData ( ) );
+}
+
+automaton::InputDrivenNPDA ExactTreePatternAutomaton::construct ( const tree::PrefixRankedTree & tree ) {
+	alphabet::Symbol S = alphabet::symbolFrom ( 'S' );
+	alphabet::Symbol subtreeWildcard ( alphabet::SubtreeWildcardSymbol::SUBTREE_WILDCARD );
+	automaton::InputDrivenNPDA res ( automaton::State ( 0 ), S );
+
+	for ( const alphabet::RankedSymbol & rankedSymbol : tree.getAlphabet ( ) ) {
+		alphabet::Symbol symbol ( rankedSymbol );
+		res.addInputSymbol ( symbol );
+		res.setPushdownStoreOperation ( symbol, std::vector < alphabet::Symbol > { 1, S }, std::vector < alphabet::Symbol > { rankedSymbol.getRank ( ).getData ( ), S } );
+	}
+
+	res.addInputSymbol ( subtreeWildcard );
+	res.setPushdownStoreOperation ( subtreeWildcard, std::vector < alphabet::Symbol > { 1, S }, std::vector < alphabet::Symbol > { } );
+
+	int i = 1;
+	std::deque < std::pair < unsigned, int > > subtreeJumps;
+
+	for ( const alphabet::RankedSymbol & rankedSymbol : tree.getContent ( ) ) {
+		alphabet::Symbol symbol ( rankedSymbol );
+		subtreeJumps.push_back ( std::make_pair ( rankedSymbol.getRank ( ).getData ( ), i - 1 ) );
+
+		res.addState ( automaton::State ( i ) );
+		res.addTransition ( automaton::State ( i - 1 ), symbol, automaton::State ( i ) );
+		res.addTransition ( automaton::State ( 0 ), std::move ( symbol ), automaton::State ( i ) );
+
+		while ( subtreeJumps.size ( ) && subtreeJumps.back ( ).first == 0 ) {
+			res.addTransition ( automaton::State ( subtreeJumps.back ( ).second ), subtreeWildcard, automaton::State ( i ) );
+			subtreeJumps.pop_back ( );
+		}
+
+		if ( subtreeJumps.size ( ) ) subtreeJumps.back ( ).first--;
+
+		i++;
+	}
+
+	return res;
+}
+
+auto ExactTreePatternAutomatonPrefixRankedTree = ExactTreePatternAutomaton::RegistratorWrapper < automaton::InputDrivenNPDA, tree::PrefixRankedTree > ( ExactTreePatternAutomaton::getInstance ( ), ExactTreePatternAutomaton::construct );
+
+} /* namespace exact */
+
+} /* namespace arbology */
diff --git a/alib2algo/src/arbology/exact/ExactTreePatternAutomaton.h b/alib2algo/src/arbology/exact/ExactTreePatternAutomaton.h
new file mode 100644
index 0000000000..b90c0eca7b
--- /dev/null
+++ b/alib2algo/src/arbology/exact/ExactTreePatternAutomaton.h
@@ -0,0 +1,41 @@
+/*
+ * ExactTreePatternAutomaton.h
+ *
+ *  Created on: 7. 4. 2015
+ *      Author: Jan Travnicek
+ */
+
+#ifndef _EXACT_TREE_PATTERN_AUTOMATON_H__
+#define _EXACT_TREE_PATTERN_AUTOMATON_H__
+
+#include <automaton/AutomatonFeatures.h>
+#include <tree/TreeFeatures.h>
+#include <common/multipleDispatch.hpp>
+
+namespace arbology {
+
+namespace exact {
+
+class ExactTreePatternAutomaton : public std::SingleDispatch < automaton::Automaton, tree::TreeBase > {
+public:
+	/**
+	 * Performs conversion.
+	 * @return left regular grammar equivalent to source automaton.
+	 */
+	static automaton::Automaton construct ( const tree::Tree & tree );
+
+	static automaton::InputDrivenNPDA construct ( const tree::PrefixRankedTree & tree );
+
+	static ExactTreePatternAutomaton & getInstance ( ) {
+		static ExactTreePatternAutomaton res;
+
+		return res;
+	}
+
+};
+
+} /* namespace exact */
+
+} /* namespace arbology */
+
+#endif /* _EXACT_TREE_PATTERN_AUTOMATON_H__ */
-- 
GitLab