From d916f4993410f3957ad76bff72d64a50f7c80d7e Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Fri, 2 Mar 2018 13:20:51 +0100 Subject: [PATCH] document some more automata --- alib2data/src/automaton/PDA/SinglePopNPDA.h | 629 +++++++++++++- .../src/automaton/PDA/VisiblyPushdownDPDA.h | 761 +++++++++++++++- .../src/automaton/PDA/VisiblyPushdownNPDA.h | 812 ++++++++++++++++-- 3 files changed, 2059 insertions(+), 143 deletions(-) diff --git a/alib2data/src/automaton/PDA/SinglePopNPDA.h b/alib2data/src/automaton/PDA/SinglePopNPDA.h index 52773fd324..6d3ae8b353 100644 --- a/alib2data/src/automaton/PDA/SinglePopNPDA.h +++ b/alib2data/src/automaton/PDA/SinglePopNPDA.h @@ -1,6 +1,22 @@ /* * SinglePopNPDA.h * + * This file is part of Algorithms library toolkit. + * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz) + + * Algorithms library toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * Algorithms library toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>. + * * Created on: Apr 10, 2013 * Author: Jan Travnicek */ @@ -39,201 +55,572 @@ class FinalStates; class InitialState; /** - * Push Down Automaton + * Nondeterministic pushdown automaton requiring a symbol pop from pushdown store on each transition use. Accepts subset of context free languages. + * + * \details + * Definition + * A = (Q, T, G, I, Z, \delta, F), + * Q (States) = nonempty finite set of states, + * T (TerminalAlphabet) = finite set of terminal symbols - having this empty won't let automaton do much though, + * G (PushdownStoreAlphabet) = finite set of pushdown store symbol - having this empty makes the automaton equivalent to DFA + * I (InitialState) = initial state, + * Z (InitialPushdownStoreSymbol) = initial pushdown store symbol + * \delta = transition function of the form A \times a \times g -> B \times \beta, where A, B \in Q, a \in T \cup { \eps }, g \in G, and \beta \in G*, + * F (FinalStates) = set of final states + * + * \tparam InputSymbolType used for the terminal alphabet + * \tparam EpsilonSymbolType used for the epsilon in the automaton + * \tparam PushdownSymbolType used for the pushdown store alphabet + * \tparam StateType used to the states, and the initial state of the automaton. */ template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > class SinglePopNPDA final : public AutomatonBase, public core::Components < SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType >, ext::set < InputSymbolType >, component::Set, InputAlphabet, ext::set < PushdownStoreSymbolType >, component::Set, PushdownStoreAlphabet, PushdownStoreSymbolType, component::Value, InitialSymbol, ext::set < StateType >, component::Set, std::tuple < States, FinalStates >, StateType, component::Value, InitialState > { protected: + /** + * Transition function as mapping from a state times an input symbol or epsilon times pushdown store symbol on the left hand side to a state times string of pushdown store symbols. + */ ext::map < ext::tuple < StateType, ext::variant < EpsilonType, InputSymbolType >, PushdownStoreSymbolType >, ext::set < ext::pair < StateType, ext::vector < PushdownStoreSymbolType > > > > transitions; public: + /** + * \brief Creates a new instance of the automaton with a concrete set of states, input alphabet, pushdown store alphabet, initial state, initial pushdown symbol and a set of final states. + * + * \param states the initial set of states of the automaton + * \param inputAlphabet the initial input alphabet + * \param pushdownStoreAlphabet the initial set of symbols used in the pushdown store by the automaton + * \param initialState the initial state of the automaton + * \param initialPushdownSymbol the initial pushdown symbol of the automaton + * \param finalStates the initial set of final states of the automaton + */ explicit SinglePopNPDA ( ext::set < StateType > states, ext::set < InputSymbolType > inputAlphabet, ext::set < PushdownStoreSymbolType > pushdownStoreSymbol, StateType initialState, PushdownStoreSymbolType initialPushdownSymbol, ext::set < StateType > finalStates ); + + /** + * \brief Creates a new instance of the automaton with a concrete initial state and initial pushdown store symbol. + * + * \param initialState the initial state of the automaton + * \param initialPushdownSymbol the initial pushdown symbol of the automaton + */ explicit SinglePopNPDA ( StateType initialState, PushdownStoreSymbolType initialPushdownSymbol ); - virtual AutomatonBase * clone ( ) const; + /** + * @copydoc automaton::AutomatonBase::clone() + */ + virtual AutomatonBase * clone ( ) const override; - virtual AutomatonBase * plunder ( ) &&; + /** + * @copydoc automaton::AutomatonBase::plunder() + */ + virtual AutomatonBase * plunder ( ) && override; + /** + * Getter of the initial state. + * + * \returns the initial state of the automaton + */ const StateType & getInitialState ( ) const & { - return this->template accessComponent < InitialState > ( ).get ( ); + return this-> template accessComponent < InitialState > ( ).get ( ); } + /** + * Getter of the initial state. + * + * \returns the initial state of the automaton + */ StateType && getInitialState ( ) && { - return std::move ( this->template accessComponent < InitialState > ( ).get ( ) ); + return std::move ( this-> template accessComponent < InitialState > ( ).get ( ) ); } + /** + * Setter of the initial state. + * + * \param state new initial state of the automaton + * + * \returns true if the initial state was indeed changed + */ bool setInitialState ( StateType state ) { - return this->template accessComponent < InitialState > ( ).set ( std::move ( state ) ); + return this-> template accessComponent < InitialState > ( ).set ( std::move ( state ) ); } + /** + * Getter of states. + * + * \returns the states of the automaton + */ const ext::set < StateType > & getStates ( ) const & { - return this->template accessComponent < States > ( ).get ( ); + return this-> template accessComponent < States > ( ).get ( ); } + /** + * Getter of states. + * + * \returns the states of the automaton + */ ext::set < StateType > && getStates ( ) && { - return std::move ( this->template accessComponent < States > ( ).get ( ) ); + return std::move ( this-> template accessComponent < States > ( ).get ( ) ); } + /** + * Adder of a state. + * + * \param state the new state to be added to a set of states + * + * \returns true if the state was indeed added + */ bool addState ( StateType state ) { - return this->template accessComponent < States > ( ).add ( std::move ( state ) ); + return this-> template accessComponent < States > ( ).add ( std::move ( state ) ); } + /** + * Setter of states. + * + * \param states completely new set of states + */ void setStates ( ext::set < StateType > states ) { - this->template accessComponent < States > ( ).set ( std::move ( states ) ); + this-> template accessComponent < States > ( ).set ( std::move ( states ) ); } + /** + * Remover of a state. + * + * \param state a state to be removed from a set of states + * + * \returns true if the state was indeed removed + */ void removeState ( const StateType & state ) { - this->template accessComponent < States > ( ).remove ( state ); + this-> template accessComponent < States > ( ).remove ( state ); } + /** + * Getter of final states. + * + * \returns the final states of the automaton + */ const ext::set < StateType > & getFinalStates ( ) const & { - return this->template accessComponent < FinalStates > ( ).get ( ); + return this-> template accessComponent < FinalStates > ( ).get ( ); } + /** + * Getter of final states. + * + * \returns the final states of the automaton + */ ext::set < StateType > && getFinalStates ( ) && { - return std::move ( this->template accessComponent < FinalStates > ( ).get ( ) ); + return std::move ( this-> template accessComponent < FinalStates > ( ).get ( ) ); } + /** + * Adder of a final state. + * + * \param state the new state to be added to a set of final states + * + * \returns true if the state was indeed added + */ bool addFinalState ( StateType state ) { - return this->template accessComponent < FinalStates > ( ).add ( std::move ( state ) ); + return this-> template accessComponent < FinalStates > ( ).add ( std::move ( state ) ); } + /** + * Setter of final states. + * + * \param states completely new set of final states + */ void setFinalStates ( ext::set < StateType > states ) { - this->template accessComponent < FinalStates > ( ).set ( std::move ( states ) ); + this-> template accessComponent < FinalStates > ( ).set ( std::move ( states ) ); } + /** + * Remover of a final state. + * + * \param state a state to be removed from a set of final states + * + * \returns true if the state was indeed removed + */ void removeFinalState ( const StateType & state ) { - this->template accessComponent < FinalStates > ( ).remove ( state ); + this-> template accessComponent < FinalStates > ( ).remove ( state ); } + /** + * Getter of the pushdown store alphabet. + * + * \returns the pushdown store alphabet of the automaton + */ const ext::set < PushdownStoreSymbolType > & getPushdownStoreAlphabet ( ) const & { return this->template accessComponent < PushdownStoreAlphabet > ( ).get ( ); } + /** + * Getter of the pushdown store alphabet. + * + * \returns the pushdown store alphabet of the automaton + */ ext::set < PushdownStoreSymbolType > && getPushdownStoreAlphabet ( ) && { return std::move ( this->template accessComponent < PushdownStoreAlphabet > ( ).get ( ) ); } + /** + * Adder of a pushdown store symbol. + * + * \param symbol the new symbol to be added to a pushdown store alphabet + * + * \returns true if the symbol was indeed added + */ bool addPushdownStoreSymbol ( PushdownStoreSymbolType symbol ) { return this->template accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbol ) ); } + /** + * Adder of pushdown store symbols. + * + * \param symbols new symbols to be added to a pushdown store alphabet + */ void addPushdownStoreSymbols ( ext::set < PushdownStoreSymbolType > symbols ) { this->template accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbols ) ); } + /** + * Setter of a pushdown store alphabet. + * + * \param symbols completely new pushdown store alphabet + */ void setPushdownStoreAlphabet ( ext::set < PushdownStoreSymbolType > symbols ) { this->template accessComponent < PushdownStoreAlphabet > ( ).set ( std::move ( symbols ) ); } + /** + * Remover of an pushdown store symbol. + * + * \param symbol a symbol to be removed from a pushdown store alphabet + * + * \returns true if the symbol was indeed removed + */ void removePushdownStoreSymbol ( const PushdownStoreSymbolType & symbol ) { this->template accessComponent < PushdownStoreAlphabet > ( ).remove ( symbol ); } + /** + * Getter of the initial pushdown store symbol. + * + * \returns the initial pushdown store symbol of the automaton + */ const PushdownStoreSymbolType & getInitialSymbol ( ) const & { return this->template accessComponent < InitialSymbol > ( ).get ( ); } + /** + * Getter of the initial pushdown store symbol. + * + * \returns the initial pushdown store symbol of the automaton + */ PushdownStoreSymbolType && getInitialSymbol ( ) && { return std::move ( this->template accessComponent < InitialSymbol > ( ).get ( ) ); } + /** + * Setter of the initial pushdown store symbol. + * + * \param symbol new initial pushdown store symbol of the automaton + * + * \returns true if the initial pushdown store symbol was indeed changed + */ bool setInitialSymbol ( PushdownStoreSymbolType symbol ) { return this->template accessComponent < InitialSymbol > ( ).set ( std::move ( symbol ) ); } + /** + * Getter of the input alphabet. + * + * \returns the input alphabet of the automaton + */ const ext::set < InputSymbolType > & getInputAlphabet ( ) const & { - return this->template accessComponent < InputAlphabet > ( ).get ( ); + return this-> template accessComponent < InputAlphabet > ( ).get ( ); } + /** + * Getter of the input alphabet. + * + * \returns the input alphabet of the automaton + */ ext::set < InputSymbolType > && getInputAlphabet ( ) && { - return std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) ); + return std::move ( this-> template accessComponent < InputAlphabet > ( ).get ( ) ); } + /** + * Adder of a input symbol. + * + * \param symbol the new symbol to be added to an input alphabet + * + * \returns true if the symbol was indeed added + */ bool addInputSymbol ( InputSymbolType symbol ) { - return this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) ); + return this-> template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) ); } + /** + * Adder of input symbols. + * + * \param symbols new symbols to be added to an input alphabet + */ void addInputSymbols ( ext::set < InputSymbolType > symbols ) { - this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) ); + this-> template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) ); } + /** + * Setter of input alphabet. + * + * \param symbols completely new input alphabet + */ void setInputAlphabet ( ext::set < InputSymbolType > symbols ) { - this->template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) ); + this-> template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) ); } + /** + * Remover of an input symbol. + * + * \param symbol a symbol to be removed from an input alphabet + * + * \returns true if the symbol was indeed removed + */ void removeInputSymbol ( const InputSymbolType & symbol ) { - this->template accessComponent < InputAlphabet > ( ).remove ( symbol ); + this-> template accessComponent < InputAlphabet > ( ).remove ( symbol ); } /** - * Adds transition to the SinglePopNPDA. - * @param transition transition to add - * @throws AutomatonException when some part of the transition is not present - * in the SinglePopNPDA (state, input symbol, stack symbol) or when transition already exists + * \brief Adds a transition to the automaton. + * + * \details The transition is in a form A \times a \times g -> B \times \beta, where A, B \in Q, a \in T \cup { \eps }, g \in G, and \beta \in G* + * + * \param from the source state (A) + * \param input the input symbol or epsilon (a) + * \param pop symbols to be poped from pushdown store on the transition use (g) + * \param to the target state (B) + * \param push symbols to be pushed to the pushdown store on the transition use (\beta) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + * + * \returns true if the transition was indeed added */ bool addTransition ( StateType from, ext::variant < EpsilonType, InputSymbolType > input, PushdownStoreSymbolType pop, StateType to, ext::vector < PushdownStoreSymbolType > push ); + /** + * \brief Adds a transition to the automaton. + * + * \details The transition is in a form A \times a \times g -> B \times \beta, where A, B \in Q, a \in T, g \in G, and \beta \in G* + * + * \param from the source state (A) + * \param input the input symbol (a) + * \param pop symbols to be poped from pushdown store on the transition use (g) + * \param to the target state (B) + * \param push symbols to be pushed to the pushdown store on the transition use (\beta) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + * + * \returns true if the transition was indeed added + */ bool addTransition ( StateType from, InputSymbolType input, PushdownStoreSymbolType pop, StateType to, ext::vector < PushdownStoreSymbolType > push ); + /** + * \brief Adds a transition to the automaton. + * + * \details The transition is in a form A \times \eps \times g -> B \times \beta, where A, B \in Q, g \in G, and \beta \in G* + * + * \param from the source state (A) + * \param pop symbols to be poped from pushdown store on the transition use (g) + * \param to the target state (B) + * \param push symbols to be pushed to the pushdown store on the transition use (\beta) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + * + * \returns true if the transition was indeed added + */ bool addTransition ( StateType from, PushdownStoreSymbolType pop, StateType to, ext::vector < PushdownStoreSymbolType > push ); /** - * Adds transition to the SinglePopNPDA. - * @param transition transition to add - * @throws AutomatonException when some part of the transition is not present - * in the SinglePopNPDA (state, input symbol, stack symbol) or when transition already exists + * \brief Adds transitions to the automaton. + * + * \details The transitions are in a form A \times a \times g -> B \times \beta | C \times \gamma | ..., where A, B, C \in Q, a \in T \cup { \eps }, g \in G, and \beta, \gamma \in G* + * + * \param from the source state (A) + * \param input the input symbol or epsilon (a) + * \param pop symbols to be poped from pushdown store on the transition use (g) + * \param targets the set of target state (B, C ...) and symbols to be pushed to the pushdown store on the transition use (\beta, \gamma, ...) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components */ void addTransitions ( StateType from, ext::variant < EpsilonType, InputSymbolType > input, PushdownStoreSymbolType pop, ext::set < ext::pair < StateType, ext::vector < PushdownStoreSymbolType > > > push ); + /** + * \brief Adds transitions to the automaton. + * + * \details The transitions are in a form A \times a \times g -> B \times \beta | C \gamma | ..., where A, B, C \in Q, a \in T, g \in G, and \beta, \gamma \in G* + * + * \param from the source state (A) + * \param input the input symbol (a) + * \param pop symbols to be poped from pushdown store on the transition use (g) + * \param targets the set of target state (B, C ...) and symbols to be pushed to the pushdown store on the transition use (\beta, \gamma, ...) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + */ void addTransitions ( StateType from, InputSymbolType input, PushdownStoreSymbolType pop, ext::set < ext::pair < StateType, ext::vector < PushdownStoreSymbolType > > > push ); + /** + * \brief Adds transitions to the automaton. + * + * \details The transitions are in a form A \times \eps \times g -> B \times \beta | C \gamma | ..., where A, B, C \in Q and \alpha, \beta, \gamma \in G* + * + * \param from the source state (A) + * \param pop symbols to be poped from pushdown store on the transition use (g) + * \param targets the set of target state (B, C ...) and symbols to be pushed to the pushdown store on the transition use (\beta, \gamma, ...) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + */ void addTransitions ( StateType from, PushdownStoreSymbolType pop, ext::set < ext::pair < StateType, ext::vector < PushdownStoreSymbolType > > > push ); /** - * Removes the transition from the SinglePopNPDA. - * @param transition transition to remove - * @throws AutomatonException when transition is not present in the SinglePopNPDA + * \brief Removes a transition from the automaton. + * + * \details The transition is in a form A \times a \times g -> B \times \beta, where A, B \in Q, a \in T \cup { \eps }, g \in G, and \beta \in G* + * + * \param from the source state (A) + * \param input the input symbol or epsilon (a) + * \param pop symbols poped from pushdown store on the transition use (\alpha) + * \param to the target state (B) + * \param push symbols pushed to the pushdown store on the transition use (\beta) + * + * \throws AutomatonException when removed transition left hand side was found but the right hand side did not match. + * + * \returns true if the transition was indeed removed */ bool removeTransition ( const StateType & from, const ext::variant < EpsilonType, InputSymbolType > & input, const PushdownStoreSymbolType & pop, const StateType & to, const ext::vector < PushdownStoreSymbolType > & push ); + /** + * \brief Removes a transition from the automaton. + * + * \details The transition is in a form A \times a \times g -> B \times \beta, where A, B \in Q, a \in T, g \in G, and \beta \in G* + * + * \param from the source state (A) + * \param input the input symbol (a) + * \param pop symbols poped from pushdown store on the transition use (g) + * \param to the target state (B) + * \param push symbols pushed to the pushdown store on the transition use (\beta) + * + * \throws AutomatonException when removed transition left hand side was found but the right hand side did not match. + * + * \returns true if the transition was indeed removed + */ bool removeTransition ( const StateType & from, const InputSymbolType & input, const PushdownStoreSymbolType & pop, const StateType & to, const ext::vector < PushdownStoreSymbolType > & push ); + /** + * \brief Removes a transition from the automaton. + * + * \details The transition is in a form A \times \eps \times g -> B \times \beta, where A, B \in Q, g \in G, and \beta \in G* + * + * \param from the source state (A) + * \param pop symbols poped from pushdown store on the transition use (g) + * \param to the target state (B) + * \param push symbols pushed to the pushdown store on the transition use (\beta) + * + * \throws AutomatonException when removed transition left hand side was found but the right hand side did not match. + * + * \returns true if the transition was indeed removed + */ bool removeTransition ( const StateType & from, const PushdownStoreSymbolType & pop, const StateType & to, const ext::vector < PushdownStoreSymbolType > & push ); /** - * @return SinglePopNPDA transitions + * Get the transition function of the automaton in its natural form. + * + * \returns transition function of the automaton */ const ext::map < ext::tuple < StateType, ext::variant < EpsilonType, InputSymbolType >, PushdownStoreSymbolType >, ext::set < ext::pair < StateType, ext::vector < PushdownStoreSymbolType > > > > & getTransitions ( ) const &; + /** + * Get the transition function of the automaton in its natural form. + * + * \returns transition function of the automaton + */ ext::map < ext::tuple < StateType, ext::variant < EpsilonType, InputSymbolType >, PushdownStoreSymbolType >, ext::set < ext::pair < StateType, ext::vector < PushdownStoreSymbolType > > > > && getTransitions ( ) &&; - virtual int compare ( const ObjectBase & other ) const { + /** + * @copydoc alib::CommonBase<ObjectBase>::compare ( const ObjectBase & ) + */ + virtual int compare ( const ObjectBase & other ) const override { if ( ext::type_index ( typeid ( * this ) ) == ext::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return ext::type_index ( typeid ( * this ) ) - ext::type_index ( typeid ( other ) ); } - virtual int compare ( const SinglePopNPDA & other ) const; + /** + * The actual compare method + * + * \param other the other instance + * + * \returns the actual relation between two by type same automata instances + */ + int compare ( const SinglePopNPDA & other ) const; - virtual void operator >>( std::ostream & os ) const; + /** + * @copydoc alib::CommonBase<ObjectBase>::operator >> ( std::ostream & ) + */ + virtual void operator >>( std::ostream & os ) const override; - virtual explicit operator std::string ( ) const; + /** + * @copydoc alib::CommonBase<ObjectBase>::operator std::string ( ) + */ + virtual explicit operator std::string ( ) const override; + /** + * \brief The XML tag name of class. + * + * \details Intentionaly a static member function to be safe in the initialisation before the main function starts. + * + * \returns string representing the XML tag name of the class + */ static const std::string & getXmlTagName() { static std::string xmlTagName = "SinglePopNPDA"; return xmlTagName; } + /** + * Parsing from a sequence of xml tokens helper. + * + * \params input the iterator to sequence of xml tokens to parse from + * + * \returns the new instance of the automaton + */ static SinglePopNPDA parse ( ext::deque < sax::Token >::iterator & input ); + + /** + * Helper for parsing of individual transitions of the automaton from a sequence of xml tokens. + * + * \params input the iterator to sequence of xml tokens to parse from + * \params automaton the automaton to add the rule to + */ static void parseTransition ( ext::deque < sax::Token >::iterator & input, SinglePopNPDA & automaton ); + /** + * Composing to a sequence of xml tokens helper. + * + * \param out the sink for new xml tokens representing the automaton + * \param automaton the automaton to compose + */ static void compose ( ext::deque < sax::Token > & out, const SinglePopNPDA & automaton ); + + /** + * Helper for composing transitions of the automaton to a sequence of xml tokens. + * + * \param out the sink for xml tokens representing the rules of the automaton + * \param automaton the automaton to compose + */ static void composeTransitions ( ext::deque < sax::Token > & out, const SinglePopNPDA & automaton ); - virtual object::ObjectBase * inc ( ) &&; + /** + * @copydoc alib::GrammarBase::inc() + */ + virtual object::ObjectBase * inc ( ) && override; + /** + * Type of normalized automaton. + */ typedef SinglePopNPDA < > normalized_type; }; @@ -472,9 +859,25 @@ object::ObjectBase* SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreS namespace core { +/** + * Helper class specifying constraints for the automaton's internal input alphabet component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam EpsilonSymbolType used for the epsilon in the automaton + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType >, InputSymbolType, automaton::InputAlphabet > { public: + /** + * Returns true if the symbol is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the symbol is used, false othervise + */ static bool used ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { for ( const std::pair<const ext::tuple<StateType, ext::variant < EpsilonType, InputSymbolType >, PushdownStoreSymbolType>, ext::set<ext::pair<StateType, ext::vector<PushdownStoreSymbolType> > > >& transition : automaton.getTransitions()) if (std::get<1>(transition.first).template is < InputSymbolType >() && symbol == std::get<1>(transition.first).template get < InputSymbolType >()) @@ -483,17 +886,47 @@ public: return false; } + /** + * Returns true as all symbols are possibly available to be elements of the input alphabet. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > &, const InputSymbolType & ) { return true; } + /** + * All symbols are valid as input symbols. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > &, const InputSymbolType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal pushdown store alphabet component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam EpsilonSymbolType used for the epsilon in the automaton + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType >, PushdownStoreSymbolType, automaton::PushdownStoreAlphabet > { public: + /** + * Returns true if the symbol is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the symbol is used, false othervise + */ static bool used ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & automaton, const PushdownStoreSymbolType & symbol ) { if(automaton.getInitialSymbol() == symbol) return true; @@ -510,28 +943,80 @@ public: return false; } + /** + * Returns true as all symbols are possibly available to be elements of the pushdown store alphabet. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > &, const PushdownStoreSymbolType & ) { return true; } + /** + * All symbols are valid as pushdown store symbols. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > &, const PushdownStoreSymbolType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal pushdown store initial element. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam EpsilonSymbolType used for the epsilon in the automaton + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > class ElementConstraint< automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType >, PushdownStoreSymbolType, automaton::InitialSymbol > { public: + /** + * Determines whether the initial pushdown store symbol is available in the automaton's pushdown store alphabet. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the pushdown store symbol is already in the pushdown store alphabet of the automaton + */ static bool available ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & automaton, const PushdownStoreSymbolType & symbol ) { return automaton.template accessComponent < automaton::PushdownStoreAlphabet > ( ).get ( ).count ( symbol ); } + /** + * All pushdown store symbols are valid as an initial pusdown store symbol of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + */ static void valid ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > &, const PushdownStoreSymbolType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal states component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam EpsilonSymbolType used for the epsilon in the automaton + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType >, StateType, automaton::States > { public: + /** + * Returns true if the state is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is used, false othervise + */ static bool used ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & automaton, const StateType & state ) { if ( automaton.getInitialState ( ) == state ) return true; @@ -551,36 +1036,102 @@ public: return false; } + /** + * Returns true as all states are possibly available to be elements of the states. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > &, const StateType & ) { return true; } + /** + * All states are valid as a state of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > &, const StateType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal final states component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam EpsilonSymbolType used for the epsilon in the automaton + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType >, StateType, automaton::FinalStates > { public: + /** + * Returns true if the state is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is used, false othervise + */ static bool used ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > &, const StateType & ) { return false; } + /** + * Determines whether the state is available in the automaton's states set. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is already in the set of states of the automaton + */ static bool available ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & automaton, const StateType & state ) { return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state ); } + /** + * All states are valid as a final state of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > &, const StateType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal initial state element. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam EpsilonSymbolType used for the epsilon in the automaton + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > class ElementConstraint< automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType >, StateType, automaton::InitialState > { public: + /** + * Determines whether the state is available in the automaton's states set. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is already in the set of states of the automaton + */ static bool available ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & automaton, const StateType & state ) { return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state ); } + /** + * All states are valid as an initial state of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::SinglePopNPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > &, const StateType & ) { } }; diff --git a/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.h b/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.h index 9d50f8a9ee..b06b14bcae 100644 --- a/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.h +++ b/alib2data/src/automaton/PDA/VisiblyPushdownDPDA.h @@ -1,6 +1,22 @@ /* * VisiblyPushdownDPDA.h * + * This file is part of Algorithms library toolkit. + * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz) + + * Algorithms library toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * Algorithms library toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>. + * * Created on: Mar 25, 2013 * Author: Jan Travnicek */ @@ -40,277 +56,714 @@ class FinalStates; class InitialState; /** - * Represents Finite Automaton. - * Can store nondeterministic finite automaton without epsilon transitions. + * \brief + * Deterministic visibly pushdown automaton. Accepts subset of context free languages. + + * \details + * Definition + * A = (Q, T = C \cup R \cup L, G, I, Z, \delta, F), + * Q (States) = nonempty finite set of states, + * T (TerminalAlphabet) = finite set of terminal symbols split to three disjoint parts + * - C (CallAlphabet) = symbols used with transitions increasing the pushdown store, + * - R (ReturnAlphabet) = symbols used with transitions decreasing the pushdown store, + * - L (LocalAlphabet) = symbols used with transitions leaving the height of the pushdown store unchanged + * G (PushdownStoreAlphabet) = finite set of pushdown store symbol - having this empty makes the automaton equivalent to DFA + * I (InitialState) = initial state, + * Z (BottomOfTheStackSymbol) = initial pushdown store symbol + * \delta is split to three disjoint parts + * - \delta_{call} of the form A \times c -> B \times g, where A, B \in Q, a \in C, and g \in G + * - \delta_{return} of the form A \times r \times g -> B, where A, B \in Q, r \in C, and g \in G + * - \delta_{local} of the form A \times l -> B, where A, B \in Q, a \in C + * F (FinalStates) = set of final states + * + * The transition functions must meet following criteria. Othervise adding conflicting transition will cause exception. + * $|\delta_{call} (q, c)| \leq 1$, $\forall q \in Q, c \in C$ + * $|\delta_{return} (q, r, g)| \leq 1$, $\forall q \in Q, r \in L, g \in G$ + * $|\delta_{local} (q, l)| \leq 1$, $\forall q \in Q, l \in R$ + * + * \tparam InputSymbolType used for the terminal alphabet + * \tparam EpsilonSymbolType used for the epsilon in the automaton + * \tparam PushdownSymbolType used for the pushdown store alphabet + * \tparam StateType used to the states, and the initial state of the automaton. */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class VisiblyPushdownDPDA final : public AutomatonBase, public core::Components < VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, ext::set < InputSymbolType >, component::Set, std::tuple < CallAlphabet, ReturnAlphabet, LocalAlphabet >, ext::set < PushdownStoreSymbolType >, component::Set, PushdownStoreAlphabet, PushdownStoreSymbolType, component::Value, BottomOfTheStackSymbol, ext::set < StateType >, component::Set, std::tuple < States, FinalStates >, StateType, component::Value, InitialState > { protected: + /** + * Call transition function as mapping from a state times an input symbol on the left hand side to a state times pushdown store symbol on the right hand side. + */ ext::map < ext::pair < StateType, InputSymbolType >, ext::pair < StateType, PushdownStoreSymbolType > > callTransitions; + + /** + * Return transition function as mapping from a state times an input symbol times pushdown store symbol on the left hand side to a state on the right hand side. + */ ext::map < ext::tuple < StateType, InputSymbolType, PushdownStoreSymbolType >, StateType > returnTransitions; + + /** + * Local transition function as mapping from a state times an input symbol on the left hand side to a state on the right hand side. + */ ext::map < ext::pair < StateType, InputSymbolType >, StateType > localTransitions; public: + /** + * StateType template param + */ using StateType_t = StateType; + + /** + * PushdownStoreSymbolType template param + */ using PushdownStoreSymbolType_t = PushdownStoreSymbolType; + /** + * \brief Creates a new instance of the automaton with a concrete set of states, call, return, and local alphabets, pushdown store alphabet, initial state, bottom of the stack symbol, and a set of final states. + * + * \param states the initial set of states of the automaton + * \param callAlphabet the initial input alphabet + * \param returnAlphabet the initial input alphabet + * \param localAlphabet the initial input alphabet + * \param pushdownStoreAlphabet the initial set of symbols used in the pushdown store by the automaton + * \param initialState the initial state of the automaton + * \param bottomOfTheStackSymbol the initial pushdown symbol of the automaton + * \param finalStates the initial set of final states of the automaton + */ explicit VisiblyPushdownDPDA ( ext::set < StateType > states, ext::set < InputSymbolType > callAlphabet, ext::set < InputSymbolType > returnAlphabet, ext::set < InputSymbolType > localAlphabet, ext::set < PushdownStoreSymbolType > pushdownStoreSymbol, StateType initialState, PushdownStoreSymbolType bottomOfTheStackSymbol, ext::set < StateType > finalStates ); + + /** + * \brief Creates a new instance of the automaton with a concrete initial state and bottom of the stack symbol. + * + * \param initialState the initial state of the automaton + * \param bottomOfTheStackSymbol the bottom of the stack symbol of the automaton + */ explicit VisiblyPushdownDPDA ( StateType initialSymbol, PushdownStoreSymbolType bottomOfTheStackSymbol ); - virtual AutomatonBase * clone ( ) const; + /** + * @copydoc automaton::AutomatonBase::clone() + */ + virtual AutomatonBase * clone ( ) const override; - virtual AutomatonBase * plunder ( ) &&; + /** + * @copydoc automaton::AutomatonBase::plunder() + */ + virtual AutomatonBase * plunder ( ) && override; + /** + * Getter of the initial state. + * + * \returns the initial state of the automaton + */ const StateType & getInitialState ( ) const & { - return this->template accessComponent < InitialState > ( ).get ( ); + return this-> template accessComponent < InitialState > ( ).get ( ); } + /** + * Getter of the initial state. + * + * \returns the initial state of the automaton + */ StateType && getInitialState ( ) && { - return std::move ( this->template accessComponent < InitialState > ( ).get ( ) ); + return std::move ( this-> template accessComponent < InitialState > ( ).get ( ) ); } + /** + * Setter of the initial state. + * + * \param state new initial state of the automaton + * + * \returns true if the initial state was indeed changed + */ bool setInitialState ( StateType state ) { - return this->template accessComponent < InitialState > ( ).set ( std::move ( state ) ); + return this-> template accessComponent < InitialState > ( ).set ( std::move ( state ) ); } + /** + * Getter of states. + * + * \returns the states of the automaton + */ const ext::set < StateType > & getStates ( ) const & { - return this->template accessComponent < States > ( ).get ( ); + return this-> template accessComponent < States > ( ).get ( ); } + /** + * Getter of states. + * + * \returns the states of the automaton + */ ext::set < StateType > && getStates ( ) && { - return std::move ( this->template accessComponent < States > ( ).get ( ) ); + return std::move ( this-> template accessComponent < States > ( ).get ( ) ); } + /** + * Adder of a state. + * + * \param state the new state to be added to a set of states + * + * \returns true if the state was indeed added + */ bool addState ( StateType state ) { - return this->template accessComponent < States > ( ).add ( std::move ( state ) ); + return this-> template accessComponent < States > ( ).add ( std::move ( state ) ); } + /** + * Setter of states. + * + * \param states completely new set of states + */ void setStates ( ext::set < StateType > states ) { - this->template accessComponent < States > ( ).set ( std::move ( states ) ); + this-> template accessComponent < States > ( ).set ( std::move ( states ) ); } + /** + * Remover of a state. + * + * \param state a state to be removed from a set of states + * + * \returns true if the state was indeed removed + */ bool removeState ( const StateType & state ) { - return this->template accessComponent < States > ( ).remove ( state ); + return this-> template accessComponent < States > ( ).remove ( state ); } + /** + * Getter of final states. + * + * \returns the final states of the automaton + */ const ext::set < StateType > & getFinalStates ( ) const & { - return this->template accessComponent < FinalStates > ( ).get ( ); + return this-> template accessComponent < FinalStates > ( ).get ( ); } + /** + * Getter of final states. + * + * \returns the final states of the automaton + */ ext::set < StateType > && getFinalStates ( ) && { - return std::move ( this->template accessComponent < FinalStates > ( ).get ( ) ); + return std::move ( this-> template accessComponent < FinalStates > ( ).get ( ) ); } + /** + * Adder of a final state. + * + * \param state the new state to be added to a set of final states + * + * \returns true if the state was indeed added + */ bool addFinalState ( StateType state ) { - return this->template accessComponent < FinalStates > ( ).add ( std::move ( state ) ); + return this-> template accessComponent < FinalStates > ( ).add ( std::move ( state ) ); } + /** + * Setter of final states. + * + * \param states completely new set of final states + */ void setFinalStates ( ext::set < StateType > states ) { - this->template accessComponent < FinalStates > ( ).set ( std::move ( states ) ); + this-> template accessComponent < FinalStates > ( ).set ( std::move ( states ) ); } + /** + * Remover of a final state. + * + * \param state a state to be removed from a set of final states + * + * \returns true if the state was indeed removed + */ bool removeFinalState ( const StateType & state ) { - return this->template accessComponent < FinalStates > ( ).remove ( state ); + return this-> template accessComponent < FinalStates > ( ).remove ( state ); } + /** + * Getter of the pushdown store alphabet. + * + * \returns the pushdown store alphabet of the automaton + */ const ext::set < PushdownStoreSymbolType > & getPushdownStoreAlphabet ( ) const & { return this->template accessComponent < PushdownStoreAlphabet > ( ).get ( ); } + /** + * Getter of the pushdown store alphabet. + * + * \returns the pushdown store alphabet of the automaton + */ ext::set < PushdownStoreSymbolType > && getPushdownStoreAlphabet ( ) && { return std::move ( this->template accessComponent < PushdownStoreAlphabet > ( ).get ( ) ); } + /** + * Adder of a pushdown store symbol. + * + * \param symbol the new symbol to be added to a pushdown store alphabet + * + * \returns true if the symbol was indeed added + */ bool addPushdownStoreSymbol ( PushdownStoreSymbolType symbol ) { return this->template accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbol ) ); } + /** + * Adder of pushdown store symbols. + * + * \param symbols new symbols to be added to a pushdown store alphabet + */ void addPushdownStoreSymbols ( ext::set < PushdownStoreSymbolType > symbols ) { this->template accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbols ) ); } + /** + * Setter of a pushdown store alphabet. + * + * \param symbols completely new pushdown store alphabet + */ void setPushdownStoreAlphabet ( ext::set < PushdownStoreSymbolType > symbols ) { this->template accessComponent < PushdownStoreAlphabet > ( ).set ( std::move ( symbols ) ); } + /** + * Remover of an pushdown store symbol. + * + * \param symbol a symbol to be removed from a pushdown store alphabet + * + * \returns true if the symbol was indeed removed + */ bool removePushdownStoreSymbol ( const PushdownStoreSymbolType & symbol ) { return this->template accessComponent < PushdownStoreAlphabet > ( ).remove ( symbol ); } + /** + * Getter of the bottom of the stack symbol. + * + * \returns the bottom of the stack symbol of the automaton + */ const PushdownStoreSymbolType & getBottomOfTheStackSymbol ( ) const & { return this->template accessComponent < BottomOfTheStackSymbol > ( ).get ( ); } + /** + * Getter of the bottom of the stack symbol. + * + * \returns the bottom of the stack symbol of the automaton + */ PushdownStoreSymbolType && getBottomOfTheStackSymbol ( ) && { return std::move ( this->template accessComponent < BottomOfTheStackSymbol > ( ).get ( ) ); } + /** + * Setter of the bottom of the stack symbol. + * + * \param symbol new bottom of the stack symbol of the automaton + * + * \returns true if the bottom of the stack symbol was indeed changed + */ bool setBottomOfTheStackSymbol ( PushdownStoreSymbolType symbol ) { return this->template accessComponent < BottomOfTheStackSymbol > ( ).set ( std::move ( symbol ) ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the call part of the input alphabet of the automaton + */ const ext::set < InputSymbolType > & getCallInputAlphabet ( ) const & { return this->template accessComponent < CallAlphabet > ( ).get ( ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the the call part of the input alphabet of the automaton + */ ext::set < InputSymbolType > && getCallInputAlphabet ( ) && { return std::move ( this->template accessComponent < CallAlphabet > ( ).get ( ) ); } + /** + * Adder of a symbol to call part of the input alphabet. + * + * \param symbol the new symbol to be added to a call part of the input alphabet + * + * \returns true if the symbol was indeed added + */ bool addCallInputSymbol ( InputSymbolType symbol ) { return this->template accessComponent < CallAlphabet > ( ).add ( std::move ( symbol ) ); } + /** + * Adder of a symbol to a call part of the input alphabet. + * + * \param symbols new symbols to be added to a call part of the input alphabet + */ void addCallInputSymbols ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < CallAlphabet > ( ).add ( std::move ( symbols ) ); } + /** + * Setter of a call part of the input alphabet. + * + * \param symbols completely new call part of the input alphabet + */ void setCallInputAlphabet ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < CallAlphabet > ( ).set ( std::move ( symbols ) ); } + /** + * Remover of a call input symbol. + * + * \param symbol a symbol to be removed from a call part of the input alphabet + * + * \returns true if the symbol was indeed removed + */ bool removeCallInputSymbol ( const InputSymbolType & symbol ) { return this->template accessComponent < CallAlphabet > ( ).remove ( symbol ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the call part of the input alphabet of the automaton + */ const ext::set < InputSymbolType > & getReturnInputAlphabet ( ) const & { return this->template accessComponent < ReturnAlphabet > ( ).get ( ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the the call part of the input alphabet of the automaton + */ ext::set < InputSymbolType > && getReturnInputAlphabet ( ) && { return std::move ( this->template accessComponent < ReturnAlphabet > ( ).get ( ) ); } + /** + * Adder of a symbol to call part of the input alphabet. + * + * \param symbol the new symbol to be added to a call part of the input alphabet + * + * \returns true if the symbol was indeed added + */ bool addReturnInputSymbol ( InputSymbolType symbol ) { return this->template accessComponent < ReturnAlphabet > ( ).add ( std::move ( symbol ) ); } + /** + * Adder of a symbol to a call part of the input alphabet. + * + * \param symbols new symbols to be added to a call part of the input alphabet + */ void addReturnInputSymbols ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < ReturnAlphabet > ( ).add ( std::move ( symbols ) ); } + /** + * Setter of a call part of the input alphabet. + * + * \param symbols completely new call part of the input alphabet + */ void setReturnInputAlphabet ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < ReturnAlphabet > ( ).set ( std::move ( symbols ) ); } + /** + * Remover of a call input symbol. + * + * \param symbol a symbol to be removed from a call part of the input alphabet + * + * \returns true if the symbol was indeed removed + */ bool removeReturnInputSymbol ( const InputSymbolType & symbol ) { return this->template accessComponent < ReturnAlphabet > ( ).remove ( symbol ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the call part of the input alphabet of the automaton + */ const ext::set < InputSymbolType > & getLocalInputAlphabet ( ) const & { return this->template accessComponent < LocalAlphabet > ( ).get ( ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the the call part of the input alphabet of the automaton + */ ext::set < InputSymbolType > && getLocalInputAlphabet ( ) && { return std::move ( this->template accessComponent < LocalAlphabet > ( ).get ( ) ); } + /** + * Adder of a symbol to call part of the input alphabet. + * + * \param symbol the new symbol to be added to a call part of the input alphabet + * + * \returns true if the symbol was indeed added + */ bool addLocalInputSymbol ( InputSymbolType symbol ) { return this->template accessComponent < LocalAlphabet > ( ).add ( std::move ( symbol ) ); } + /** + * Adder of a symbol to a call part of the input alphabet. + * + * \param symbols new symbols to be added to a call part of the input alphabet + */ void addLocalInputSymbols ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < LocalAlphabet > ( ).add ( std::move ( symbols ) ); } + /** + * Setter of a call part of the input alphabet. + * + * \param symbols completely new call part of the input alphabet + */ void setLocalInputAlphabet ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < LocalAlphabet > ( ).set ( std::move ( symbols ) ); } + /** + * Remover of a call input symbol. + * + * \param symbol a symbol to be removed from a call part of the input alphabet + * + * \returns true if the symbol was indeed removed + */ bool removeLocalInputSymbol ( const InputSymbolType & symbol ) { return this->template accessComponent < LocalAlphabet > ( ).remove ( symbol ); } + /** + * Remover of a input symbol. + * + * \param symbol a symbol to be removed from the input alphabet + * + * \returns true if the symbol was indeed removed + */ bool removeInputSymbol(const InputSymbolType& symbol) { return removeCallInputSymbol(symbol) || removeReturnInputSymbol(symbol) || removeLocalInputSymbol(symbol); } /** - * Adds call transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + * \brief Adds a call transition to the automaton. + * + * \details The transition is in a form A \times a -> B \times g, where A, B \in Q, a \in C, and g \in G + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param to the target state (B) + * \param push symbol to be pushed to the pushdown store on the transition use (g) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components or when the transition would cause nondeterminism + * + * \returns true if the transition was indeed added */ bool addCallTransition ( StateType current, InputSymbolType input, StateType next, PushdownStoreSymbolType push ); /** - * Adds return transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + * \brief Adds a return transition to the automaton. + * + * \details The transition is in a form A \times a \times g -> B, where A, B \in Q, a \in C, and g \in G + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param pop symbol to be poped to the pushdown store on the transition use (g) + * \param to the target state (B) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components or when the transition would cause nondeterminism + * + * \returns true if the transition was indeed added */ bool addReturnTransition ( StateType current, InputSymbolType input, PushdownStoreSymbolType pop, StateType next ); /** - * Adds local transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + * \brief Adds a local transition to the automaton. + * + * \details The transition is in a form A \times a -> B, where A, B \in Q and a \in C + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param to the target state (B) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components or when the transition would cause nondeterminism + * + * \returns true if the transition was indeed added */ bool addLocalTransition ( StateType current, InputSymbolType input, StateType next ); /** - * Removes call transition from the automaton. - * @param transition transition to remove - * @throws AutomatonException when transition doesn't exists. + * \brief Removes a call transition from the automaton. + * + * \details The transition is in a form A \times a -> B \times g, where A, B \in Q, a \in C, and g \in G + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param to the target state (B) + * \param push symbol to be pushed to the pushdown store on the transition use (g) + * + * \throws AutomatonException when removed transition left hand side was found but the right hand side did not match. + * + * \returns true if the transition was indeed removed */ bool removeCallTransition ( const StateType & current, const InputSymbolType & input, const StateType & next, const PushdownStoreSymbolType & push ); /** - * Removes return transition from the automaton. - * @param transition transition to remove - * @throws AutomatonException when transition doesn't exists. + * \brief Removes a return transition from the automaton. + * + * \details The transition is in a form A \times a \times g -> B, where A, B \in Q, a \in C, and g \in G + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param pop symbol to be poped to the pushdown store on the transition use (g) + * \param to the target state (B) + * + * \throws AutomatonException when removed transition left hand side was found but the right hand side did not match. + * + * \returns true if the transition was indeed removed */ bool removeReturnTransition ( const StateType & current, const InputSymbolType & input, const PushdownStoreSymbolType & pop, const StateType & next ); /** - * Removes transition local from the automaton. - * @param transition transition to remove - * @throws AutomatonException when transition doesn't exists. + * \brief Removes a local transition from the automaton. + * + * \details The transition is in a form A \times a -> B, where A, B \in Q and a \in C + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param to the target state (B) + * + * \throws AutomatonException when removed transition left hand side was found but the right hand side did not match. + * + * \returns true if the transition was indeed removed */ bool removeLocalTransition ( const StateType & current, const InputSymbolType & input, const StateType & next ); + /** + * Get the call transition function of the automaton in its natural form. + * + * \returns call transition function of the automaton + */ const ext::map < ext::pair < StateType, InputSymbolType >, ext::pair < StateType, PushdownStoreSymbolType > > & getCallTransitions ( ) const &; + /** + * Get the call transition function of the automaton in its natural form. + * + * \returns call transition function of the automaton + */ ext::map < ext::pair < StateType, InputSymbolType >, ext::pair < StateType, PushdownStoreSymbolType > > && getCallTransitions ( ) &&; + /** + * Get the return transition function of the automaton in its natural form. + * + * \returns return transition function of the automaton + */ const ext::map < ext::tuple < StateType, InputSymbolType, PushdownStoreSymbolType >, StateType > & getReturnTransitions ( ) const &; + /** + * Get the return transition function of the automaton in its natural form. + * + * \returns return transition function of the automaton + */ ext::map < ext::tuple < StateType, InputSymbolType, PushdownStoreSymbolType >, StateType > && getReturnTransitions ( ) &&; + /** + * Get the local transition function of the automaton in its natural form. + * + * \returns local transition function of the automaton + */ const ext::map < ext::pair < StateType, InputSymbolType >, StateType > & getLocalTransitions ( ) const &; + /** + * Get the local transition function of the automaton in its natural form. + * + * \returns local transition function of the automaton + */ ext::map < ext::pair < StateType, InputSymbolType >, StateType > && getLocalTransitions ( ) &&; - virtual int compare ( const ObjectBase & other ) const { + /** + * @copydoc alib::CommonBase<ObjectBase>::compare ( const ObjectBase & ) + */ + virtual int compare ( const ObjectBase & other ) const override { if ( ext::type_index ( typeid ( * this ) ) == ext::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return ext::type_index ( typeid ( * this ) ) - ext::type_index ( typeid ( other ) ); } - virtual int compare ( const VisiblyPushdownDPDA & other ) const; + /** + * The actual compare method + * + * \param other the other instance + * + * \returns the actual relation between two by type same automata instances + */ + int compare ( const VisiblyPushdownDPDA & other ) const; - virtual void operator >>( std::ostream & os ) const; + /** + * @copydoc alib::CommonBase<ObjectBase>::operator >> ( std::ostream & ) + */ + virtual void operator >>( std::ostream & os ) const override; - virtual explicit operator std::string ( ) const; + /** + * @copydoc alib::CommonBase<ObjectBase>::operator std::string ( ) + */ + virtual explicit operator std::string ( ) const override; + /** + * \brief The XML tag name of class. + * + * \details Intentionaly a static member function to be safe in the initialisation before the main function starts. + * + * \returns string representing the XML tag name of the class + */ static const std::string & getXmlTagName() { static std::string xmlTagName = "VisiblyPushdownDPDA"; return xmlTagName; } + /** + * Parsing from a sequence of xml tokens helper. + * + * \params input the iterator to sequence of xml tokens to parse from + * + * \returns the new instance of the automaton + */ static VisiblyPushdownDPDA parse ( ext::deque < sax::Token >::iterator & input ); + + /** + * Helper for parsing of individual transitions of the automaton from a sequence of xml tokens. + * + * \params input the iterator to sequence of xml tokens to parse from + * \params automaton the automaton to add the rule to + */ static void parseTransition ( ext::deque < sax::Token >::iterator & input, VisiblyPushdownDPDA & automaton ); + /** + * Composing to a sequence of xml tokens helper. + * + * \param out the sink for new xml tokens representing the automaton + * \param automaton the automaton to compose + */ static void compose ( ext::deque < sax::Token > & out, const VisiblyPushdownDPDA & automaton ); + + /** + * Helper for composing transitions of the automaton to a sequence of xml tokens. + * + * \param out the sink for xml tokens representing the rules of the automaton + * \param automaton the automaton to compose + */ static void composeTransitions ( ext::deque < sax::Token > & out, const VisiblyPushdownDPDA & automaton ); - virtual object::ObjectBase * inc ( ) &&; + /** + * @copydoc alib::GrammarBase::inc() + */ + virtual object::ObjectBase * inc ( ) && override; + /** + * Type of normalized automaton. + */ typedef VisiblyPushdownDPDA < > normalized_type; }; @@ -671,9 +1124,24 @@ object::ObjectBase* VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolTy namespace core { +/** + * Helper class specifying constraints for the automaton's internal call input alphabet component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, InputSymbolType, automaton::CallAlphabet > { public: + /** + * Returns true if the symbol is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the symbol is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { for ( const std::pair<const ext::pair<StateType, InputSymbolType>, ext::pair<StateType, PushdownStoreSymbolType> >& callTransition : automaton.getCallTransitions()) if (symbol == callTransition.first.second) @@ -682,10 +1150,24 @@ public: return false; } + /** + * Returns true as all symbols are possibly available to be elements of the call input alphabet. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const InputSymbolType & ) { return true; } + /** + * All symbols are valid as call input symbols. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { if(automaton.getLocalInputAlphabet().count(symbol)) throw automaton::AutomatonException("Input symbol " + ext::to_string ( symbol ) + " already in local alphabet"); @@ -694,9 +1176,24 @@ public: } }; +/** + * Helper class specifying constraints for the automaton's internal return input alphabet component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, InputSymbolType, automaton::ReturnAlphabet > { public: + /** + * Returns true if the symbol is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the symbol is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { for ( const std::pair<const ext::tuple<StateType, InputSymbolType, PushdownStoreSymbolType>, StateType>& returnTransition : automaton.getReturnTransitions()) if (symbol == std::get<1>(returnTransition.first)) @@ -705,10 +1202,24 @@ public: return false; } + /** + * Returns true as all symbols are possibly available to be elements of the return input alphabet. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const InputSymbolType & ) { return true; } + /** + * All symbols are valid as return input symbols. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { if(automaton.getLocalInputAlphabet().count(symbol)) throw automaton::AutomatonException("Input symbol " + ext::to_string ( symbol ) + " already in local alphabet"); @@ -717,9 +1228,24 @@ public: } }; +/** + * Helper class specifying constraints for the automaton's internal local input alphabet component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, InputSymbolType, automaton::LocalAlphabet > { public: + /** + * Returns true if the symbol is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the symbol is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { for ( const std::pair<const ext::pair<StateType, InputSymbolType>, StateType>& localTransition : automaton.getLocalTransitions()) if (symbol == localTransition.first.second) @@ -728,10 +1254,24 @@ public: return false; } + /** + * Returns true as all symbols are possibly available to be elements of the local input alphabet. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const InputSymbolType & ) { return true; } + /** + * All symbols are valid as local input symbols. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { if(automaton.getReturnInputAlphabet().count(symbol)) throw automaton::AutomatonException("Input symbol " + ext::to_string ( symbol ) + " already in return alphabet"); @@ -740,9 +1280,24 @@ public: } }; +/** + * Helper class specifying constraints for the automaton's internal pushdown store alphabet component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, PushdownStoreSymbolType, automaton::PushdownStoreAlphabet > { public: + /** + * Returns true if the symbol is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the symbol is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const PushdownStoreSymbolType & symbol ) { for ( const std::pair<const ext::pair<StateType, InputSymbolType>, ext::pair<StateType, PushdownStoreSymbolType> >& callTransition : automaton.getCallTransitions()) if (symbol == callTransition.second.second) @@ -758,28 +1313,78 @@ public: return false; } + /** + * Returns true as all symbols are possibly available to be elements of the pushdown store alphabet. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const PushdownStoreSymbolType & ) { return true; } + /** + * All symbols are valid as pushdown store symbols. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const PushdownStoreSymbolType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal bottom of the stack symbol element. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class ElementConstraint< automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, PushdownStoreSymbolType, automaton::BottomOfTheStackSymbol > { public: + /** + * Determines whether the bottom of the stack symbol is available in the automaton's pushdown store alphabet. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the pushdown store symbol is already in the pushdown store alphabet of the automaton + */ static bool available ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const PushdownStoreSymbolType & symbol ) { return automaton.template accessComponent < automaton::PushdownStoreAlphabet > ( ).get ( ).count ( symbol ); } + /** + * All pushdown store symbols are valid as a bottom of the stack symbol symbol of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + */ static void valid ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const PushdownStoreSymbolType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal states component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, StateType, automaton::States > { public: + /** + * Returns true if the state is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const StateType & state ) { if ( automaton.getInitialState ( ) == state ) return true; @@ -802,36 +1407,100 @@ public: return false; } + /** + * Returns true as all states are possibly available to be elements of the states. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { return true; } + /** + * All states are valid as a state of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal final states component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, StateType, automaton::FinalStates > { public: + /** + * Returns true if the state is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { return false; } + /** + * Determines whether the state is available in the automaton's states set. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is already in the set of states of the automaton + */ static bool available ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const StateType & state ) { return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state ); } + /** + * All states are valid as a final state of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal initial state element. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class ElementConstraint< automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, StateType, automaton::InitialState > { public: + /** + * Determines whether the state is available in the automaton's states set. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is already in the set of states of the automaton + */ static bool available ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const StateType & state ) { return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state ); } + /** + * All states are valid as an initial state of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownDPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { } }; diff --git a/alib2data/src/automaton/PDA/VisiblyPushdownNPDA.h b/alib2data/src/automaton/PDA/VisiblyPushdownNPDA.h index 440c82aa30..81eec4e6dd 100644 --- a/alib2data/src/automaton/PDA/VisiblyPushdownNPDA.h +++ b/alib2data/src/automaton/PDA/VisiblyPushdownNPDA.h @@ -1,6 +1,22 @@ /* * VisiblyPushdownNPDA.h * + * This file is part of Algorithms library toolkit. + * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz) + + * Algorithms library toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * Algorithms library toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>. + * * Created on: Mar 25, 2013 * Author: Jan Travnicek */ @@ -40,309 +56,765 @@ class FinalStates; class InitialStates; /** - * Represents Finite Automaton. - * Can store nondeterministic finite automaton without epsilon transitions. + * \brief + * Nondeterministic visibly pushdown automaton. Accepts subset of context free languages. + + * \details + * Definition + * A = (Q, T = C \cup R \cup L, G, I, Z, \delta, F), + * Q (States) = nonempty finite set of states, + * T (TerminalAlphabet) = finite set of terminal symbols split to three disjoint parts + * - C (CallAlphabet) = symbols used with transitions increasing the pushdown store, + * - R (ReturnAlphabet) = symbols used with transitions decreasing the pushdown store, + * - L (LocalAlphabet) = symbols used with transitions leaving the height of the pushdown store unchanged + * G (PushdownStoreAlphabet) = finite set of pushdown store symbol - having this empty makes the automaton equivalent to DFA + * I (InitialStates) = set of initial states, + * Z (BottomOfTheStackSymbol) = initial pushdown store symbol + * \delta is split to three disjoint parts + * - \delta_{call} of the form A \times c -> B \times g, where A, B \in Q, a \in C, and g \in G + * - \delta_{return} of the form A \times r \times g -> B, where A, B \in Q, r \in C, and g \in G + * - \delta_{local} of the form A \times l -> B, where A, B \in Q, a \in C + * F (FinalStates) = set of final states + * + * \tparam InputSymbolType used for the terminal alphabet + * \tparam EpsilonSymbolType used for the epsilon in the automaton + * \tparam PushdownSymbolType used for the pushdown store alphabet + * \tparam StateType used to the states, and the initial state of the automaton. */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class VisiblyPushdownNPDA final : public AutomatonBase, public core::Components < VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, ext::set < InputSymbolType >, component::Set, std::tuple < CallAlphabet, ReturnAlphabet, LocalAlphabet >, ext::set < PushdownStoreSymbolType >, component::Set, PushdownStoreAlphabet, PushdownStoreSymbolType, component::Value, BottomOfTheStackSymbol, ext::set < StateType >, component::Set, std::tuple < States, InitialStates, FinalStates > > { protected: + /** + * Call transition function as mapping from a state times an input symbol on the left hand side to a state times pushdown store symbol on the right hand side. + */ ext::map < ext::pair < StateType, InputSymbolType >, ext::set < ext::pair < StateType, PushdownStoreSymbolType > > > callTransitions; + + /** + * Return transition function as mapping from a state times an input symbol times pushdown store symbol on the left hand side to a state on the right hand side. + */ ext::map < ext::tuple < StateType, InputSymbolType, PushdownStoreSymbolType >, ext::set < StateType > > returnTransitions; + + /** + * Local transition function as mapping from a state times an input symbol on the left hand side to a state on the right hand side. + */ ext::map < ext::pair < StateType, InputSymbolType >, ext::set < StateType > > localTransitions; public: + /** + * \brief Creates a new instance of the automaton with a concrete set of states, call, return, and local alphabets, pushdown store alphabet, initial state, bottom of the stack symbol, and a set of final states. + * + * \param states the initial set of states of the automaton + * \param callAlphabet the initial input alphabet + * \param returnAlphabet the initial input alphabet + * \param localAlphabet the initial input alphabet + * \param pushdownStoreAlphabet the initial set of symbols used in the pushdown store by the automaton + * \param initialState the initial state of the automaton + * \param bottomOfTheStackSymbol the initial pushdown symbol of the automaton + * \param finalStates the initial set of final states of the automaton + */ explicit VisiblyPushdownNPDA ( ext::set < StateType > states, ext::set < InputSymbolType > callAlphabet, ext::set < InputSymbolType > returnAlphabet, ext::set < InputSymbolType > localAlphabet, ext::set < PushdownStoreSymbolType > pushdownStoreSymbol, ext::set < StateType > initialStates, PushdownStoreSymbolType bottomOfTheStackSymbol, ext::set < StateType > finalStates ); + + /** + * \brief Creates a new instance of the automaton with a concrete initial state and bottom of the stack symbol. + * + * \param initialState the initial state of the automaton + * \param bottomOfTheStackSymbol the bottom of the stack symbol of the automaton + */ explicit VisiblyPushdownNPDA ( PushdownStoreSymbolType bottomOfTheStackSymbol ); - virtual AutomatonBase * clone ( ) const; + /** + * @copydoc automaton::AutomatonBase::clone() + */ + virtual AutomatonBase * clone ( ) const override; - virtual AutomatonBase * plunder ( ) &&; + /** + * @copydoc automaton::AutomatonBase::plunder() + */ + virtual AutomatonBase * plunder ( ) && override; + /** + * Getter of initial states. + * + * \returns the initial states of the automaton + */ const ext::set < StateType > & getInitialStates ( ) const & { return this->template accessComponent < InitialStates > ( ).get ( ); } + /** + * Getter of initial states. + * + * \returns the initial states of the automaton + */ ext::set < StateType > && getInitialStates ( ) && { return std::move ( this->template accessComponent < InitialStates > ( ).get ( ) ); } + /** + * Adder of a initial state. + * + * \param state the new state to be added to a set of initial states + * + * \returns true if the state was indeed added + */ bool addInitialState ( StateType state ) { return this->template accessComponent < InitialStates > ( ).add ( std::move ( state ) ); } + /** + * Setter of initial states. + * + * \param states completely new set of initial states + */ void setInitialStates ( ext::set < StateType > states ) { this->template accessComponent < InitialStates > ( ).set ( std::move ( states ) ); } + /** + * Remover of a initial state. + * + * \param state a state to be removed from a set of initial states + * + * \returns true if the state was indeed removed + */ bool removeInitialState ( const StateType & state ) { return this->template accessComponent < InitialStates > ( ).remove ( state ); } + /** + * Getter of states. + * + * \returns the states of the automaton + */ const ext::set < StateType > & getStates ( ) const & { - return this->template accessComponent < States > ( ).get ( ); + return this-> template accessComponent < States > ( ).get ( ); } + /** + * Getter of states. + * + * \returns the states of the automaton + */ ext::set < StateType > && getStates ( ) && { - return std::move ( this->template accessComponent < States > ( ).get ( ) ); + return std::move ( this-> template accessComponent < States > ( ).get ( ) ); } + /** + * Adder of a state. + * + * \param state the new state to be added to a set of states + * + * \returns true if the state was indeed added + */ bool addState ( StateType state ) { - return this->template accessComponent < States > ( ).add ( std::move ( state ) ); + return this-> template accessComponent < States > ( ).add ( std::move ( state ) ); } + /** + * Setter of states. + * + * \param states completely new set of states + */ void setStates ( ext::set < StateType > states ) { - this->template accessComponent < States > ( ).set ( std::move ( states ) ); + this-> template accessComponent < States > ( ).set ( std::move ( states ) ); } + /** + * Remover of a state. + * + * \param state a state to be removed from a set of states + * + * \returns true if the state was indeed removed + */ bool removeState ( const StateType & state ) { - return this->template accessComponent < States > ( ).remove ( state ); + return this-> template accessComponent < States > ( ).remove ( state ); } + /** + * Getter of final states. + * + * \returns the final states of the automaton + */ const ext::set < StateType > & getFinalStates ( ) const & { - return this->template accessComponent < FinalStates > ( ).get ( ); + return this-> template accessComponent < FinalStates > ( ).get ( ); } + /** + * Getter of final states. + * + * \returns the final states of the automaton + */ ext::set < StateType > && getFinalStates ( ) && { - return std::move ( this->template accessComponent < FinalStates > ( ).get ( ) ); + return std::move ( this-> template accessComponent < FinalStates > ( ).get ( ) ); } + /** + * Adder of a final state. + * + * \param state the new state to be added to a set of final states + * + * \returns true if the state was indeed added + */ bool addFinalState ( StateType state ) { - return this->template accessComponent < FinalStates > ( ).add ( std::move ( state ) ); + return this-> template accessComponent < FinalStates > ( ).add ( std::move ( state ) ); } + /** + * Setter of final states. + * + * \param states completely new set of final states + */ void setFinalStates ( ext::set < StateType > states ) { - this->template accessComponent < FinalStates > ( ).set ( std::move ( states ) ); + this-> template accessComponent < FinalStates > ( ).set ( std::move ( states ) ); } + /** + * Remover of a final state. + * + * \param state a state to be removed from a set of final states + * + * \returns true if the state was indeed removed + */ bool removeFinalState ( const StateType & state ) { - return this->template accessComponent < FinalStates > ( ).remove ( state ); + return this-> template accessComponent < FinalStates > ( ).remove ( state ); } + /** + * Getter of the pushdown store alphabet. + * + * \returns the pushdown store alphabet of the automaton + */ const ext::set < PushdownStoreSymbolType > & getPushdownStoreAlphabet ( ) const & { return this->template accessComponent < PushdownStoreAlphabet > ( ).get ( ); } + /** + * Getter of the pushdown store alphabet. + * + * \returns the pushdown store alphabet of the automaton + */ ext::set < PushdownStoreSymbolType > && getPushdownStoreAlphabet ( ) && { return std::move ( this->template accessComponent < PushdownStoreAlphabet > ( ).get ( ) ); } + /** + * Adder of a pushdown store symbol. + * + * \param symbol the new symbol to be added to a pushdown store alphabet + * + * \returns true if the symbol was indeed added + */ bool addPushdownStoreSymbol ( PushdownStoreSymbolType symbol ) { return this->template accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbol ) ); } + /** + * Adder of pushdown store symbols. + * + * \param symbols new symbols to be added to a pushdown store alphabet + */ void addPushdownStoreSymbols ( ext::set < PushdownStoreSymbolType > symbols ) { this->template accessComponent < PushdownStoreAlphabet > ( ).add ( std::move ( symbols ) ); } + /** + * Setter of a pushdown store alphabet. + * + * \param symbols completely new pushdown store alphabet + */ void setPushdownStoreAlphabet ( ext::set < PushdownStoreSymbolType > symbols ) { this->template accessComponent < PushdownStoreAlphabet > ( ).set ( std::move ( symbols ) ); } + /** + * Remover of an pushdown store symbol. + * + * \param symbol a symbol to be removed from a pushdown store alphabet + * + * \returns true if the symbol was indeed removed + */ bool removePushdownStoreSymbol ( const PushdownStoreSymbolType & symbol ) { return this->template accessComponent < PushdownStoreAlphabet > ( ).remove ( symbol ); } + /** + * Getter of the bottom of the stack symbol. + * + * \returns the bottom of the stack symbol of the automaton + */ const PushdownStoreSymbolType & getBottomOfTheStackSymbol ( ) const & { return this->template accessComponent < BottomOfTheStackSymbol > ( ).get ( ); } + /** + * Getter of the bottom of the stack symbol. + * + * \returns the bottom of the stack symbol of the automaton + */ PushdownStoreSymbolType && getBottomOfTheStackSymbol ( ) && { return std::move ( this->template accessComponent < BottomOfTheStackSymbol > ( ).get ( ) ); } + /** + * Setter of the bottom of the stack symbol. + * + * \param symbol new bottom of the stack symbol of the automaton + * + * \returns true if the bottom of the stack symbol was indeed changed + */ bool setBottomOfTheStackSymbol ( PushdownStoreSymbolType symbol ) { return this->template accessComponent < BottomOfTheStackSymbol > ( ).set ( std::move ( symbol ) ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the call part of the input alphabet of the automaton + */ const ext::set < InputSymbolType > & getCallInputAlphabet ( ) const & { return this->template accessComponent < CallAlphabet > ( ).get ( ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the the call part of the input alphabet of the automaton + */ ext::set < InputSymbolType > && getCallInputAlphabet ( ) && { return std::move ( this->template accessComponent < CallAlphabet > ( ).get ( ) ); } + /** + * Adder of a symbol to call part of the input alphabet. + * + * \param symbol the new symbol to be added to a call part of the input alphabet + * + * \returns true if the symbol was indeed added + */ bool addCallInputSymbol ( InputSymbolType symbol ) { return this->template accessComponent < CallAlphabet > ( ).add ( std::move ( symbol ) ); } + /** + * Adder of a symbol to a call part of the input alphabet. + * + * \param symbols new symbols to be added to a call part of the input alphabet + */ void addCallInputSymbols ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < CallAlphabet > ( ).add ( std::move ( symbols ) ); } + /** + * Setter of a call part of the input alphabet. + * + * \param symbols completely new call part of the input alphabet + */ void setCallInputAlphabet ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < CallAlphabet > ( ).set ( std::move ( symbols ) ); } + /** + * Remover of a call input symbol. + * + * \param symbol a symbol to be removed from a call part of the input alphabet + * + * \returns true if the symbol was indeed removed + */ bool removeCallInputSymbol ( const InputSymbolType & symbol ) { return this->template accessComponent < CallAlphabet > ( ).remove ( symbol ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the call part of the input alphabet of the automaton + */ const ext::set < InputSymbolType > & getReturnInputAlphabet ( ) const & { return this->template accessComponent < ReturnAlphabet > ( ).get ( ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the the call part of the input alphabet of the automaton + */ ext::set < InputSymbolType > && getReturnInputAlphabet ( ) && { return std::move ( this->template accessComponent < ReturnAlphabet > ( ).get ( ) ); } + /** + * Adder of a symbol to call part of the input alphabet. + * + * \param symbol the new symbol to be added to a call part of the input alphabet + * + * \returns true if the symbol was indeed added + */ bool addReturnInputSymbol ( InputSymbolType symbol ) { return this->template accessComponent < ReturnAlphabet > ( ).add ( std::move ( symbol ) ); } + /** + * Adder of a symbol to a call part of the input alphabet. + * + * \param symbols new symbols to be added to a call part of the input alphabet + */ void addReturnInputSymbols ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < ReturnAlphabet > ( ).add ( std::move ( symbols ) ); } + /** + * Setter of a call part of the input alphabet. + * + * \param symbols completely new call part of the input alphabet + */ void setReturnInputAlphabet ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < ReturnAlphabet > ( ).set ( std::move ( symbols ) ); } + /** + * Remover of a call input symbol. + * + * \param symbol a symbol to be removed from a call part of the input alphabet + * + * \returns true if the symbol was indeed removed + */ bool removeReturnInputSymbol ( const InputSymbolType & symbol ) { return this->template accessComponent < ReturnAlphabet > ( ).remove ( symbol ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the call part of the input alphabet of the automaton + */ const ext::set < InputSymbolType > & getLocalInputAlphabet ( ) const & { return this->template accessComponent < LocalAlphabet > ( ).get ( ); } + /** + * Getter of the call part of the input alphabet. + * + * \returns the the call part of the input alphabet of the automaton + */ ext::set < InputSymbolType > && getLocalInputAlphabet ( ) && { return std::move ( this->template accessComponent < LocalAlphabet > ( ).get ( ) ); } + /** + * Adder of a symbol to call part of the input alphabet. + * + * \param symbol the new symbol to be added to a call part of the input alphabet + * + * \returns true if the symbol was indeed added + */ bool addLocalInputSymbol ( InputSymbolType symbol ) { return this->template accessComponent < LocalAlphabet > ( ).add ( std::move ( symbol ) ); } + /** + * Adder of a symbol to a call part of the input alphabet. + * + * \param symbols new symbols to be added to a call part of the input alphabet + */ void addLocalInputSymbols ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < LocalAlphabet > ( ).add ( std::move ( symbols ) ); } + /** + * Setter of a call part of the input alphabet. + * + * \param symbols completely new call part of the input alphabet + */ void setLocalInputAlphabet ( ext::set < InputSymbolType > symbols ) { this->template accessComponent < LocalAlphabet > ( ).set ( std::move ( symbols ) ); } + /** + * Remover of a call input symbol. + * + * \param symbol a symbol to be removed from a call part of the input alphabet + * + * \returns true if the symbol was indeed removed + */ bool removeLocalInputSymbol ( const InputSymbolType & symbol ) { return this->template accessComponent < LocalAlphabet > ( ).remove ( symbol ); } + /** + * Remover of a input symbol. + * + * \param symbol a symbol to be removed from the input alphabet + * + * \returns true if the symbol was indeed removed + */ bool removeInputSymbol(const InputSymbolType& symbol) { return removeCallInputSymbol(symbol) || removeReturnInputSymbol(symbol) || removeLocalInputSymbol(symbol); } /** - * Adds call transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + * \brief Adds a call transition to the automaton. + * + * \details The transition is in a form A \times a -> B \times g, where A, B \in Q, a \in C, and g \in G + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param to the target state (B) + * \param push symbol to be pushed to the pushdown store on the transition use (g) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + * + * \returns true if the transition was indeed added */ bool addCallTransition ( StateType current, InputSymbolType input, StateType next, PushdownStoreSymbolType push ); /** - * Adds return transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + * \brief Adds a return transition to the automaton. + * + * \details The transition is in a form A \times a \times g -> B, where A, B \in Q, a \in C, and g \in G + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param pop symbol to be poped to the pushdown store on the transition use (g) + * \param to the target state (B) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + * + * \returns true if the transition was indeed added */ bool addReturnTransition ( StateType current, InputSymbolType input, PushdownStoreSymbolType pop, StateType next ); /** - * Adds local transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + * \brief Adds a local transition to the automaton. + * + * \details The transition is in a form A \times a -> B, where A, B \in Q and a \in C + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param to the target state (B) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + * + * \returns true if the transition was indeed added */ bool addLocalTransition ( StateType current, InputSymbolType input, StateType next ); /** - * Adds call transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + * \brief Adds call transitions to the automaton. + * + * \details The transition is in a form A \times a -> B \times g | C \times h | ..., where A, B, C, ... \in Q, a \in C, and g, h, ... \in G + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param targets the set of target state (B, C, ...) and symbol to be pushed to the pushdown store on the transition use (g, h, ...) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + * + * \returns true if the transition was indeed added */ void addCallTransitions ( StateType current, InputSymbolType input, ext::set < ext::pair < StateType, PushdownStoreSymbolType > > targets ); /** - * Adds return transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + * \brief Adds return transitions to the automaton. + * + * \details The transition is in a form A \times a \times g -> B | C | ..., where A, B, C ... \in Q, a \in C, and g \in G + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param pop symbol to be poped to the pushdown store on the transition use (g) + * \param targets the set of target states (B, C, ...) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + * + * \returns true if the transition was indeed added */ void addReturnTransitions ( StateType current, InputSymbolType input, PushdownStoreSymbolType pop, ext::set < StateType > next ); /** - * Adds local transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + * \brief Adds local transitions to the automaton. + * + * \details The transition is in a form A \times a -> B | C | ..., where A, B, C, ... \in Q and a \in C + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param targets the set of target states (B, C, ...) + * + * \throws AutomatonException when transition contains state or symbol not present in the automaton components + * + * \returns true if the transition was indeed added */ void addLocalTransitions ( StateType current, InputSymbolType input, ext::set < StateType > next ); /** - * Removes call transition from the automaton. - * @param transition transition to remove - * @throws AutomatonException when transition doesn't exists. + * \brief Removes a call transition from the automaton. + * + * \details The transition is in a form A \times a -> B \times g, where A, B \in Q, a \in C, and g \in G + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param to the target state (B) + * \param push symbol to be pushed to the pushdown store on the transition use (g) + * + * \throws AutomatonException when removed transition left hand side was found but the right hand side did not match. + * + * \returns true if the transition was indeed removed */ bool removeCallTransition ( const StateType & current, const InputSymbolType & input, const StateType & next, const PushdownStoreSymbolType & push ); /** - * Removes return transition from the automaton. - * @param transition transition to remove - * @throws AutomatonException when transition doesn't exists. + * \brief Removes a return transition from the automaton. + * + * \details The transition is in a form A \times a \times g -> B, where A, B \in Q, a \in C, and g \in G + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param pop symbol to be poped to the pushdown store on the transition use (g) + * \param to the target state (B) + * + * \throws AutomatonException when removed transition left hand side was found but the right hand side did not match. + * + * \returns true if the transition was indeed removed */ bool removeReturnTransition ( const StateType & current, const InputSymbolType & input, const PushdownStoreSymbolType & pop, const StateType & next ); /** - * Removes transition local from the automaton. - * @param transition transition to remove - * @throws AutomatonException when transition doesn't exists. + * \brief Removes a local transition from the automaton. + * + * \details The transition is in a form A \times a -> B, where A, B \in Q and a \in C + * + * \param from the source state (A) + * \param input the call input symbol (a) + * \param to the target state (B) + * + * \throws AutomatonException when removed transition left hand side was found but the right hand side did not match. + * + * \returns true if the transition was indeed removed */ bool removeLocalTransition ( const StateType & current, const InputSymbolType & input, const StateType & next ); + /** + * Get the call transition function of the automaton in its natural form. + * + * \returns call transition function of the automaton + */ const ext::map < ext::pair < StateType, InputSymbolType >, ext::set < ext::pair < StateType, PushdownStoreSymbolType > > > & getCallTransitions ( ) const &; + /** + * Get the call transition function of the automaton in its natural form. + * + * \returns call transition function of the automaton + */ ext::map < ext::pair < StateType, InputSymbolType >, ext::set < ext::pair < StateType, PushdownStoreSymbolType > > > && getCallTransitions ( ) &&; + /** + * Get the return transition function of the automaton in its natural form. + * + * \returns return transition function of the automaton + */ const ext::map < ext::tuple < StateType, InputSymbolType, PushdownStoreSymbolType >, ext::set < StateType > > & getReturnTransitions ( ) const &; + /** + * Get the return transition function of the automaton in its natural form. + * + * \returns return transition function of the automaton + */ ext::map < ext::tuple < StateType, InputSymbolType, PushdownStoreSymbolType >, ext::set < StateType > > && getReturnTransitions ( ) &&; + /** + * Get the local transition function of the automaton in its natural form. + * + * \returns local transition function of the automaton + */ const ext::map < ext::pair < StateType, InputSymbolType >, ext::set < StateType > > & getLocalTransitions ( ) const &; + /** + * Get the local transition function of the automaton in its natural form. + * + * \returns local transition function of the automaton + */ ext::map < ext::pair < StateType, InputSymbolType >, ext::set < StateType > > && getLocalTransitions ( ) &&; - virtual int compare ( const ObjectBase & other ) const { + /** + * @copydoc alib::CommonBase<ObjectBase>::compare ( const ObjectBase & ) + */ + virtual int compare ( const ObjectBase & other ) const override { if ( ext::type_index ( typeid ( * this ) ) == ext::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return ext::type_index ( typeid ( * this ) ) - ext::type_index ( typeid ( other ) ); } - virtual int compare ( const VisiblyPushdownNPDA & other ) const; + /** + * The actual compare method + * + * \param other the other instance + * + * \returns the actual relation between two by type same automata instances + */ + int compare ( const VisiblyPushdownNPDA & other ) const; - virtual void operator >>( std::ostream & os ) const; + /** + * @copydoc alib::CommonBase<ObjectBase>::operator >> ( std::ostream & ) + */ + virtual void operator >>( std::ostream & os ) const override; - virtual explicit operator std::string ( ) const; + /** + * @copydoc alib::CommonBase<ObjectBase>::operator std::string ( ) + */ + virtual explicit operator std::string ( ) const override; + /** + * \brief The XML tag name of class. + * + * \details Intentionaly a static member function to be safe in the initialisation before the main function starts. + * + * \returns string representing the XML tag name of the class + */ static const std::string & getXmlTagName() { static std::string xmlTagName = "VisiblyPushdownNPDA"; return xmlTagName; } + /** + * Parsing from a sequence of xml tokens helper. + * + * \params input the iterator to sequence of xml tokens to parse from + * + * \returns the new instance of the automaton + */ static VisiblyPushdownNPDA parse ( ext::deque < sax::Token >::iterator & input ); + + /** + * Helper for parsing of individual transitions of the automaton from a sequence of xml tokens. + * + * \params input the iterator to sequence of xml tokens to parse from + * \params automaton the automaton to add the rule to + */ static void parseTransition ( ext::deque < sax::Token >::iterator & input, VisiblyPushdownNPDA & automaton ); + /** + * Composing to a sequence of xml tokens helper. + * + * \param out the sink for new xml tokens representing the automaton + * \param automaton the automaton to compose + */ static void compose ( ext::deque < sax::Token > & out, const VisiblyPushdownNPDA & automaton ); + + /** + * Helper for composing transitions of the automaton to a sequence of xml tokens. + * + * \param out the sink for xml tokens representing the rules of the automaton + * \param automaton the automaton to compose + */ static void composeTransitions ( ext::deque < sax::Token > & out, const VisiblyPushdownNPDA & automaton ); - virtual object::ObjectBase * inc ( ) &&; + /** + * @copydoc alib::GrammarBase::inc() + */ + virtual object::ObjectBase * inc ( ) && override; + /** + * Type of normalized automaton. + */ typedef VisiblyPushdownNPDA < > normalized_type; }; @@ -694,9 +1166,24 @@ object::ObjectBase* VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolTy namespace core { +/** + * Helper class specifying constraints for the automaton's internal call input alphabet component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, InputSymbolType, automaton::CallAlphabet > { public: + /** + * Returns true if the symbol is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the symbol is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { for ( const std::pair<const ext::pair<StateType, InputSymbolType>, ext::set < ext::pair<StateType, PushdownStoreSymbolType> > >& callTransition : automaton.getCallTransitions()) if (symbol == callTransition.first.second) @@ -705,10 +1192,24 @@ public: return false; } + /** + * Returns true as all symbols are possibly available to be elements of the call input alphabet. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const InputSymbolType & ) { return true; } + /** + * All symbols are valid as call input symbols. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { if(automaton.getLocalInputAlphabet().count(symbol)) throw automaton::AutomatonException("Input symbol " + ext::to_string ( symbol ) + " already in local alphabet"); @@ -717,9 +1218,24 @@ public: } }; +/** + * Helper class specifying constraints for the automaton's internal return input alphabet component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, InputSymbolType, automaton::ReturnAlphabet > { public: + /** + * Returns true if the symbol is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the symbol is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { for ( const std::pair<const ext::tuple<StateType, InputSymbolType, PushdownStoreSymbolType>, ext::set < StateType> >& returnTransition : automaton.getReturnTransitions()) if (symbol == std::get<1>(returnTransition.first)) @@ -728,10 +1244,24 @@ public: return false; } + /** + * Returns true as all symbols are possibly available to be elements of the return input alphabet. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const InputSymbolType & ) { return true; } + /** + * All symbols are valid as return input symbols. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { if(automaton.getLocalInputAlphabet().count(symbol)) throw automaton::AutomatonException("Input symbol " + ext::to_string ( symbol ) + " already in local alphabet"); @@ -740,9 +1270,24 @@ public: } }; +/** + * Helper class specifying constraints for the automaton's internal local input alphabet component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, InputSymbolType, automaton::LocalAlphabet > { public: + /** + * Returns true if the symbol is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the symbol is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { for ( const std::pair<const ext::pair<StateType, InputSymbolType>, ext::set < StateType >>& localTransition : automaton.getLocalTransitions()) if (symbol == localTransition.first.second) @@ -751,10 +1296,24 @@ public: return false; } + /** + * Returns true as all symbols are possibly available to be elements of the local input alphabet. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const InputSymbolType & ) { return true; } + /** + * All symbols are valid as local input symbols. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const InputSymbolType & symbol ) { if(automaton.getReturnInputAlphabet().count(symbol)) throw automaton::AutomatonException("Input symbol " + ext::to_string ( symbol ) + " already in return alphabet"); @@ -763,9 +1322,24 @@ public: } }; +/** + * Helper class specifying constraints for the automaton's internal pushdown store alphabet component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, PushdownStoreSymbolType, automaton::PushdownStoreAlphabet > { public: + /** + * Returns true if the symbol is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the symbol is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const PushdownStoreSymbolType & symbol ) { for ( const std::pair<const ext::pair<StateType, InputSymbolType>, ext::set<ext::pair<StateType, PushdownStoreSymbolType> > >& callTransition : automaton.getCallTransitions()) for(const ext::pair<StateType, PushdownStoreSymbolType>& to : callTransition.second) @@ -782,28 +1356,78 @@ public: return false; } + /** + * Returns true as all symbols are possibly available to be elements of the pushdown store alphabet. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const PushdownStoreSymbolType & ) { return true; } + /** + * All symbols are valid as pushdown store symbols. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const PushdownStoreSymbolType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal bottom of the stack symbol element. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class ElementConstraint< automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, PushdownStoreSymbolType, automaton::BottomOfTheStackSymbol > { public: + /** + * Determines whether the bottom of the stack symbol is available in the automaton's pushdown store alphabet. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + * + * \returns true if the pushdown store symbol is already in the pushdown store alphabet of the automaton + */ static bool available ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const PushdownStoreSymbolType & symbol ) { return automaton.template accessComponent < automaton::PushdownStoreAlphabet > ( ).get ( ).count ( symbol ); } + /** + * All pushdown store symbols are valid as a bottom of the stack symbol symbol of the automaton. + * + * \param automaton the tested automaton + * \param symbol the tested symbol + */ static void valid ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const PushdownStoreSymbolType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal states component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, StateType, automaton::States > { public: + /** + * Returns true if the state is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const StateType & state ) { if ( automaton.getInitialStates ( ).count ( state ) ) return true; @@ -841,40 +1465,112 @@ public: return false; } + /** + * Returns true as all states are possibly available to be elements of the states. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true + */ static bool available ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { return true; } + /** + * All states are valid as a state of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal final states component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, StateType, automaton::FinalStates > { public: + /** + * Returns true if the state is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { return false; } + /** + * Determines whether the state is available in the automaton's states set. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is already in the set of states of the automaton + */ static bool available ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const StateType & state ) { return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state ); } + /** + * All states are valid as a final state of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { } }; +/** + * Helper class specifying constraints for the automaton's internal initial state component. + * + * \tparam InputSymbolType used for the terminal alphabet of the automaton. + * \tparam PushdownSymbolType used for the pushdown store alphabet of the automaton. + * \tparam StateType used for the terminal alphabet of the automaton. + */ template < class InputSymbolType, class PushdownStoreSymbolType, class StateType > class SetConstraint< automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType >, StateType, automaton::InitialStates > { public: + /** + * Returns true if the state is still used in some transition of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is used, false othervise + */ static bool used ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { return false; } + /** + * Determines whether the state is available in the automaton's states set. + * + * \param automaton the tested automaton + * \param state the tested state + * + * \returns true if the state is already in the set of states of the automaton + */ static bool available ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > & automaton, const StateType & state ) { return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state ); } + /** + * All states are valid as a initial state of the automaton. + * + * \param automaton the tested automaton + * \param state the tested state + */ static void valid ( const automaton::VisiblyPushdownNPDA < InputSymbolType, PushdownStoreSymbolType, StateType > &, const StateType & ) { } }; -- GitLab