diff --git a/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.cpp b/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.cpp index 8a0ed10e591b0c84b8b5f65850a9d2ef9b999805..3b51759bd3775c118a7cca10b671947aef897a0d 100644 --- a/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.cpp +++ b/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.cpp @@ -19,8 +19,10 @@ namespace automaton { -VisiblyPushdownDPDA::VisiblyPushdownDPDA(State initialState, alphabet::Symbol bottomOfTheStackSymbol) : SingleInitialState(std::move(initialState)), BottomOfTheStackSymbolPushdownStoreAlphabet(std::move(bottomOfTheStackSymbol)) { +VisiblyPushdownDPDA::VisiblyPushdownDPDA ( std::set < State > states, std::set < alphabet::Symbol > callAlphabet, std::set < alphabet::Symbol > returnAlphabet, std::set < alphabet::Symbol > localAlphabet, std::set < alphabet::Symbol > pushdownStoreAlphabet, State initialState, alphabet::Symbol bottomOfTheStackSymbol, std::set < State > finalStates ) : std::Components < VisiblyPushdownDPDA, alphabet::Symbol, std::tuple < CallAlphabet, ReturnAlphabet, LocalAlphabet, PushdownStoreAlphabet >, std::tuple < BottomOfTheStackSymbol >, automaton::State, std::tuple < States, FinalStates >, std::tuple < InitialState > > ( std::make_tuple ( std::move ( callAlphabet ), std::move ( returnAlphabet ), std::move ( localAlphabet ), std::move ( pushdownStoreAlphabet ) ), std::make_tuple ( std::move ( bottomOfTheStackSymbol ) ), std::make_tuple ( std::move ( states ), std::move ( finalStates ) ), std::make_tuple ( std::move ( initialState ) ) ) { +} +VisiblyPushdownDPDA::VisiblyPushdownDPDA(State initialState, alphabet::Symbol bottomOfTheStackSymbol) : VisiblyPushdownDPDA ( std::set < State > { initialState }, std::set < alphabet::Symbol > { }, std::set < alphabet::Symbol > { }, std::set < alphabet::Symbol > { }, std::set < alphabet::Symbol > { bottomOfTheStackSymbol }, initialState, bottomOfTheStackSymbol, std::set < automaton::State > { }) { } AutomatonBase* VisiblyPushdownDPDA::clone() const { @@ -31,90 +33,20 @@ AutomatonBase* VisiblyPushdownDPDA::plunder() && { return new VisiblyPushdownDPDA(std::move(*this)); } -bool VisiblyPushdownDPDA::removeState(const State& state) { - if (initialState == state) { - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is initial state."); - } - - if (finalStates.find(state) != finalStates.end()) { - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is final state."); - } - - for (const std::pair<const std::pair<State, alphabet::Symbol>, std::pair<State, alphabet::Symbol> >& callTransition : callTransitions) { - if (state == callTransition.first.first) - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); - if(callTransition.second.first == state) - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); - } - - for (const std::pair<const std::tuple<State, alphabet::Symbol, alphabet::Symbol>, State>& returnTransition : returnTransitions) { - if (state == std::get<0>(returnTransition.first)) - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); - if(returnTransition.second == state) - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); - } - - for (const std::pair<const std::pair<State, alphabet::Symbol>, State>& localTransition : localTransitions) { - if (state == localTransition.first.first) - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); - if(localTransition.second == state) - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); - } - - return states.erase(state); -} - -bool VisiblyPushdownDPDA::removeInputSymbol(const alphabet::Symbol& symbol) { - for (const std::pair<const std::pair<State, alphabet::Symbol>, std::pair<State, alphabet::Symbol> >& callTransition : callTransitions) { - if (symbol == callTransition.first.second) - throw AutomatonException("Input symbol \"" + (std::string) symbol + "\" is used in transition."); - } - - for (const std::pair<const std::tuple<State, alphabet::Symbol, alphabet::Symbol>, State>& returnTransition : returnTransitions) { - if (symbol == std::get<1>(returnTransition.first)) - throw AutomatonException("Input symbol \"" + (std::string) symbol + "\" is used in transition."); - } - - for (const std::pair<const std::pair<State, alphabet::Symbol>, State>& localTransition : localTransitions) { - if (symbol == localTransition.first.second) - throw AutomatonException("Input symbol \"" + (std::string) symbol + "\" is used in transition."); - } - - return callInputAlphabet.erase(symbol) || returnInputAlphabet.erase(symbol) || localInputAlphabet.erase(symbol); -} - -bool VisiblyPushdownDPDA::removePushdownStoreSymbol(const alphabet::Symbol& symbol) { - for (const std::pair<const std::pair<State, alphabet::Symbol>, std::pair<State, alphabet::Symbol> >& callTransition : callTransitions) { - if (symbol == callTransition.second.second) - throw AutomatonException("Pushdown store symbol \"" + (std::string) symbol + "\" is used in transition."); - } - - for (const std::pair<const std::tuple<State, alphabet::Symbol, alphabet::Symbol>, State>& returnTransition : returnTransitions) { - if (symbol == std::get<2>(returnTransition.first)) - throw AutomatonException("Pushdown store symbol \"" + (std::string) symbol + "\" is used in transition."); - } - - if(bottomOfTheStackSymbol == symbol) { - throw AutomatonException("Pushdown store symbol \"" + (std::string) symbol + "\" is start symbol."); - } - - return pushdownStoreAlphabet.erase(symbol); -} - bool VisiblyPushdownDPDA::addCallTransition(State from, alphabet::Symbol input, State to, alphabet::Symbol push) { - if (states.find(from) == states.end()) { + if (!getStates().count(from)) { throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); } - if (callInputAlphabet.find(input) == callInputAlphabet.end()) { + if (!getCallInputAlphabet().count(input)) { throw AutomatonException("Input symbol \"" + (std::string) input + "\" doesn't exist."); } - if (states.find(to) == states.end()) { + if (!getStates().count(to)) { throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); } - if (pushdownStoreAlphabet.find(push) == pushdownStoreAlphabet.end()) { + if (!getPushdownStoreAlphabet().count(push)) { throw AutomatonException("Pushdown store symbol \"" + (std::string) push + "\" doesn't exist."); } @@ -141,19 +73,19 @@ bool VisiblyPushdownDPDA::addCallTransition(State from, alphabet::Symbol input, } bool VisiblyPushdownDPDA::addReturnTransition(State from, alphabet::Symbol input, alphabet::Symbol pop, State to) { - if (states.find(from) == states.end()) { + if (!getStates().count(from)) { throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); } - if (returnInputAlphabet.find(input) == returnInputAlphabet.end()) { + if (!getReturnInputAlphabet().count(input)) { throw AutomatonException("Input symbol \"" + (std::string) input + "\" doesn't exist."); } - if (states.find(to) == states.end()) { + if (!getStates().count(to)) { throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); } - if (pushdownStoreAlphabet.find(pop) == pushdownStoreAlphabet.end()) { + if (!getPushdownStoreAlphabet().count(pop)) { throw AutomatonException("Pushdown store symbol \"" + (std::string) pop + "\" doesn't exist."); } @@ -179,15 +111,15 @@ bool VisiblyPushdownDPDA::addReturnTransition(State from, alphabet::Symbol input } bool VisiblyPushdownDPDA::addLocalTransition(State from, alphabet::Symbol input, State to) { - if (states.find(from) == states.end()) { + if (!getStates().count(from)) { throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); } - if (localInputAlphabet.find(input) == localInputAlphabet.end()) { + if (!getLocalInputAlphabet().count(input)) { throw AutomatonException("Input symbol \"" + (std::string) input + "\" doesn't exist."); } - if (states.find(to) == states.end()) { + if (!getStates().count(to)) { throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); } @@ -271,8 +203,8 @@ const std::map<std::pair<State, alphabet::Symbol>, State>& VisiblyPushdownDPDA:: } int VisiblyPushdownDPDA::compare(const VisiblyPushdownDPDA& other) const { - auto first = std::tie(states, callInputAlphabet, returnInputAlphabet, localInputAlphabet, initialState, finalStates, pushdownStoreAlphabet, bottomOfTheStackSymbol, callTransitions, returnTransitions, localTransitions); - auto second = std::tie(other.states, other.callInputAlphabet, other.returnInputAlphabet, other.localInputAlphabet, other.initialState, other.finalStates, other.pushdownStoreAlphabet, other.bottomOfTheStackSymbol, other.callTransitions, other.returnTransitions, other.localTransitions); + auto first = std::tie(getStates(), getCallInputAlphabet(), getReturnInputAlphabet(), getLocalInputAlphabet(), getInitialState(), getFinalStates(), getPushdownStoreAlphabet(), getBottomOfTheStackSymbol(), callTransitions, returnTransitions, localTransitions); + auto second = std::tie(other.getStates(), other.getCallInputAlphabet(), other.getReturnInputAlphabet(), other.getLocalInputAlphabet(), other.getInitialState(), other.getFinalStates(), other.getPushdownStoreAlphabet(), other.getBottomOfTheStackSymbol(), other.callTransitions, other.returnTransitions, other.localTransitions); std::compare<decltype(first)> comp; return comp(first, second); @@ -280,14 +212,14 @@ int VisiblyPushdownDPDA::compare(const VisiblyPushdownDPDA& other) const { void VisiblyPushdownDPDA::operator>>(std::ostream& out) const { out << "(VisiblyPushdownDPDA " - << "states = " << states - << "callInputAlphabet = " << callInputAlphabet - << "returnInputAlphabet = " << returnInputAlphabet - << "localInputAlphabet = " << localInputAlphabet - << "initialState = " << initialState - << "finalStates = " << finalStates - << "pushdownStoreAlphabet = " << pushdownStoreAlphabet - << "bottomOfTheStackSymbol = " << bottomOfTheStackSymbol + << "states = " << getStates() + << "callAlphabet = " << getCallInputAlphabet() + << "returnAlphabet = " << getReturnInputAlphabet() + << "localAlphabet = " << getLocalInputAlphabet() + << "initialState = " << getInitialState() + << "finalStates = " << getFinalStates() + << "pushdownStoreAlphabet = " << getPushdownStoreAlphabet() + << "bottomOfTheStackSymbol = " << getBottomOfTheStackSymbol() << "callTransitions = " << callTransitions << "returnTransitions = " << returnTransitions << "localTransitions = " << localTransitions @@ -306,9 +238,9 @@ VisiblyPushdownDPDA VisiblyPushdownDPDA::parse(std::deque<sax::Token>::iterator& sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, VisiblyPushdownDPDA::XML_TAG_NAME); std::set<State> states = AutomatonFromXMLParser::parseStates(input); - std::set<alphabet::Symbol> callInputAlphabet = AutomatonFromXMLParser::parseCallInputAlphabet(input); - std::set<alphabet::Symbol> returnInputAlphabet = AutomatonFromXMLParser::parseReturnInputAlphabet(input); - std::set<alphabet::Symbol> localInputAlphabet = AutomatonFromXMLParser::parseLocalInputAlphabet(input); + std::set<alphabet::Symbol> callAlphabet = AutomatonFromXMLParser::parseCallInputAlphabet(input); + std::set<alphabet::Symbol> returnAlphabet = AutomatonFromXMLParser::parseReturnInputAlphabet(input); + std::set<alphabet::Symbol> localAlphabet = AutomatonFromXMLParser::parseLocalInputAlphabet(input); std::set<alphabet::Symbol> stackSymbols = AutomatonFromXMLParser::parsePushdownStoreAlphabet(input); State initialState = AutomatonFromXMLParser::parseInitialState(input); alphabet::Symbol bottomOfTheStackSymbol = AutomatonFromXMLParser::parseBottomOfTheStackSymbol(input); @@ -316,9 +248,9 @@ VisiblyPushdownDPDA VisiblyPushdownDPDA::parse(std::deque<sax::Token>::iterator& VisiblyPushdownDPDA automaton(std::move(initialState), std::move(bottomOfTheStackSymbol)); automaton.setStates(std::move(states)); - automaton.setCallInputAlphabet(std::move(callInputAlphabet)); - automaton.setReturnInputAlphabet(std::move(returnInputAlphabet)); - automaton.setLocalInputAlphabet(std::move(localInputAlphabet)); + automaton.setCallInputAlphabet(std::move(callAlphabet)); + automaton.setReturnInputAlphabet(std::move(returnAlphabet)); + automaton.setLocalInputAlphabet(std::move(localAlphabet)); automaton.setPushdownStoreAlphabet(std::move(stackSymbols)); automaton.setFinalStates(std::move(finalStates)); @@ -410,6 +342,187 @@ void VisiblyPushdownDPDA::composeTransitions(std::deque<sax::Token>& out) const } /* namespace automaton */ +namespace std { + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::CallAlphabet >::used ( const alphabet::Symbol & symbol ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + for (const std::pair<const std::pair<automaton::State, alphabet::Symbol>, std::pair<automaton::State, alphabet::Symbol> >& callTransition : automaton->getCallTransitions()) + if (symbol == callTransition.first.second) + return true; + + return false; +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::CallAlphabet >::available ( const alphabet::Symbol & ) const { + return true; +} + +template < > +void automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::CallAlphabet >::valid ( const alphabet::Symbol & symbol ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + if(automaton->getLocalInputAlphabet().count(symbol)) + throw automaton::AutomatonException("Input symbol " + (std::string) symbol + " already in local alphabet"); + if(automaton->getReturnInputAlphabet().count(symbol)) + throw automaton::AutomatonException("Input symbol " + (std::string) symbol + " already in return alphabet"); +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::ReturnAlphabet >::used ( const alphabet::Symbol & symbol ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + for (const std::pair<const std::tuple<automaton::State, alphabet::Symbol, alphabet::Symbol>, automaton::State>& returnTransition : automaton->getReturnTransitions()) + if (symbol == std::get<1>(returnTransition.first)) + return true; + + return false; +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::ReturnAlphabet >::available ( const alphabet::Symbol & ) const { + return true; +} + +template < > +void automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::ReturnAlphabet >::valid ( const alphabet::Symbol & symbol ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + if(automaton->getLocalInputAlphabet().count(symbol)) + throw automaton::AutomatonException("Input symbol " + (std::string) symbol + " already in local alphabet"); + if(automaton->getCallInputAlphabet().count(symbol)) + throw automaton::AutomatonException("Input symbol " + (std::string) symbol + " already in call alphabet"); +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::LocalAlphabet >::used ( const alphabet::Symbol & symbol ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + for (const std::pair<const std::pair<automaton::State, alphabet::Symbol>, automaton::State>& localTransition : automaton->getLocalTransitions()) + if (symbol == localTransition.first.second) + return true; + + return false; +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::LocalAlphabet >::available ( const alphabet::Symbol & ) const { + return true; +} + +template < > +void automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::LocalAlphabet >::valid ( const alphabet::Symbol & symbol ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + if(automaton->getReturnInputAlphabet().count(symbol)) + throw automaton::AutomatonException("Input symbol " + (std::string) symbol + " already in return alphabet"); + if(automaton->getCallInputAlphabet().count(symbol)) + throw automaton::AutomatonException("Input symbol " + (std::string) symbol + " already in call alphabet"); +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::PushdownStoreAlphabet >::used ( const alphabet::Symbol & symbol ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + for (const std::pair<const std::pair<automaton::State, alphabet::Symbol>, std::pair<automaton::State, alphabet::Symbol> >& callTransition : automaton->getCallTransitions()) + if (symbol == callTransition.second.second) + return true; + + for (const std::pair<const std::tuple<automaton::State, alphabet::Symbol, alphabet::Symbol>, automaton::State>& returnTransition : automaton->getReturnTransitions()) + if (symbol == std::get<2>(returnTransition.first)) + return true; + + if(automaton->getBottomOfTheStackSymbol() == symbol) + return true; + + return false; +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::PushdownStoreAlphabet >::available ( const alphabet::Symbol & ) const { + return true; +} + +template < > +void automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::PushdownStoreAlphabet >::valid ( const alphabet::Symbol & ) const { +} + +template < > +bool automaton::VisiblyPushdownDPDA::Element < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::BottomOfTheStackSymbol >::available ( const alphabet::Symbol & symbol ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + return automaton->accessComponent < automaton::PushdownStoreAlphabet > ( ).get ( ).count ( symbol ); +} + +template < > +void automaton::VisiblyPushdownDPDA::Element < automaton::VisiblyPushdownDPDA, alphabet::Symbol, automaton::BottomOfTheStackSymbol >::valid ( const alphabet::Symbol & ) const { +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, automaton::State, automaton::States >::used ( const automaton::State & state ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + if ( automaton->getInitialState ( ) == state ) + return true; + + if ( automaton->getFinalStates ( ).count ( state ) ) + return true; + + for (const std::pair<const std::pair<automaton::State, alphabet::Symbol>, std::pair<automaton::State, alphabet::Symbol> >& callTransition : automaton->getCallTransitions()) + if (state == callTransition.first.first || callTransition.second.first == state) + return true; + + for (const std::pair<const std::tuple<automaton::State, alphabet::Symbol, alphabet::Symbol>, automaton::State>& returnTransition : automaton->getReturnTransitions()) + if (state == std::get<0>(returnTransition.first) || returnTransition.second == state) + return true; + + for (const std::pair<const std::pair<automaton::State, alphabet::Symbol>, automaton::State>& localTransition : automaton->getLocalTransitions()) + if (state == localTransition.first.first || localTransition.second == state) + return true; + + return false; +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, automaton::State, automaton::States >::available ( const automaton::State & ) const { + return true; +} + +template < > +void automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, automaton::State, automaton::States >::valid ( const automaton::State & ) const { +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, automaton::State, automaton::FinalStates >::used ( const automaton::State & ) const { + return false; +} + +template < > +bool automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, automaton::State, automaton::FinalStates >::available ( const automaton::State & state ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + return automaton->accessComponent < automaton::States > ( ).get ( ).count ( state ); +} + +template < > +void automaton::VisiblyPushdownDPDA::Component < automaton::VisiblyPushdownDPDA, automaton::State, automaton::FinalStates >::valid ( const automaton::State & ) const { +} + +template < > +bool automaton::VisiblyPushdownDPDA::Element < automaton::VisiblyPushdownDPDA, automaton::State, automaton::InitialState >::available ( const automaton::State & state ) const { + const automaton::VisiblyPushdownDPDA * automaton = static_cast < const automaton::VisiblyPushdownDPDA * > ( this ); + + return automaton->accessComponent < automaton::States > ( ).get ( ).count ( state ); +} + +template < > +void automaton::VisiblyPushdownDPDA::Element < automaton::VisiblyPushdownDPDA, automaton::State, automaton::InitialState >::valid ( const automaton::State & ) const { +} + +} /* namespace std */ + namespace alib { auto visiblyPushdownDPDAParserRegister = xmlApi<automaton::Automaton>::ParserRegister<automaton::VisiblyPushdownDPDA>(); diff --git a/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.h b/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.h index 1d89a890176795f62b0be22805c266087e50f465..f150ca46773171642134f717ad438e63175c2932 100644 --- a/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.h +++ b/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.h @@ -10,45 +10,171 @@ #include <map> #include <vector> +#include <core/components.hpp> #include "../AutomatonBase.h" -#include "../common/SingleInitialState.h" -#include "../common/CallReturnLocalInputAlphabet.h" -#include "../common/BottomOfTheStackSymbolPushdownStoreAlphabet.h" +#include "../common/State.h" #include "../../alphabet/Symbol.h" namespace automaton { +class CallAlphabet; +class ReturnAlphabet; +class LocalAlphabet; +class PushdownStoreAlphabet; +class BottomOfTheStackSymbol; +class States; +class FinalStates; +class InitialState; + /** * Represents Finite Automaton. * Can store nondeterministic finite automaton without epsilon transitions. */ -class VisiblyPushdownDPDA : public AutomatonBase, public CallReturnLocalInputAlphabet, public SingleInitialState, public BottomOfTheStackSymbolPushdownStoreAlphabet { +class VisiblyPushdownDPDA : public AutomatonBase, public std::Components < VisiblyPushdownDPDA, alphabet::Symbol, std::tuple < CallAlphabet, ReturnAlphabet, LocalAlphabet, PushdownStoreAlphabet >, std::tuple < BottomOfTheStackSymbol >, automaton::State, std::tuple < States, FinalStates >, std::tuple < InitialState > > { protected: std::map < std::pair < State, alphabet::Symbol >, std::pair < State, alphabet::Symbol > > callTransitions; std::map < std::tuple < State, alphabet::Symbol, alphabet::Symbol >, State > returnTransitions; std::map < std::pair < State, alphabet::Symbol >, State > localTransitions; public: + explicit VisiblyPushdownDPDA ( std::set < State > states, std::set < alphabet::Symbol > callAlphabet, std::set < alphabet::Symbol > returnAlphabet, std::set < alphabet::Symbol > localAlphabet, std::set < alphabet::Symbol > pushdownStoreSymbol, State initialState, alphabet::Symbol bottomOfTheStackSymbol, std::set < automaton::State > finalStates ); explicit VisiblyPushdownDPDA ( State initialSymbol, alphabet::Symbol bottomOfTheStackSymbol ); virtual AutomatonBase * clone ( ) const; virtual AutomatonBase * plunder ( ) &&; - /** - * @copydoc Automaton::removeState(const State&) - */ - virtual bool removeState ( const State & state ); + const State & getInitialState ( ) const { + return accessElement < InitialState > ( ).get ( ); + } - /** - * @copydoc Automaton::removeInputSymbol(const Symbol&) - */ - virtual bool removeInputSymbol ( const alphabet::Symbol & symbol ); + bool setInitialState ( State state ) { + return accessElement < InitialState > ( ).set ( std::move ( state ) ); + } - /* - * @copydoc Automaton::removePushdownStoreSymbol(const Symbol&) - */ - virtual bool removePushdownStoreSymbol ( const alphabet::Symbol & symbol ); + const std::set < State > & getStates ( ) const { + return accessComponent < States > ( ).get ( ); + } + + bool addState ( State state ) { + return accessComponent < States > ( ).add ( std::move ( state ) ); + } + + void setStates ( std::set < State > states ) { + accessComponent < States > ( ).set ( std::move ( states ) ); + } + + bool removeState ( const State & state ) { + return accessComponent < States > ( ).remove ( state ); + } + + const std::set < State > & getFinalStates ( ) const { + return accessComponent < FinalStates > ( ).get ( ); + } + + bool addFinalState ( State state ) { + return accessComponent < FinalStates > ( ).add ( std::move ( state ) ); + } + + void setFinalStates ( std::set < State > states ) { + accessComponent < FinalStates > ( ).set ( std::move ( states ) ); + } + + bool removeFinalState ( const State & state ) { + return accessComponent < FinalStates > ( ).remove ( state ); + } + + const std::set < alphabet::Symbol > & getPushdownStoreAlphabet ( ) const { + return accessComponent < PushdownStoreAlphabet > ( ).get ( ); + } + + bool addPushdownStoreSymbol ( alphabet::Symbol symbol ) { + return accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbol ) ); + } + + void addPushdownStoreSymbols ( std::set < alphabet::Symbol > symbols ) { + accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbols ) ); + } + + void setPushdownStoreAlphabet ( std::set < alphabet::Symbol > symbols ) { + accessComponent < PushdownStoreAlphabet > ( ).set ( std::move ( symbols ) ); + } + + bool removePushdownStoreSymbol ( const alphabet::Symbol & symbol ) { + return accessComponent < PushdownStoreAlphabet > ( ).remove ( symbol ); + } + + const alphabet::Symbol & getBottomOfTheStackSymbol ( ) const { + return accessElement < BottomOfTheStackSymbol > ( ).get ( ); + } + + bool setBottomOfTheStackSymbol ( alphabet::Symbol symbol ) { + return accessElement < BottomOfTheStackSymbol > ( ).set ( std::move ( symbol ) ); + } + + const std::set < alphabet::Symbol > & getCallInputAlphabet ( ) const { + return accessComponent < CallAlphabet > ( ).get ( ); + } + + bool addCallInputSymbol ( alphabet::Symbol symbol ) { + return accessComponent < CallAlphabet > ( ).add ( std::move ( symbol ) ); + } + + void addCallInputSymbols ( std::set < alphabet::Symbol > symbols ) { + accessComponent < CallAlphabet > ( ).add ( std::move ( symbols ) ); + } + + void setCallInputAlphabet ( std::set < alphabet::Symbol > symbols ) { + accessComponent < CallAlphabet > ( ).set ( std::move ( symbols ) ); + } + + bool removeCallInputSymbol ( const alphabet::Symbol & symbol ) { + return accessComponent < CallAlphabet > ( ).remove ( symbol ); + } + + const std::set < alphabet::Symbol > & getReturnInputAlphabet ( ) const { + return accessComponent < ReturnAlphabet > ( ).get ( ); + } + + bool addReturnInputSymbol ( alphabet::Symbol symbol ) { + return accessComponent < ReturnAlphabet > ( ).add ( std::move ( symbol ) ); + } + + void addReturnInputSymbols ( std::set < alphabet::Symbol > symbols ) { + accessComponent < ReturnAlphabet > ( ).add ( std::move ( symbols ) ); + } + + void setReturnInputAlphabet ( std::set < alphabet::Symbol > symbols ) { + accessComponent < ReturnAlphabet > ( ).set ( std::move ( symbols ) ); + } + + bool removeReturnInputSymbol ( const alphabet::Symbol & symbol ) { + return accessComponent < ReturnAlphabet > ( ).remove ( symbol ); + } + + const std::set < alphabet::Symbol > & getLocalInputAlphabet ( ) const { + return accessComponent < LocalAlphabet > ( ).get ( ); + } + + bool addLocalInputSymbol ( alphabet::Symbol symbol ) { + return accessComponent < LocalAlphabet > ( ).add ( std::move ( symbol ) ); + } + + void addLocalInputSymbols ( std::set < alphabet::Symbol > symbols ) { + accessComponent < LocalAlphabet > ( ).add ( std::move ( symbols ) ); + } + + void setLocalInputAlphabet ( std::set < alphabet::Symbol > symbols ) { + accessComponent < LocalAlphabet > ( ).set ( std::move ( symbols ) ); + } + + bool removeLocalInputSymbol ( const alphabet::Symbol & symbol ) { + return accessComponent < LocalAlphabet > ( ).remove ( symbol ); + } + + bool removeInputSymbol(const alphabet::Symbol& symbol) { + return removeCallInputSymbol(symbol) || removeReturnInputSymbol(symbol) || removeLocalInputSymbol(symbol); + } /** * Adds call transition defined by parameters to the automaton. diff --git a/alib2data/src/automaton/common/MultiInitialSymbolsPushdownStoreAlphabet.cpp b/alib2data/src/automaton/common/MultiInitialSymbolsPushdownStoreAlphabet.cpp deleted file mode 100644 index df6fc0928679d0592779cdb4326a10b2596f7622..0000000000000000000000000000000000000000 --- a/alib2data/src/automaton/common/MultiInitialSymbolsPushdownStoreAlphabet.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * MultiInitialSymbols.cpp - * - * Created on: Apr 10, 2013 - * Author: Jan Travnicek - */ - -#include "MultiInitialSymbolsPushdownStoreAlphabet.h" -#include "../AutomatonException.h" -#include <algorithm> - -namespace automaton { - -bool MultiInitialSymbolsPushdownStoreAlphabet::addInitialSymbol(alphabet::Symbol start) { - if (!pushdownStoreAlphabet.count(start)) { - throw AutomatonException("Initial symbol \"" + (std::string) start + "\" not in a pushdown store alphabet."); - } - - return this->initialSymbols.insert(std::move(start)).second; -} - -bool MultiInitialSymbolsPushdownStoreAlphabet::removeInitialSymbol(const alphabet::Symbol& start) { - return this->initialSymbols.erase(start); -} - -void MultiInitialSymbolsPushdownStoreAlphabet::setInitialSymbols(std::set<alphabet::Symbol> symbols) { - if(!std::includes(pushdownStoreAlphabet.begin(), pushdownStoreAlphabet.end(), symbols.begin(), symbols.end())) { - throw AutomatonException("Initial symbol ? not in a pushdown store alphabet."); - } - - this->initialSymbols = std::move(symbols); -} - -const std::set<alphabet::Symbol>& MultiInitialSymbolsPushdownStoreAlphabet::getInitialSymbols() const { - return initialSymbols; -} - -} /* namespace automaton */ diff --git a/alib2data/src/automaton/common/MultiInitialSymbolsPushdownStoreAlphabet.h b/alib2data/src/automaton/common/MultiInitialSymbolsPushdownStoreAlphabet.h deleted file mode 100644 index d229236c12fd2040307f1bd2ebb3492ec88ec166..0000000000000000000000000000000000000000 --- a/alib2data/src/automaton/common/MultiInitialSymbolsPushdownStoreAlphabet.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * MultiInitialSymbols.h - * - * Created on: Apr 10, 2013 - * Author: Jan Travnicek - */ - -#ifndef MULTI_INITIAL_SYMBOLS_PUSHDOWN_STORE_ALPHABET_H_ -#define MULTI_INITIAL_SYMBOLS_PUSHDOWN_STORE_ALPHABET_H_ - -#include <set> -#include "../../alphabet/Symbol.h" -#include "PushdownStoreAlphabet.h" - -namespace automaton { - -/** - * Push Down Automaton - */ -class MultiInitialSymbolsPushdownStoreAlphabet : public PushdownStoreAlphabet { -protected: - std::set<alphabet::Symbol> initialSymbols; -public: - /** - * Adds initial symbol. Initial symbols are symbols that are pushed - * to the stack when MultiInitialSymbols is created. - * @param start new initial symbol - * @throws AutomatonException when symbol is not present in the stack alphabet or it is already in the set - */ - bool addInitialSymbol(alphabet::Symbol start); - - /** - * Adds initial symbol. Initial symbols are symbols that are pushed - * to the stack when MultiInitialSymbols is created. - * @param start new initial symbol - * @throws AutomatonException when symbol is not present in the set of initial symbols - */ - bool removeInitialSymbol(const alphabet::Symbol& start); - - /** - * Sets initial symbols. Initial symbols are symbols that are pushed - * to the stack when MultiInitialSymbols is created. - * @param symbols new initial symbols - * @throws AutomatonException when any of symbols is not present in the stack alphabet - */ - void setInitialSymbols(std::set<alphabet::Symbol> symbols); - - /** - * @return list of start symbols - */ - const std::set<alphabet::Symbol>& getInitialSymbols() const; - -}; - -} /* namespace automaton */ - -#endif /* MULTI_INITIAL_SYMBOLS_PUSHDOWN_STORE_ALPHABET_H_ */ diff --git a/alib2data/src/automaton/common/SingleInitialState.cpp b/alib2data/src/automaton/common/SingleInitialState.cpp deleted file mode 100644 index 670d0bfceb0e91c540807281e07c44ce5a3a82f3..0000000000000000000000000000000000000000 --- a/alib2data/src/automaton/common/SingleInitialState.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* - * SingleInitialState.cpp - * - * Created on: Apr 16, 2013 - * Author: Jan Travnicek - */ - -#include "SingleInitialState.h" - -#include "../AutomatonException.h" - -namespace automaton { - -SingleInitialState::SingleInitialState(State initialState) : initialState(initialState) { - addState(std::move(initialState)); -} - -void SingleInitialState::setInitialState(State state) { - if (!states.count(state)) { - throw AutomatonException("State " + (std::string) state.getName() + " cannot be set as initial state. It is not present in the automaton."); - } - - initialState = std::move(state); -} - -const State& SingleInitialState::getInitialState() const { - return initialState; -} - -} /* namespace automaton */ - diff --git a/alib2data/src/automaton/common/SingleInitialState.h b/alib2data/src/automaton/common/SingleInitialState.h deleted file mode 100644 index c4d80262c0f11705a4d4fffd09a2e9f8102db912..0000000000000000000000000000000000000000 --- a/alib2data/src/automaton/common/SingleInitialState.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SingleInitialStateAutomaton.h - * - * Created on: Apr 10, 2013 - * Author: Jan Travnicek - */ - -#ifndef SINGLE_INITIAL_STATE_H_ -#define SINGLE_INITIAL_STATE_H_ - -#include "States.h" -#include "State.h" - -namespace automaton { - -/** - * Base class for deterministic automata. Contains common elements of automata. - */ -class SingleInitialState : public States { -protected: - State initialState; -public: - SingleInitialState(State initialState); - - /** - * Set the initial state. - * @param state State to set - * @throws AutomatonException when state is not present - * in the automaton - */ - void setInitialState(State state); - - /** - * @return initial state - */ - const State& getInitialState() const; -}; - -} /* namespace automaton */ - -#endif /* SINGLE_INITIAL_STATE_H_ */ - diff --git a/alib2data/src/automaton/common/SingleInitialSymbolPushdownStoreAlphabet.cpp b/alib2data/src/automaton/common/SingleInitialSymbolPushdownStoreAlphabet.cpp deleted file mode 100644 index fef2ce2c17fa92e30480854b1237967155f1d950..0000000000000000000000000000000000000000 --- a/alib2data/src/automaton/common/SingleInitialSymbolPushdownStoreAlphabet.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - * SingleInitialSymbolPushdownStore.cpp - * - * Created on: Apr 10, 2013 - * Author: Jan Travnicek - */ - -#include "SingleInitialSymbolPushdownStoreAlphabet.h" -#include "../AutomatonException.h" -#include <algorithm> - -namespace automaton { - -SingleInitialSymbolPushdownStoreAlphabet::SingleInitialSymbolPushdownStoreAlphabet(alphabet::Symbol initialSymbol) : initialSymbol(initialSymbol) { - addPushdownStoreSymbol(std::move(initialSymbol)); -} - -void SingleInitialSymbolPushdownStoreAlphabet::setInitialSymbol(alphabet::Symbol start) { - if (!pushdownStoreAlphabet.count(start)) { - throw AutomatonException("Pushdown store symbol \"" + (std::string) start + "\" doesn't exist."); - } - - initialSymbol = std::move(start); -} - -const alphabet::Symbol& SingleInitialSymbolPushdownStoreAlphabet::getInitialSymbol() const { - return initialSymbol; -} - -} /* namespace automaton */ diff --git a/alib2data/src/automaton/common/SingleInitialSymbolPushdownStoreAlphabet.h b/alib2data/src/automaton/common/SingleInitialSymbolPushdownStoreAlphabet.h deleted file mode 100644 index ddb005243849b057461cf094be3fad84e75f2484..0000000000000000000000000000000000000000 --- a/alib2data/src/automaton/common/SingleInitialSymbolPushdownStoreAlphabet.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SingleInitialSymbolPushdownStore.h - * - * Created on: Apr 10, 2013 - * Author: Jan Travnicek - */ - -#ifndef SINGLE_INITIAL_SYMBOL_PUSHDOWN_STORE_ALPHABET_H_ -#define SINGLE_INITIAL_SYMBOL_PUSHDOWN_STORE_ALPHABET_H_ - -#include <set> -#include "../../alphabet/Symbol.h" -#include "PushdownStoreAlphabet.h" - -namespace automaton { - -/** - * Push Down Automaton - */ -class SingleInitialSymbolPushdownStoreAlphabet : public PushdownStoreAlphabet { -protected: - alphabet::Symbol initialSymbol; -public: - SingleInitialSymbolPushdownStoreAlphabet(alphabet::Symbol initialSymbol); - - /** - * Set initial symbol. Initial symbol is symbol that is pushed - * to the stack when SingleInitialSymbolPushdownStore is created. - * @param start new initial symbol - * @throws AutomatonException when symbol is not present in the stack alphabet - */ - void setInitialSymbol(alphabet::Symbol start); - - /** - * @return list of start symbols - */ - const alphabet::Symbol& getInitialSymbol() const; - -}; - -} /* namespace automaton */ - -#endif /* SINGLE_INITIAL_SYMBOL_PUSHDOWN_STORE_ALPHABET_H_ */