diff --git a/acompare2/src/AutomatonCompare.cpp b/acompare2/src/AutomatonCompare.cpp index 39488f4fead9e6df425f2f0782cf6fabb0ac7c0c..6c356638fc166041ba652e4d9d490e82e17f8041 100644 --- a/acompare2/src/AutomatonCompare.cpp +++ b/acompare2/src/AutomatonCompare.cpp @@ -194,7 +194,7 @@ bool AutomatonCompare::testCompare(const automaton::RealTimeHeightDeterministicN a.getLocalTransitions() == b.getLocalTransitions() ; } -bool AutomatonCompare::testCompare(const automaton::SinglePopDPDA& a, const automaton::SinglePopDPDA& b) { +bool AutomatonCompare::testCompare(const automaton::SinglePopDPDA < > & a, const automaton::SinglePopDPDA < > & b) { return a.getFinalStates() == b.getFinalStates() && a.getInitialState() == b.getInitialState() && // a.getInputAlphabet() == b.getInputAlphabet() && @@ -1022,7 +1022,7 @@ void AutomatonCompare::printCompare(const automaton::RealTimeHeightDeterministic } } -void AutomatonCompare::printCompare(const automaton::SinglePopDPDA& a, const automaton::SinglePopDPDA& b) { +void AutomatonCompare::printCompare(const automaton::SinglePopDPDA < > & a, const automaton::SinglePopDPDA < > & b) { std::cout << "AutomatonCompareer" << std::endl; if(a.getFinalStates() != b.getFinalStates()){ @@ -1348,7 +1348,7 @@ int AutomatonCompare::compare(const automaton::RealTimeHeightDeterministicNPDA < auto AutomatonCompareRealTimeHeightDeterministicNPDA = AutomatonCompare::RegistratorWrapper<int, automaton::RealTimeHeightDeterministicNPDA < >, automaton::RealTimeHeightDeterministicNPDA < > >(AutomatonCompare::compare); -int AutomatonCompare::compare(const automaton::SinglePopDPDA& a, const automaton::SinglePopDPDA& b) { +int AutomatonCompare::compare(const automaton::SinglePopDPDA < > & a, const automaton::SinglePopDPDA < > & b) { if(!AutomatonCompare::testCompare(a, b)) { AutomatonCompare::printCompare(a, b); return 1; @@ -1357,7 +1357,7 @@ int AutomatonCompare::compare(const automaton::SinglePopDPDA& a, const automaton } } -auto AutomatonCompareSinglePopDPDA = AutomatonCompare::RegistratorWrapper<int, automaton::SinglePopDPDA, automaton::SinglePopDPDA>(AutomatonCompare::compare); +auto AutomatonCompareSinglePopDPDA = AutomatonCompare::RegistratorWrapper<int, automaton::SinglePopDPDA < >, automaton::SinglePopDPDA < > >(AutomatonCompare::compare); int AutomatonCompare::compare(const automaton::SinglePopNPDA < > & a, const automaton::SinglePopNPDA < > & b) { if(!AutomatonCompare::testCompare(a, b)) { diff --git a/acompare2/src/AutomatonCompare.h b/acompare2/src/AutomatonCompare.h index 5f6f2c599bd57593e4729f0fd780ff5a9b537638..63ba88206dc7ccc51e2d47367d1c682947cfeae8 100644 --- a/acompare2/src/AutomatonCompare.h +++ b/acompare2/src/AutomatonCompare.h @@ -68,8 +68,8 @@ private: static bool testCompare(const automaton::RealTimeHeightDeterministicNPDA < > & a, const automaton::RealTimeHeightDeterministicNPDA < > & b); static void printCompare(const automaton::RealTimeHeightDeterministicNPDA < > & a, const automaton::RealTimeHeightDeterministicNPDA < > & b); - static bool testCompare(const automaton::SinglePopDPDA& a, const automaton::SinglePopDPDA& b); - static void printCompare(const automaton::SinglePopDPDA& a, const automaton::SinglePopDPDA& b); + static bool testCompare(const automaton::SinglePopDPDA < > & a, const automaton::SinglePopDPDA < > & b); + static void printCompare(const automaton::SinglePopDPDA < > & a, const automaton::SinglePopDPDA < > & b); static bool testCompare(const automaton::SinglePopNPDA < > & a, const automaton::SinglePopNPDA < > & b); static void printCompare(const automaton::SinglePopNPDA < > & a, const automaton::SinglePopNPDA < > & b); @@ -97,7 +97,7 @@ public: static int compare(const automaton::InputDrivenNPDA < > & a, const automaton::InputDrivenNPDA < > & b); static int compare(const automaton::RealTimeHeightDeterministicDPDA < > & a, const automaton::RealTimeHeightDeterministicDPDA < > & b); static int compare(const automaton::RealTimeHeightDeterministicNPDA < > & a, const automaton::RealTimeHeightDeterministicNPDA < > & b); - static int compare(const automaton::SinglePopDPDA& a, const automaton::SinglePopDPDA& b); + static int compare(const automaton::SinglePopDPDA < > & a, const automaton::SinglePopDPDA < > & b); static int compare(const automaton::SinglePopNPDA < > & a, const automaton::SinglePopNPDA < > & b); static int compare(const automaton::VisiblyPushdownDPDA& a, const automaton::VisiblyPushdownDPDA& b); static int compare(const automaton::VisiblyPushdownNPDA& a, const automaton::VisiblyPushdownNPDA& b); diff --git a/aconvert2/src/DotConverter.cpp b/aconvert2/src/DotConverter.cpp index 73332bf1b6b5d56ae463465846268ea276ab39c9..544c1ffe69cab0e65ebb48a7aae94040c0597d14 100644 --- a/aconvert2/src/DotConverter.cpp +++ b/aconvert2/src/DotConverter.cpp @@ -341,7 +341,7 @@ void DotConverter::convert(std::ostream& out, const automaton::DPDA < > & a) { auto DotConverterDPDA = DotConverter::RegistratorWrapper<void, automaton::DPDA < > >(DotConverter::convert); -void DotConverter::convert(std::ostream& out, const automaton::SinglePopDPDA& a) { +void DotConverter::convert(std::ostream& out, const automaton::SinglePopDPDA < > & a) { out << "digraph automaton {\n"; out << "rankdir=LR;\n"; int cnt = 1; @@ -372,7 +372,7 @@ void DotConverter::convert(std::ostream& out, const automaton::SinglePopDPDA& a) out << "}"; } -auto DotConverterSinglePopDPDA = DotConverter::RegistratorWrapper<void, automaton::SinglePopDPDA>(DotConverter::convert); +auto DotConverterSinglePopDPDA = DotConverter::RegistratorWrapper<void, automaton::SinglePopDPDA < > >(DotConverter::convert); void DotConverter::convert(std::ostream& out, const automaton::InputDrivenDPDA < > & a) { out << "digraph automaton {\n"; @@ -1045,7 +1045,7 @@ void DotConverter::transitions(const automaton::DPDA < > & pda, const std::map<l } } -void DotConverter::transitions(const automaton::SinglePopDPDA& pda, const std::map<label::Label, int>& states, std::ostream& out) { +void DotConverter::transitions(const automaton::SinglePopDPDA < > & pda, const std::map<label::Label, int>& states, std::ostream& out) { std::map<std::pair<int, int>, std::string> transitions; for (const auto& transition : pda.getTransitions()) { diff --git a/aconvert2/src/DotConverter.h b/aconvert2/src/DotConverter.h index 7991467ceaa3411d972b24b0bc267c0497f58c9e..9e3cda8a4304a9d71166fc62de83774a757f37d1 100644 --- a/aconvert2/src/DotConverter.h +++ b/aconvert2/src/DotConverter.h @@ -29,7 +29,7 @@ class DotConverter : public std::SingleDispatchFirstStaticParam<DotConverter, vo static void transitions(const automaton::NFTA& fsm, const std::map<label::Label, int>& states, std::ostream& out); static void transitions(const automaton::DFTA& fsm, const std::map<label::Label, int>& states, std::ostream& out); static void transitions(const automaton::DPDA < > & pda, const std::map<label::Label, int>& states, std::ostream& out); - static void transitions(const automaton::SinglePopDPDA& tm, const std::map<label::Label, int>& states, std::ostream& out); + static void transitions(const automaton::SinglePopDPDA < > & tm, const std::map<label::Label, int>& states, std::ostream& out); static void transitions(const automaton::InputDrivenDPDA < > & pda, const std::map<label::Label, int>& states, std::ostream& out); static void transitions(const automaton::InputDrivenNPDA < > & pda, const std::map<label::Label, int>& states, std::ostream& out); static void transitions(const automaton::VisiblyPushdownDPDA& tm, const std::map<label::Label, int>& states, std::ostream& out); @@ -51,7 +51,7 @@ public: static void convert(std::ostream& out, const automaton::NFTA& a); static void convert(std::ostream& out, const automaton::DFTA& a); static void convert(std::ostream& out, const automaton::DPDA < > & a); - static void convert(std::ostream& out, const automaton::SinglePopDPDA& a); + static void convert(std::ostream& out, const automaton::SinglePopDPDA < > & a); static void convert(std::ostream& out, const automaton::InputDrivenDPDA < > & a); static void convert(std::ostream& out, const automaton::InputDrivenNPDA < > & a); static void convert(std::ostream& out, const automaton::VisiblyPushdownDPDA& a); diff --git a/aconvert2/src/GasTexConverter.cpp b/aconvert2/src/GasTexConverter.cpp index 1e505cf48f857b26e8a06594dfdf76cfbecf3a3d..1073281008084d3d53cc7c962680d1f45c65d162 100644 --- a/aconvert2/src/GasTexConverter.cpp +++ b/aconvert2/src/GasTexConverter.cpp @@ -337,7 +337,7 @@ void GasTexConverter::convert(std::ostream& out, const automaton::DPDA < > & a) auto GasTexConverterDPDA = GasTexConverter::RegistratorWrapper<void, automaton::DPDA < > >(GasTexConverter::convert); -void GasTexConverter::convert(std::ostream& out, const automaton::SinglePopDPDA& a) { +void GasTexConverter::convert(std::ostream& out, const automaton::SinglePopDPDA < > & a) { out << "\\begin{center}\n"; out << "\\begin{picture}(,)(,)\n"; @@ -376,7 +376,7 @@ void GasTexConverter::convert(std::ostream& out, const automaton::SinglePopDPDA& out << "\\end{picture}\n"; } -auto GasTexConverterSinglePopDPDA = GasTexConverter::RegistratorWrapper<void, automaton::SinglePopDPDA>(GasTexConverter::convert); +auto GasTexConverterSinglePopDPDA = GasTexConverter::RegistratorWrapper<void, automaton::SinglePopDPDA < > >(GasTexConverter::convert); void GasTexConverter::convert(std::ostream& out, const automaton::InputDrivenDPDA < > & a) { out << "\\begin{center}\n"; @@ -945,7 +945,7 @@ void GasTexConverter::transitions(const automaton::DPDA < > & pda, std::ostream& printTransitionMap(transitionMap, out); } -void GasTexConverter::transitions(const automaton::SinglePopDPDA& pda, std::ostream& out) { +void GasTexConverter::transitions(const automaton::SinglePopDPDA < > & pda, std::ostream& out) { std::map<std::pair<std::string, std::string>, std::string> transitionMap; for (const auto& transition : pda.getTransitions()) { diff --git a/aconvert2/src/GasTexConverter.h b/aconvert2/src/GasTexConverter.h index 8e46d529a6edf22d2e5fd455cd38f289b2255992..9070b9ddb34d7745024b69c34a1a172e6de62bb8 100644 --- a/aconvert2/src/GasTexConverter.h +++ b/aconvert2/src/GasTexConverter.h @@ -30,7 +30,7 @@ class GasTexConverter : public std::SingleDispatchFirstStaticParam<GasTexConvert static void transitions(const automaton::NFTA& fsm, std::ostream& out); static void transitions(const automaton::DFTA& fsm, std::ostream& out); static void transitions(const automaton::DPDA < > & pda, std::ostream& out); - static void transitions(const automaton::SinglePopDPDA& tm, std::ostream& out); + static void transitions(const automaton::SinglePopDPDA < > & tm, std::ostream& out); static void transitions(const automaton::InputDrivenDPDA < > & pda, std::ostream& out); static void transitions(const automaton::InputDrivenNPDA < > & pda, std::ostream& out); static void transitions(const automaton::VisiblyPushdownDPDA& tm, std::ostream& out); @@ -52,7 +52,7 @@ public: static void convert(std::ostream& out, const automaton::NFTA& a); static void convert(std::ostream& out, const automaton::DFTA& a); static void convert(std::ostream& out, const automaton::DPDA < > & a); - static void convert(std::ostream& out, const automaton::SinglePopDPDA& a); + static void convert(std::ostream& out, const automaton::SinglePopDPDA < > & a); static void convert(std::ostream& out, const automaton::InputDrivenDPDA < > & a); static void convert(std::ostream& out, const automaton::InputDrivenNPDA < > & a); static void convert(std::ostream& out, const automaton::VisiblyPushdownDPDA& a); diff --git a/aconvert2/src/TikZConverter.cpp b/aconvert2/src/TikZConverter.cpp index c840eeb9a5694629e1c06e8fcd8c3ce86fa4632c..03f220f212bb7c48cf419a1142908bb13b9a3857 100644 --- a/aconvert2/src/TikZConverter.cpp +++ b/aconvert2/src/TikZConverter.cpp @@ -306,7 +306,7 @@ void TikZConverter::convert ( std::ostream & out, const automaton::DPDA < > & a auto TikZConverterDPDA = TikZConverter::RegistratorWrapper < void, automaton::DPDA < > > ( TikZConverter::convert ); -void TikZConverter::convert ( std::ostream & out, const automaton::SinglePopDPDA & a ) { +void TikZConverter::convert ( std::ostream & out, const automaton::SinglePopDPDA < > & a ) { out << "\\begin{tikzpicture}\n"; int cnt = 1; @@ -333,7 +333,7 @@ void TikZConverter::convert ( std::ostream & out, const automaton::SinglePopDPDA out << "\\end{tikzpicture}"; } -auto TikZConverterSinglePopDPDA = TikZConverter::RegistratorWrapper < void, automaton::SinglePopDPDA > ( TikZConverter::convert ); +auto TikZConverterSinglePopDPDA = TikZConverter::RegistratorWrapper < void, automaton::SinglePopDPDA < > > ( TikZConverter::convert ); void TikZConverter::convert ( std::ostream & out, const automaton::InputDrivenDPDA < > & a ) { out << "\\begin{tikzpicture}\n"; @@ -1043,7 +1043,7 @@ void TikZConverter::transitions ( const automaton::DPDA < > & pda, const std::ma } } -void TikZConverter::transitions ( const automaton::SinglePopDPDA & pda, const std::map < label::Label, int > & states, std::ostream & out ) { +void TikZConverter::transitions ( const automaton::SinglePopDPDA < > & pda, const std::map < label::Label, int > & states, std::ostream & out ) { std::map < std::pair < int, int >, std::string > transitions; for ( const auto & transition : pda.getTransitions ( ) ) { diff --git a/aconvert2/src/TikZConverter.h b/aconvert2/src/TikZConverter.h index 7b7b87701c82ce99e29e689bb087f49648d1ba66..c517829786c6232a4200a08b3830d12b2a6ac02a 100644 --- a/aconvert2/src/TikZConverter.h +++ b/aconvert2/src/TikZConverter.h @@ -29,7 +29,7 @@ class TikZConverter : public std::SingleDispatchFirstStaticParam < TikZConverter static void transitions ( const automaton::NFTA & fsm, const std::map < label::Label, int > & states, std::ostream & out ); static void transitions ( const automaton::DFTA & fsm, const std::map < label::Label, int > & states, std::ostream & out ); static void transitions ( const automaton::DPDA < > & pda, const std::map < label::Label, int > & states, std::ostream & out ); - static void transitions ( const automaton::SinglePopDPDA & tm, const std::map < label::Label, int > & states, std::ostream & out ); + static void transitions ( const automaton::SinglePopDPDA < > & tm, const std::map < label::Label, int > & states, std::ostream & out ); static void transitions ( const automaton::InputDrivenDPDA < > & pda, const std::map < label::Label, int > & states, std::ostream & out ); static void transitions ( const automaton::InputDrivenNPDA < > & pda, const std::map < label::Label, int > & states, std::ostream & out ); static void transitions ( const automaton::VisiblyPushdownDPDA & tm, const std::map < label::Label, int > & states, std::ostream & out ); @@ -52,7 +52,7 @@ public: static void convert ( std::ostream & out, const automaton::NFTA & a ); static void convert ( std::ostream & out, const automaton::DFTA & a ); static void convert ( std::ostream & out, const automaton::DPDA < > & a ); - static void convert ( std::ostream & out, const automaton::SinglePopDPDA & a ); + static void convert ( std::ostream & out, const automaton::SinglePopDPDA < > & a ); static void convert ( std::ostream & out, const automaton::InputDrivenDPDA < > & a ); static void convert ( std::ostream & out, const automaton::InputDrivenNPDA < > & a ); static void convert ( std::ostream & out, const automaton::VisiblyPushdownDPDA & a ); diff --git a/alib2algo/src/automaton/determinize/Determinize.cpp b/alib2algo/src/automaton/determinize/Determinize.cpp index 1967592faec9f1f17011f704444b3b5cbb6bfeb0..809ae52e03439fcdcf5122d2da94b7a4822ccce8 100644 --- a/alib2algo/src/automaton/determinize/Determinize.cpp +++ b/alib2algo/src/automaton/determinize/Determinize.cpp @@ -41,11 +41,11 @@ DPDA < > Determinize::determinize(const automaton::DPDA < > & automaton) { auto DeterminizeDPDA = Determinize::RegistratorWrapper<automaton::DPDA < >, automaton::DPDA < > >(Determinize::determinize); -SinglePopDPDA Determinize::determinize(const automaton::SinglePopDPDA& automaton) { +SinglePopDPDA < > Determinize::determinize(const automaton::SinglePopDPDA < > & automaton) { return automaton; } -auto DeterminizeSinglePopDPDA = Determinize::RegistratorWrapper<automaton::SinglePopDPDA, automaton::SinglePopDPDA>(Determinize::determinize); +auto DeterminizeSinglePopDPDA = Determinize::RegistratorWrapper<automaton::SinglePopDPDA < >, automaton::SinglePopDPDA < > >(Determinize::determinize); InputDrivenDPDA < > Determinize::determinize(const automaton::InputDrivenDPDA < > & automaton) { return automaton; diff --git a/alib2algo/src/automaton/determinize/Determinize.h b/alib2algo/src/automaton/determinize/Determinize.h index 927135b744d7d305acb612d001ab9ade92a67cc4..7d96c64b70f5532cbfd10e24a6bee40841ef1f95 100644 --- a/alib2algo/src/automaton/determinize/Determinize.h +++ b/alib2algo/src/automaton/determinize/Determinize.h @@ -36,7 +36,7 @@ public: static automaton::DFA<> determinize(const automaton::MultiInitialStateNFA < > & nfa); static automaton::DPDA < > determinize(const automaton::DPDA < > & dpda); static automaton::DPDA < > determinize(const automaton::NPDA < > & dpda); - static automaton::SinglePopDPDA determinize(const automaton::SinglePopDPDA& dpda); + static automaton::SinglePopDPDA < > determinize(const automaton::SinglePopDPDA < > & dpda); static automaton::VisiblyPushdownDPDA determinize(const automaton::VisiblyPushdownDPDA& nondeterministic); static automaton::VisiblyPushdownDPDA determinize(const automaton::VisiblyPushdownNPDA& nondeterministic); static automaton::InputDrivenDPDA < > determinize(const automaton::InputDrivenDPDA < > & nfa); diff --git a/alib2algo/src/automaton/simplify/Rename.cpp b/alib2algo/src/automaton/simplify/Rename.cpp index 51fbff6b7d0493c520208265b91ab111ea453b4e..ce1630e0d57c389c65df079665a2632e4792d3d2 100644 --- a/alib2algo/src/automaton/simplify/Rename.cpp +++ b/alib2algo/src/automaton/simplify/Rename.cpp @@ -98,7 +98,7 @@ automaton::DPDA < > Rename::rename(const automaton::DPDA < > & pda) { auto RenameDPDA = Rename::RegistratorWrapper<automaton::DPDA < >, automaton::DPDA < > >(Rename::rename); -automaton::SinglePopDPDA Rename::rename(const automaton::SinglePopDPDA& pda) { +automaton::SinglePopDPDA < > Rename::rename(const automaton::SinglePopDPDA < > & pda) { int counterState = 0; std::map<label::Label, int > renamingDataState; int counterSymbol = 0; @@ -111,7 +111,7 @@ automaton::SinglePopDPDA Rename::rename(const automaton::SinglePopDPDA& pda) { for(const alphabet::Symbol & symbol : pda.getPushdownStoreAlphabet()) renamingDataSymbol.insert(std::make_pair(symbol, counterSymbol++)); - automaton::SinglePopDPDA result(label::labelFrom(renamingDataState.find(pda.getInitialState())->second), alphabet::symbolFrom(renamingDataSymbol.find(pda.getInitialSymbol())->second)); + automaton::SinglePopDPDA < > result(label::labelFrom(renamingDataState.find(pda.getInitialState())->second), alphabet::symbolFrom(renamingDataSymbol.find(pda.getInitialSymbol())->second)); result.setInputAlphabet(pda.getInputAlphabet()); @@ -137,7 +137,7 @@ automaton::SinglePopDPDA Rename::rename(const automaton::SinglePopDPDA& pda) { return result; } -auto RenameSinglePopDPDA = Rename::RegistratorWrapper<automaton::SinglePopDPDA, automaton::SinglePopDPDA>(Rename::rename); +auto RenameSinglePopDPDA = Rename::RegistratorWrapper<automaton::SinglePopDPDA < >, automaton::SinglePopDPDA < > >(Rename::rename); automaton::InputDrivenDPDA < > Rename::rename(const automaton::InputDrivenDPDA < > & pda) { int counter = 0; diff --git a/alib2algo/src/automaton/simplify/Rename.h b/alib2algo/src/automaton/simplify/Rename.h index 937a6f93c5cf60832bcdc32d4e4e0fa9ae0cfddf..ccec6f45b6c0df3cc8d59851f93fb9e8d1005fc8 100644 --- a/alib2algo/src/automaton/simplify/Rename.h +++ b/alib2algo/src/automaton/simplify/Rename.h @@ -30,7 +30,7 @@ public: static automaton::DFA<> rename(const automaton::DFA<>& dfa); static automaton::DPDA < > rename(const automaton::DPDA < > & pda); - static automaton::SinglePopDPDA rename(const automaton::SinglePopDPDA& pda); + static automaton::SinglePopDPDA < > rename(const automaton::SinglePopDPDA < > & pda); static automaton::InputDrivenDPDA < > rename(const automaton::InputDrivenDPDA < > & pda); static automaton::VisiblyPushdownDPDA rename(const automaton::VisiblyPushdownDPDA& pda); static automaton::RealTimeHeightDeterministicDPDA < > rename(const automaton::RealTimeHeightDeterministicDPDA < > & pda); diff --git a/alib2data/src/automaton/AutomatonFeatures.h b/alib2data/src/automaton/AutomatonFeatures.h index 0db509b815011c5116f43b6dfca0ab3751cb97ff..21ca8ffe2f508176ef6cf7711af2f4648b374669 100644 --- a/alib2data/src/automaton/AutomatonFeatures.h +++ b/alib2data/src/automaton/AutomatonFeatures.h @@ -67,6 +67,7 @@ template<class SymbolType = typename alphabet::Symbol, class EpsilonType = typen class NPDA; template<class SymbolType = typename alphabet::Symbol, class EpsilonType = typename string::Epsilon < SymbolType >, class StateType = typename label::Label > class NPDTA; +template<class SymbolType = typename alphabet::Symbol, class EpsilonType = typename string::Epsilon < SymbolType >, class StateType = typename label::Label > class SinglePopDPDA; template<class SymbolType = typename alphabet::Symbol, class EpsilonType = typename string::Epsilon < SymbolType >, class StateType = typename label::Label > class SinglePopNPDA; diff --git a/alib2data/src/automaton/PDA/SinglePopDPDA.cpp b/alib2data/src/automaton/PDA/SinglePopDPDA.cpp index ba69138bd6f89f4023b9fb3692fb4b933d6371c2..db623a842492380cc56804dfe9df2aa03812a26e 100644 --- a/alib2data/src/automaton/PDA/SinglePopDPDA.cpp +++ b/alib2data/src/automaton/PDA/SinglePopDPDA.cpp @@ -6,223 +6,13 @@ */ #include "SinglePopDPDA.h" -#include <algorithm> -#include <sstream> -#include <ostream> - -#include <sax/FromXMLParserHelper.h> -#include "../common/AutomatonFromXMLParser.h" -#include "../common/AutomatonToXMLComposer.h" #include "../Automaton.h" #include <object/Object.h> #include <XmlApi.hpp> -namespace automaton { - -SinglePopDPDA::SinglePopDPDA ( std::set < label::Label > states, std::set < alphabet::Symbol > inputAlphabet, std::set < alphabet::Symbol > pushdownStoreAlphabet, label::Label initialState, alphabet::Symbol initialSymbol, std::set < label::Label > finalStates ) : std::Components < SinglePopDPDA, alphabet::Symbol, std::tuple < InputAlphabet, PushdownStoreAlphabet >, std::tuple < InitialSymbol >, label::Label, std::tuple < States, FinalStates >, std::tuple < InitialState > > ( std::make_tuple ( std::move ( inputAlphabet ), std::move ( pushdownStoreAlphabet ) ), std::make_tuple ( std::move ( initialSymbol ) ), std::make_tuple ( std::move ( states ), std::move ( finalStates ) ), std::make_tuple ( std::move ( initialState ) ) ) { -} - -SinglePopDPDA::SinglePopDPDA(label::Label initialState, alphabet::Symbol initialPushdownSymbol) : SinglePopDPDA ( std::set < label::Label > { initialState }, std::set < alphabet::Symbol > { }, std::set < alphabet::Symbol > { initialPushdownSymbol }, initialState, initialPushdownSymbol, std::set < label::Label > { }) { -} - - -AutomatonBase* SinglePopDPDA::clone() const { - return new SinglePopDPDA(*this); -} - -AutomatonBase* SinglePopDPDA::plunder() && { - return new SinglePopDPDA(std::move(*this)); -} - -bool SinglePopDPDA::addTransition(label::Label from, std::variant<string::Epsilon < >, alphabet::Symbol> input, alphabet::Symbol pop, label::Label to, std::vector<alphabet::Symbol> push) { - if (!getStates().count(from)) { - throw AutomatonException("State \"" + std::to_string ( from ) + "\" doesn't exist."); - } - - if (input.is<alphabet::Symbol>() && ! getInputAlphabet().count(input.get<alphabet::Symbol>())) { - throw AutomatonException("Input symbol \"" + std::to_string ( input.get<alphabet::Symbol>() ) + "\" doesn't exist."); - } - - if (!getStates().count(to)) { - throw AutomatonException("State \"" + std::to_string ( to ) + "\" doesn't exist."); - } - - if (!getPushdownStoreAlphabet().count(pop)) { - throw AutomatonException("Pushdown store symbol \"" + std::to_string ( pop ) + "\" doesn't exist."); - } - - for(const alphabet::Symbol& pushSymbol : push) { - if (!getPushdownStoreAlphabet().count(pushSymbol)) { - throw AutomatonException("Pushdown store symbol \"" + std::to_string ( pushSymbol ) + "\" doesn't exist."); - } - } - - std::tuple<label::Label, std::variant<string::Epsilon < >, alphabet::Symbol>, alphabet::Symbol> key(std::move(from), std::move(input), std::move(pop)); - std::pair<label::Label, std::vector<alphabet::Symbol> > value = std::make_pair(std::move(to), std::move(push)); - - if (transitions.find(key) != transitions.end()) { - if(transitions.find(key)->second == value) - return false; - else if (input.is<alphabet::Symbol>()) - throw AutomatonException("Transition (\"" + std::to_string ( std::get<0>(key) ) + "\", \"" + std::to_string ( std::get<1>(key).get<alphabet::Symbol>() ) + "\", \"" + std::to_string ( std::get<2>(key) ) + "\") -> ?? already exists."); - else - throw AutomatonException("Transition (\"" + std::to_string ( std::get<0>(key) ) + "\", \"" + std::to_string ( std::get<1>(key).get<string::Epsilon < >>() ) + "\", \"" + std::to_string ( std::get<2>(key) ) + "\") -> ?? already exists."); - } - - if(input.is<string::Epsilon < >>()) { - if(std::any_of(transitions.begin(), transitions.end(), [&](const std::pair<std::tuple<label::Label, std::variant<string::Epsilon < >, alphabet::Symbol>, alphabet::Symbol>, std::pair<label::Label, std::vector<alphabet::Symbol> > > & transition) { - if(std::get<0>(transition.first) == std::get<0>(key) && std::get<2>(transition.first) == std::get<2>(key)) return true; - return false; - })) - throw AutomatonException("Conflicting transition"); - } else { - std::tuple<label::Label, std::variant<string::Epsilon < >, alphabet::Symbol>, alphabet::Symbol> key2(std::get<0>(key), std::variant<string::Epsilon < >, alphabet::Symbol>{ string::Epsilon < >::EPSILON }, std::get<2>(key)); - if(transitions.find(key2) != transitions.end()) - throw AutomatonException("Conflicting transition"); - } - - transitions.insert(std::make_pair(std::move(key), std::move(value)) ); - return true; -} - -bool SinglePopDPDA::addTransition(label::Label from, alphabet::Symbol input, alphabet::Symbol pop, label::Label to, std::vector<alphabet::Symbol> push) { - std::variant<string::Epsilon < >, alphabet::Symbol> inputVariant(std::move(input)); - return addTransition(std::move(from), std::move(inputVariant), std::move(pop), std::move(to), std::move(push)); -} - -bool SinglePopDPDA::addTransition(label::Label from, alphabet::Symbol pop, label::Label to, std::vector<alphabet::Symbol> push) { - std::variant<string::Epsilon < >, alphabet::Symbol> inputVariant(string::Epsilon < >::EPSILON); - return addTransition(std::move(from), std::move(inputVariant), std::move(pop), std::move(to), std::move(push)); -} - -bool SinglePopDPDA::removeTransition(const label::Label& from, const std::variant<string::Epsilon < >, alphabet::Symbol>& input, const alphabet::Symbol& pop, const label::Label& to, const std::vector<alphabet::Symbol>& push) { - std::tuple<label::Label, std::variant<string::Epsilon < >, alphabet::Symbol>, alphabet::Symbol> key(from, input, pop); - std::pair<label::Label, std::vector<alphabet::Symbol> > value = std::make_pair(to, push); - - if (transitions.find(key) == transitions.end()) - return false; - - if(transitions.find(key)->second != value) { - if (input.is<alphabet::Symbol>()) - throw AutomatonException("Transition (\"" + std::to_string ( from ) + "\", \"" + std::to_string ( input.get<alphabet::Symbol>() ) + "\") -> \"" + std::to_string ( to ) + "\" doesn't exist."); - else - throw AutomatonException("Transition (\"" + std::to_string ( from ) + "\", \"" + std::to_string ( input.get<string::Epsilon < >>() ) + "\") -> \"" + std::to_string ( to ) + "\" doesn't exist."); - } - - transitions.erase(key); - return true; -} - -bool SinglePopDPDA::removeTransition(const label::Label& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const label::Label& to, const std::vector<alphabet::Symbol>& push) { - std::variant<string::Epsilon < >, alphabet::Symbol> inputVariant(input); - return removeTransition(from, inputVariant, pop, to, push); -} - -bool SinglePopDPDA::removeTransition(const label::Label& from, const alphabet::Symbol& pop, const label::Label& to, const std::vector<alphabet::Symbol>& push) { - std::variant<string::Epsilon < >, alphabet::Symbol> inputVariant(string::Epsilon < >::EPSILON); - return removeTransition(from, inputVariant, pop, to, push); -} - -const std::map<std::tuple<label::Label, std::variant<string::Epsilon < >, alphabet::Symbol>, alphabet::Symbol>, std::pair<label::Label, std::vector<alphabet::Symbol> > >& SinglePopDPDA::getTransitions() const { - return transitions; -} - -int SinglePopDPDA::compare(const SinglePopDPDA& other) const { - auto first = std::tie(getStates(), getInputAlphabet(), getInitialState(), getFinalStates(), getPushdownStoreAlphabet(), getInitialSymbol(), transitions); - auto second = std::tie(other.getStates(), other.getInputAlphabet(), other.getInitialState(), other.getFinalStates(), other.getPushdownStoreAlphabet(), other.getInitialSymbol(), other.transitions); - - std::compare<decltype(first)> comp; - return comp(first, second); -} - -void SinglePopDPDA::operator>>(std::ostream& out) const { - out << "(DPDA " - << "states = " << getStates() - << "inputAlphabet = " << getInputAlphabet() - << "initialState = " << getInitialState() - << "finalStates = " << getFinalStates() - << "pushdownStoreAlphabet = " << getPushdownStoreAlphabet() - << "initialSymbol = " << getInitialSymbol() - << "transitions = " << transitions - << ")"; -} - -SinglePopDPDA::operator std::string () const { - std::stringstream ss; - ss << *this; - return ss.str(); -} - -SinglePopDPDA SinglePopDPDA::parse(std::deque<sax::Token>::iterator& input) { - sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, SinglePopDPDA::getXmlTagName()); - - std::set<label::Label> states = AutomatonFromXMLParser::parseStates<label::Label>(input); - std::set<alphabet::Symbol> inputSymbols = AutomatonFromXMLParser::parseInputAlphabet<alphabet::Symbol>(input); - std::set<alphabet::Symbol> stackSymbols = AutomatonFromXMLParser::parsePushdownStoreAlphabet<alphabet::Symbol>(input); - label::Label initialState = AutomatonFromXMLParser::parseInitialState<label::Label>(input); - alphabet::Symbol initialPushdownStoreSymbol = AutomatonFromXMLParser::parseInitialPushdownStoreSymbol<alphabet::Symbol>(input); - std::set<label::Label> finalStates = AutomatonFromXMLParser::parseFinalStates<label::Label>(input); - - SinglePopDPDA automaton(std::move(initialState), std::move(initialPushdownStoreSymbol)); - automaton.setStates(std::move(states)); - automaton.setInputAlphabet(std::move(inputSymbols)); - automaton.setPushdownStoreAlphabet(std::move(stackSymbols)); - automaton.setFinalStates(std::move(finalStates)); - - AutomatonFromXMLParser::parseTransitions<SinglePopDPDA>(input, automaton); - - sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::END_ELEMENT, SinglePopDPDA::getXmlTagName()); - return automaton; -} - -void SinglePopDPDA::parseTransition(std::deque<sax::Token>::iterator& input, SinglePopDPDA& automaton) { - sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, "transition"); - label::Label from = AutomatonFromXMLParser::parseTransitionFrom<label::Label>(input); - std::variant<string::Epsilon < >, alphabet::Symbol> inputSymbol = AutomatonFromXMLParser::parseTransitionInputEpsilonSymbol<string::Epsilon < >, alphabet::Symbol>(input); - alphabet::Symbol pop = AutomatonFromXMLParser::parseTransitionSinglePop<alphabet::Symbol>(input); - label::Label to = AutomatonFromXMLParser::parseTransitionTo<label::Label>(input); - std::vector<alphabet::Symbol> push = AutomatonFromXMLParser::parseTransitionPush<alphabet::Symbol>(input); - sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::END_ELEMENT, "transition"); - - automaton.addTransition(std::move(from), std::move(inputSymbol), std::move(pop), std::move(to), std::move(push)); -} - -void SinglePopDPDA::compose(std::deque<sax::Token>& out) const { - out.emplace_back(SinglePopDPDA::getXmlTagName(), sax::Token::TokenType::START_ELEMENT); - - AutomatonToXMLComposer::composeStates(out, this->getStates()); - AutomatonToXMLComposer::composeInputAlphabet(out, this->getInputAlphabet()); - AutomatonToXMLComposer::composePushdownStoreAlphabet(out, this->getPushdownStoreAlphabet()); - AutomatonToXMLComposer::composeInitialState(out, this->getInitialState()); - AutomatonToXMLComposer::composeInitialPushdownStoreSymbol(out, this->getInitialSymbol()); - AutomatonToXMLComposer::composeFinalStates(out, this->getFinalStates()); - composeTransitions(out); - - out.emplace_back(SinglePopDPDA::getXmlTagName(), sax::Token::TokenType::END_ELEMENT); -} - -void SinglePopDPDA::composeTransitions(std::deque<sax::Token>& out) const { - out.emplace_back("transitions", sax::Token::TokenType::START_ELEMENT); - for(const auto& transition : this->getTransitions()) { - out.emplace_back("transition", sax::Token::TokenType::START_ELEMENT); - - AutomatonToXMLComposer::composeTransitionFrom(out, std::get<0>(transition.first)); - AutomatonToXMLComposer::composeTransitionInputEpsilonSymbol(out, std::get<1>(transition.first)); - AutomatonToXMLComposer::composeTransitionSinglePop(out, std::get<2>(transition.first)); - AutomatonToXMLComposer::composeTransitionTo(out, transition.second.first); - AutomatonToXMLComposer::composeTransitionPush(out, transition.second.second); - - out.emplace_back("transition", sax::Token::TokenType::END_ELEMENT); - } - - out.emplace_back("transitions", sax::Token::TokenType::END_ELEMENT); -} - -} /* namespace automaton */ - namespace alib { -auto singlePopDPDAParserRegister = xmlApi<automaton::Automaton>::ParserRegister<automaton::SinglePopDPDA>(); -auto singlePopDPDAParserRegister2 = xmlApi<alib::Object>::ParserRegister<automaton::SinglePopDPDA>(); +auto singlePopDPDAParserRegister = xmlApi<automaton::Automaton>::ParserRegister<automaton::SinglePopDPDA < > >(); +auto singlePopDPDAParserRegister2 = xmlApi<alib::Object>::ParserRegister<automaton::SinglePopDPDA < > >(); } /* namespace alib */ diff --git a/alib2data/src/automaton/PDA/SinglePopDPDA.h b/alib2data/src/automaton/PDA/SinglePopDPDA.h index 2ba15c9668756364aa35ed7218bf9473b5624c62..53262d2e7b0d00cf8a7d483ac62dbcd90505b921 100644 --- a/alib2data/src/automaton/PDA/SinglePopDPDA.h +++ b/alib2data/src/automaton/PDA/SinglePopDPDA.h @@ -8,16 +8,22 @@ #ifndef SINGLE_POP_DPDA_H_ #define SINGLE_POP_DPDA_H_ -#include "../AutomatonException.h" #include <set> #include <map> #include <vector> #include <variant> +#include <algorithm> +#include <sstream> +#include <ostream> + #include <core/components.hpp> +#include <sax/FromXMLParserHelper.h> + #include "../AutomatonBase.h" -#include "../../alphabet/Symbol.h" -#include "../../label/Label.h" -#include "../../string/Epsilon.h" +#include "../AutomatonFeatures.h" +#include "../AutomatonException.h" +#include "../common/AutomatonFromXMLParser.h" +#include "../common/AutomatonToXMLComposer.h" namespace automaton { @@ -35,104 +41,105 @@ class InitialState; * if $\delta (q, a, r) \neq \emptyset$, $\delta (q, a, s) \neq \emptyset$ then $r \neq s$. * if $\delta(q, a, r) \neq \emptyset$, $\delta (q, \varepsilon, s) \neq \emptyset$, then $r \neq s$. */ -class SinglePopDPDA : public AutomatonBase, public std::Components < SinglePopDPDA, alphabet::Symbol, std::tuple < InputAlphabet, PushdownStoreAlphabet >, std::tuple < InitialSymbol >, label::Label, std::tuple < States, FinalStates >, std::tuple < InitialState > > { +template < class SymbolType, class EpsilonType, class StateType > +class SinglePopDPDA : public AutomatonBase, public std::Components < SinglePopDPDA < SymbolType, EpsilonType, StateType >, SymbolType, std::tuple < InputAlphabet, PushdownStoreAlphabet >, std::tuple < InitialSymbol >, StateType, std::tuple < States, FinalStates >, std::tuple < InitialState > > { protected: - std::map < std::tuple < label::Label, std::variant < string::Epsilon < >, alphabet::Symbol >, alphabet::Symbol >, std::pair < label::Label, std::vector < alphabet::Symbol > > > transitions; + std::map < std::tuple < StateType, std::variant < EpsilonType, SymbolType >, SymbolType >, std::pair < StateType, std::vector < SymbolType > > > transitions; public: - explicit SinglePopDPDA ( std::set < label::Label > states, std::set < alphabet::Symbol > inputAlphabet, std::set < alphabet::Symbol > pushdownStoreSymbol, label::Label initialState, alphabet::Symbol initialPushdownSymbol, std::set < label::Label > finalStates ); - explicit SinglePopDPDA ( label::Label initialState, alphabet::Symbol initialPushdownSymbol ); + explicit SinglePopDPDA ( std::set < StateType > states, std::set < SymbolType > inputAlphabet, std::set < SymbolType > pushdownStoreSymbol, StateType initialState, SymbolType initialPushdownSymbol, std::set < StateType > finalStates ); + explicit SinglePopDPDA ( StateType initialState, SymbolType initialPushdownSymbol ); virtual AutomatonBase * clone ( ) const; virtual AutomatonBase * plunder ( ) &&; - const label::Label & getInitialState ( ) const { - return accessElement < InitialState > ( ).get ( ); + const StateType & getInitialState ( ) const { + return this->template accessElement < InitialState > ( ).get ( ); } - bool setInitialState ( label::Label state ) { - return accessElement < InitialState > ( ).set ( std::move ( state ) ); + bool setInitialState ( StateType state ) { + return this->template accessElement < InitialState > ( ).set ( std::move ( state ) ); } - const std::set < label::Label > & getStates ( ) const { - return accessComponent < States > ( ).get ( ); + const std::set < StateType > & getStates ( ) const { + return this->template accessComponent < States > ( ).get ( ); } - bool addState ( label::Label state ) { - return accessComponent < States > ( ).add ( std::move ( state ) ); + bool addState ( StateType state ) { + return this->template accessComponent < States > ( ).add ( std::move ( state ) ); } - void setStates ( std::set < label::Label > states ) { - accessComponent < States > ( ).set ( std::move ( states ) ); + void setStates ( std::set < StateType > states ) { + this->template accessComponent < States > ( ).set ( std::move ( states ) ); } - void removeState ( const label::Label & state ) { - accessComponent < States > ( ).remove ( state ); + void removeState ( const StateType & state ) { + this->template accessComponent < States > ( ).remove ( state ); } - const std::set < label::Label > & getFinalStates ( ) const { - return accessComponent < FinalStates > ( ).get ( ); + const std::set < StateType > & getFinalStates ( ) const { + return this->template accessComponent < FinalStates > ( ).get ( ); } - bool addFinalState ( label::Label state ) { - return accessComponent < FinalStates > ( ).add ( std::move ( state ) ); + bool addFinalState ( StateType state ) { + return this->template accessComponent < FinalStates > ( ).add ( std::move ( state ) ); } - void setFinalStates ( std::set < label::Label > states ) { - accessComponent < FinalStates > ( ).set ( std::move ( states ) ); + void setFinalStates ( std::set < StateType > states ) { + this->template accessComponent < FinalStates > ( ).set ( std::move ( states ) ); } - void removeFinalState ( const label::Label & state ) { - accessComponent < FinalStates > ( ).remove ( state ); + void removeFinalState ( const StateType & state ) { + this->template accessComponent < FinalStates > ( ).remove ( state ); } - const std::set < alphabet::Symbol > & getPushdownStoreAlphabet ( ) const { - return accessComponent < PushdownStoreAlphabet > ( ).get ( ); + const std::set < SymbolType > & getPushdownStoreAlphabet ( ) const { + return this->template accessComponent < PushdownStoreAlphabet > ( ).get ( ); } - bool addPushdownStoreSymbol ( alphabet::Symbol symbol ) { - return accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbol ) ); + bool addPushdownStoreSymbol ( SymbolType symbol ) { + return this->template accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbol ) ); } - void addPushdownStoreSymbols ( std::set < alphabet::Symbol > symbols ) { - accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbols ) ); + void addPushdownStoreSymbols ( std::set < SymbolType > symbols ) { + this->template accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbols ) ); } - void setPushdownStoreAlphabet ( std::set < alphabet::Symbol > symbols ) { - accessComponent < PushdownStoreAlphabet > ( ).set ( std::move ( symbols ) ); + void setPushdownStoreAlphabet ( std::set < SymbolType > symbols ) { + this->template accessComponent < PushdownStoreAlphabet > ( ).set ( std::move ( symbols ) ); } - void removePushdownStoreSymbol ( const alphabet::Symbol & symbol ) { - accessComponent < PushdownStoreAlphabet > ( ).remove ( symbol ); + void removePushdownStoreSymbol ( const SymbolType & symbol ) { + this->template accessComponent < PushdownStoreAlphabet > ( ).remove ( symbol ); } - const alphabet::Symbol & getInitialSymbol ( ) const { - return accessElement < InitialSymbol > ( ).get ( ); + const SymbolType & getInitialSymbol ( ) const { + return this->template accessElement < InitialSymbol > ( ).get ( ); } - bool setInitialSymbol ( alphabet::Symbol symbol ) { - return accessElement < InitialSymbol > ( ).set ( std::move ( symbol ) ); + bool setInitialSymbol ( SymbolType symbol ) { + return this->template accessElement < InitialSymbol > ( ).set ( std::move ( symbol ) ); } - const std::set < alphabet::Symbol > & getInputAlphabet ( ) const { - return accessComponent < InputAlphabet > ( ).get ( ); + const std::set < SymbolType > & getInputAlphabet ( ) const { + return this->template accessComponent < InputAlphabet > ( ).get ( ); } - bool addInputSymbol ( alphabet::Symbol symbol ) { - return accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) ); + bool addInputSymbol ( SymbolType symbol ) { + return this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) ); } - void addInputSymbols ( std::set < alphabet::Symbol > symbols ) { - accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) ); + void addInputSymbols ( std::set < SymbolType > symbols ) { + this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) ); } - void setInputAlphabet ( std::set < alphabet::Symbol > symbols ) { - accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) ); + void setInputAlphabet ( std::set < SymbolType > symbols ) { + this->template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) ); } - void removeInputSymbol ( const alphabet::Symbol & symbol ) { - accessComponent < InputAlphabet > ( ).remove ( symbol ); + void removeInputSymbol ( const SymbolType & symbol ) { + this->template accessComponent < InputAlphabet > ( ).remove ( symbol ); } /** @@ -141,27 +148,27 @@ public: * @throws AutomatonException when some part of the transition is not present * in the SinglePopDPDA (state, input symbol, stack symbol) or when transition already exists */ - bool addTransition ( label::Label from, std::variant < string::Epsilon < >, alphabet::Symbol > input, alphabet::Symbol pop, label::Label to, std::vector < alphabet::Symbol > push ); + bool addTransition ( StateType from, std::variant < EpsilonType, SymbolType > input, SymbolType pop, StateType to, std::vector < SymbolType > push ); - bool addTransition ( label::Label from, alphabet::Symbol input, alphabet::Symbol pop, label::Label to, std::vector < alphabet::Symbol > push ); + bool addTransition ( StateType from, SymbolType input, SymbolType pop, StateType to, std::vector < SymbolType > push ); - bool addTransition ( label::Label from, alphabet::Symbol pop, label::Label to, std::vector < alphabet::Symbol > push ); + bool addTransition ( StateType from, SymbolType pop, StateType to, std::vector < SymbolType > push ); /** * Removes the transition from the SinglePopDPDA. * @param transition transition to remove * @throws AutomatonException when transition is not present in the SinglePopDPDA */ - bool removeTransition ( const label::Label & from, const std::variant < string::Epsilon < >, alphabet::Symbol > & input, const alphabet::Symbol & pop, const label::Label & to, const std::vector < alphabet::Symbol > & push ); + bool removeTransition ( const StateType & from, const std::variant < EpsilonType, SymbolType > & input, const SymbolType & pop, const StateType & to, const std::vector < SymbolType > & push ); - bool removeTransition ( const label::Label & from, const alphabet::Symbol & input, const alphabet::Symbol & pop, const label::Label & to, const std::vector < alphabet::Symbol > & push ); + bool removeTransition ( const StateType & from, const SymbolType & input, const SymbolType & pop, const StateType & to, const std::vector < SymbolType > & push ); - bool removeTransition ( const label::Label & from, const alphabet::Symbol & pop, const label::Label & to, const std::vector < alphabet::Symbol > & push ); + bool removeTransition ( const StateType & from, const SymbolType & pop, const StateType & to, const std::vector < SymbolType > & push ); /** * @return SinglePopDPDA transitions */ - const std::map < std::tuple < label::Label, std::variant < string::Epsilon < >, alphabet::Symbol >, alphabet::Symbol >, std::pair < label::Label, std::vector < alphabet::Symbol > > > & getTransitions ( ) const; + const std::map < std::tuple < StateType, std::variant < EpsilonType, SymbolType >, SymbolType >, std::pair < StateType, std::vector < SymbolType > > > & getTransitions ( ) const; virtual int compare ( const ObjectBase & other ) const { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); @@ -188,34 +195,250 @@ public: void composeTransitions ( std::deque < sax::Token > & out ) const; }; +template < class SymbolType, class EpsilonType, class StateType > +SinglePopDPDA < SymbolType, EpsilonType, StateType >::SinglePopDPDA ( std::set < StateType > states, std::set < SymbolType > inputAlphabet, std::set < SymbolType > pushdownStoreAlphabet, StateType initialState, SymbolType initialSymbol, std::set < StateType > finalStates ) : std::Components < SinglePopDPDA, SymbolType, std::tuple < InputAlphabet, PushdownStoreAlphabet >, std::tuple < InitialSymbol >, StateType, std::tuple < States, FinalStates >, std::tuple < InitialState > > ( std::make_tuple ( std::move ( inputAlphabet ), std::move ( pushdownStoreAlphabet ) ), std::make_tuple ( std::move ( initialSymbol ) ), std::make_tuple ( std::move ( states ), std::move ( finalStates ) ), std::make_tuple ( std::move ( initialState ) ) ) { +} + +template < class SymbolType, class EpsilonType, class StateType > +SinglePopDPDA < SymbolType, EpsilonType, StateType >::SinglePopDPDA(StateType initialState, SymbolType initialPushdownSymbol) : SinglePopDPDA ( std::set < StateType > { initialState }, std::set < SymbolType > { }, std::set < SymbolType > { initialPushdownSymbol }, initialState, initialPushdownSymbol, std::set < StateType > { }) { +} + +template < class SymbolType, class EpsilonType, class StateType > +AutomatonBase* SinglePopDPDA < SymbolType, EpsilonType, StateType >::clone() const { + return new SinglePopDPDA(*this); +} + +template < class SymbolType, class EpsilonType, class StateType > +AutomatonBase* SinglePopDPDA < SymbolType, EpsilonType, StateType >::plunder() && { + return new SinglePopDPDA(std::move(*this)); +} + +template < class SymbolType, class EpsilonType, class StateType > +bool SinglePopDPDA < SymbolType, EpsilonType, StateType >::addTransition(StateType from, std::variant<EpsilonType, SymbolType> input, SymbolType pop, StateType to, std::vector<SymbolType> push) { + if (!getStates().count(from)) { + throw AutomatonException("State \"" + std::to_string ( from ) + "\" doesn't exist."); + } + + if (input.template is<SymbolType>() && ! getInputAlphabet().count(input.template get<SymbolType>())) { + throw AutomatonException("Input symbol \"" + std::to_string ( input.template get<SymbolType>() ) + "\" doesn't exist."); + } + + if (!getStates().count(to)) { + throw AutomatonException("State \"" + std::to_string ( to ) + "\" doesn't exist."); + } + + if (!getPushdownStoreAlphabet().count(pop)) { + throw AutomatonException("Pushdown store symbol \"" + std::to_string ( pop ) + "\" doesn't exist."); + } + + for(const SymbolType& pushSymbol : push) { + if (!getPushdownStoreAlphabet().count(pushSymbol)) { + throw AutomatonException("Pushdown store symbol \"" + std::to_string ( pushSymbol ) + "\" doesn't exist."); + } + } + + std::tuple<StateType, std::variant<EpsilonType, SymbolType>, SymbolType> key(std::move(from), std::move(input), std::move(pop)); + std::pair<StateType, std::vector<SymbolType> > value = std::make_pair(std::move(to), std::move(push)); + + if (transitions.find(key) != transitions.end()) { + if(transitions.find(key)->second == value) + return false; + else if (input.template is<SymbolType>()) + throw AutomatonException("Transition (\"" + std::to_string ( std::get<0>(key) ) + "\", \"" + std::to_string ( std::get<1>(key).template get<SymbolType>() ) + "\", \"" + std::to_string ( std::get<2>(key) ) + "\") -> ?? already exists."); + else + throw AutomatonException("Transition (\"" + std::to_string ( std::get<0>(key) ) + "\", \"" + std::to_string ( std::get<1>(key).template get<EpsilonType>() ) + "\", \"" + std::to_string ( std::get<2>(key) ) + "\") -> ?? already exists."); + } + + if(input.template is<EpsilonType>()) { + if(std::any_of(transitions.begin(), transitions.end(), [&](const std::pair<std::tuple<StateType, std::variant<EpsilonType, SymbolType>, SymbolType>, std::pair<StateType, std::vector<SymbolType> > > & transition) { + if(std::get<0>(transition.first) == std::get<0>(key) && std::get<2>(transition.first) == std::get<2>(key)) return true; + return false; + })) + throw AutomatonException("Conflicting transition"); + } else { + std::tuple<StateType, std::variant<EpsilonType, SymbolType>, SymbolType> key2(std::get<0>(key), std::variant<EpsilonType, SymbolType>{ EpsilonType::EPSILON }, std::get<2>(key)); + if(transitions.find(key2) != transitions.end()) + throw AutomatonException("Conflicting transition"); + } + + transitions.insert(std::make_pair(std::move(key), std::move(value)) ); + return true; +} + +template < class SymbolType, class EpsilonType, class StateType > +bool SinglePopDPDA < SymbolType, EpsilonType, StateType >::addTransition(StateType from, SymbolType input, SymbolType pop, StateType to, std::vector<SymbolType> push) { + std::variant<EpsilonType, SymbolType> inputVariant(std::move(input)); + return addTransition(std::move(from), std::move(inputVariant), std::move(pop), std::move(to), std::move(push)); +} + +template < class SymbolType, class EpsilonType, class StateType > +bool SinglePopDPDA < SymbolType, EpsilonType, StateType >::addTransition(StateType from, SymbolType pop, StateType to, std::vector<SymbolType> push) { + std::variant<EpsilonType, SymbolType> inputVariant(EpsilonType::EPSILON); + return addTransition(std::move(from), std::move(inputVariant), std::move(pop), std::move(to), std::move(push)); +} + +template < class SymbolType, class EpsilonType, class StateType > +bool SinglePopDPDA < SymbolType, EpsilonType, StateType >::removeTransition(const StateType& from, const std::variant<EpsilonType, SymbolType>& input, const SymbolType& pop, const StateType& to, const std::vector<SymbolType>& push) { + std::tuple<StateType, std::variant<EpsilonType, SymbolType>, SymbolType> key(from, input, pop); + std::pair<StateType, std::vector<SymbolType> > value = std::make_pair(to, push); + + if (transitions.find(key) == transitions.end()) + return false; + + if(transitions.find(key)->second != value) { + if (input.template is<SymbolType>()) + throw AutomatonException("Transition (\"" + std::to_string ( from ) + "\", \"" + std::to_string ( input.template get<SymbolType>() ) + "\") -> \"" + std::to_string ( to ) + "\" doesn't exist."); + else + throw AutomatonException("Transition (\"" + std::to_string ( from ) + "\", \"" + std::to_string ( input.template get<EpsilonType>() ) + "\") -> \"" + std::to_string ( to ) + "\" doesn't exist."); + } + + transitions.erase(key); + return true; +} + +template < class SymbolType, class EpsilonType, class StateType > +bool SinglePopDPDA < SymbolType, EpsilonType, StateType >::removeTransition(const StateType& from, const SymbolType& input, const SymbolType& pop, const StateType& to, const std::vector<SymbolType>& push) { + std::variant<EpsilonType, SymbolType> inputVariant(input); + return removeTransition(from, inputVariant, pop, to, push); +} + +template < class SymbolType, class EpsilonType, class StateType > +bool SinglePopDPDA < SymbolType, EpsilonType, StateType >::removeTransition(const StateType& from, const SymbolType& pop, const StateType& to, const std::vector<SymbolType>& push) { + std::variant<EpsilonType, SymbolType> inputVariant(EpsilonType::EPSILON); + return removeTransition(from, inputVariant, pop, to, push); +} + +template < class SymbolType, class EpsilonType, class StateType > +const std::map<std::tuple<StateType, std::variant<EpsilonType, SymbolType>, SymbolType>, std::pair<StateType, std::vector<SymbolType> > >& SinglePopDPDA < SymbolType, EpsilonType, StateType >::getTransitions() const { + return transitions; +} + +template < class SymbolType, class EpsilonType, class StateType > +int SinglePopDPDA < SymbolType, EpsilonType, StateType >::compare(const SinglePopDPDA& other) const { + auto first = std::tie(getStates(), getInputAlphabet(), getInitialState(), getFinalStates(), getPushdownStoreAlphabet(), getInitialSymbol(), transitions); + auto second = std::tie(other.getStates(), other.getInputAlphabet(), other.getInitialState(), other.getFinalStates(), other.getPushdownStoreAlphabet(), other.getInitialSymbol(), other.transitions); + + std::compare<decltype(first)> comp; + return comp(first, second); +} + +template < class SymbolType, class EpsilonType, class StateType > +void SinglePopDPDA < SymbolType, EpsilonType, StateType >::operator>>(std::ostream& out) const { + out << "(DPDA " + << "states = " << getStates() + << "inputAlphabet = " << getInputAlphabet() + << "initialState = " << getInitialState() + << "finalStates = " << getFinalStates() + << "pushdownStoreAlphabet = " << getPushdownStoreAlphabet() + << "initialSymbol = " << getInitialSymbol() + << "transitions = " << transitions + << ")"; +} + +template < class SymbolType, class EpsilonType, class StateType > +SinglePopDPDA < SymbolType, EpsilonType, StateType >::operator std::string () const { + std::stringstream ss; + ss << *this; + return ss.str(); +} + +template < class SymbolType, class EpsilonType, class StateType > +SinglePopDPDA < SymbolType, EpsilonType, StateType > SinglePopDPDA < SymbolType, EpsilonType, StateType >::parse(std::deque<sax::Token>::iterator& input) { + sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, SinglePopDPDA::getXmlTagName()); + + std::set<StateType> states = AutomatonFromXMLParser::parseStates<StateType>(input); + std::set<SymbolType> inputSymbols = AutomatonFromXMLParser::parseInputAlphabet<SymbolType>(input); + std::set<SymbolType> stackSymbols = AutomatonFromXMLParser::parsePushdownStoreAlphabet<SymbolType>(input); + StateType initialState = AutomatonFromXMLParser::parseInitialState<StateType>(input); + SymbolType initialPushdownStoreSymbol = AutomatonFromXMLParser::parseInitialPushdownStoreSymbol<SymbolType>(input); + std::set<StateType> finalStates = AutomatonFromXMLParser::parseFinalStates<StateType>(input); + + SinglePopDPDA < SymbolType, EpsilonType, StateType > automaton(std::move(initialState), std::move(initialPushdownStoreSymbol)); + automaton.setStates(std::move(states)); + automaton.setInputAlphabet(std::move(inputSymbols)); + automaton.setPushdownStoreAlphabet(std::move(stackSymbols)); + automaton.setFinalStates(std::move(finalStates)); + + AutomatonFromXMLParser::parseTransitions<SinglePopDPDA>(input, automaton); + + sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::END_ELEMENT, SinglePopDPDA::getXmlTagName()); + return automaton; +} + +template < class SymbolType, class EpsilonType, class StateType > +void SinglePopDPDA < SymbolType, EpsilonType, StateType >::parseTransition(std::deque<sax::Token>::iterator& input, SinglePopDPDA& automaton) { + sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, "transition"); + StateType from = AutomatonFromXMLParser::parseTransitionFrom<StateType>(input); + std::variant<EpsilonType, SymbolType> inputSymbol = AutomatonFromXMLParser::parseTransitionInputEpsilonSymbol<EpsilonType, SymbolType>(input); + SymbolType pop = AutomatonFromXMLParser::parseTransitionSinglePop<SymbolType>(input); + StateType to = AutomatonFromXMLParser::parseTransitionTo<StateType>(input); + std::vector<SymbolType> push = AutomatonFromXMLParser::parseTransitionPush<SymbolType>(input); + sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::END_ELEMENT, "transition"); + + automaton.addTransition(std::move(from), std::move(inputSymbol), std::move(pop), std::move(to), std::move(push)); +} + +template < class SymbolType, class EpsilonType, class StateType > +void SinglePopDPDA < SymbolType, EpsilonType, StateType >::compose(std::deque<sax::Token>& out) const { + out.emplace_back(SinglePopDPDA::getXmlTagName(), sax::Token::TokenType::START_ELEMENT); + + AutomatonToXMLComposer::composeStates(out, this->getStates()); + AutomatonToXMLComposer::composeInputAlphabet(out, this->getInputAlphabet()); + AutomatonToXMLComposer::composePushdownStoreAlphabet(out, this->getPushdownStoreAlphabet()); + AutomatonToXMLComposer::composeInitialState(out, this->getInitialState()); + AutomatonToXMLComposer::composeInitialPushdownStoreSymbol(out, this->getInitialSymbol()); + AutomatonToXMLComposer::composeFinalStates(out, this->getFinalStates()); + composeTransitions(out); + + out.emplace_back(SinglePopDPDA::getXmlTagName(), sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType, class EpsilonType, class StateType > +void SinglePopDPDA < SymbolType, EpsilonType, StateType >::composeTransitions(std::deque<sax::Token>& out) const { + out.emplace_back("transitions", sax::Token::TokenType::START_ELEMENT); + for(const auto& transition : this->getTransitions()) { + out.emplace_back("transition", sax::Token::TokenType::START_ELEMENT); + + AutomatonToXMLComposer::composeTransitionFrom(out, std::get<0>(transition.first)); + AutomatonToXMLComposer::composeTransitionInputEpsilonSymbol(out, std::get<1>(transition.first)); + AutomatonToXMLComposer::composeTransitionSinglePop(out, std::get<2>(transition.first)); + AutomatonToXMLComposer::composeTransitionTo(out, transition.second.first); + AutomatonToXMLComposer::composeTransitionPush(out, transition.second.second); + + out.emplace_back("transition", sax::Token::TokenType::END_ELEMENT); + } + + out.emplace_back("transitions", sax::Token::TokenType::END_ELEMENT); +} + } /* namespace automaton */ namespace std { -template < > -class ComponentConstraint< automaton::SinglePopDPDA, alphabet::Symbol, automaton::InputAlphabet > { +template < class SymbolType, class EpsilonType, class StateType > +class ComponentConstraint< automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType >, SymbolType, automaton::InputAlphabet > { public: - static bool used ( const automaton::SinglePopDPDA & automaton, const alphabet::Symbol & symbol ) { - for (const std::pair<const std::tuple<label::Label, std::variant<::string::Epsilon < >, alphabet::Symbol>, alphabet::Symbol>, std::pair<label::Label, std::vector<alphabet::Symbol> > >& transition : automaton.getTransitions()) - if (std::get<1>(transition.first).is<alphabet::Symbol>() && symbol == std::get<1>(transition.first).get<alphabet::Symbol>()) + static bool used ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > & automaton, const SymbolType & symbol ) { + for (const std::pair<const std::tuple<StateType, std::variant<EpsilonType, SymbolType>, SymbolType>, std::pair<StateType, std::vector<SymbolType> > >& transition : automaton.getTransitions()) + if (std::get<1>(transition.first).template is<SymbolType>() && symbol == std::get<1>(transition.first).template get<SymbolType>()) return true; return false; } - static bool available ( const automaton::SinglePopDPDA &, const alphabet::Symbol & ) { + static bool available ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > &, const SymbolType & ) { return true; } - static void valid ( const automaton::SinglePopDPDA &, const alphabet::Symbol & ) { + static void valid ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > &, const SymbolType & ) { } }; -template < > -class ComponentConstraint< automaton::SinglePopDPDA, alphabet::Symbol, automaton::PushdownStoreAlphabet > { +template < class SymbolType, class EpsilonType, class StateType > +class ComponentConstraint< automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType >, SymbolType, automaton::PushdownStoreAlphabet > { public: - static bool used ( const automaton::SinglePopDPDA & automaton, const alphabet::Symbol & symbol ) { - for (const std::pair<const std::tuple<label::Label, std::variant<::string::Epsilon < >, alphabet::Symbol>, alphabet::Symbol>, std::pair<label::Label, std::vector<alphabet::Symbol> > >& transition : automaton.getTransitions()) + static bool used ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > & automaton, const SymbolType & symbol ) { + for (const std::pair<const std::tuple<StateType, std::variant<EpsilonType, SymbolType>, SymbolType>, std::pair<StateType, std::vector<SymbolType> > >& transition : automaton.getTransitions()) if (symbol == std::get<2>(transition.first) || std::find(transition.second.second.begin(), transition.second.second.end(), symbol) != transition.second.second.end()) return true; @@ -225,73 +448,73 @@ public: return false; } - static bool available ( const automaton::SinglePopDPDA &, const alphabet::Symbol & ) { + static bool available ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > &, const SymbolType & ) { return true; } - static void valid ( const automaton::SinglePopDPDA &, const alphabet::Symbol & ) { + static void valid ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > &, const SymbolType & ) { } }; -template < > -class ElementConstraint< automaton::SinglePopDPDA, alphabet::Symbol, automaton::InitialSymbol > { +template < class SymbolType, class EpsilonType, class StateType > +class ElementConstraint< automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType >, SymbolType, automaton::InitialSymbol > { public: - static bool available ( const automaton::SinglePopDPDA & automaton, const alphabet::Symbol & symbol ) { - return automaton.accessComponent < automaton::PushdownStoreAlphabet > ( ).get ( ).count ( symbol ); + static bool available ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > & automaton, const SymbolType & symbol ) { + return automaton.template accessComponent < automaton::PushdownStoreAlphabet > ( ).get ( ).count ( symbol ); } - static void valid ( const automaton::SinglePopDPDA &, const alphabet::Symbol & ) { + static void valid ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > &, const SymbolType & ) { } }; -template < > -class ComponentConstraint< automaton::SinglePopDPDA, label::Label, automaton::States > { +template < class SymbolType, class EpsilonType, class StateType > +class ComponentConstraint< automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType >, StateType, automaton::States > { public: - static bool used ( const automaton::SinglePopDPDA & automaton, const label::Label & state ) { + static bool used ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > & automaton, const StateType & state ) { if ( automaton.getInitialState ( ) == state ) return true; if ( automaton.getFinalStates ( ).count ( state ) ) return true; - for (const std::pair<const std::tuple<label::Label, std::variant<::string::Epsilon < >, alphabet::Symbol>, alphabet::Symbol>, std::pair<label::Label, std::vector<alphabet::Symbol> > >& transition : automaton.getTransitions()) + for (const std::pair<const std::tuple<StateType, std::variant<EpsilonType, SymbolType>, SymbolType>, std::pair<StateType, std::vector<SymbolType> > >& transition : automaton.getTransitions()) if (state == std::get<0>(transition.first) || transition.second.first == state) return true; return false; } - static bool available ( const automaton::SinglePopDPDA &, const label::Label & ) { + static bool available ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > &, const StateType & ) { return true; } - static void valid ( const automaton::SinglePopDPDA &, const label::Label & ) { + static void valid ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > &, const StateType & ) { } }; -template < > -class ComponentConstraint< automaton::SinglePopDPDA, label::Label, automaton::FinalStates > { +template < class SymbolType, class EpsilonType, class StateType > +class ComponentConstraint< automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType >, StateType, automaton::FinalStates > { public: - static bool used ( const automaton::SinglePopDPDA &, const label::Label & ) { + static bool used ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > &, const StateType & ) { return false; } - static bool available ( const automaton::SinglePopDPDA & automaton, const label::Label & state ) { - return automaton.accessComponent < automaton::States > ( ).get ( ).count ( state ); + static bool available ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > & automaton, const StateType & state ) { + return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state ); } - static void valid ( const automaton::SinglePopDPDA &, const label::Label & ) { + static void valid ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > &, const StateType & ) { } }; -template < > -class ElementConstraint< automaton::SinglePopDPDA, label::Label, automaton::InitialState > { +template < class SymbolType, class EpsilonType, class StateType > +class ElementConstraint< automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType >, StateType, automaton::InitialState > { public: - static bool available ( const automaton::SinglePopDPDA & automaton, const label::Label & state ) { - return automaton.accessComponent < automaton::States > ( ).get ( ).count ( state ); + static bool available ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > & automaton, const StateType & state ) { + return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state ); } - static void valid ( const automaton::SinglePopDPDA &, const label::Label & ) { + static void valid ( const automaton::SinglePopDPDA < SymbolType, EpsilonType, StateType > &, const StateType & ) { } }; diff --git a/alib2data/test-src/automaton/AutomatonTest.cpp b/alib2data/test-src/automaton/AutomatonTest.cpp index 00880e4da43bf7fb22fadc3f9e7163d62a4ac7ae..87277fabc6ad454359c34267f5641cfbfba2d756 100644 --- a/alib2data/test-src/automaton/AutomatonTest.cpp +++ b/alib2data/test-src/automaton/AutomatonTest.cpp @@ -62,7 +62,7 @@ void AutomatonTest::testDFAParser() { } void AutomatonTest::SinglePopDPDATransitions() { - automaton::SinglePopDPDA automaton(label::labelFrom(1), alphabet::symbolFrom("S")); + automaton::SinglePopDPDA < > automaton(label::labelFrom(1), alphabet::symbolFrom("S")); automaton.addState(label::labelFrom(1)); automaton.addState(label::labelFrom(2));