From 0ba5887385e716d6916e64da5cff18b1eedc8d65 Mon Sep 17 00:00:00 2001 From: Honza <honza@Pinguino.zoo> Date: Sun, 8 Dec 2013 13:13:19 +0100 Subject: [PATCH] feature: print state transition symbol to ostream --- alib/src/alphabet/Symbol.cpp | 6 +++++ alib/src/alphabet/Symbol.h | 2 ++ alib/src/automaton/FSM/TransitionFSM.cpp | 8 +++++++ alib/src/automaton/FSM/TransitionFSM.h | 2 ++ alib/src/automaton/PDA/TransitionPDA.cpp | 27 +++++++++++++++++++++++ alib/src/automaton/PDA/TransitionPDA.h | 2 ++ alib/src/automaton/Shift.cpp | 10 +++++++++ alib/src/automaton/Shift.h | 4 ++++ alib/src/automaton/State.cpp | 5 +++++ alib/src/automaton/State.h | 2 ++ alib/src/automaton/TM/TransitionTM.cpp | 10 +++++++++ alib/src/automaton/TM/TransitionTM.h | 2 ++ alib/src/automaton/Transition.cpp | 10 +++++++++ alib/src/automaton/Transition.h | 4 ++++ alib/src/automaton/UnknownTransition.cpp | 28 ++++++++++++++++++++++++ alib/src/automaton/UnknownTransition.h | 2 ++ 16 files changed, 124 insertions(+) create mode 100644 alib/src/automaton/Shift.cpp diff --git a/alib/src/alphabet/Symbol.cpp b/alib/src/alphabet/Symbol.cpp index c81332c3e5..c03d42d206 100644 --- a/alib/src/alphabet/Symbol.cpp +++ b/alib/src/alphabet/Symbol.cpp @@ -29,4 +29,10 @@ bool Symbol::operator !=(const Symbol& other) const { return symbol != other.symbol; } +std::ostream& operator<<(std::ostream& out, const Symbol& symbol) { + + out << "Symbol " << (symbol.symbol == "" ? "\\epsilon" : symbol.symbol); + return out; +} + } /* namespace language */ diff --git a/alib/src/alphabet/Symbol.h b/alib/src/alphabet/Symbol.h index 1d2f01faeb..a49c107b60 100644 --- a/alib/src/alphabet/Symbol.h +++ b/alib/src/alphabet/Symbol.h @@ -34,6 +34,8 @@ public: bool operator <(const Symbol& other) const; bool operator ==(const Symbol& other) const; bool operator !=(const Symbol& other) const; + + friend std::ostream& operator<<(std::ostream&, const Symbol&); }; } #endif /* SYMBOL_H_ */ diff --git a/alib/src/automaton/FSM/TransitionFSM.cpp b/alib/src/automaton/FSM/TransitionFSM.cpp index 950496a955..fff1c43a7b 100644 --- a/alib/src/automaton/FSM/TransitionFSM.cpp +++ b/alib/src/automaton/FSM/TransitionFSM.cpp @@ -29,4 +29,12 @@ bool TransitionFSM::operator != (const TransitionFSM& other) const { return from != other.from || input != other.input || to != other.to; } +std::ostream& TransitionFSM::operator>>(std::ostream& out) const { + out << "TransitionFSM from = " << this->from + << " to = " << this->to + << " input = " << this->input; + + return out; +} + } /* namespace automaton */ diff --git a/alib/src/automaton/FSM/TransitionFSM.h b/alib/src/automaton/FSM/TransitionFSM.h index 17dc9e52d1..2c581370ac 100644 --- a/alib/src/automaton/FSM/TransitionFSM.h +++ b/alib/src/automaton/FSM/TransitionFSM.h @@ -29,6 +29,8 @@ public: bool operator <(const TransitionFSM& other) const; bool operator ==(const TransitionFSM& other) const; bool operator !=(const TransitionFSM& other) const; + + std::ostream& operator>>(std::ostream& out) const; }; } /* namespace automaton */ diff --git a/alib/src/automaton/PDA/TransitionPDA.cpp b/alib/src/automaton/PDA/TransitionPDA.cpp index 88c067cdfd..6f14b7d00e 100644 --- a/alib/src/automaton/PDA/TransitionPDA.cpp +++ b/alib/src/automaton/PDA/TransitionPDA.cpp @@ -120,4 +120,31 @@ bool TransitionPDA::operator !=(const TransitionPDA& other) const { return !((*this) == other); } +std::ostream& TransitionPDA::operator>>(std::ostream& out) const { + bool first; + out << "TransitionPDA from = " << this->from + << " to = " << this->to + << " input = " << this->input + << " pop = ["; + + first = true; + for(list<Symbol>::const_iterator iter = this->pop.begin(); iter != this->pop.end(); iter++) { + if(!first) out << ", "; + first = false; + out << *iter; + } + + out << "] push = ["; + + first = true; + for(list<Symbol>::const_iterator iter = this->push.begin(); iter != this->push.end(); iter++) { + if(!first) out << ", "; + first = false; + out << *iter; + } + out << "]"; + + return out; +} + } /* namespace automaton */ diff --git a/alib/src/automaton/PDA/TransitionPDA.h b/alib/src/automaton/PDA/TransitionPDA.h index 8f2f0b5614..9fcc2b71a8 100644 --- a/alib/src/automaton/PDA/TransitionPDA.h +++ b/alib/src/automaton/PDA/TransitionPDA.h @@ -32,6 +32,8 @@ public: bool operator <(const TransitionPDA& other) const; bool operator ==(const TransitionPDA& other) const; bool operator !=(const TransitionPDA& other) const; + + std::ostream& operator>>(std::ostream& out) const; }; } /* namespace automaton */ diff --git a/alib/src/automaton/Shift.cpp b/alib/src/automaton/Shift.cpp new file mode 100644 index 0000000000..03f2e55c21 --- /dev/null +++ b/alib/src/automaton/Shift.cpp @@ -0,0 +1,10 @@ +/* + * Shift.cpp + * + * Created on: Dec 8, 2013 + * Author: honza + */ + +#include "Shift.h" + +std::string SHIFT_NAMES[] = {"LEFT", "RIGHT", "NONE", "NOT_SET" }; diff --git a/alib/src/automaton/Shift.h b/alib/src/automaton/Shift.h index af9632f9e4..721caff2ad 100644 --- a/alib/src/automaton/Shift.h +++ b/alib/src/automaton/Shift.h @@ -8,6 +8,8 @@ #ifndef SHIFT_H_ #define SHIFT_H_ +#include <string> + /** * Represent movement of the reading head in the Turing machine. */ @@ -15,4 +17,6 @@ enum Shift { LEFT, RIGHT, NONE, NOT_SET }; +extern std::string SHIFT_NAMES[4]; + #endif /* SHIFT_H_ */ diff --git a/alib/src/automaton/State.cpp b/alib/src/automaton/State.cpp index 4c2a3fb370..d278b21fb3 100644 --- a/alib/src/automaton/State.cpp +++ b/alib/src/automaton/State.cpp @@ -28,4 +28,9 @@ bool State::operator != (const State& other) const{ return name != other.name; } +std::ostream& operator<<(std::ostream& out, const State& state) { + out << "State " << state.name; + return out; +} + } /* namespace automaton */ diff --git a/alib/src/automaton/State.h b/alib/src/automaton/State.h index 6ffad3c43f..1cb546d3ca 100644 --- a/alib/src/automaton/State.h +++ b/alib/src/automaton/State.h @@ -26,6 +26,8 @@ public: bool operator < (const State& other) const; bool operator == (const State& other) const; bool operator != (const State& other) const; + + friend std::ostream& operator<<(std::ostream&, const State&); }; } /* namespace automaton */ diff --git a/alib/src/automaton/TM/TransitionTM.cpp b/alib/src/automaton/TM/TransitionTM.cpp index 2c67ffdafa..ac535ee8dd 100644 --- a/alib/src/automaton/TM/TransitionTM.cpp +++ b/alib/src/automaton/TM/TransitionTM.cpp @@ -49,4 +49,14 @@ bool TransitionTM::operator !=(const TransitionTM& other) const { return !((*this) == other); } +std::ostream& TransitionTM::operator>>(std::ostream& out) const { + out << "TransitionTM from = " << this->from + << " to = " << this->to + << " input = " << this->input + << " output = " << this->output + << " shift = " << SHIFT_NAMES[this->shift]; + + return out; +} + } /* namespace automaton */ diff --git a/alib/src/automaton/TM/TransitionTM.h b/alib/src/automaton/TM/TransitionTM.h index 505681a531..3ce2559b19 100644 --- a/alib/src/automaton/TM/TransitionTM.h +++ b/alib/src/automaton/TM/TransitionTM.h @@ -56,6 +56,8 @@ public: bool operator <(const TransitionTM& other) const; bool operator ==(const TransitionTM& other) const; bool operator !=(const TransitionTM& other) const; + + std::ostream& operator>>(std::ostream& out) const; }; } /* namespace automaton */ diff --git a/alib/src/automaton/Transition.cpp b/alib/src/automaton/Transition.cpp index 48166b2337..3c0aea6d29 100644 --- a/alib/src/automaton/Transition.cpp +++ b/alib/src/automaton/Transition.cpp @@ -42,4 +42,14 @@ bool Transition::containsState(const State& state) const { return from == state || to == state; } +std::ostream& operator<<(std::ostream& out, const Transition& transition) { + transition >> out; + return out; +} + +std::ostream& Transition::operator>>(std::ostream& out) const { + out << "Transition from = " << this->from << " to = " << this->to << " input = " << this->input; + return out; +} + } /* namespace automaton */ diff --git a/alib/src/automaton/Transition.h b/alib/src/automaton/Transition.h index 857ee473ad..c3fce1ef4d 100644 --- a/alib/src/automaton/Transition.h +++ b/alib/src/automaton/Transition.h @@ -62,6 +62,10 @@ public: * @return true when transition contains the state, false otherwise */ bool containsState(const State& state) const; + + friend std::ostream& operator<<(std::ostream&, const Transition&); + + virtual std::ostream& operator>>(std::ostream&) const; }; } /* namespace automaton */ diff --git a/alib/src/automaton/UnknownTransition.cpp b/alib/src/automaton/UnknownTransition.cpp index a452f18d9e..878aededbe 100644 --- a/alib/src/automaton/UnknownTransition.cpp +++ b/alib/src/automaton/UnknownTransition.cpp @@ -138,4 +138,32 @@ bool UnknownTransition::operator !=(const UnknownTransition& other) const { return !((*this) == other); } +std::ostream& UnknownTransition::operator>>(std::ostream& out) const { + bool first; + out << "UnknownTransition from = " << this->from + << " to = " << this->to + << " input = " << this->input + << " output = " << this->output + << " pop = ["; + + first = true; + for(list<Symbol>::const_iterator iter = this->pop.begin(); iter != this->pop.end(); iter++) { + if(!first) out << ", "; + first = false; + out << *iter; + } + + out << "] push = ["; + + first = true; + for(list<Symbol>::const_iterator iter = this->push.begin(); iter != this->push.end(); iter++) { + if(!first) out << ", "; + first = false; + out << *iter; + } + out << "] shift = " << SHIFT_NAMES[this->shift]; + + return out; +} + } /* namespace automaton */ diff --git a/alib/src/automaton/UnknownTransition.h b/alib/src/automaton/UnknownTransition.h index 19621d0d92..0dcf1ee45d 100644 --- a/alib/src/automaton/UnknownTransition.h +++ b/alib/src/automaton/UnknownTransition.h @@ -81,6 +81,8 @@ public: bool operator <(const UnknownTransition& other) const; bool operator ==(const UnknownTransition& other) const; bool operator !=(const UnknownTransition& other) const; + + virtual std::ostream& operator>>(std::ostream&) const; }; } /* namespace automaton */ -- GitLab