From 75acff4617da0e6d911cc22b890a6c3d7d9d5db9 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 29 Apr 2014 22:27:43 +0200 Subject: [PATCH] implement One Tape Turing Machine --- alib2/src/automaton/Automaton.cpp | 6 - alib2/src/automaton/Automaton.h | 2 +- alib2/src/automaton/FSM/CompactFA.cpp | 6 + alib2/src/automaton/FSM/CompactFA.h | 6 + alib2/src/automaton/FSM/DFA.cpp | 6 + alib2/src/automaton/FSM/DFA.h | 5 + alib2/src/automaton/FSM/EpsilonNFA.cpp | 6 + alib2/src/automaton/FSM/EpsilonNFA.h | 5 + alib2/src/automaton/FSM/ExtendedFA.cpp | 6 + alib2/src/automaton/FSM/ExtendedFA.h | 5 + alib2/src/automaton/FSM/NFA.cpp | 6 + alib2/src/automaton/FSM/NFA.h | 5 + alib2/src/automaton/PDA/PDA.cpp | 6 + alib2/src/automaton/PDA/PDA.h | 5 + alib2/src/automaton/Shift.cpp | 4 + alib2/src/automaton/Shift.h | 4 + alib2/src/automaton/TM/OneTapeDTM.cpp | 144 +++++++++++++++++ alib2/src/automaton/TM/{TM.h => OneTapeDTM.h} | 52 ++++--- alib2/src/automaton/TM/TM.cpp | 146 ------------------ alib2/src/automaton/TM/TransitionTM.cpp | 68 -------- alib2/src/automaton/TM/TransitionTM.h | 78 ---------- 21 files changed, 247 insertions(+), 324 deletions(-) create mode 100644 alib2/src/automaton/TM/OneTapeDTM.cpp rename alib2/src/automaton/TM/{TM.h => OneTapeDTM.h} (57%) delete mode 100644 alib2/src/automaton/TM/TM.cpp delete mode 100644 alib2/src/automaton/TM/TransitionTM.cpp delete mode 100644 alib2/src/automaton/TM/TransitionTM.h diff --git a/alib2/src/automaton/Automaton.cpp b/alib2/src/automaton/Automaton.cpp index 121ac60c34..936ec1ee0e 100644 --- a/alib2/src/automaton/Automaton.cpp +++ b/alib2/src/automaton/Automaton.cpp @@ -26,12 +26,6 @@ const std::set<State>& Automaton::getStates() const { return states; } -void Automaton::addInputSymbol(const alphabet::Symbol& symbol) { - std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); - if (!ret.second) - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); -} - const std::set<alphabet::Symbol>& Automaton::getInputAlphabet() const { return inputAlphabet; } diff --git a/alib2/src/automaton/Automaton.h b/alib2/src/automaton/Automaton.h index c6d9429ac6..4c74dfe407 100644 --- a/alib2/src/automaton/Automaton.h +++ b/alib2/src/automaton/Automaton.h @@ -55,7 +55,7 @@ public: * @param symbol Symbol to add * @throws AutomatonException when symbol already exists */ - virtual void addInputSymbol(const alphabet::Symbol& symbol); + virtual void addInputSymbol(const alphabet::Symbol& symbol) = 0; /** * Removes input symbol from the input alphabet. diff --git a/alib2/src/automaton/FSM/CompactFA.cpp b/alib2/src/automaton/FSM/CompactFA.cpp index af78e0d031..7fd01ebc5e 100644 --- a/alib2/src/automaton/FSM/CompactFA.cpp +++ b/alib2/src/automaton/FSM/CompactFA.cpp @@ -11,6 +11,12 @@ namespace automaton { +void CompactFA::addInputSymbol(const alphabet::Symbol& symbol) { + std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); + if (!ret.second) + throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); +} + void CompactFA::removeState(const State& state) { for (std::set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); initialState++) { diff --git a/alib2/src/automaton/FSM/CompactFA.h b/alib2/src/automaton/FSM/CompactFA.h index 300e051459..f186ff666d 100644 --- a/alib2/src/automaton/FSM/CompactFA.h +++ b/alib2/src/automaton/FSM/CompactFA.h @@ -23,6 +23,12 @@ class CompactFA : public Automaton { protected: std::map<std::pair<State, string::String>, State> transitions; public: + + /** + * @copydoc Automaton::addInputSymbol(const Symbol&) + */ + virtual void addInputSymbol(const alphabet::Symbol& symbol); + /** * @copydoc Automaton::removeState(const State&) */ diff --git a/alib2/src/automaton/FSM/DFA.cpp b/alib2/src/automaton/FSM/DFA.cpp index f6150e783c..4239419d0b 100644 --- a/alib2/src/automaton/FSM/DFA.cpp +++ b/alib2/src/automaton/FSM/DFA.cpp @@ -11,6 +11,12 @@ namespace automaton { +void DFA::addInputSymbol(const alphabet::Symbol& symbol) { + std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); + if (!ret.second) + throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); +} + void DFA::removeState(const State& state) { for (std::set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); initialState++) { diff --git a/alib2/src/automaton/FSM/DFA.h b/alib2/src/automaton/FSM/DFA.h index 4111d13343..52ba97db87 100644 --- a/alib2/src/automaton/FSM/DFA.h +++ b/alib2/src/automaton/FSM/DFA.h @@ -23,6 +23,11 @@ class DFA : public Automaton { protected: std::map<std::pair<State, alphabet::Symbol>, State> transitions; public: + /** + * @copydoc Automaton::addInputSymbol(const Symbol&) + */ + virtual void addInputSymbol(const alphabet::Symbol& symbol); + /** * @copydoc Automaton::removeState(const State&) */ diff --git a/alib2/src/automaton/FSM/EpsilonNFA.cpp b/alib2/src/automaton/FSM/EpsilonNFA.cpp index 90bb5caec0..8e3edba670 100644 --- a/alib2/src/automaton/FSM/EpsilonNFA.cpp +++ b/alib2/src/automaton/FSM/EpsilonNFA.cpp @@ -11,6 +11,12 @@ namespace automaton { +void EpsilonNFA::addInputSymbol(const alphabet::Symbol& symbol) { + std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); + if (!ret.second) + throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); +} + void EpsilonNFA::removeState(const State& state) { for (std::set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); initialState++) { diff --git a/alib2/src/automaton/FSM/EpsilonNFA.h b/alib2/src/automaton/FSM/EpsilonNFA.h index d6c2a523c8..364adfc85d 100644 --- a/alib2/src/automaton/FSM/EpsilonNFA.h +++ b/alib2/src/automaton/FSM/EpsilonNFA.h @@ -25,6 +25,11 @@ class EpsilonNFA : public Automaton { protected: std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> > transitions; public: + /** + * @copydoc Automaton::addInputSymbol(const Symbol&) + */ + virtual void addInputSymbol(const alphabet::Symbol& symbol); + /** * @copydoc Automaton::removeState(const State&) */ diff --git a/alib2/src/automaton/FSM/ExtendedFA.cpp b/alib2/src/automaton/FSM/ExtendedFA.cpp index ead944b8cc..1d771c7be7 100644 --- a/alib2/src/automaton/FSM/ExtendedFA.cpp +++ b/alib2/src/automaton/FSM/ExtendedFA.cpp @@ -11,6 +11,12 @@ namespace automaton { +void ExtendedFA::addInputSymbol(const alphabet::Symbol& symbol) { + std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); + if (!ret.second) + throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); +} + void ExtendedFA::removeState(const State& state) { for (std::set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); initialState++) { diff --git a/alib2/src/automaton/FSM/ExtendedFA.h b/alib2/src/automaton/FSM/ExtendedFA.h index 1619ff76b8..d34a3f7ff2 100644 --- a/alib2/src/automaton/FSM/ExtendedFA.h +++ b/alib2/src/automaton/FSM/ExtendedFA.h @@ -23,6 +23,11 @@ class ExtendedFA : public Automaton { protected: std::map<std::pair<State, regexp::RegExp>, State> transitions; public: + /** + * @copydoc Automaton::addInputSymbol(const Symbol&) + */ + virtual void addInputSymbol(const alphabet::Symbol& symbol); + /** * @copydoc Automaton::removeState(const State&) */ diff --git a/alib2/src/automaton/FSM/NFA.cpp b/alib2/src/automaton/FSM/NFA.cpp index da915fce4d..3c71952df3 100644 --- a/alib2/src/automaton/FSM/NFA.cpp +++ b/alib2/src/automaton/FSM/NFA.cpp @@ -10,6 +10,12 @@ #include <ostream> namespace automaton { + +void NFA::addInputSymbol(const alphabet::Symbol& symbol) { + std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); + if (!ret.second) + throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); +} void NFA::removeState(const State& state) { for (std::set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); diff --git a/alib2/src/automaton/FSM/NFA.h b/alib2/src/automaton/FSM/NFA.h index 3193f2562b..ab065932c6 100644 --- a/alib2/src/automaton/FSM/NFA.h +++ b/alib2/src/automaton/FSM/NFA.h @@ -23,6 +23,11 @@ class NFA : public Automaton { protected: std::map<std::pair<State, alphabet::Symbol>, std::set<State> > transitions; public: + /** + * @copydoc Automaton::addInputSymbol(const Symbol&) + */ + virtual void addInputSymbol(const alphabet::Symbol& symbol); + /** * @copydoc Automaton::removeState(const State&) */ diff --git a/alib2/src/automaton/PDA/PDA.cpp b/alib2/src/automaton/PDA/PDA.cpp index 7a32d7bc6b..8e7fd58c6d 100644 --- a/alib2/src/automaton/PDA/PDA.cpp +++ b/alib2/src/automaton/PDA/PDA.cpp @@ -11,6 +11,12 @@ namespace automaton { +void PDA::addInputSymbol(const alphabet::Symbol& symbol) { + std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); + if (!ret.second) + throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); +} + void PDA::removeState(const State& state) { for (std::set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); initialState++) { diff --git a/alib2/src/automaton/PDA/PDA.h b/alib2/src/automaton/PDA/PDA.h index fb41cf33c5..74b2998746 100644 --- a/alib2/src/automaton/PDA/PDA.h +++ b/alib2/src/automaton/PDA/PDA.h @@ -28,6 +28,11 @@ protected: std::map<std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> >, std::set<std::pair<State, std::vector<alphabet::Symbol> > > > transitions; std::set<alphabet::Symbol> initialSymbols; public: + /** + * @copydoc Automaton::addInputSymbol(const Symbol&) + */ + virtual void addInputSymbol(const alphabet::Symbol& symbol); + /** * @copydoc Automaton::removeState(const State&) */ diff --git a/alib2/src/automaton/Shift.cpp b/alib2/src/automaton/Shift.cpp index 03f2e55c21..64cb3e9441 100644 --- a/alib2/src/automaton/Shift.cpp +++ b/alib2/src/automaton/Shift.cpp @@ -7,4 +7,8 @@ #include "Shift.h" +namespace automaton { + std::string SHIFT_NAMES[] = {"LEFT", "RIGHT", "NONE", "NOT_SET" }; + +} /* namepsace automaton */ diff --git a/alib2/src/automaton/Shift.h b/alib2/src/automaton/Shift.h index 721caff2ad..d9243adc02 100644 --- a/alib2/src/automaton/Shift.h +++ b/alib2/src/automaton/Shift.h @@ -10,6 +10,8 @@ #include <string> +namespace automaton { + /** * Represent movement of the reading head in the Turing machine. */ @@ -19,4 +21,6 @@ enum Shift { extern std::string SHIFT_NAMES[4]; +} /* namespace automaton */ + #endif /* SHIFT_H_ */ diff --git a/alib2/src/automaton/TM/OneTapeDTM.cpp b/alib2/src/automaton/TM/OneTapeDTM.cpp new file mode 100644 index 0000000000..4dca1a31af --- /dev/null +++ b/alib2/src/automaton/TM/OneTapeDTM.cpp @@ -0,0 +1,144 @@ +/* + * OneTapeDTM.cpp + * + * Created on: Apr 24, 2013 + * Author: martin + */ + +#include "OneTapeDTM.h" +#include "../AutomatonException.h" +#include "../../alphabet/Blank.h" + +namespace automaton { + +using namespace std; +using namespace alphabet; + +OneTapeDTM::OneTapeDTM() : blankSymbol(Blank()) { + tapeAlphabet.insert(blankSymbol); +} + +void OneTapeDTM::addInputSymbol(const alphabet::Symbol& symbol) { + if (tapeAlphabet.find(symbol) == tapeAlphabet.end()) { + throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" is not in tape alphabet."); + } + + if (symbol == blankSymbol) { + throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" cannot be blank symbol."); + } + + std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); + if (!ret.second) + throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); +} + +void OneTapeDTM::removeState(const State& state) { + if (initialStates.find(state) != initialStates.end()) { + throw AutomatonException("State \"" + state.getName() + "\" is initial state."); + } + + if (finalStates.find(state) != finalStates.end()) { + throw AutomatonException("State \"" + state.getName() + "\" is final state."); + } + + for (std::map<std::pair<State, alphabet::Symbol>, std::tuple<State, alphabet::Symbol, Shift> >::const_iterator transition = transitions.begin(); transition != transitions.end(); transition++) { + if (state == transition->first.first || state == std::get<0>(transition->second)) + throw AutomatonException("State \"" + state.getName() + "\" is used in a transition."); + } + + int removed = states.erase(state); + if (!removed) + throw AutomatonException("State \"" + state.getName() + "\" doesn't exist."); +} + +void OneTapeDTM::removeInputSymbol(const alphabet::Symbol& symbol) { + int removed = inputAlphabet.erase(symbol); + if (!removed) + throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" doesn't exist."); +} + +void OneTapeDTM::addTapeSymbol(const alphabet::Symbol& symbol) { + std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = tapeAlphabet.insert(symbol); + if (!ret.second) + throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" already exists."); +} + +void OneTapeDTM::removeTapeSymbol(const alphabet::Symbol& symbol) { + if (inputAlphabet.find(symbol) != inputAlphabet.end()) { + throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" is in input alphabet."); + } + + for (std::map<std::pair<State, alphabet::Symbol>, std::tuple<State, alphabet::Symbol, Shift> >::const_iterator transition = transitions.begin(); transition != transitions.end(); transition++) { + if (symbol == transition->first.second || symbol == std::get<1>(transition->second)) + throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" is used in transition."); + } + + int removed = inputAlphabet.erase(symbol); + if (!removed) + throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" doesn't exist."); + +} + +const std::set<alphabet::Symbol>& OneTapeDTM::getTapeAlphabet() const { + return tapeAlphabet; +} + +void OneTapeDTM::addTransition(const State& from, const alphabet::Symbol& input, const State& to, const alphabet::Symbol& output, const Shift& shift) { + if (states.find(from) == states.end()) { + throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); + } + + if (input != blankSymbol) + if (tapeAlphabet.find(input) == tapeAlphabet.end()) { + throw AutomatonException("Tape symbol \"" + input.getSymbol() + "\" doesn't exist."); + } + + if (states.find(to) == states.end()) { + throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); + } + + if (input != blankSymbol) + if (tapeAlphabet.find(output) == tapeAlphabet.end()) { + throw AutomatonException("Tape symbol \"" + output.getSymbol() + "\" doesn't exist."); + } + + std::pair<State, alphabet::Symbol> key = std::make_pair(from, input); + + std::tuple<State, alphabet::Symbol, Shift > value(to, output, shift); + + if (transitions.find(key) != transitions.end()) + throw AutomatonException( + "Transition (\"" + from.getName() + "\", \"" + input.getSymbol() + "\") -> ? already exists."); + + transitions.insert(std::make_pair(key, value)); +} + +void OneTapeDTM::removeTransition(const State& from, const alphabet::Symbol& input, const State& to, const alphabet::Symbol& output, const Shift& shift) { + std::pair<State, alphabet::Symbol> key = std::make_pair(from, input); + + if (transitions.find(key) == transitions.end()) + throw AutomatonException( + "Transition (\"" + from.getName() + "\", \"" + input.getSymbol() + "\") -> ? doesn't exists."); + + transitions.erase(key); +} + +const std::map<std::pair<State, alphabet::Symbol>, std::tuple<State, alphabet::Symbol, Shift> >& OneTapeDTM::getTransitions() const { + return transitions; +} + +void OneTapeDTM::setBlankSymbol(const alphabet::Symbol& symbol) { + if (inputAlphabet.find(symbol) != inputAlphabet.end()) + throw AutomatonException("Blank symbol \"" + symbol.getSymbol() + "\" is in input alphabet."); + + if (tapeAlphabet.find(symbol) == tapeAlphabet.end()) + throw AutomatonException("Blank symbol \"" + symbol.getSymbol() + "\" is not in tape alphabet."); + + blankSymbol = symbol; + +} +const alphabet::Symbol& OneTapeDTM::getBlankSymbol() const { + return blankSymbol; +} + +} /* namespace automaton */ diff --git a/alib2/src/automaton/TM/TM.h b/alib2/src/automaton/TM/OneTapeDTM.h similarity index 57% rename from alib2/src/automaton/TM/TM.h rename to alib2/src/automaton/TM/OneTapeDTM.h index 818083810a..5d009369dd 100644 --- a/alib2/src/automaton/TM/TM.h +++ b/alib2/src/automaton/TM/OneTapeDTM.h @@ -1,32 +1,33 @@ /* - * TM.h + * OneTapeDTM.h * * Created on: Apr 24, 2013 - * Author: martin + * Author: Martin Zak */ -#ifndef TM_H_ -#define TM_H_ +#ifndef ONE_TAPE_DTM_H_ +#define ONE_TAPE_DTM_H_ #include "../Automaton.h" -#include "TransitionTM.h" +#include "../Shift.h" -namespace automaton { +#include <map> +#include <set> +#include <tuple> -using namespace std; -using namespace alphabet; +namespace automaton { /** - * Turing machine + * One tape turing machine */ -class TM: public automaton::Automaton { +class OneTapeDTM : public Automaton { protected: - set<Symbol> tapeAlphabet; - set<TransitionTM> transitions; - Symbol blankSymbol; + std::set<alphabet::Symbol> tapeAlphabet; + std::map<std::pair<State, alphabet::Symbol>, std::tuple<State, alphabet::Symbol, Shift> > transitions; + alphabet::Symbol blankSymbol; public: - TM(); + OneTapeDTM(); /** * Adds symbol to the input alphabet. @@ -34,7 +35,7 @@ public: * @throws AutomatonException when symbol is not present in tape alphabet, * when it's blank symbol or when it is already present in input alphabet */ - virtual void addInputSymbol(const Symbol& symbol); + virtual void addInputSymbol(const alphabet::Symbol& symbol); /** * @copydoc Automaton::removeState(const State&) @@ -46,14 +47,14 @@ public: * @param symbol Symbol to remove * @throws AutomatonException when symbol is part of the transition */ - virtual void removeInputSymbol(const Symbol& symbol); + virtual void removeInputSymbol(const alphabet::Symbol& symbol); /** * Adds symbol to the tape alphabet. * @param symbol Symbol to add * @throw AutomatonException when Symbol is already present in tape alphabet */ - void addTapeSymbol(const Symbol& symbol); + void addTapeSymbol(const alphabet::Symbol& symbol); /** * Removes symbol from the tape alphabet. @@ -61,12 +62,12 @@ public: * @throw AutomatonException when Symbol is not present in the tape alphabet, * when it is used in transition or when it is present in input alphabet */ - void removeTapeSymbol(const Symbol& symbol); + void removeTapeSymbol(const alphabet::Symbol& symbol); /** * @return tape alphabet */ - const set<Symbol>& getTapeAlphabet() const; + const std::set<alphabet::Symbol>& getTapeAlphabet() const; /** * Adds transition to the automaton. @@ -74,32 +75,33 @@ public: * @throws AutomatonException when some part of the transition is not present * in the TM (state, tape symbol) or when transition already exists */ - void addTransition(const TransitionTM& transition); + void addTransition(const State& from, const alphabet::Symbol& input, const State& to, const alphabet::Symbol& output, const Shift& shift); /** * Removes the transition from the TM. * @param transition transition to remove * @throws AutomatonException when transition is not present in the TM */ - void removeTransition(const TransitionTM& transition); + void removeTransition(const State& from, const alphabet::Symbol& input, const State& to, const alphabet::Symbol& output, const Shift& shift); /** * @return TM transitions */ - const set<TransitionTM>& getTransitions() const; + const std::map<std::pair<State, alphabet::Symbol>, std::tuple<State, alphabet::Symbol, Shift> >& getTransitions() const; /** * Sets the blank symbol = symbol representing empty cell of the tape * @param symbol Symbol to set */ - void setBlankSymbol(const Symbol& symbol); + void setBlankSymbol(const alphabet::Symbol& symbol); /** * @return symbol representing empty cell of the tape */ - const Symbol& getBlankSymbol() const; + const alphabet::Symbol& getBlankSymbol() const; }; } /* namespace automaton */ -#endif /* TM_H_ */ + +#endif /* ONE_TAPE_DTM_H_ */ diff --git a/alib2/src/automaton/TM/TM.cpp b/alib2/src/automaton/TM/TM.cpp deleted file mode 100644 index 8ece4ab902..0000000000 --- a/alib2/src/automaton/TM/TM.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/* - * TM.cpp - * - * Created on: Apr 24, 2013 - * Author: martin - */ - -#include "TM.h" -#include "../AutomatonException.h" - -namespace automaton { - -using namespace std; -using namespace alphabet; - -TM::TM() : - blankSymbol(Symbol("")) { -} - -void TM::addInputSymbol(const Symbol& symbol) { - if (tapeAlphabet.find(symbol) == tapeAlphabet.end()) { - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" is not in tape alphabet."); - } - - if (symbol == blankSymbol) { - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" cannot be blank symbol."); - } - - std::pair<std::set<Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); - if (!ret.second) - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); -} - -void TM::removeState(const State& state) { - for (set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); - initialState++) { - if (*initialState == state) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); - } - } - - for (set<State>::const_iterator finalState = finalStates.begin(); finalState != finalStates.end(); finalState++) { - if (*finalState == state) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); - } - } - - for (set<TransitionTM>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { - if (state == t->getFrom() || state == t->getTo()) - throw AutomatonException("State \"" + state.getName() + "\" is used."); - } - - int removed = states.erase(state); - if (!removed) - throw AutomatonException("State \"" + state.getName() + "\" doesn't exist."); -} - -void TM::removeInputSymbol(const Symbol& symbol) { - int removed = inputAlphabet.erase(symbol); - if (!removed) - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" doesn't exist."); -} - -void TM::addTapeSymbol(const Symbol& symbol) { - pair<set<Symbol>::iterator, bool> ret = tapeAlphabet.insert(symbol); - if (!ret.second) - throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" already exists."); -} - -void TM::removeTapeSymbol(const Symbol& symbol) { - for (set<Symbol>::const_iterator it = inputAlphabet.begin(); it != inputAlphabet.end(); it++) { - if (symbol == (*it)) { - throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" is in input alphabet."); - } - } - - for (set<TransitionTM>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { - if (symbol == t->getInput() || symbol == t->getOutput()) - throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" is used in transition."); - } - - int removed = inputAlphabet.erase(symbol); - if (!removed) - throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" doesn't exist."); - -} - -const std::set<Symbol>& TM::getTapeAlphabet() const { - return tapeAlphabet; -} - -void TM::addTransition(const TransitionTM& transition) { - if (states.find(transition.getFrom()) == states.end()) { - throw AutomatonException("State \"" + transition.getFrom().getName() + "\" doesn't exist."); - } - - if (transition.getInput().getSymbol() != "") { - if (tapeAlphabet.find(transition.getInput()) == tapeAlphabet.end()) { - throw AutomatonException("Tape symbol \"" + transition.getInput().getSymbol() + "\" doesn't exist."); - } - } - - if (states.find(transition.getTo()) == states.end()) { - throw AutomatonException("State \"" + transition.getTo().getName() + "\" doesn't exist."); - } - - if (tapeAlphabet.find(transition.getOutput()) == tapeAlphabet.end()) { - throw AutomatonException("Tape symbol \"" + transition.getOutput().getSymbol() + "\" doesn't exist."); - } - - pair<set<TransitionTM>::iterator, bool> ret = transitions.insert(transition); - if (!ret.second) { - throw AutomatonException("Transition already exists."); - } - -} - -void TM::removeTransition(const TransitionTM& transition) { - int removed = transitions.erase(transition); - if (!removed) { - throw AutomatonException("Transition doesn't exist."); - } -} - -const std::set<TransitionTM>& TM::getTransitions() const { - return transitions; -} - -void TM::setBlankSymbol(const Symbol& symbol) { - for (set<Symbol>::const_iterator it = inputAlphabet.begin(); it != inputAlphabet.end(); it++) { - if (symbol == (*it)) { - throw AutomatonException("Blank symbol \"" + symbol.getSymbol() + "\" is in input alphabet."); - } - } - - if (tapeAlphabet.find(symbol) == tapeAlphabet.end()) - throw AutomatonException("Blank symbol \"" + symbol.getSymbol() + "\" is not in tape alphabet."); - - blankSymbol = symbol; - -} -const Symbol& TM::getBlankSymbol() const { - return blankSymbol; -} - -} /* namespace automaton */ diff --git a/alib2/src/automaton/TM/TransitionTM.cpp b/alib2/src/automaton/TM/TransitionTM.cpp deleted file mode 100644 index 21df387bee..0000000000 --- a/alib2/src/automaton/TM/TransitionTM.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * TransitionTM.cpp - * - * Created on: Apr 24, 2013 - * Author: martin - */ - -#include "TransitionTM.h" - -namespace automaton { - -TransitionTM::TransitionTM(const State& from, const Symbol& input, const State& to, const Symbol& output, - Shift shift) : - Transition(from, to), input(input), output(output), shift(shift) { -} - -const Symbol& TransitionTM::getOutput() const { - return output; -} -void TransitionTM::setOutput(const Symbol& output) { - this->output = output; -} -const Symbol& TransitionTM::getInput() const { - return input; -} -void TransitionTM::setInput(const Symbol& input) { - this->input = input; -} -Shift TransitionTM::getShift() const { - return shift; -} -void TransitionTM::setShift(Shift shift) { - this->shift = shift; -} - -bool TransitionTM::operator <(const TransitionTM& other) const { - if (from != other.from) { - return from < other.from; - } else if (input != other.input) { - return input < other.input; - } else if (to != to) { - return to < other.to; - } else if (output != output) { - return output < other.output; - } else { - return shift < other.shift; - } - -} -bool TransitionTM::operator ==(const TransitionTM& other) const { - return from == other.from && input == other.input && to == other.to && output == other.output - && shift == other.shift; -} -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/alib2/src/automaton/TM/TransitionTM.h b/alib2/src/automaton/TM/TransitionTM.h deleted file mode 100644 index 124274b023..0000000000 --- a/alib2/src/automaton/TM/TransitionTM.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * TransitionTM.h - * - * Created on: Apr 24, 2013 - * Author: martin - */ - -#ifndef TRANSITIONTM_H_ -#define TRANSITIONTM_H_ - -#include "../../alphabet/Symbol.h" -#include "../Transition.h" -#include "../Shift.h" - -namespace automaton { - -using namespace std; -using namespace alphabet; - -class TransitionTM: public Transition { -protected: - Symbol input; - Symbol output; - Shift shift; -public: - - /** - * Creates new transition with given parameters. - * @param from from state - * @param input input symbol - * @param to to state - * @param output output symbol - * @param shift direction of movement of the reading head - */ - TransitionTM(const State& from, const Symbol& input, const State& to, const Symbol& output, Shift shift); - - /** - * @return input Symbol - */ - const Symbol& getInput() const; - - /** - * Sets the input symbol. - * @param input Symbol - */ - void setInput(const Symbol& input); - - /** - * @return output Symbol - */ - const Symbol& getOutput() const; - - /** - * Sets the output symbol. - * @param output Symbol - */ - void setOutput(const Symbol& output); - - /** - * @return direction of movement of the reading head - */ - Shift getShift() const; - - /** - * Sets the direction of movement of the reading head - * @param shift Shift to set - */ - void setShift(Shift shift); - - 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 */ -#endif /* TRANSITIONTM_H_ */ -- GitLab