diff --git a/alib2algo/src/automaton/transform/PDAToRHPDA.cpp b/alib2algo/src/automaton/transform/PDAToRHPDA.cpp index 5e523ce2ce432dfb78c1637b6381fa2df20185ec..b3a0a33cd461e21b07d00bf9862d0d9afaa4601c 100644 --- a/alib2algo/src/automaton/transform/PDAToRHPDA.cpp +++ b/alib2algo/src/automaton/transform/PDAToRHPDA.cpp @@ -21,162 +21,176 @@ namespace automaton { -automaton::RealTimeHeightDeterministicDPDA PDAToRHPDA::convert( const automaton::RealTimeHeightDeterministicDPDA & pda ) { +automaton::RealTimeHeightDeterministicDPDA PDAToRHPDA::convert ( const automaton::RealTimeHeightDeterministicDPDA & pda ) { return pda; } -auto PDAToRHPDARealTimeHeightDeterministicDPDA = PDAToRHPDA::RegistratorWrapper<automaton::RealTimeHeightDeterministicDPDA, automaton::RealTimeHeightDeterministicDPDA>(PDAToRHPDA::getInstance(), PDAToRHPDA::convert); +auto PDAToRHPDARealTimeHeightDeterministicDPDA = PDAToRHPDA::RegistratorWrapper < automaton::RealTimeHeightDeterministicDPDA, automaton::RealTimeHeightDeterministicDPDA > ( PDAToRHPDA::getInstance ( ), PDAToRHPDA::convert ); -automaton::RealTimeHeightDeterministicNPDA PDAToRHPDA::convert( const automaton::RealTimeHeightDeterministicNPDA & pda ) { +automaton::RealTimeHeightDeterministicNPDA PDAToRHPDA::convert ( const automaton::RealTimeHeightDeterministicNPDA & pda ) { return pda; } -auto PDAToRHPDARealTimeHeightDeterministicNPDA = PDAToRHPDA::RegistratorWrapper<automaton::RealTimeHeightDeterministicNPDA, automaton::RealTimeHeightDeterministicNPDA>(PDAToRHPDA::getInstance(), PDAToRHPDA::convert); +auto PDAToRHPDARealTimeHeightDeterministicNPDA = PDAToRHPDA::RegistratorWrapper < automaton::RealTimeHeightDeterministicNPDA, automaton::RealTimeHeightDeterministicNPDA > ( PDAToRHPDA::getInstance ( ), PDAToRHPDA::convert ); -automaton::RealTimeHeightDeterministicDPDA PDAToRHPDA::convert( const automaton::DPDA & pda ) { - automaton::State q0 = automaton::createUniqueState(automaton::State("q0"), pda.getStates()); +automaton::RealTimeHeightDeterministicDPDA PDAToRHPDA::convert ( const automaton::DPDA & pda ) { + automaton::State q0 = automaton::createUniqueState ( automaton::State ( "q0" ), pda.getStates ( ) ); - RealTimeHeightDeterministicDPDA res(q0, alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); + RealTimeHeightDeterministicDPDA res ( q0, alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); - res.setInputAlphabet(pda.getInputAlphabet()); - for(const auto& state : pda.getStates()) - res.addState(state); - res.setFinalStates(pda.getFinalStates()); - std::set<alphabet::Symbol> stackSymbols = pda.getStackAlphabet(); - stackSymbols.insert( alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); - res.setStackAlphabet(stackSymbols); + res.setInputAlphabet ( pda.getInputAlphabet ( ) ); - res.addCallTransition(q0, pda.getInitialState(), pda.getInitialSymbol()); + for ( const auto & state : pda.getStates ( ) ) + res.addState ( state ); - std::string us("us"); + res.setFinalStates ( pda.getFinalStates ( ) ); + std::set < alphabet::Symbol > stackSymbols = pda.getStackAlphabet ( ); + stackSymbols.insert ( alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); + res.setStackAlphabet ( stackSymbols ); + + res.addCallTransition ( q0, pda.getInitialState ( ), pda.getInitialSymbol ( ) ); + + std::string us ( "us" ); int i = 0; - for(const auto& transition : pda.getTransitions()) { - const auto& to = transition.second; - if(std::get<2>(transition.first).size() == 0 && to.second.size() == 0) - res.addLocalTransition(std::get<0>(transition.first), std::get<1>(transition.first), to.first); - else if(std::get<2>(transition.first).size() == 1 && to.second.size() == 0) - res.addReturnTransition(std::get<0>(transition.first), std::get<1>(transition.first), std::get<2>(transition.first)[0], to.first); - else if(std::get<2>(transition.first).size() == 0 && to.second.size() == 1) - res.addCallTransition(std::get<0>(transition.first), std::get<1>(transition.first), to.first, to.second[0]); - else { + + for ( const auto & transition : pda.getTransitions ( ) ) { + const auto & to = transition.second; + + if ( ( std::get < 2 > ( transition.first ).size ( ) == 0 ) && ( to.second.size ( ) == 0 ) ) { + res.addLocalTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), to.first ); + } else if ( ( std::get < 2 > ( transition.first ).size ( ) == 1 ) && ( to.second.size ( ) == 0 ) ) { + res.addReturnTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), std::get < 2 > ( transition.first )[0], to.first ); + } else if ( ( std::get < 2 > ( transition.first ).size ( ) == 0 ) && ( to.second.size ( ) == 1 ) ) { + res.addCallTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), to.first, to.second[0] ); + } else { int popPushIndex = 0; - int popPushSymbols = std::get<2>(transition.first).size() + to.second.size(); - - automaton::State lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(i)), res.getStates()); - std::for_each (std::get<2>(transition.first).begin(), std::get<2>(transition.first).end(), [&](const alphabet::Symbol& pop) { - automaton::State fromState = (popPushIndex == 0) ? std::get<0>(transition.first) : lastUS; - if(popPushIndex != 0) lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(++i)), res.getStates()); - automaton::State toState = (popPushIndex == popPushSymbols - 1) ? to.first : lastUS; - - res.addState(fromState); - res.addState(toState); - - if(popPushIndex == 0) - res.addReturnTransition(fromState, std::get<1>(transition.first), pop, toState); - else - res.addReturnTransition(fromState, pop, toState); - popPushIndex++; - }); - std::for_each (to.second.rbegin(), to.second.rend(), [&](const alphabet::Symbol& push) { - automaton::State fromState = (popPushIndex == 0) ? std::get<0>(transition.first) : lastUS; - if(popPushIndex != 0) lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(++i)), res.getStates()); - automaton::State toState = (popPushIndex == popPushSymbols - 1) ? to.first : lastUS; - - res.addState(fromState); - res.addState(toState); - - if(popPushIndex == 0) - res.addCallTransition(fromState, std::get<1>(transition.first), toState, push); - else - res.addCallTransition(fromState, toState, push); - popPushIndex++; - }); + int popPushSymbols = std::get < 2 > ( transition.first ).size ( ) + to.second.size ( ); + + automaton::State lastUS = automaton::createUniqueState ( automaton::State ( us + std::to_string ( i ) ), res.getStates ( ) ); + std::for_each ( std::get < 2 > ( transition.first ).begin ( ), std::get < 2 > ( transition.first ).end ( ), [&] ( const alphabet::Symbol & pop ) { + automaton::State fromState = ( popPushIndex == 0 ) ? std::get < 0 > ( transition.first ) : lastUS; + + if ( popPushIndex != 0 ) lastUS = automaton::createUniqueState ( automaton::State ( us + std::to_string ( ++i ) ), res.getStates ( ) ); + + automaton::State toState = ( popPushIndex == popPushSymbols - 1 ) ? to.first : lastUS; + + res.addState ( fromState ); + res.addState ( toState ); + + if ( popPushIndex == 0 ) + res.addReturnTransition ( fromState, std::get < 1 > ( transition.first ), pop, toState ); + else + res.addReturnTransition ( fromState, pop, toState ); + + popPushIndex++; + } ); + std::for_each ( to.second.rbegin ( ), to.second.rend ( ), [&] ( const alphabet::Symbol & push ) { + automaton::State fromState = ( popPushIndex == 0 ) ? std::get < 0 > ( transition.first ) : lastUS; + + if ( popPushIndex != 0 ) lastUS = automaton::createUniqueState ( automaton::State ( us + std::to_string ( ++i ) ), res.getStates ( ) ); + + automaton::State toState = ( popPushIndex == popPushSymbols - 1 ) ? to.first : lastUS; + + res.addState ( fromState ); + res.addState ( toState ); + + if ( popPushIndex == 0 ) + res.addCallTransition ( fromState, std::get < 1 > ( transition.first ), toState, push ); + else + res.addCallTransition ( fromState, toState, push ); + + popPushIndex++; + } ); } } return res; } -auto PDAToRHPDADPDA = PDAToRHPDA::RegistratorWrapper<automaton::RealTimeHeightDeterministicDPDA, automaton::DPDA>(PDAToRHPDA::getInstance(), PDAToRHPDA::convert); +auto PDAToRHPDADPDA = PDAToRHPDA::RegistratorWrapper < automaton::RealTimeHeightDeterministicDPDA, automaton::DPDA > ( PDAToRHPDA::getInstance ( ), PDAToRHPDA::convert ); -automaton::RealTimeHeightDeterministicNPDA PDAToRHPDA::convert( const automaton::NPDA & pda ) { - RealTimeHeightDeterministicNPDA res(alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); +automaton::RealTimeHeightDeterministicNPDA PDAToRHPDA::convert ( const automaton::NPDA & pda ) { + RealTimeHeightDeterministicNPDA res ( alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); - res.setInputAlphabet(pda.getInputAlphabet()); - res.setStates(pda.getStates()); - res.setFinalStates(pda.getFinalStates()); - std::set<alphabet::Symbol> stackSymbols = pda.getStackAlphabet(); - stackSymbols.insert( alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); - res.setStackAlphabet(stackSymbols); + res.setInputAlphabet ( pda.getInputAlphabet ( ) ); + res.setStates ( pda.getStates ( ) ); + res.setFinalStates ( pda.getFinalStates ( ) ); + std::set < alphabet::Symbol > stackSymbols = pda.getStackAlphabet ( ); + stackSymbols.insert ( alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); + res.setStackAlphabet ( stackSymbols ); - automaton::State q0 = automaton::createUniqueState(automaton::State("q0"), res.getStates()); - res.addState(q0); - res.addInitialState(q0); + automaton::State q0 = automaton::createUniqueState ( automaton::State ( "q0" ), res.getStates ( ) ); + res.addState ( q0 ); + res.addInitialState ( q0 ); - res.addCallTransition(q0, pda.getInitialState(), pda.getInitialSymbol()); + res.addCallTransition ( q0, pda.getInitialState ( ), pda.getInitialSymbol ( ) ); - std::string us("us"); + std::string us ( "us" ); int i = 0; - for(const auto& transition : pda.getTransitions()) { - for(const auto& to : transition.second) { - if(std::get<2>(transition.first).size() == 0 && to.second.size() == 0) - res.addLocalTransition(std::get<0>(transition.first), std::get<1>(transition.first), to.first); - else if(std::get<2>(transition.first).size() == 1 && to.second.size() == 0) - res.addReturnTransition(std::get<0>(transition.first), std::get<1>(transition.first), std::get<2>(transition.first)[0], to.first); - else if(std::get<2>(transition.first).size() == 0 && to.second.size() == 1) - res.addCallTransition(std::get<0>(transition.first), std::get<1>(transition.first), to.first, to.second[0]); - else { + + for ( const auto & transition : pda.getTransitions ( ) ) + for ( const auto & to : transition.second ) { + if ( ( std::get < 2 > ( transition.first ).size ( ) == 0 ) && ( to.second.size ( ) == 0 ) ) { + res.addLocalTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), to.first ); + } else if ( ( std::get < 2 > ( transition.first ).size ( ) == 1 ) && ( to.second.size ( ) == 0 ) ) { + res.addReturnTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), std::get < 2 > ( transition.first )[0], to.first ); + } else if ( ( std::get < 2 > ( transition.first ).size ( ) == 0 ) && ( to.second.size ( ) == 1 ) ) { + res.addCallTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), to.first, to.second[0] ); + } else { int popPushIndex = 0; - int popPushSymbols = std::get<2>(transition.first).size() + to.second.size(); + int popPushSymbols = std::get < 2 > ( transition.first ).size ( ) + to.second.size ( ); - automaton::State lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(i)), res.getStates()); - std::for_each (std::get<2>(transition.first).begin(), std::get<2>(transition.first).end(), [&](const alphabet::Symbol& pop) { - automaton::State fromState = (popPushIndex == 0) ? std::get<0>(transition.first) : lastUS; - if(popPushIndex != 0) lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(++i)), res.getStates()); - automaton::State toState = (popPushIndex == popPushSymbols - 1) ? to.first : lastUS; + automaton::State lastUS = automaton::createUniqueState ( automaton::State ( us + std::to_string ( i ) ), res.getStates ( ) ); + std::for_each ( std::get < 2 > ( transition.first ).begin ( ), std::get < 2 > ( transition.first ).end ( ), [&] ( const alphabet::Symbol & pop ) { + automaton::State fromState = ( popPushIndex == 0 ) ? std::get < 0 > ( transition.first ) : lastUS; - res.addState(fromState); - res.addState(toState); + if ( popPushIndex != 0 ) lastUS = automaton::createUniqueState ( automaton::State ( us + std::to_string ( ++i ) ), res.getStates ( ) ); - if(popPushIndex == 0) - res.addReturnTransition(fromState, std::get<1>(transition.first), pop, toState); - else - res.addReturnTransition(fromState, pop, toState); - popPushIndex++; - }); - std::for_each (to.second.rbegin(), to.second.rend(), [&](const alphabet::Symbol& push) { - automaton::State fromState = (popPushIndex == 0) ? std::get<0>(transition.first) : lastUS; - if(popPushIndex != 0) lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(++i)), res.getStates()); - automaton::State toState = (popPushIndex == popPushSymbols - 1) ? to.first : lastUS; + automaton::State toState = ( popPushIndex == popPushSymbols - 1 ) ? to.first : lastUS; - res.addState(fromState); - res.addState(toState); + res.addState ( fromState ); + res.addState ( toState ); - if(popPushIndex == 0) - res.addCallTransition(fromState, std::get<1>(transition.first), toState, push); - else - res.addCallTransition(fromState, toState, push); - popPushIndex++; - }); + if ( popPushIndex == 0 ) + res.addReturnTransition ( fromState, std::get < 1 > ( transition.first ), pop, toState ); + else + res.addReturnTransition ( fromState, pop, toState ); + + popPushIndex++; + } ); + std::for_each ( to.second.rbegin ( ), to.second.rend ( ), [&] ( const alphabet::Symbol & push ) { + automaton::State fromState = ( popPushIndex == 0 ) ? std::get < 0 > ( transition.first ) : lastUS; + + if ( popPushIndex != 0 ) lastUS = automaton::createUniqueState ( automaton::State ( us + std::to_string ( ++i ) ), res.getStates ( ) ); + + automaton::State toState = ( popPushIndex == popPushSymbols - 1 ) ? to.first : lastUS; + + res.addState ( fromState ); + res.addState ( toState ); + + if ( popPushIndex == 0 ) + res.addCallTransition ( fromState, std::get < 1 > ( transition.first ), toState, push ); + else + res.addCallTransition ( fromState, toState, push ); + + popPushIndex++; + } ); } } - } - + return res; } -auto PDAToRHPDANPDA = PDAToRHPDA::RegistratorWrapper<automaton::RealTimeHeightDeterministicNPDA, automaton::NPDA>(PDAToRHPDA::getInstance(), PDAToRHPDA::convert); +auto PDAToRHPDANPDA = PDAToRHPDA::RegistratorWrapper < automaton::RealTimeHeightDeterministicNPDA, automaton::NPDA > ( PDAToRHPDA::getInstance ( ), PDAToRHPDA::convert ); -automaton::Automaton PDAToRHPDA::convert(const Automaton& automaton) { - return getInstance().dispatch(automaton.getData()); +automaton::Automaton PDAToRHPDA::convert ( const Automaton & automaton ) { + return getInstance ( ).dispatch ( automaton.getData ( ) ); } } namespace alib { -auto RealTimeHeightDeterministicDPDAFromDPDA = castApi::CastRegister<automaton::RealTimeHeightDeterministicDPDA, automaton::DPDA>(automaton::PDAToRHPDA::convert); -auto RealTimeHeightDeterministicNPDAFromNPDA = castApi::CastRegister<automaton::RealTimeHeightDeterministicNPDA, automaton::NPDA>(automaton::PDAToRHPDA::convert); - +auto RealTimeHeightDeterministicDPDAFromDPDA = castApi::CastRegister < automaton::RealTimeHeightDeterministicDPDA, automaton::DPDA > ( automaton::PDAToRHPDA::convert ); +auto RealTimeHeightDeterministicNPDAFromNPDA = castApi::CastRegister < automaton::RealTimeHeightDeterministicNPDA, automaton::NPDA > ( automaton::PDAToRHPDA::convert ); } - diff --git a/alib2algo/src/automaton/transform/RHPDAToPDA.cpp b/alib2algo/src/automaton/transform/RHPDAToPDA.cpp index 3d12613dcb25fa1aa05aed76e2ded8ef56259434..0382ec32d684116e49f9af40ad555a1b169add16 100644 --- a/alib2algo/src/automaton/transform/RHPDAToPDA.cpp +++ b/alib2algo/src/automaton/transform/RHPDAToPDA.cpp @@ -22,246 +22,251 @@ namespace automaton { -automaton::DPDA RHPDAToPDA::convert( const automaton::RealTimeHeightDeterministicDPDA & pda ) { - std::map<std::tuple<automaton::State, alphabet::Symbol, std::vector<alphabet::Symbol>>, std::set<std::pair<automaton::State, std::vector<alphabet::Symbol>>>> readingTransitions; - std::map<automaton::State, std::set<std::tuple<std::vector<alphabet::Symbol>, automaton::State, std::vector<alphabet::Symbol>>>> epsilonTransitions; - - for(const auto& transition : pda.getCallTransitions()) { - if(std::get<1>(transition.first).is<string::Epsilon>()) { - auto& epsT = epsilonTransitions[std::get<0>(transition.first)]; - const auto& to = transition.second; - epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{}, to.first, std::vector<alphabet::Symbol>{to.second})); +automaton::DPDA RHPDAToPDA::convert ( const automaton::RealTimeHeightDeterministicDPDA & pda ) { + std::map < std::tuple < automaton::State, alphabet::Symbol, std::vector < alphabet::Symbol > >, std::set < std::pair < automaton::State, std::vector < alphabet::Symbol > > > > readingTransitions; + std::map < automaton::State, std::set < std::tuple < std::vector < alphabet::Symbol >, automaton::State, std::vector < alphabet::Symbol > > > > epsilonTransitions; + + for ( const auto & transition : pda.getCallTransitions ( ) ) { + if ( std::get < 1 > ( transition.first ).is < string::Epsilon > ( ) ) { + auto & epsT = epsilonTransitions[std::get < 0 > ( transition.first )]; + const auto & to = transition.second; + epsT.insert ( std::make_tuple ( std::vector < alphabet::Symbol > { }, to.first, std::vector < alphabet::Symbol > { to.second } ) ); } else { - auto& readT = readingTransitions[std::make_tuple(std::get<0>(transition.first), std::get<1>(transition.first).get<alphabet::Symbol>(), std::vector<alphabet::Symbol>{})]; - const auto& to = transition.second; - readT.insert(std::make_pair(to.first, std::vector<alphabet::Symbol>{to.second})); + auto & readT = readingTransitions[std::make_tuple ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ).get < alphabet::Symbol > ( ), std::vector < alphabet::Symbol > { } )]; + const auto & to = transition.second; + readT.insert ( std::make_pair ( to.first, std::vector < alphabet::Symbol > { to.second } ) ); } } - for(const auto& transition : pda.getLocalTransitions()) { - if(std::get<1>(transition.first).is<string::Epsilon>()) { - auto& epsT = epsilonTransitions[std::get<0>(transition.first)]; - const auto& to = transition.second; - epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{}, to, std::vector<alphabet::Symbol>{})); + for ( const auto & transition : pda.getLocalTransitions ( ) ) { + if ( std::get < 1 > ( transition.first ).is < string::Epsilon > ( ) ) { + auto & epsT = epsilonTransitions[std::get < 0 > ( transition.first )]; + const auto & to = transition.second; + epsT.insert ( std::make_tuple ( std::vector < alphabet::Symbol > { }, to, std::vector < alphabet::Symbol > { } ) ); } else { - auto& readT = readingTransitions[std::make_tuple(std::get<0>(transition.first), std::get<1>(transition.first).get<alphabet::Symbol>(), std::vector<alphabet::Symbol>{})]; - const auto& to = transition.second; - readT.insert(std::make_pair(to, std::vector<alphabet::Symbol>{})); + auto & readT = readingTransitions[std::make_tuple ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ).get < alphabet::Symbol > ( ), std::vector < alphabet::Symbol > { } )]; + const auto & to = transition.second; + readT.insert ( std::make_pair ( to, std::vector < alphabet::Symbol > { } ) ); } } - for(const auto& transition : pda.getReturnTransitions()) { - if(std::get<2>(transition.first).getData() == alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK) continue; - if(std::get<1>(transition.first).is<string::Epsilon>()) { - auto& epsT = epsilonTransitions[std::get<0>(transition.first)]; - const auto& to = transition.second; - epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{std::get<2>(transition.first)}, to, std::vector<alphabet::Symbol>{})); + for ( const auto & transition : pda.getReturnTransitions ( ) ) { + if ( std::get < 2 > ( transition.first ).getData ( ) == alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK ) continue; + + if ( std::get < 1 > ( transition.first ).is < string::Epsilon > ( ) ) { + auto & epsT = epsilonTransitions[std::get < 0 > ( transition.first )]; + const auto & to = transition.second; + epsT.insert ( std::make_tuple ( std::vector < alphabet::Symbol > { std::get < 2 > ( transition.first ) }, to, std::vector < alphabet::Symbol > { } ) ); } else { - auto& readT = readingTransitions[std::make_tuple(std::get<0>(transition.first), std::get<1>(transition.first).get<alphabet::Symbol>(), std::vector<alphabet::Symbol>{std::get<2>(transition.first)})]; - const auto& to = transition.second; - readT.insert(std::make_pair(to, std::vector<alphabet::Symbol>{})); + auto & readT = readingTransitions[std::make_tuple ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ).get < alphabet::Symbol > ( ), std::vector < alphabet::Symbol > { std::get < 2 > ( transition.first ) } )]; + const auto & to = transition.second; + readT.insert ( std::make_pair ( to, std::vector < alphabet::Symbol > { } ) ); } } - for(const auto& st : epsilonTransitions) { - if(st.second.size() != 1) throw exception::AlibException("Temporary states has more than one leaving transition"); - } + for ( const auto & st : epsilonTransitions ) + if ( st.second.size ( ) != 1 ) throw exception::AlibException ( "Temporary states has more than one leaving transition" ); - if(epsilonTransitions[pda.getInitialState()].empty()) { - throw exception::AlibException("Cannot determine initial pushdown store symbol"); - } + if ( epsilonTransitions[pda.getInitialState ( )].empty ( ) ) + throw exception::AlibException ( "Cannot determine initial pushdown store symbol" ); - const auto& st = *epsilonTransitions[pda.getInitialState()].begin(); + const auto & st = * epsilonTransitions[pda.getInitialState ( )].begin ( ); - std::vector<alphabet::Symbol> pops; - std::vector<alphabet::Symbol> pushes; + std::vector < alphabet::Symbol > pops; + std::vector < alphabet::Symbol > pushes; - pops.insert(pops.end(), std::get<0>(st).begin(), std::get<0>(st).end()); - pushes.insert(pushes.begin(), std::get<2>(st).rbegin(), std::get<2>(st).rend()); + pops.insert ( pops.end ( ), std::get < 0 > ( st ).begin ( ), std::get < 0 > ( st ).end ( ) ); + pushes.insert ( pushes.begin ( ), std::get < 2 > ( st ).rbegin ( ), std::get < 2 > ( st ).rend ( ) ); - automaton::State toState = std::get<1>(st); - while(!epsilonTransitions[toState].empty()) { - const auto& epsilonT = *epsilonTransitions[toState].begin(); + automaton::State toState = std::get < 1 > ( st ); - pops.insert(pops.end(), std::get<0>(epsilonT).begin(), std::get<0>(epsilonT).end()); - pushes.insert(pushes.begin(), std::get<2>(epsilonT).rbegin(), std::get<2>(epsilonT).rend()); + while ( !epsilonTransitions[toState].empty ( ) ) { + const auto & epsilonT = * epsilonTransitions[toState].begin ( ); - toState = std::get<1>(epsilonT); + pops.insert ( pops.end ( ), std::get < 0 > ( epsilonT ).begin ( ), std::get < 0 > ( epsilonT ).end ( ) ); + pushes.insert ( pushes.begin ( ), std::get < 2 > ( epsilonT ).rbegin ( ), std::get < 2 > ( epsilonT ).rend ( ) ); + + toState = std::get < 1 > ( epsilonT ); } - if(pops.size() != 0 && pushes.size() != 1) - throw exception::AlibException("Cannot convert"); + if ( ( pops.size ( ) != 0 ) && ( pushes.size ( ) != 1 ) ) + throw exception::AlibException ( "Cannot convert" ); - automaton::DPDA res(toState, pushes[0]); + automaton::DPDA res ( toState, pushes[0] ); - res.setInputAlphabet(pda.getInputAlphabet()); - res.setStackAlphabet(pda.getStackAlphabet()); - res.removeStackSymbol(pda.getBottomOfTheStackSymbol()); + res.setInputAlphabet ( pda.getInputAlphabet ( ) ); + res.setStackAlphabet ( pda.getStackAlphabet ( ) ); + res.removeStackSymbol ( pda.getBottomOfTheStackSymbol ( ) ); - for(const auto& st : readingTransitions) { - for(const auto& to : st.second) { - std::vector<alphabet::Symbol> pops; - std::vector<alphabet::Symbol> pushes; + for ( const auto & st : readingTransitions ) + for ( const auto & to : st.second ) { + std::vector < alphabet::Symbol > pops; + std::vector < alphabet::Symbol > pushes; - pops.insert(pops.end(), std::get<2>(st.first).begin(), std::get<2>(st.first).end()); - pushes.insert(pushes.begin(), to.second.rbegin(), to.second.rend()); + pops.insert ( pops.end ( ), std::get < 2 > ( st.first ).begin ( ), std::get < 2 > ( st.first ).end ( ) ); + pushes.insert ( pushes.begin ( ), to.second.rbegin ( ), to.second.rend ( ) ); automaton::State toState = to.first; - while(!epsilonTransitions[toState].empty()) { - const auto& epsilonT = *epsilonTransitions[toState].begin(); - pops.insert(pops.end(), std::get<0>(epsilonT).begin(), std::get<0>(epsilonT).end()); - pushes.insert(pushes.begin(), std::get<2>(epsilonT).rbegin(), std::get<2>(epsilonT).rend()); + while ( !epsilonTransitions[toState].empty ( ) ) { + const auto & epsilonT = * epsilonTransitions[toState].begin ( ); + + pops.insert ( pops.end ( ), std::get < 0 > ( epsilonT ).begin ( ), std::get < 0 > ( epsilonT ).end ( ) ); + pushes.insert ( pushes.begin ( ), std::get < 2 > ( epsilonT ).rbegin ( ), std::get < 2 > ( epsilonT ).rend ( ) ); - toState = std::get<1>(epsilonT); + toState = std::get < 1 > ( epsilonT ); } - res.addState(std::get<0>(st.first)); - res.addState(toState); + res.addState ( std::get < 0 > ( st.first ) ); + res.addState ( toState ); - res.addTransition(std::get<0>(st.first), std::get<1>(st.first), pops, toState, pushes); + res.addTransition ( std::get < 0 > ( st.first ), std::get < 1 > ( st.first ), pops, toState, pushes ); } - } - res.setFinalStates(pda.getFinalStates()); + res.setFinalStates ( pda.getFinalStates ( ) ); return res; } -auto RHPDAToPDARealTimeHeightDeterministicDPDA = RHPDAToPDA::RegistratorWrapper<automaton::DPDA, automaton::RealTimeHeightDeterministicDPDA>(RHPDAToPDA::getInstance(), RHPDAToPDA::convert); +auto RHPDAToPDARealTimeHeightDeterministicDPDA = RHPDAToPDA::RegistratorWrapper < automaton::DPDA, automaton::RealTimeHeightDeterministicDPDA > ( RHPDAToPDA::getInstance ( ), RHPDAToPDA::convert ); + + // This may not work correctly -- generation of initial state and initial symbol +automaton::NPDA RHPDAToPDA::convert ( const automaton::RealTimeHeightDeterministicNPDA & pda ) { + std::map < std::tuple < automaton::State, alphabet::Symbol, std::vector < alphabet::Symbol > >, std::set < std::pair < automaton::State, std::vector < alphabet::Symbol > > > > readingTransitions; + std::map < automaton::State, std::set < std::tuple < std::vector < alphabet::Symbol >, automaton::State, std::vector < alphabet::Symbol > > > > epsilonTransitions; -//This may not work correctly -- generation of initial state and initial symbol -automaton::NPDA RHPDAToPDA::convert( const automaton::RealTimeHeightDeterministicNPDA & pda ) { - std::map<std::tuple<automaton::State, alphabet::Symbol, std::vector<alphabet::Symbol>>, std::set<std::pair<automaton::State, std::vector<alphabet::Symbol>>>> readingTransitions; - std::map<automaton::State, std::set<std::tuple<std::vector<alphabet::Symbol>, automaton::State, std::vector<alphabet::Symbol>>>> epsilonTransitions; + for ( const auto & transition : pda.getCallTransitions ( ) ) { + if ( std::get < 1 > ( transition.first ).is < string::Epsilon > ( ) ) { + auto & epsT = epsilonTransitions[std::get < 0 > ( transition.first )]; - for(const auto& transition : pda.getCallTransitions()) { - if(std::get<1>(transition.first).is<string::Epsilon>()) { - auto& epsT = epsilonTransitions[std::get<0>(transition.first)]; - for(const auto& to : transition.second) { - epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{}, to.first, std::vector<alphabet::Symbol>{to.second})); + for ( const auto & to : transition.second ) { + epsT.insert ( std::make_tuple ( std::vector < alphabet::Symbol > { }, to.first, std::vector < alphabet::Symbol > { to.second } ) ); } } else { - auto& readT = readingTransitions[std::make_tuple(std::get<0>(transition.first), std::get<1>(transition.first).get<alphabet::Symbol>(), std::vector<alphabet::Symbol>{})]; - for(const auto& to : transition.second) { - readT.insert(std::make_pair(to.first, std::vector<alphabet::Symbol>{to.second})); + auto & readT = readingTransitions[std::make_tuple ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ).get < alphabet::Symbol > ( ), std::vector < alphabet::Symbol > { } )]; + + for ( const auto & to : transition.second ) { + readT.insert ( std::make_pair ( to.first, std::vector < alphabet::Symbol > { to.second } ) ); } } } - for(const auto& transition : pda.getLocalTransitions()) { - if(std::get<1>(transition.first).is<string::Epsilon>()) { - auto& epsT = epsilonTransitions[std::get<0>(transition.first)]; - for(const auto& to : transition.second) { - epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{}, to, std::vector<alphabet::Symbol>{})); + for ( const auto & transition : pda.getLocalTransitions ( ) ) { + if ( std::get < 1 > ( transition.first ).is < string::Epsilon > ( ) ) { + auto & epsT = epsilonTransitions[std::get < 0 > ( transition.first )]; + + for ( const auto & to : transition.second ) { + epsT.insert ( std::make_tuple ( std::vector < alphabet::Symbol > { }, to, std::vector < alphabet::Symbol > { } ) ); } } else { - auto& readT = readingTransitions[std::make_tuple(std::get<0>(transition.first), std::get<1>(transition.first).get<alphabet::Symbol>(), std::vector<alphabet::Symbol>{})]; - for(const auto& to : transition.second) { - readT.insert(std::make_pair(to, std::vector<alphabet::Symbol>{})); + auto & readT = readingTransitions[std::make_tuple ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ).get < alphabet::Symbol > ( ), std::vector < alphabet::Symbol > { } )]; + + for ( const auto & to : transition.second ) { + readT.insert ( std::make_pair ( to, std::vector < alphabet::Symbol > { } ) ); } } } - for(const auto& transition : pda.getReturnTransitions()) { - if(std::get<2>(transition.first).getData() == alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK) continue; - if(std::get<1>(transition.first).is<string::Epsilon>()) { - auto& epsT = epsilonTransitions[std::get<0>(transition.first)]; - for(const auto& to : transition.second) { - epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{std::get<2>(transition.first)}, to, std::vector<alphabet::Symbol>{})); + for ( const auto & transition : pda.getReturnTransitions ( ) ) { + if ( std::get < 2 > ( transition.first ).getData ( ) == alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK ) continue; + + if ( std::get < 1 > ( transition.first ).is < string::Epsilon > ( ) ) { + auto & epsT = epsilonTransitions[std::get < 0 > ( transition.first )]; + + for ( const auto & to : transition.second ) { + epsT.insert ( std::make_tuple ( std::vector < alphabet::Symbol > { std::get < 2 > ( transition.first ) }, to, std::vector < alphabet::Symbol > { } ) ); } } else { - auto& readT = readingTransitions[std::make_tuple(std::get<0>(transition.first), std::get<1>(transition.first).get<alphabet::Symbol>(), std::vector<alphabet::Symbol>{std::get<2>(transition.first)})]; - for(const auto& to : transition.second) { - readT.insert(std::make_pair(to, std::vector<alphabet::Symbol>{})); + auto & readT = readingTransitions[std::make_tuple ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ).get < alphabet::Symbol > ( ), std::vector < alphabet::Symbol > { std::get < 2 > ( transition.first ) } )]; + + for ( const auto & to : transition.second ) { + readT.insert ( std::make_pair ( to, std::vector < alphabet::Symbol > { } ) ); } } } - for(const auto& st : epsilonTransitions) { - if(st.second.size() != 1) throw exception::AlibException("Temporary states has more than one leaving transition"); - } + for ( const auto & st : epsilonTransitions ) + if ( st.second.size ( ) != 1 ) throw exception::AlibException ( "Temporary states has more than one leaving transition" ); + + if ( pda.getInitialStates ( ).size ( ) != 1 ) + throw exception::AlibException ( "Cannot convert" ); - if(pda.getInitialStates().size() != 1) - throw exception::AlibException("Cannot convert"); + const automaton::State & initialState = * pda.getInitialStates ( ).begin ( ); - const automaton::State& initialState = *pda.getInitialStates().begin(); + if ( epsilonTransitions[initialState].empty ( ) ) + throw exception::AlibException ( "Cannot convert" ); - if(epsilonTransitions[initialState].empty()) - throw exception::AlibException("Cannot convert"); + // -------------------------------------------------------------------- initial state and initial stack symbol + const auto & st = * epsilonTransitions[initialState].begin ( ); - //-------------------------------------------------------------------- initial state and initial stack symbol - const auto& st = *epsilonTransitions[initialState].begin(); + std::vector < alphabet::Symbol > pops; + std::vector < alphabet::Symbol > pushes; - std::vector<alphabet::Symbol> pops; - std::vector<alphabet::Symbol> pushes; + pops.insert ( pops.end ( ), std::get < 0 > ( st ).begin ( ), std::get < 0 > ( st ).end ( ) ); + pushes.insert ( pushes.begin ( ), std::get < 2 > ( st ).rbegin ( ), std::get < 2 > ( st ).rend ( ) ); - pops.insert(pops.end(), std::get<0>(st).begin(), std::get<0>(st).end()); - pushes.insert(pushes.begin(), std::get<2>(st).rbegin(), std::get<2>(st).rend()); + automaton::State toState = std::get < 1 > ( st ); - automaton::State toState = std::get<1>(st); - while(!epsilonTransitions[toState].empty()) { - const auto& epsilonT = *epsilonTransitions[toState].begin(); + while ( !epsilonTransitions[toState].empty ( ) ) { + const auto & epsilonT = * epsilonTransitions[toState].begin ( ); - pops.insert(pops.end(), std::get<0>(epsilonT).begin(), std::get<0>(epsilonT).end()); - pushes.insert(pushes.begin(), std::get<2>(epsilonT).rbegin(), std::get<2>(epsilonT).rend()); + pops.insert ( pops.end ( ), std::get < 0 > ( epsilonT ).begin ( ), std::get < 0 > ( epsilonT ).end ( ) ); + pushes.insert ( pushes.begin ( ), std::get < 2 > ( epsilonT ).rbegin ( ), std::get < 2 > ( epsilonT ).rend ( ) ); - toState = std::get<1>(epsilonT); + toState = std::get < 1 > ( epsilonT ); } - if(pops.size() != 0 && pushes.size() != 1) - throw exception::AlibException("Cannot convert"); + if ( ( pops.size ( ) != 0 ) && ( pushes.size ( ) != 1 ) ) + throw exception::AlibException ( "Cannot convert" ); - //-------------------------------------------------------------------- initial state and initial stack symbol + // -------------------------------------------------------------------- initial state and initial stack symbol - automaton::NPDA res(toState, pushes[0]); + automaton::NPDA res ( toState, pushes[0] ); - res.setInputAlphabet(pda.getInputAlphabet()); - res.setStackAlphabet(pda.getStackAlphabet()); - res.removeStackSymbol(pda.getBottomOfTheStackSymbol()); + res.setInputAlphabet ( pda.getInputAlphabet ( ) ); + res.setStackAlphabet ( pda.getStackAlphabet ( ) ); + res.removeStackSymbol ( pda.getBottomOfTheStackSymbol ( ) ); - for(const auto& st : readingTransitions) { - for(const auto& to : st.second) { - std::vector<alphabet::Symbol> pops; - std::vector<alphabet::Symbol> pushes; + for ( const auto & st : readingTransitions ) + for ( const auto & to : st.second ) { + std::vector < alphabet::Symbol > pops; + std::vector < alphabet::Symbol > pushes; - pops.insert(pops.end(), std::get<2>(st.first).begin(), std::get<2>(st.first).end()); - pushes.insert(pushes.begin(), to.second.rbegin(), to.second.rend()); + pops.insert ( pops.end ( ), std::get < 2 > ( st.first ).begin ( ), std::get < 2 > ( st.first ).end ( ) ); + pushes.insert ( pushes.begin ( ), to.second.rbegin ( ), to.second.rend ( ) ); automaton::State toState = to.first; - while(!epsilonTransitions[toState].empty()) { - const auto& epsilonT = *epsilonTransitions[toState].begin(); - pops.insert(pops.end(), std::get<0>(epsilonT).begin(), std::get<0>(epsilonT).end()); - pushes.insert(pushes.begin(), std::get<2>(epsilonT).rbegin(), std::get<2>(epsilonT).rend()); + while ( !epsilonTransitions[toState].empty ( ) ) { + const auto & epsilonT = * epsilonTransitions[toState].begin ( ); - toState = std::get<1>(epsilonT); + pops.insert ( pops.end ( ), std::get < 0 > ( epsilonT ).begin ( ), std::get < 0 > ( epsilonT ).end ( ) ); + pushes.insert ( pushes.begin ( ), std::get < 2 > ( epsilonT ).rbegin ( ), std::get < 2 > ( epsilonT ).rend ( ) ); + + toState = std::get < 1 > ( epsilonT ); } - res.addState(std::get<0>(st.first)); - res.addState(toState); + res.addState ( std::get < 0 > ( st.first ) ); + res.addState ( toState ); - res.addTransition(std::get<0>(st.first), std::get<1>(st.first), pops, toState, pushes); + res.addTransition ( std::get < 0 > ( st.first ), std::get < 1 > ( st.first ), pops, toState, pushes ); } - } - res.setFinalStates(pda.getFinalStates()); + res.setFinalStates ( pda.getFinalStates ( ) ); return res; } -auto RHPDAToPDARealTimeHeightDeterministicNPDA = RHPDAToPDA::RegistratorWrapper<automaton::NPDA, automaton::RealTimeHeightDeterministicNPDA>(RHPDAToPDA::getInstance(), RHPDAToPDA::convert); +auto RHPDAToPDARealTimeHeightDeterministicNPDA = RHPDAToPDA::RegistratorWrapper < automaton::NPDA, automaton::RealTimeHeightDeterministicNPDA > ( RHPDAToPDA::getInstance ( ), RHPDAToPDA::convert ); -automaton::Automaton RHPDAToPDA::convert(const Automaton& automaton) { - return getInstance().dispatch(automaton.getData()); +automaton::Automaton RHPDAToPDA::convert ( const Automaton & automaton ) { + return getInstance ( ).dispatch ( automaton.getData ( ) ); } } namespace alib { -auto DPDAFromRealTimeHeightDeterministicNPDA = castApi::CastRegister<automaton::DPDA, automaton::RealTimeHeightDeterministicDPDA>(automaton::RHPDAToPDA::convert); -auto NPDAFromRealTimeHeightDeterministicNPDA = castApi::CastRegister<automaton::NPDA, automaton::RealTimeHeightDeterministicNPDA>(automaton::RHPDAToPDA::convert); - +auto DPDAFromRealTimeHeightDeterministicNPDA = castApi::CastRegister < automaton::DPDA, automaton::RealTimeHeightDeterministicDPDA > ( automaton::RHPDAToPDA::convert ); +auto NPDAFromRealTimeHeightDeterministicNPDA = castApi::CastRegister < automaton::NPDA, automaton::RealTimeHeightDeterministicNPDA > ( automaton::RHPDAToPDA::convert ); } -