diff --git a/alib2algo/src/automaton/simplify/Rename.cpp b/alib2algo/src/automaton/simplify/Rename.cpp index 4108f75c26ac2f0f8e4717a58325820ad2297f04..8ae7524f40484902aef0c0ad0de3da4a007a4701 100644 --- a/alib2algo/src/automaton/simplify/Rename.cpp +++ b/alib2algo/src/automaton/simplify/Rename.cpp @@ -18,12 +18,24 @@ auto RenameDFA = registration::AbstractRegister < Rename, automaton::DFA < Defau @param automaton finite automaton to normalize\n\ @return @p automaton with renamed properties" ); +auto RenameNFA = registration::AbstractRegister < Rename, automaton::NFA < DefaultSymbolType, unsigned >, const automaton::NFA < > & > ( Rename::rename, "automaton" ).setDocumentation ( +"Rename automaton's states.\n\ +\n\ +@param automaton finite automaton to normalize\n\ +@return @p automaton with renamed properties" ); + auto RenameDPDA = registration::AbstractRegister < Rename, automaton::DPDA < DefaultSymbolType, DefaultEpsilonType, unsigned, unsigned >, const automaton::DPDA < > & > ( Rename::rename, "automaton" ).setDocumentation ( "Rename automaton's states and pushdown store symbols.\n\ \n\ @param pda pushdown automaton to rename\n\ @return @p pda with renamed states and pushdown store symbols" ); +auto RenameNPDA = registration::AbstractRegister < Rename, automaton::NPDA < DefaultSymbolType, DefaultEpsilonType, unsigned, unsigned >, const automaton::NPDA < > & > ( Rename::rename, "automaton" ).setDocumentation ( +"Rename automaton's states and pushdown store symbols.\n\ +\n\ +@param pda pushdown automaton to rename\n\ +@return @p pda with renamed states and pushdown store symbols" ); + auto RenameSinglePopDPDA = registration::AbstractRegister < Rename, automaton::SinglePopDPDA < DefaultSymbolType, DefaultEpsilonType, unsigned, unsigned >, const automaton::SinglePopDPDA < > & > ( Rename::rename, "automaton" ).setDocumentation ( "Rename automaton's states and pushdown store symbols.\n\ \n\ diff --git a/alib2algo/src/automaton/simplify/Rename.h b/alib2algo/src/automaton/simplify/Rename.h index e276ea00d46f91c9ddf5891b4b7c25d4ba06b285..6c29385c58fc66169145dc7aee47d9579ef282af 100644 --- a/alib2algo/src/automaton/simplify/Rename.h +++ b/alib2algo/src/automaton/simplify/Rename.h @@ -31,7 +31,9 @@ #include <sstream> #include <automaton/FSM/DFA.h> +#include <automaton/FSM/NFA.h> #include <automaton/PDA/DPDA.h> +#include <automaton/PDA/NPDA.h> #include <automaton/PDA/InputDrivenDPDA.h> #include <automaton/PDA/RealTimeHeightDeterministicDPDA.h> #include <automaton/PDA/SinglePopDPDA.h> @@ -64,6 +66,19 @@ public: template < class SymbolType, class StateType > static automaton::DFA < SymbolType, unsigned > rename ( const automaton::DFA < SymbolType, StateType > & dfa ); + /** + * Rename automaton's states. + * + * @tparam SymbolType Type for input symbols + * @tparam StateType Type for states + * + * @param nfa finite automaton to rename + * + * @return @p dfa with renamed states + */ + template < class SymbolType, class StateType > + static automaton::NFA < SymbolType, unsigned > rename ( const automaton::NFA < SymbolType, StateType > & nfa ); + /** * @overload * Rename automaton's states and pushdown store symbols. @@ -80,6 +95,22 @@ public: template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > static automaton::DPDA < InputSymbolType, EpsilonType, unsigned, unsigned > rename ( const automaton::DPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & pda ); + /** + * @overload + * Rename automaton's states and pushdown store symbols. + * + * @tparam InputSymbolType Type for input symbols + * @tparam EpsilonType Type of epsilon representation + * @tparam PushdownStoreSymbolType Type of epsilon representation + * @tparam StateType Type for states + * + * @param pda pushdown automaton to rename + * + * @return @p pda with renamed states and pushdown store symbols + */ + template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > + static automaton::NPDA < InputSymbolType, EpsilonType, unsigned, unsigned > rename ( const automaton::NPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & pda ); + /** * @overload * Rename automaton's states and pushdown store symbols. @@ -167,6 +198,31 @@ automaton::DFA < SymbolType, unsigned > Rename::rename ( const automaton::DFA < return result; } +template < class SymbolType, class StateType > +automaton::NFA < SymbolType, unsigned > Rename::rename ( const automaton::NFA < SymbolType, StateType > & fsm ) { + unsigned counter = 0; + ext::map < StateType, unsigned > renamingData; + + for ( const StateType & state : fsm.getStates ( ) ) + renamingData.insert ( std::make_pair ( state, counter++ ) ); + + automaton::NFA < SymbolType, unsigned > result ( renamingData.at ( fsm.getInitialState ( ) ) ); + + result.setInputAlphabet ( fsm.getInputAlphabet ( ) ); + + for ( const StateType & state : fsm.getStates ( ) ) + result.addState ( renamingData.at ( state ) ); + + for ( const StateType & state : fsm.getFinalStates ( ) ) + result.addFinalState ( renamingData.at ( state ) ); + + for ( const auto & transition : fsm.getTransitions ( ) ) + for ( const auto & toState : transition.second ) + result.addTransition ( renamingData.at ( transition.first.first ), transition.first.second, renamingData.at ( toState ) ); + + return result; +} + template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > automaton::DPDA < InputSymbolType, EpsilonType, unsigned, unsigned > Rename::rename ( const automaton::DPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & pda ) { unsigned counterState = 0; @@ -210,6 +266,51 @@ automaton::DPDA < InputSymbolType, EpsilonType, unsigned, unsigned > Rename::ren return result; } +template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > +automaton::NPDA < InputSymbolType, EpsilonType, unsigned, unsigned > Rename::rename ( const automaton::NPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & pda ) { + unsigned counterState = 0; + ext::map < StateType, unsigned > renamingDataState; + unsigned counterSymbol = 0; + ext::map < InputSymbolType, unsigned > renamingDataSymbol; + + for ( const StateType & state : pda.getStates ( ) ) + renamingDataState.insert ( std::make_pair ( state, counterState++ ) ); + + for ( const InputSymbolType & symbol : pda.getPushdownStoreAlphabet ( ) ) + renamingDataSymbol.insert ( std::make_pair ( symbol, counterSymbol++ ) ); + + automaton::NPDA < InputSymbolType, EpsilonType, unsigned, unsigned > result ( renamingDataState.at ( pda.getInitialState ( ) ), renamingDataSymbol.at ( pda.getInitialSymbol ( ) ) ); + + result.setInputAlphabet ( pda.getInputAlphabet ( ) ); + + for ( const InputSymbolType & symbol : pda.getPushdownStoreAlphabet ( ) ) + result.addPushdownStoreSymbol ( renamingDataSymbol.at ( symbol ) ); + + for ( const StateType & state : pda.getStates ( ) ) + result.addState ( renamingDataState.at ( state ) ); + + for ( const StateType & state : pda.getFinalStates ( ) ) + result.addFinalState ( renamingDataState.at ( state ) ); + + for ( const auto & transition : pda.getTransitions ( ) ) { + ext::vector < unsigned > pop; + + for ( const InputSymbolType & symbol : std::get < 2 > ( transition.first ) ) + pop.push_back ( renamingDataSymbol.at ( symbol ) ); + + for ( const auto & transitionTo : transition.second ) { + ext::vector < unsigned > push; + + for ( const InputSymbolType & symbol : transitionTo.second ) + push.push_back ( renamingDataSymbol.at ( symbol ) ); + + result.addTransition ( renamingDataState.at ( std::get < 0 > ( transition.first ) ), std::get < 1 > ( transition.first ), pop, renamingDataState.at ( transitionTo.first ), push ); + } + } + + return result; +} + template < class InputSymbolType, class EpsilonType, class PushdownStoreSymbolType, class StateType > automaton::SinglePopDPDA < InputSymbolType, EpsilonType, unsigned, unsigned > Rename::rename ( const automaton::SinglePopDPDA < InputSymbolType, EpsilonType, PushdownStoreSymbolType, StateType > & pda ) { int counterState = 0;