Skip to content
Snippets Groups Projects
Commit e84b6654 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

use to string composers

parent 01068e2d
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,11 @@
 
#include <exception/AlibException.h>
 
#include <alphabet/SymbolToStringComposer.h>
#include <regexp/RegExpToStringComposer.h>
#include <string/StringToStringComposer.h>
#include <label/LabelToStringComposer.h>
#include <set>
#include <map>
#include <list>
......@@ -30,11 +35,13 @@ void DotConverter::convert(const automaton::Automaton& a, std::ostream& out) {
a.getData().Accept((void*) &out, DotConverter::instance);
}
 
void DotConverter::convert(const automaton::UnknownAutomaton& a, std::ostream& out) {
// TODO
void DotConverter::convert(const automaton::UnknownAutomaton&, std::ostream&) {
throw exception::AlibException("Unknown automaton is not convertible to dot.");
}
 
void DotConverter::convert(const automaton::EpsilonNFA& a, std::ostream& out) {
label::LabelToStringComposer composer;
out << "digraph automaton {\n";
out << "rankdir=LR;\n";
int cnt = 1;
......@@ -47,13 +54,13 @@ void DotConverter::convert(const automaton::EpsilonNFA& a, std::ostream& out) {
 
//Print final states
for (const automaton::State& state : a.getFinalStates()) {
out << "node [shape = doublecircle, label=\"" << state.getName() << "\"]; " << states.find(state)->second << ";\n";
out << "node [shape = doublecircle, label=\"" << composer.compose(state.getName()) << "\"]; " << states.find(state)->second << ";\n";
}
 
//Print nonfinal states
for (const auto& state : states) {
if (!a.getFinalStates().count(state.first)) {
out << "node [shape = circle, label=\"" << state.first.getName() << "\" ]; " << state.second << ";\n";
out << "node [shape = circle, label=\"" << composer.compose(state.first.getName()) << "\" ]; " << state.second << ";\n";
}
}
 
......@@ -68,6 +75,8 @@ void DotConverter::convert(const automaton::EpsilonNFA& a, std::ostream& out) {
}
 
void DotConverter::convert(const automaton::NFA& a, std::ostream& out) {
label::LabelToStringComposer composer;
out << "digraph automaton {\n";
out << "rankdir=LR;\n";
int cnt = 1;
......@@ -80,13 +89,13 @@ void DotConverter::convert(const automaton::NFA& a, std::ostream& out) {
 
//Print final states
for (const automaton::State& state : a.getFinalStates()) {
out << "node [shape = doublecircle, label=\"" << state.getName() << "\"]; " << states.find(state)->second << ";\n";
out << "node [shape = doublecircle, label=\"" << composer.compose(state.getName()) << "\"]; " << states.find(state)->second << ";\n";
}
 
//Print nonfinal states
for (const auto& state : states) {
if (!a.getFinalStates().count(state.first)) {
out << "node [shape = circle, label=\"" << state.first.getName() << "\" ]; " << state.second << ";\n";
out << "node [shape = circle, label=\"" << composer.compose(state.first.getName()) << "\" ]; " << state.second << ";\n";
}
}
 
......@@ -101,6 +110,8 @@ void DotConverter::convert(const automaton::NFA& a, std::ostream& out) {
}
 
void DotConverter::convert(const automaton::DFA& a, std::ostream& out) {
label::LabelToStringComposer composer;
out << "digraph automaton {\n";
out << "rankdir=LR;\n";
int cnt = 1;
......@@ -113,13 +124,13 @@ void DotConverter::convert(const automaton::DFA& a, std::ostream& out) {
 
//Print final states
for (const automaton::State& state : a.getFinalStates()) {
out << "node [shape = doublecircle, label=\"" << state.getName() << "\"]; " << states.find(state)->second << ";\n";
out << "node [shape = doublecircle, label=\"" << composer.compose(state.getName()) << "\"]; " << states.find(state)->second << ";\n";
}
 
//Print nonfinal states
for (const auto& state : states) {
if (!a.getFinalStates().count(state.first)) {
out << "node [shape = circle, label=\"" << state.first.getName() << "\" ]; " << state.second << ";\n";
out << "node [shape = circle, label=\"" << composer.compose(state.first.getName()) << "\" ]; " << state.second << ";\n";
}
}
 
......@@ -132,6 +143,8 @@ void DotConverter::convert(const automaton::DFA& a, std::ostream& out) {
}
 
void DotConverter::convert(const automaton::ExtendedNFA& a, std::ostream& out) {
label::LabelToStringComposer composer;
out << "digraph automaton {\n";
out << "rankdir=LR;\n";
int cnt = 1;
......@@ -144,13 +157,13 @@ void DotConverter::convert(const automaton::ExtendedNFA& a, std::ostream& out) {
 
//Print final states
for (const automaton::State& state : a.getFinalStates()) {
out << "node [shape = doublecircle, label=\"" << state.getName() << "\"]; " << states.find(state)->second << ";\n";
out << "node [shape = doublecircle, label=\"" << composer.compose(state.getName()) << "\"]; " << states.find(state)->second << ";\n";
}
 
//Print nonfinal states
for (const auto& state : states) {
if (!a.getFinalStates().count(state.first)) {
out << "node [shape = circle, label=\"" << state.first.getName() << "\" ]; " << state.second << ";\n";
out << "node [shape = circle, label=\"" << composer.compose(state.first.getName()) << "\" ]; " << state.second << ";\n";
}
}
 
......@@ -165,6 +178,8 @@ void DotConverter::convert(const automaton::ExtendedNFA& a, std::ostream& out) {
}
 
void DotConverter::convert(const automaton::CompactNFA& a, std::ostream& out) {
label::LabelToStringComposer composer;
out << "digraph automaton {\n";
out << "rankdir=LR;\n";
int cnt = 1;
......@@ -177,13 +192,13 @@ void DotConverter::convert(const automaton::CompactNFA& a, std::ostream& out) {
 
//Print final states
for (const automaton::State& state : a.getFinalStates()) {
out << "node [shape = doublecircle, label=\"" << state.getName() << "\"]; " << states.find(state)->second << ";\n";
out << "node [shape = doublecircle, label=\"" << composer.compose(state.getName()) << "\"]; " << states.find(state)->second << ";\n";
}
 
//Print nonfinal states
for (const auto& state : states) {
if (!a.getFinalStates().count(state.first)) {
out << "node [shape = circle, label=\"" << state.first.getName() << "\" ]; " << state.second << ";\n";
out << "node [shape = circle, label=\"" << composer.compose(state.first.getName()) << "\" ]; " << state.second << ";\n";
}
}
 
......@@ -198,6 +213,8 @@ void DotConverter::convert(const automaton::CompactNFA& a, std::ostream& out) {
}
 
void DotConverter::convert(const automaton::NPDA& a, std::ostream& out) {
label::LabelToStringComposer composer;
out << "digraph automaton {\n";
out << "rankdir=LR;\n";
int cnt = 1;
......@@ -210,13 +227,13 @@ void DotConverter::convert(const automaton::NPDA& a, std::ostream& out) {
 
//Print final states
for (const automaton::State& state : a.getFinalStates()) {
out << "node [shape = doublecircle, label=\"" << state.getName() << "\"]; " << states.find(state)->second << ";\n";
out << "node [shape = doublecircle, label=\"" << composer.compose(state.getName()) << "\"]; " << states.find(state)->second << ";\n";
}
 
//Print nonfinal states
for (const auto& state : states) {
if (!a.getFinalStates().count(state.first)) {
out << "node [shape = circle, label=\"" << state.first.getName() << "\" ]; " << state.second << ";\n";
out << "node [shape = circle, label=\"" << composer.compose(state.first.getName()) << "\" ]; " << state.second << ";\n";
}
}
 
......@@ -231,6 +248,8 @@ void DotConverter::convert(const automaton::NPDA& a, std::ostream& out) {
}
 
void DotConverter::convert(const automaton::SinglePopNPDA& a, std::ostream& out) {
label::LabelToStringComposer composer;
out << "digraph automaton {\n";
out << "rankdir=LR;\n";
int cnt = 1;
......@@ -243,13 +262,13 @@ void DotConverter::convert(const automaton::SinglePopNPDA& a, std::ostream& out)
 
//Print final states
for (const automaton::State& state : a.getFinalStates()) {
out << "node [shape = doublecircle, label=\"" << state.getName() << "\"]; " << states.find(state)->second << ";\n";
out << "node [shape = doublecircle, label=\"" << composer.compose(state.getName()) << "\"]; " << states.find(state)->second << ";\n";
}
 
//Print nonfinal states
for (const auto& state : states) {
if (!a.getFinalStates().count(state.first)) {
out << "node [shape = circle, label=\"" << state.first.getName() << "\" ]; " << state.second << ";\n";
out << "node [shape = circle, label=\"" << composer.compose(state.first.getName()) << "\" ]; " << state.second << ";\n";
}
}
 
......@@ -264,6 +283,8 @@ void DotConverter::convert(const automaton::SinglePopNPDA& a, std::ostream& out)
}
 
void DotConverter::convert(const automaton::OneTapeDTM& a, std::ostream& out) {
label::LabelToStringComposer composer;
out << "digraph automaton {\n";
out << "rankdir=LR;\n";
int cnt = 1;
......@@ -276,13 +297,13 @@ void DotConverter::convert(const automaton::OneTapeDTM& a, std::ostream& out) {
 
//Print final states
for (const automaton::State& state : a.getFinalStates()) {
out << "node [shape = doublecircle, label=\"" << state.getName() << "\"]; " << states.find(state)->second << ";\n";
out << "node [shape = doublecircle, label=\"" << composer.compose(state.getName()) << "\"]; " << states.find(state)->second << ";\n";
}
 
//Print nonfinal states
for (const auto& state : states) {
if (!a.getFinalStates().count(state.first)) {
out << "node [shape = circle, label=\"" << state.first.getName() << "\" ]; " << state.second << ";\n";
out << "node [shape = circle, label=\"" << composer.compose(state.first.getName()) << "\" ]; " << state.second << ";\n";
}
}
 
......@@ -303,7 +324,8 @@ void DotConverter::transitions(const automaton::EpsilonNFA& fsm, const std::map<
if (transition.first.second.is<string::Epsilon>()) {
symbol = "&epsilon;";
} else {
symbol = (std::string) transition.first.second.get<alphabet::Symbol>();
alphabet::SymbolToStringComposer composer;
symbol = composer.compose(std::get<1>(transition.first).get<alphabet::Symbol>());
}
 
for(const automaton::State& to : transition.second) {
......@@ -313,7 +335,7 @@ void DotConverter::transitions(const automaton::EpsilonNFA& fsm, const std::map<
if (mapit == transitions.end()) {
transitions.insert(std::make_pair(key, symbol));
} else {
mapit->second += "," + symbol;
mapit->second += ", " + symbol;
}
}
}
......@@ -330,7 +352,8 @@ void DotConverter::transitions(const automaton::NFA& fsm, const std::map<automat
 
//put transitions from automaton to "transitions"
for (const auto& transition : fsm.getTransitions()) {
std::string symbol = (std::string) transition.first.second;
alphabet::SymbolToStringComposer composer;
std::string symbol = composer.compose(transition.first.second);
 
for(const automaton::State& to : transition.second) {
std::pair<int, int> key(states.find(transition.first.first)->second, states.find(to)->second);
......@@ -339,7 +362,7 @@ void DotConverter::transitions(const automaton::NFA& fsm, const std::map<automat
if (mapit == transitions.end()) {
transitions.insert(std::make_pair(key, symbol));
} else {
mapit->second += "," + symbol;
mapit->second += ", " + symbol;
}
}
}
......@@ -356,7 +379,8 @@ void DotConverter::transitions(const automaton::DFA& fsm, const std::map<automat
 
//put transitions from automaton to "transitions"
for (const auto& transition : fsm.getTransitions()) {
std::string symbol = (std::string) transition.first.second;
alphabet::SymbolToStringComposer composer;
std::string symbol = composer.compose(transition.first.second);
 
std::pair<int, int> key(states.find(transition.first.first)->second, states.find(transition.second)->second);
std::map<std::pair<int, int>, std::string>::iterator mapit = transitions.find(key);
......@@ -364,7 +388,7 @@ void DotConverter::transitions(const automaton::DFA& fsm, const std::map<automat
if (mapit == transitions.end()) {
transitions.insert(std::make_pair(key, symbol));
} else {
mapit->second += "," + symbol;
mapit->second += ", " + symbol;
}
}
 
......@@ -376,11 +400,57 @@ void DotConverter::transitions(const automaton::DFA& fsm, const std::map<automat
}
 
void DotConverter::transitions(const automaton::ExtendedNFA& fsm, const std::map<automaton::State, int>& states, std::ostream& out) {
// TODO
std::map<std::pair<int, int>, std::string> transitions;
//put transitions from automaton to "transitions"
for (const auto& transition : fsm.getTransitions()) {
regexp::RegExpToStringComposer composer;
std::string symbol = composer.compose(transition.first.second);
for(const automaton::State& to : transition.second) {
std::pair<int, int> key(states.find(transition.first.first)->second, states.find(to)->second);
std::map<std::pair<int, int>, std::string>::iterator mapit = transitions.find(key);
if (mapit == transitions.end()) {
transitions.insert(std::make_pair(key, symbol));
} else {
mapit->second += ", " + symbol;
}
}
}
//print the map
for (const std::pair<std::pair<int, int>, std::string> transition : transitions) {
out << transition.first.first << " -> " << transition.first.second;
out << "[label=\"" << transition.second << "\"]\n";
}
}
 
void DotConverter::transitions(const automaton::CompactNFA& fsm, const std::map<automaton::State, int>& states, std::ostream& out) {
// TODO
std::map<std::pair<int, int>, std::string> transitions;
//put transitions from automaton to "transitions"
for (const auto& transition : fsm.getTransitions()) {
string::StringToStringComposer composer;
std::string symbol = composer.compose(string::String { transition.first.second } );
for(const automaton::State& to : transition.second) {
std::pair<int, int> key(states.find(transition.first.first)->second, states.find(to)->second);
std::map<std::pair<int, int>, std::string>::iterator mapit = transitions.find(key);
if (mapit == transitions.end()) {
transitions.insert(std::make_pair(key, symbol));
} else {
mapit->second += ", " + symbol;
}
}
}
//print the map
for (const std::pair<std::pair<int, int>, std::string> transition : transitions) {
out << transition.first.first << " -> " << transition.first.second;
out << "[label=\"" << transition.second << "\"]\n";
}
}
 
void DotConverter::transitions(const automaton::NPDA& pda, const std::map<automaton::State, int>& states, std::ostream& out) {
......@@ -393,7 +463,8 @@ void DotConverter::transitions(const automaton::NPDA& pda, const std::map<automa
if (std::get<1>(transition.first).is<string::Epsilon>()) {
symbol = "&epsilon;";
} else {
symbol = std::get<1>(transition.first).get<alphabet::Symbol>();
alphabet::SymbolToStringComposer composer;
symbol = composer.compose(std::get<1>(transition.first).get<alphabet::Symbol>());
}
 
symbol += " |";
......@@ -403,7 +474,8 @@ void DotConverter::transitions(const automaton::NPDA& pda, const std::map<automa
symbol += " &epsilon;";
} else {
for (alphabet::Symbol symb : std::get<2>(transition.first)) {
symbol += " " + (std::string) symb;
alphabet::SymbolToStringComposer composer;
symbol += " " + composer.compose(symb);
}
 
}
......@@ -416,7 +488,8 @@ void DotConverter::transitions(const automaton::NPDA& pda, const std::map<automa
symbol += " &epsilon;";
} else {
for (alphabet::Symbol symb : to.second) {
symbol += " " + (std::string) symb;
alphabet::SymbolToStringComposer composer;
symbol += " " + composer.compose(symb);
}
 
}
......@@ -450,13 +523,15 @@ void DotConverter::transitions(const automaton::SinglePopNPDA& pda, const std::m
if (std::get<1>(transition.first).is<string::Epsilon>()) {
symbol = "&epsilon;";
} else {
symbol = std::get<1>(transition.first).get<alphabet::Symbol>();
alphabet::SymbolToStringComposer composer;
symbol = composer.compose(std::get<1>(transition.first).get<alphabet::Symbol>());
}
 
symbol += " |";
 
//Pop part
symbol += " " + (std::string) std::get<2>(transition.first);
alphabet::SymbolToStringComposer composer;
symbol += " " + composer.compose(std::get<2>(transition.first));
 
symbol += " ->";
 
......@@ -466,7 +541,8 @@ void DotConverter::transitions(const automaton::SinglePopNPDA& pda, const std::m
symbol += " &epsilon;";
} else {
for (alphabet::Symbol symb : to.second) {
symbol += " " + (std::string) symb;
alphabet::SymbolToStringComposer composer;
symbol += " " + composer.compose(symb);
}
 
}
......@@ -493,13 +569,15 @@ void DotConverter::transitions(const automaton::SinglePopNPDA& pda, const std::m
void DotConverter::transitions(const automaton::OneTapeDTM& tm, const std::map<automaton::State, int>& states, std::ostream& out) {
std::map<std::pair<int, int>, std::string> transitions;
for (const auto& transition : tm.getTransitions()) {
alphabet::SymbolToStringComposer composer;
std::string symbol;
 
//input symbol
symbol = "(";
symbol += transition.first.second;
symbol += composer.compose(transition.first.second);
symbol += ", ";
symbol += std::get<1>(transition.second);
symbol += composer.compose(std::get<1>(transition.second));
symbol += " ";
switch(std::get<2>(transition.second)) {
case automaton::Shift::LEFT:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment