From feda8a50dad69089d33e38ab739748f22a743196 Mon Sep 17 00:00:00 2001
From: Martin Zak <zakmart1@fit.cvut.cz>
Date: Tue, 12 Nov 2013 21:12:49 +0100
Subject: [PATCH] Implements UnknownAutomaton.toXML method, adds const to some
 UnknonwAutomaton methods

---
 acat/src/acat.cpp                       |  2 +-
 alib/src/automaton/AutomatonPrinter.cpp | 70 +++++++++++++++++++++++++
 alib/src/automaton/AutomatonPrinter.h   |  4 ++
 alib/src/automaton/UnknownAutomaton.cpp | 20 +++----
 alib/src/automaton/UnknownAutomaton.h   |  8 +--
 5 files changed, 89 insertions(+), 15 deletions(-)

diff --git a/acat/src/acat.cpp b/acat/src/acat.cpp
index b93e7b3084..866baceed8 100644
--- a/acat/src/acat.cpp
+++ b/acat/src/acat.cpp
@@ -47,7 +47,7 @@ int main(int argc, char** argv) {
 			UnknownAutomaton unknownAutomaton = AutomatonParser::parse(tokens);
 			Automaton* automaton = AutomatonFactory::buildAutomaton(
 					&unknownAutomaton);
-			automaton->toXML(cout);
+			unknownAutomaton.toXML(cout);
 		} else if (tokens.front().getData() == "grammar") {
 			UnknownGrammar grammar = GrammarParser::parse(tokens);
 			grammar.toXML(cout);
diff --git a/alib/src/automaton/AutomatonPrinter.cpp b/alib/src/automaton/AutomatonPrinter.cpp
index 8ee7593f5c..6515cffdba 100644
--- a/alib/src/automaton/AutomatonPrinter.cpp
+++ b/alib/src/automaton/AutomatonPrinter.cpp
@@ -27,6 +27,37 @@ void AutomatonPrinter::printAlphabet(const set<Symbol>& alphabet, ostream& out,
 	out << prefix << "</" << tagName << ">\n";
 }
 
+void AutomatonPrinter::printUnknownTransitions(const set<UnknownTransition>& transitions, ostream& out, string prefix) {
+	string contentPrefix = prefix + INDENTATION + INDENTATION;
+
+	out << prefix << "<transitions>\n";
+	for (auto transition : transitions) {
+		out << prefix << INDENTATION << "<transition>\n";
+		printState(transition.getFrom(), out, "from", contentPrefix);
+		printSymbol(transition.getInput(), out, "input", contentPrefix);
+		printState(transition.getTo(), out, "to", contentPrefix);
+
+		if (transition.getPop().size() > 0) {
+			printSymbolList(transition.getPop(), out, "pop", contentPrefix);
+		}
+		if (transition.getPush().size() > 0) {
+			printSymbolList(transition.getPush(), out, "push", contentPrefix);
+		}
+
+		if (transition.getOutput().getSymbol() != "") {
+			printSymbol(transition.getOutput(), out, "output", contentPrefix);
+		}
+
+		if (transition.getShift() != Shift::NOT_SET) {
+			printShift(transition.getShift(), out, "shift", contentPrefix);
+		}
+
+		out << prefix << INDENTATION << "</transition>\n";
+	}
+
+	out << prefix << "</transitions>\n";
+}
+
 void AutomatonPrinter::printTransitionsFSM(const set<TransitionFSM>& transitions, ostream& out, string prefix) {
 	string contentPrefix = prefix + INDENTATION + INDENTATION;
 
@@ -99,6 +130,45 @@ void AutomatonPrinter::printShift(const Shift& shift, ostream& out, string tagNa
 	out << "</" << tagName << ">\n";
 }
 
+void AutomatonPrinter::toXML(const UnknownAutomaton& automaton, ostream& out) {
+	out << "<automaton>\n";
+
+	if (automaton.getStates().size() > 0) {
+		printStates(automaton.getStates(), out, "states", INDENTATION);
+	}
+
+	if (automaton.getInputAlphabet().size() > 0) {
+		printAlphabet(automaton.getInputAlphabet(), out, "inputAlphabet", INDENTATION);
+	}
+	if (automaton.getTapeAlphabet().size() > 0) {
+		printAlphabet(automaton.getTapeAlphabet(), out, "tapeAlphabet", INDENTATION);
+	}
+	if (automaton.getStackAlphabet().size() > 0) {
+		printAlphabet(automaton.getStackAlphabet(), out, "stackAlphabet", INDENTATION);
+	}
+
+	if (automaton.getTransitions().size() > 0) {
+		printUnknownTransitions(automaton.getTransitions(), out, INDENTATION);
+	}
+
+	if (automaton.getBlankSymbol().getSymbol() != "") {
+		printSymbol(automaton.getBlankSymbol(), out, "blankSymbol", INDENTATION);
+	}
+	if (automaton.getStartSymbols().size() > 0) {
+		printSymbolList(automaton.getStartSymbols(), out, "startSymbols", INDENTATION);
+	}
+
+	if (automaton.getInitialStates().size() > 0) {
+		printStates(automaton.getInitialStates(), out, "initialStates", INDENTATION);
+	}
+	if (automaton.getFinalStates().size() > 0) {
+		printStates(automaton.getFinalStates(), out, "finalStates", INDENTATION);
+	}
+
+	out << "</automaton>\n";
+
+}
+
 void AutomatonPrinter::toXML(const FSM& automaton, ostream& out) {
 	out << "<automaton>\n";
 	printStates(automaton.getStates(), out, "states", INDENTATION);
diff --git a/alib/src/automaton/AutomatonPrinter.h b/alib/src/automaton/AutomatonPrinter.h
index cb3cfd59ba..a15e2ee5fa 100644
--- a/alib/src/automaton/AutomatonPrinter.h
+++ b/alib/src/automaton/AutomatonPrinter.h
@@ -9,6 +9,7 @@
 #define AUTOMATONPRINTER_H_
 
 #include <ostream>
+#include "UnknownAutomaton.h"
 #include "FSM/FSM.h"
 #include "PDA/PDA.h"
 #include "TM/TM.h"
@@ -26,16 +27,19 @@ protected:
 	static void printStates(const set<State>& states, ostream& out, string tagName, string prefix);
 	static void printAlphabet(const set<Symbol>& alphabet, ostream& out, string tagName, string prefix);
 
+	static void printUnknownTransitions(const set<UnknownTransition>& transitions, ostream& out, string prefix);
 	static void printTransitionsFSM(const set<TransitionFSM>& transitions, ostream& out, string prefix);
 	static void printTransitionsPDA(const set<TransitionPDA>& transitions, ostream& out, string prefix);
 	static void printTransitionsTM(const set<TransitionTM>& transitions, ostream& out, string prefix);
 
+
 	static void printState(const State& state, ostream& out, string tagName, string prefix);
 	static void printSymbol(const Symbol& symbol, ostream& out, string tagName, string prefix);
 	static void printSymbolList(const list<Symbol>& symbols, ostream& out, string tagName, string prefix);
 	static void printShift(const Shift& shift, ostream& out, string tagName, string prefix);
 
 public:
+	static void toXML(const UnknownAutomaton& automaton, ostream& out);
 	static void toXML(const FSM& automaton, ostream& out);
 	static void toXML(const PDA& automaton, ostream& out);
 	static void toXML(const TM& automaton, ostream& out);
diff --git a/alib/src/automaton/UnknownAutomaton.cpp b/alib/src/automaton/UnknownAutomaton.cpp
index 791fbb80d1..ea394d6d98 100644
--- a/alib/src/automaton/UnknownAutomaton.cpp
+++ b/alib/src/automaton/UnknownAutomaton.cpp
@@ -7,6 +7,7 @@
 
 #include "UnknownAutomaton.h"
 
+#include "AutomatonPrinter.h"
 #include "exception/AutomatonException.h"
 
 namespace automaton {
@@ -39,12 +40,12 @@ void UnknownAutomaton::removeFinalState(const State& state) {
 		throw AutomatonException("Final state \"" + state.getName() + "\" doesn't exist.");
 }
 
-const set<Symbol>& UnknownAutomaton::getStackAlphabet() {
+const set<Symbol>& UnknownAutomaton::getStackAlphabet() const {
 	return stackAlphabet;
 }
 
 void UnknownAutomaton::addStackSymbol(const Symbol& symbol) {
-	if(!stackAlphabet.insert(symbol).second) {
+	if (!stackAlphabet.insert(symbol).second) {
 		throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" already exists.");
 	}
 }
@@ -54,7 +55,7 @@ void UnknownAutomaton::removeStackSymbol(const Symbol& symbol) {
 		throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" doesn't exist.");
 }
 
-const list<Symbol>& UnknownAutomaton::getStartSymbols() {
+const list<Symbol>& UnknownAutomaton::getStartSymbols() const {
 	return startSymbols;
 }
 
@@ -62,12 +63,12 @@ void UnknownAutomaton::setStartSymbols(const list<Symbol>& newSymbols) {
 	this->startSymbols = newSymbols;
 }
 
-const set<Symbol>& UnknownAutomaton::getTapeAlphabet() {
+const set<Symbol>& UnknownAutomaton::getTapeAlphabet() const {
 	return tapeAlphabet;
 }
 
 void UnknownAutomaton::addTapeSymbol(const Symbol& symbol) {
-	if(!tapeAlphabet.insert(symbol).second) {
+	if (!tapeAlphabet.insert(symbol).second) {
 		throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" already exists.");
 	}
 }
@@ -79,7 +80,7 @@ void UnknownAutomaton::removeTapeSymbol(const Symbol& symbol) {
 	}
 }
 
-const Symbol& UnknownAutomaton::getBlankSymbol() {
+const Symbol& UnknownAutomaton::getBlankSymbol() const {
 	return blankSymbol;
 }
 
@@ -92,20 +93,19 @@ const set<UnknownTransition>& UnknownAutomaton::getTransitions() const {
 }
 
 void UnknownAutomaton::addTransition(const UnknownTransition& transition) {
-	if(!transitions.insert(transition).second) {
+	if (!transitions.insert(transition).second) {
 		throw AutomatonException("Transition already exists.");
 	}
 }
 
 void UnknownAutomaton::removeTransition(const UnknownTransition& transition) {
-	if(!transitions.erase(transition)) {
+	if (!transitions.erase(transition)) {
 		throw AutomatonException("Transition doesn't exist.");
 	}
 }
 
 void UnknownAutomaton::toXML(ostream& out) const {
-	//TODO Martin Zak: implement this
-	throw AutomatonException("Not yet implemented");
+	AutomatonPrinter::toXML(*this, out);
 }
 
 } /* namespace automaton */
diff --git a/alib/src/automaton/UnknownAutomaton.h b/alib/src/automaton/UnknownAutomaton.h
index 24a5820f4a..7f820ceec5 100644
--- a/alib/src/automaton/UnknownAutomaton.h
+++ b/alib/src/automaton/UnknownAutomaton.h
@@ -45,18 +45,18 @@ public:
 	void removeInitialState(const State& state);
 	void removeFinalState(const State& state);
 
-	const set<Symbol>& getStackAlphabet();
+	const set<Symbol>& getStackAlphabet() const;
 	void addStackSymbol(const Symbol& symbol);
 	void removeStackSymbol(const Symbol& symbol);
 
-	const list<Symbol>& getStartSymbols();
+	const list<Symbol>& getStartSymbols() const;
 	void setStartSymbols(const list<Symbol>& newSymbols);
 
-	const set<Symbol>& getTapeAlphabet();
+	const set<Symbol>& getTapeAlphabet() const;
 	void addTapeSymbol(const Symbol& symbol);
 	void removeTapeSymbol(const Symbol& symbol);
 
-	const Symbol& getBlankSymbol();
+	const Symbol& getBlankSymbol() const;
 	void setBlankSymbol(const Symbol& symbol);
 
 	const set<UnknownTransition>& getTransitions() const;
-- 
GitLab