diff --git a/aconversions2/src/ConversionHandler.cpp b/aconversions2/src/ConversionHandler.cpp index 12d41daaaaa0847a6b221bf655ecdfe08af69f57..b799488ec1fa4d9dc0721a7110634692382fe09d 100644 --- a/aconversions2/src/ConversionHandler.cpp +++ b/aconversions2/src/ConversionHandler.cpp @@ -25,6 +25,7 @@ #include "regexp/convert/ToGrammarRightRGGlushkov.h" #include "regexp/convert/ToGrammarRightRGDerivation.h" +#include "rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.h" #include "rte/convert/ToPostfixPushdownAutomatonGlushkov.h" #include <factory/XmlDataFactory.hpp> @@ -341,8 +342,17 @@ void ConversionHandler::convertRTEtoPDA ( void ) { measurements::start ( "Algorithm", measurements::Type::MAIN ); switch ( m_algorithm ) { + case GLUSHKOV_RTE_NAIVE: { + automaton::NPDA < > automaton = rte::convert::ToPostfixPushdownAutomatonGlushkovNaive::convert ( rte ); + + measurements::end ( ); + measurements::start ( "Output write", measurements::Type::AUXILIARY ); + + alib::XmlDataFactory::toStdout ( automaton ); + break; + } case GLUSHKOV_RTE: { - automaton::NPDA < > automaton = rte::convert::ToPostfixPushdownAutomatonGlushkov::convert ( rte ); + automaton::Automaton automaton = rte::convert::ToPostfixPushdownAutomatonGlushkov::convert ( rte ); measurements::end ( ); measurements::start ( "Output write", measurements::Type::AUXILIARY ); @@ -434,6 +444,8 @@ ConversionHandler::TAlgorithm ConversionHandler::parseAlgorithmFromString ( cons if ( algorithm == "bottomup" ) return BOTTOM_UP; + if ( algorithm == "glushkovrtenaive" ) return GLUSHKOV_RTE_NAIVE; + if ( algorithm == "glushkovrte" ) return GLUSHKOV_RTE; if ( ( algorithm == "" ) || ( algorithm == "default" ) ) return DEFAULT; diff --git a/aconversions2/src/ConversionHandler.h b/aconversions2/src/ConversionHandler.h index 8127f9b0b7931db2f7dfad6808e1d7862e423505..5d9ae39fd9dba75287375fcd757538398356b1d3 100644 --- a/aconversions2/src/ConversionHandler.h +++ b/aconversions2/src/ConversionHandler.h @@ -30,7 +30,7 @@ public: /* CFG to PDA */ BOTTOM_UP, TOP_DOWN, /* RTE to PDA */ - GLUSHKOV_RTE, + GLUSHKOV_RTE_NAIVE, GLUSHKOV_RTE, }; enum TFormalism { diff --git a/aconversions2/src/aconversion.cpp b/aconversions2/src/aconversion.cpp index a8c76e94a8fcd0870bd11a2f9398978c0c8b1600..07e44cdc09da8441e8e880bbee92bb58c7ea6581 100644 --- a/aconversions2/src/aconversion.cpp +++ b/aconversions2/src/aconversion.cpp @@ -30,7 +30,7 @@ int main ( int argc, char * argv[] ) { cmd.add ( target ); std::vector < std::string > algorithms { - "algebraic", "elimination", "brzozowski", "glushkov", "thompson", "incoming", "outgoing", "bottomup", "topdown", "default", "glushkovrte" + "algebraic", "elimination", "brzozowski", "glushkov", "thompson", "incoming", "outgoing", "bottomup", "topdown", "default", "glushkovrtenaive", "glushkovrte" }; TCLAP::ValuesConstraint < std::string > allowedAlgorithms ( algorithms ); TCLAP::ValueArg < std::string > algorithm ( "a", "algorithm", "Specifies algorithm to use", false, "default", & allowedAlgorithms ); diff --git a/alib2algo/src/rte/convert/ToPostfixPushdownAutomaton.cpp b/alib2algo/src/rte/convert/ToPostfixPushdownAutomaton.cpp index 4d10836452b4b2a6a46ae4c03fd222e20539876c..638b45232ca5511184e1ceaced24d240b960c6c2 100644 --- a/alib2algo/src/rte/convert/ToPostfixPushdownAutomaton.cpp +++ b/alib2algo/src/rte/convert/ToPostfixPushdownAutomaton.cpp @@ -6,7 +6,7 @@ */ #include "ToPostfixPushdownAutomaton.h" -#include "ToPostfixPushdownAutomatonGlushkov.h" +#include "ToPostfixPushdownAutomatonGlushkovNaive.h" #include <exception/CommonException.h> #include <registration/AlgoRegistration.hpp> @@ -19,7 +19,7 @@ automaton::Automaton ToPostfixPushdownAutomaton::convert ( const rte::RTE & rte } automaton::Automaton ToPostfixPushdownAutomaton::convert ( const rte::FormalRTE < > & rte ) { - return automaton::Automaton ( ToPostfixPushdownAutomatonGlushkov::convert ( rte ) ); + return automaton::Automaton ( ToPostfixPushdownAutomatonGlushkovNaive::convert ( rte ) ); } auto ToAutomatonFormalRegExp = registration::OverloadRegister < ToPostfixPushdownAutomaton, automaton::Automaton, rte::FormalRTE < > > ( ToPostfixPushdownAutomaton::convert ); diff --git a/alib2algo/src/rte/convert/ToPostfixPushdownAutomaton.h b/alib2algo/src/rte/convert/ToPostfixPushdownAutomaton.h index 27e3a9a775ff1e70692998ec674af239bc399f04..c77dac628256eec1f2d80aeee6053928bbdad1f5 100644 --- a/alib2algo/src/rte/convert/ToPostfixPushdownAutomaton.h +++ b/alib2algo/src/rte/convert/ToPostfixPushdownAutomaton.h @@ -13,8 +13,6 @@ #include <rte/RTE.h> #include <rte/formal/FormalRTE.h> -// #include <rte/unbounded/UnboundedRegExp.h> - #include <automaton/Automaton.h> namespace rte { diff --git a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp index 2ca12a3f593e838d68c3705f978fa49fb01a6f49..a3d8158d85ff3eb8c93466531949184b7e193e00 100644 --- a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp +++ b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp @@ -1,43 +1,42 @@ /* - * ToPostfixPushdownAutomatonGlushkov.h + * ToPostfixPushdownAutomatonGlushkov.cpp * - * Created on: 11. 4. 2016 + * Created on: 26. 7. 2017 * Author: Tomas Pecka */ #include "ToPostfixPushdownAutomatonGlushkov.h" -#include "alphabet/BottomOfTheStackSymbol.h" -#include "alphabet/EndSymbol.h" - #include <automaton/Automaton.h> +#include <alphabet/BottomOfTheStackSymbol.h> +#include <alphabet/EndSymbol.h> -#include "global/GlobalData.h" +#include <global/GlobalData.h> +#include <registration/AlgoRegistration.hpp> #include "../glushkov/GlushkovFollow.h" #include "../glushkov/GlushkovIndexate.h" #include "../glushkov/GlushkovFirst.h" -#include <registration/AlgoRegistration.hpp> +#include "../glushkov/GlushkovSubstitutionMap.h" namespace rte { namespace convert { -automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::RTE & rte ) { +automaton::Automaton ToPostfixPushdownAutomatonGlushkov::convert ( const rte::RTE & rte ) { return dispatch ( rte.getData ( ) ); } -ext::vector < DefaultSymbolType > phi ( const ext::vector < common::ranked_symbol < > > & follow ) { - return ext::transform < DefaultSymbolType > ( follow, []( const common::ranked_symbol < > & symbol ) { return DefaultSymbolType ( alphabet::RankedSymbol < > ( symbol ) ); } ); -} - -bool isSubstSymbolPresent ( const ext::set < common::ranked_symbol < > > & container, const ext::set < common::ranked_symbol < > > & substAlphabet ) { +template < class SymbolType, class RankType > +bool ToPostfixPushdownAutomatonGlushkov::isSubstSymbolPresent ( const ext::set < common::ranked_symbol < SymbolType, RankType > > & container, const TAlphabet < SymbolType, RankType > & substAlphabet ) { ext::vector < common::ranked_symbol < > > intersection; std::set_intersection ( container.begin ( ), container.end ( ), substAlphabet.begin ( ), substAlphabet.end ( ), std::back_inserter ( intersection ) ); return intersection.size ( ) > 0; } -automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::FormalRTE < > & rte ) { +template < class SymbolType, class RankType > +automaton::NPDA < SymbolType, DefaultEpsilonType, ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < SymbolType, RankType > > >, DefaultStateType > +ToPostfixPushdownAutomatonGlushkov::convert ( const rte::FormalRTE < > & rte ) { // step 1; index RTE rte::FormalRTE < > indexedRTE = rte::GlushkovIndexate::index ( rte ); @@ -47,26 +46,31 @@ automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::For const ext::set < common::ranked_symbol < > > firstSet = rte::GlushkovFirst::first ( indexedRTE ); // - follow set for every element of (non-indexed) RTE alphabet element - ext::map < common::ranked_symbol < >, ext::set < ext::vector < common::ranked_symbol < > > > > followSet; + const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, ext::map < common::ranked_symbol < >, ext::set < common::ranked_symbol < > > > > substMapTree = GlushkovSubstitutionMap::substMap ( indexedRTE ); + ext::map < common::ranked_symbol < >, ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > > followSet; for ( const common::ranked_symbol < > & symbol : indexedRTE.getAlphabet ( ) ) - followSet.insert ( std::make_pair ( symbol, rte::GlushkovFollow::follow ( indexedRTE, symbol ) ) ); + followSet.insert ( std::make_pair ( symbol, rte::GlushkovFollow::follow ( indexedRTE, symbol, substMapTree ) ) ); - /* check for exceptions -> there must be NO substitution symbol in first or follow sets */ + /* check for exceptions -> there must be NO substitution symbol in first set */ if ( isSubstSymbolPresent ( firstSet, rte.getSubstitutionAlphabet ( ) ) ) throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in the first set" ); + /* check end */ - for ( const auto & kv : followSet ) - for ( const auto & followTuple : kv.second ) - if ( isSubstSymbolPresent ( ext::set < common::ranked_symbol < > > ( followTuple.begin ( ), followTuple.end ( ) ), rte.getSubstitutionAlphabet ( ) ) ) - throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in a follow set" ); - + /* check for exceptions -> there must be NO substitution symbol in follow sets */ + for ( const std::pair < const common::ranked_symbol < SymbolType, RankType >, ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > > & kv : followSet ) + for ( const GlushkovFollow::TFollowTuple < SymbolType, RankType > & followTuple : kv.second ) // TFollowTuple = vector < set < ranked_symbol > > + for ( const ext::set < common::ranked_symbol < SymbolType, RankType > > & followTupleElem : followTuple ) + if ( isSubstSymbolPresent ( followTupleElem, rte.getSubstitutionAlphabet ( ) ) ) + throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in a follow set" ); /* check end */ // step 3; create PDA (w/o transitions yet) and initialize input alphabet = (non-indexed) RTE alphabet and END symbol DefaultStateType q = DefaultStateType ( 'q' ); DefaultStateType f = DefaultStateType ( 'f' ); - automaton::NPDA < > automaton ( q, alphabet::BottomOfTheStackSymbol::instance < DefaultSymbolType > ( ) ); + + auto BotS = ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < SymbolType, RankType > > >::template from < alphabet::BottomOfTheStackSymbol > ( ); + automaton::NPDA < SymbolType, DefaultEpsilonType, ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < SymbolType, RankType > > >, DefaultStateType > automaton ( q, BotS ); automaton.addState ( f ); automaton.addFinalState ( f ); @@ -76,11 +80,22 @@ automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::For automaton.addInputSymbol ( alphabet::EndSymbol::instance < DefaultSymbolType > ( ) ); - // step 4; create pushdown store alphabet; it consists of elements of indexed RTE alphabet and BotS symbol + // step 4; create pushdown store alphabet; + + // simple for ( const common::ranked_symbol < > & symb : indexedRTE.getAlphabet ( ) ) - automaton.addPushdownStoreSymbol ( DefaultSymbolType ( alphabet::RankedSymbol < > ( symb ) ) ); + automaton.addPushdownStoreSymbol ( ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < SymbolType, RankType > > > ( { symb } ) ); - /* DEBUG */ + // complex + for ( const std::pair < const rte::FormalRTEElement < SymbolType, RankType >* const, ext::map < common::ranked_symbol < >, ext::set < common::ranked_symbol < > > > > & kv : substMapTree ) { + if ( dynamic_cast < const rte::FormalRTESymbolSubst < SymbolType, RankType > * const > ( kv.first ) ) { + for ( const std::pair < common::ranked_symbol < >, ext::set < common::ranked_symbol < > > > & kv2 : kv.second ) { + automaton.addPushdownStoreSymbol ( kv2.second ); + } + } + } + + /* DEBUG */ if ( common::GlobalData::verbose ) { std::cerr << "RTE:" << std::endl; @@ -96,47 +111,122 @@ automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::For std::cerr << std::endl; - for ( const auto & kv : followSet ) { + // ext::map < common::ranked_symbol < >, ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > > followSet; + for ( const std::pair < const common::ranked_symbol < >, ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > >& kv : followSet ) { std::cerr << "Follow(RTE, " << kv.first << "):" << std::endl; if ( kv.second.empty ( ) ) std::cerr << "\t" << "{}" << std::endl; - for ( const auto & follow : kv.second ) { - for ( const auto & symbol : follow ) - std::cerr << "\t" << symbol << std::endl; + for ( const GlushkovFollow::TFollowTuple < SymbolType, RankType > & followTuple : kv.second ) { // TFollowTuple = vector < set < ranked_symbol > > + std::cerr << " \t - FollowTuple:" << std::endl; + for ( const ext::set < common::ranked_symbol < > > & child : followTuple ) + std::cerr << "\t\t - " << child << std::endl; std::cerr << std::endl; } std::cerr << std::endl; } + + std::cerr << "---------------------------------------------------------" << std::endl; + std::cerr << "PDA:" << std::endl; + std::cerr << "pds symbols" << std::endl; + + for ( const auto & symb : automaton.getPushdownStoreAlphabet ( ) ) + std::cerr << "\t" << symb << std::endl; + std::cerr << std::endl; } /* DEBUG END */ + + /* TRANSITIONS */ + // Pattern 3 and 2 for ( const common::ranked_symbol < > & symb : indexedRTE.getAlphabet ( ) ) { - if ( symb.getRank ( ) == primitive::Unsigned ( 0 ) ) - automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), { }, q, { DefaultSymbolType ( alphabet::RankedSymbol < > ( symb ) ) } ); - else - for ( const ext::vector < common::ranked_symbol < > > & follow : followSet[symb] ) { - ext::vector < DefaultSymbolType > fstring = phi ( follow ); - std::reverse ( fstring.begin ( ), fstring.end ( ) ); - automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), fstring, q, { DefaultSymbolType ( alphabet::RankedSymbol < > ( symb ) ) } ); + if ( symb.getRank ( ) == primitive::Unsigned ( 0 ) ) { + ext::vector < ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < SymbolType, RankType > > > > push; + push.push_back( ext::set < common::ranked_symbol < SymbolType, RankType > > { symb } ); + + if ( common::GlobalData::verbose ) { + std::cerr << "Transition 3: " << rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ) << " | " << std::endl << + "\t" << "[]" << std::endl << + "\t ->" << std::endl << + "\t" << push << std::endl << std::endl; } + automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), { }, q, push ); + } else { + for ( const GlushkovFollow::TFollowTuple < SymbolType, RankType > & followTuple : followSet [ symb ] ) { //tuple = vector < set < symb > > + ext::vector < ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < SymbolType, RankType > > > > pop, push; + + /* + for ( const auto & e : symbFollowTuple ) + pop.push_back ( e ); + std::reverse ( pop.begin ( ), pop.end ( ) ); // <------------ WTF. CRASHES HERE // FIXME + */ + for ( const auto & e : followTuple ) + pop.insert ( pop.begin ( ), e ); + + push.push_back( ext::set < common::ranked_symbol < SymbolType, RankType > > { symb } ); + + if ( common::GlobalData::verbose ) { + std::cerr << "Transition 2: " << rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ) << " | " << std::endl << + "\t" << pop << std::endl << + "\t ->" << std::endl << + "\t" << push << std::endl << std::endl; + } + automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), pop, q, push ); + } + } + } + + // Pattern 1 + for ( const common::ranked_symbol < > & symb : indexedRTE.getAlphabet ( ) ) { + for ( const std::pair < const rte::FormalRTEElement < SymbolType, RankType >* const, ext::map < common::ranked_symbol < >, ext::set < common::ranked_symbol < > > > > & kv : substMapTree ) { + if ( dynamic_cast < const rte::FormalRTESymbolSubst < SymbolType, RankType > * const > ( kv.first ) == nullptr ) // not a SubstSymbol node + continue; + for ( const std::pair < common::ranked_symbol < >, ext::set < common::ranked_symbol < > > > & kv2 : kv.second ) { + if ( kv2.second.count ( symb ) == 0 ) + continue; + + for ( const GlushkovFollow::TFollowTuple < SymbolType, RankType > & symbFollowTuple : followSet [ symb ] ) { + ext::vector < ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < SymbolType, RankType > > > > pop, push; + + if ( ( size_t ) symb.getRank ( ) > 0 ) { + for ( const auto & e : symbFollowTuple ) + pop.insert ( pop.begin ( ), e ); + } + + push.push_back( kv2.second ); + + if ( common::GlobalData::verbose ) { + std::cerr << "Transition 1" << ( pop.empty() ? "a" : "b" ) << ": " << rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ) << " | " << std::endl << + "\t" << pop << std::endl << + "\t ->" << std::endl << + "\t" << push << std::endl << std::endl; + } + + automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), pop, q, push ); + } + } + } } + // Final for ( const common::ranked_symbol < > & symb : firstSet ) { - ext::vector < DefaultSymbolType > pop; - pop.push_back ( DefaultSymbolType ( alphabet::RankedSymbol < > ( symb ) ) ); - pop.push_back ( alphabet::BottomOfTheStackSymbol::instance < DefaultSymbolType > ( ) ); + ext::vector < ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < SymbolType, RankType > > > > pop; + pop.push_back ( ext::set < common::ranked_symbol < SymbolType, RankType > > { symb } ); + pop.push_back ( BotS ); automaton.addTransition ( q, alphabet::EndSymbol::instance < DefaultSymbolType > ( ), pop, f, { } ); } + /* TRANSITIONS END */ return automaton; } -auto ToAutomatonGlushkovFormalRegExp = registration::OverloadRegister < ToPostfixPushdownAutomatonGlushkov, automaton::NPDA < >, rte::FormalRTE < > > ( ToPostfixPushdownAutomatonGlushkov::convert ); +auto ToPostfixPushdownAutomatonGlushkovFormalRTE = registration::OverloadRegister < ToPostfixPushdownAutomatonGlushkov, + automaton::NPDA < DefaultSymbolType, DefaultEpsilonType, ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < alib::Object, primitive::Unsigned > > >, DefaultStateType >, + rte::FormalRTE < > > ( ToPostfixPushdownAutomatonGlushkov::convert ); } /* namespace convert */ diff --git a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.h b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.h index dee3b1b44da7cc9b4eda83ab7e4b2290cd401f67..86b9bde61049ef3b5d1c3f49ce6f1ee16c81a003 100644 --- a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.h +++ b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.h @@ -1,7 +1,7 @@ /* * ToPostfixPushdownAutomatonGlushkov.h * - * Created on: 11. 4. 2016 + * Created on: 26. 7. 2017 * Author: Tomas Pecka */ @@ -11,12 +11,13 @@ #include <core/multipleDispatch.hpp> #include <map> -#include <automaton/FSM/NFA.h> #include <automaton/PDA/NPDA.h> + #include <rte/RTE.h> #include <rte/formal/FormalRTE.h> -// #include <rte/unbounded/UnboundedRegExp.h> +#include <container/ObjectsVariant.h> +#include <container/ObjectsSet.h> namespace rte { @@ -26,19 +27,32 @@ namespace convert { * Converts regular tree expression to real-time height-deterministic pda * Source: Master Thesis, Pecka Tomas, CTU FIT, 2016, chapter 4.2 */ -class ToPostfixPushdownAutomatonGlushkov : public alib::SingleDispatch < ToPostfixPushdownAutomatonGlushkov, automaton::NPDA < >, const rte::RTEBase & > { +class ToPostfixPushdownAutomatonGlushkov : public alib::SingleDispatch < ToPostfixPushdownAutomatonGlushkov, automaton::Automaton, const rte::RTEBase & > { +private: + // -------------------------------------------------------------------- + + template < class SymbolType, class RankType > + using TAlphabet = ext::set < common::ranked_symbol < SymbolType, RankType > >; + + // -------------------------------------------------------------------- + + template < class SymbolType, class RankType > + static bool isSubstSymbolPresent ( const ext::set < common::ranked_symbol < SymbolType, RankType > > & container, const TAlphabet < SymbolType, RankType > & substAlphabet ); + public: /** * Performs conversion. * @param re Original regular tree expression. * @return rhNPDA equivalent to original regular expression. */ - static automaton::NPDA < > convert ( const rte::RTE & rte ); - static automaton::NPDA < > convert ( const rte::FormalRTE < > & rte ); + static automaton::Automaton convert ( const rte::RTE & rte ); + + template < class SymbolType, class RankType > + static automaton::NPDA < SymbolType, DefaultEpsilonType, ext::variant < alphabet::BottomOfTheStackSymbol, ext::set < common::ranked_symbol < SymbolType, RankType > > >, DefaultStateType > convert ( const rte::FormalRTE < > & rte ); }; } /* namespace convert */ } /* namespace rte */ -#endif /* TO_POSTFIX_PUSHDOWN_AUTOMATON_GLUSHKOV_H_ */ +#endif /* TO_POSTFIX_PUSHDOWN_AUTOMATON_GLUSHKOV_V3_H_ */ diff --git a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.cpp b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8874ac12735c3d73f4d4b765921df607cfe771c --- /dev/null +++ b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.cpp @@ -0,0 +1,144 @@ +/* + * ToPostfixPushdownAutomatonGlushkovNaive.h + * + * Created on: 11. 4. 2016 + * Author: Tomas Pecka + */ + +#include "ToPostfixPushdownAutomatonGlushkovNaive.h" + +#include <alphabet/BottomOfTheStackSymbol.h> +#include <alphabet/EndSymbol.h> + +#include <automaton/Automaton.h> + +#include <global/GlobalData.h> + +#include <registration/AlgoRegistration.hpp> + +#include "../glushkov/GlushkovFollowNaive.h" +#include "../glushkov/GlushkovIndexate.h" +#include "../glushkov/GlushkovFirst.h" + +namespace rte { + +namespace convert { + +automaton::NPDA < > ToPostfixPushdownAutomatonGlushkovNaive::convert ( const rte::RTE & rte ) { + return dispatch ( rte.getData ( ) ); +} + +ext::vector < DefaultSymbolType > phi ( const ext::vector < common::ranked_symbol < > > & follow ) { + return ext::transform < DefaultSymbolType > ( follow, []( const common::ranked_symbol < > & symbol ) { return DefaultSymbolType ( alphabet::RankedSymbol < > ( symbol ) ); } ); +} + +bool isSubstSymbolPresent ( const ext::set < common::ranked_symbol < > > & container, const ext::set < common::ranked_symbol < > > & substAlphabet ) { + ext::vector < common::ranked_symbol < > > intersection; + std::set_intersection ( container.begin ( ), container.end ( ), substAlphabet.begin ( ), substAlphabet.end ( ), std::back_inserter ( intersection ) ); + return intersection.size ( ) > 0; +} + +automaton::NPDA < > ToPostfixPushdownAutomatonGlushkovNaive::convert ( const rte::FormalRTE < > & rte ) { + + // step 1; index RTE + rte::FormalRTE < > indexedRTE = rte::GlushkovIndexate::index ( rte ); + + // step 2; compute: + // - first set + const ext::set < common::ranked_symbol < > > firstSet = rte::GlushkovFirst::first ( indexedRTE ); + + // - follow set for every element of (non-indexed) RTE alphabet element + ext::map < common::ranked_symbol < >, ext::set < ext::vector < common::ranked_symbol < > > > > followSet; + + for ( const common::ranked_symbol < > & symbol : indexedRTE.getAlphabet ( ) ) + followSet.insert ( std::make_pair ( symbol, rte::GlushkovFollowNaive::follow ( indexedRTE, symbol ) ) ); + + /* check for exceptions -> there must be NO substitution symbol in first or follow sets */ + if ( isSubstSymbolPresent ( firstSet, rte.getSubstitutionAlphabet ( ) ) ) + throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in the first set" ); + + for ( const auto & kv : followSet ) + for ( const auto & followTuple : kv.second ) + if ( isSubstSymbolPresent ( ext::set < common::ranked_symbol < > > ( followTuple.begin ( ), followTuple.end ( ) ), rte.getSubstitutionAlphabet ( ) ) ) + throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in a follow set" ); + + /* check end */ + + // step 3; create PDA (w/o transitions yet) and initialize input alphabet = (non-indexed) RTE alphabet and END symbol + DefaultStateType q = DefaultStateType ( 'q' ); + DefaultStateType f = DefaultStateType ( 'f' ); + automaton::NPDA < > automaton ( q, alphabet::BottomOfTheStackSymbol::instance < DefaultSymbolType > ( ) ); + + automaton.addState ( f ); + automaton.addFinalState ( f ); + + for ( const common::ranked_symbol < > & symbol : rte.getAlphabet ( ) ) + automaton.addInputSymbol ( symbol.getSymbol ( ) ); + + automaton.addInputSymbol ( alphabet::EndSymbol::instance < DefaultSymbolType > ( ) ); + + // step 4; create pushdown store alphabet; it consists of elements of indexed RTE alphabet and BotS symbol + for ( const common::ranked_symbol < > & symb : indexedRTE.getAlphabet ( ) ) + automaton.addPushdownStoreSymbol ( DefaultSymbolType ( alphabet::RankedSymbol < > ( symb ) ) ); + + /* DEBUG */ + if ( common::GlobalData::verbose ) { + std::cerr << "RTE:" << std::endl; + + for ( const auto & symbol : indexedRTE.getAlphabet ( ) ) + std::cerr << "\t" << symbol << std::endl; + + std::cerr << std::endl; + + std::cerr << "First(RTE):" << std::endl; + + for ( const auto & symbol : firstSet ) + std::cerr << "\t" << symbol << std::endl; + + std::cerr << std::endl; + + for ( const auto & kv : followSet ) { + std::cerr << "Follow(RTE, " << kv.first << "):" << std::endl; + + if ( kv.second.empty ( ) ) + std::cerr << "\t" << "{}" << std::endl; + + for ( const auto & follow : kv.second ) { + for ( const auto & symbol : follow ) + std::cerr << "\t" << symbol << std::endl; + + std::cerr << std::endl; + } + + std::cerr << std::endl; + } + } + /* DEBUG END */ + + for ( const common::ranked_symbol < > & symb : indexedRTE.getAlphabet ( ) ) { + if ( symb.getRank ( ) == primitive::Unsigned ( 0 ) ) + automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), { }, q, { DefaultSymbolType ( alphabet::RankedSymbol < > ( symb ) ) } ); + else + for ( const ext::vector < common::ranked_symbol < > > & follow : followSet[symb] ) { + ext::vector < DefaultSymbolType > fstring = phi ( follow ); + std::reverse ( fstring.begin ( ), fstring.end ( ) ); + automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), fstring, q, { DefaultSymbolType ( alphabet::RankedSymbol < > ( symb ) ) } ); + } + + } + + for ( const common::ranked_symbol < > & symb : firstSet ) { + ext::vector < DefaultSymbolType > pop; + pop.push_back ( DefaultSymbolType ( alphabet::RankedSymbol < > ( symb ) ) ); + pop.push_back ( alphabet::BottomOfTheStackSymbol::instance < DefaultSymbolType > ( ) ); + automaton.addTransition ( q, alphabet::EndSymbol::instance < DefaultSymbolType > ( ), pop, f, { } ); + } + + return automaton; +} + +auto ToPostfixPushdownAutomatonGlushkovNaiveFormalRTE = registration::OverloadRegister < ToPostfixPushdownAutomatonGlushkovNaive, automaton::NPDA < >, rte::FormalRTE < > > ( ToPostfixPushdownAutomatonGlushkovNaive::convert ); + +} /* namespace convert */ + +} /* namespace rte */ diff --git a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.h b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.h new file mode 100644 index 0000000000000000000000000000000000000000..327ba27f7aead52e41de42c001d0515255fb441a --- /dev/null +++ b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.h @@ -0,0 +1,44 @@ +/* + * ToPostfixPushdownAutomatonGlushkovNaive.h + * + * Created on: 11. 4. 2016 + * Author: Tomas Pecka + */ + +#ifndef TO_POSTFIX_PUSHDOWN_AUTOMATON_GLUSHKOV_NAIVE_H_ +#define TO_POSTFIX_PUSHDOWN_AUTOMATON_GLUSHKOV_NAIVE_H_ + +#include <core/multipleDispatch.hpp> +#include <map> + +#include <automaton/FSM/NFA.h> +#include <automaton/PDA/NPDA.h> +#include <rte/RTE.h> +#include <rte/formal/FormalRTE.h> + +// #include <rte/unbounded/UnboundedRegExp.h> + +namespace rte { + +namespace convert { + +/** + * Converts regular tree expression to real-time height-deterministic pda + * Source: Master Thesis, Pecka Tomas, CTU FIT, 2016, chapter 4.2 + */ +class ToPostfixPushdownAutomatonGlushkovNaive : public alib::SingleDispatch < ToPostfixPushdownAutomatonGlushkovNaive, automaton::NPDA < >, const rte::RTEBase & > { +public: + /** + * Performs conversion. + * @param re Original regular tree expression. + * @return rhNPDA equivalent to original regular expression. + */ + static automaton::NPDA < > convert ( const rte::RTE & rte ); + static automaton::NPDA < > convert ( const rte::FormalRTE < > & rte ); +}; + +} /* namespace convert */ + +} /* namespace rte */ + +#endif /* TO_POSTFIX_PUSHDOWN_AUTOMATON_GLUSHKOV_NAIVE_H_ */ diff --git a/alib2algo/src/rte/glushkov/GlushkovFollow.h b/alib2algo/src/rte/glushkov/GlushkovFollow.h index aece7eddd064cbda6be01a9cf446308fd1420a71..c98b31fa56d91b5cb8ba814890ca385bb4fe49d8 100644 --- a/alib2algo/src/rte/glushkov/GlushkovFollow.h +++ b/alib2algo/src/rte/glushkov/GlushkovFollow.h @@ -1,16 +1,16 @@ /* * GlushkovFollow.h * - * Created on: 14. 4. 2016 + * Created on: 26. 7. 2017 * Author: Tomas Pecka */ #ifndef RTE_GLUSHKOV_FOLLOW_H_ #define RTE_GLUSHKOV_FOLLOW_H_ -#include <vector> #include <map> #include <set> +#include <vector> #include <rte/formal/FormalRTE.h> #include <rte/formal/FormalRTEElements.h> @@ -25,137 +25,88 @@ namespace rte { class GlushkovFollow { + + // -------------------------------------------------------------------- +public: + + template < class SymbolType, class RankType > + using TFollowTuple = ext::vector < ext::set < common::ranked_symbol < SymbolType, RankType > > >; + + template < class SymbolType, class RankType > + using TAlphabet = ext::set < common::ranked_symbol < SymbolType, RankType > >; + +private: template < class SymbolType, class RankType > - static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > replaceConstants ( const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, const ext::vector < common::ranked_symbol < SymbolType, RankType > > & follow, const ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subMap2 ); + using TSubstMap = ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > >; + + // -------------------------------------------------------------------- + template < class SymbolType, class RankType > - static void preprocessSubMap ( const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subMap ); - template < class T > - static ext::vector < ext::vector < T > > cartesian ( const ext::vector < ext::vector < T > > & input ); - template < class T > - static void cartesian_rec ( const ext::vector < ext::vector < T > > & input, ext::vector < ext::vector < T > > & ret, ext::vector < T > & current, size_t depth ); + static ext::set < TFollowTuple < SymbolType, RankType > > replaceConstants ( const TAlphabet < SymbolType, RankType > & alphabetK, const ext::vector < common::ranked_symbol < SymbolType, RankType > > & follow, const TSubstMap < SymbolType, RankType > & subMap2 ); + public: /** * @param re rte to probe * @param symbol FormalRTESymbol for which we need the follow(), i.e., Follow(RTE, symbol) + * @param substMapTree Tree substitution mapping from, rte::GlushkovSubstitutionMap::substMap * @return all symbols that can follow specific symbol in word */ template < class SymbolType, class RankType > - static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > follow ( const rte::FormalRTE < SymbolType, RankType > & re, const common::ranked_symbol < SymbolType, RankType > & symbol ); + static ext::set < TFollowTuple < SymbolType, RankType > > follow ( const rte::FormalRTE < SymbolType, RankType > & rte, const common::ranked_symbol < SymbolType, RankType > & symbol, const ext::map < const rte::FormalRTEElement < SymbolType, RankType > *, TSubstMap < SymbolType, RankType > > & substMapTree ); template < class SymbolType, class RankType > class Formal { public: - static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTEElement < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subM ); - static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTEAlternation < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subM ); - static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTESubstitution < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subM ); - static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTEIteration < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subM ); - static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTESymbolAlphabet < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subM ); - static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTESymbolSubst < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subM ); - static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTEEmpty < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subM ); + static ext::set < TFollowTuple < SymbolType, RankType > > visit ( const rte::FormalRTEElement < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ); + static ext::set < TFollowTuple < SymbolType, RankType > > visit ( const rte::FormalRTEAlternation < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ); + static ext::set < TFollowTuple < SymbolType, RankType > > visit ( const rte::FormalRTESubstitution < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ); + static ext::set < TFollowTuple < SymbolType, RankType > > visit ( const rte::FormalRTEIteration < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ); + static ext::set < TFollowTuple < SymbolType, RankType > > visit ( const rte::FormalRTESymbolAlphabet < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ); + static ext::set < TFollowTuple < SymbolType, RankType > > visit ( const rte::FormalRTESymbolSubst < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ); + static ext::set < TFollowTuple < SymbolType, RankType > > visit ( const rte::FormalRTEEmpty < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ); + }; + }; template < class SymbolType, class RankType > -ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollow::follow ( const rte::FormalRTE < SymbolType, RankType > & rte, const common::ranked_symbol < SymbolType, RankType > & symbol ) { - ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > subMap; - - /* Init substitution map, ie \forall a \in K: sub[a] = \emptyset */ - for ( const common::ranked_symbol < SymbolType, RankType > & ssymb : rte.getSubstitutionAlphabet ( ) ) - subMap.insert ( std::make_pair ( ssymb, ext::set < common::ranked_symbol < SymbolType, RankType > > { } ) ); - - /* recursively compute follow */ - return rte.getRTE ( ).getStructure ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbol, rte.getSubstitutionAlphabet ( ), subMap ); +ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > GlushkovFollow::follow ( const rte::FormalRTE < SymbolType, RankType > & rte, const common::ranked_symbol < SymbolType, RankType > & symbol, const ext::map < const rte::FormalRTEElement < SymbolType, RankType > *, TSubstMap < SymbolType, RankType > > & substMapTree ) { + return rte.getRTE ( ).getStructure ( ).template accept < ext::set < TFollowTuple < SymbolType, RankType > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbol, rte.getSubstitutionAlphabet ( ), substMapTree ); } // ----------------------------------------------------------------------------- -template < class T > -void GlushkovFollow::cartesian_rec ( const ext::vector < ext::vector < T > > & input, ext::vector < ext::vector < T > > & ret, ext::vector < T > & current, size_t depth ) { - if ( depth == input.size ( ) ) - ret.push_back ( current ); - else - for ( size_t i = 0; i < input[depth].size ( ); i++ ) { - current[depth] = input[depth][i]; - cartesian_rec ( input, ret, current, depth + 1 ); - } - -} - -template < class T > -ext::vector < ext::vector < T > > GlushkovFollow::cartesian ( const ext::vector < ext::vector < T > > & input ) { - ext::vector < ext::vector < T > > ret; - ext::vector < T > current ( input.size ( ), T ( 0, 0 ) ); - - cartesian_rec ( input, ret, current, 0 ); - - return ret; -} - -template < class SymbolType, class RankType > -void GlushkovFollow::preprocessSubMap ( const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subMap ) { - for ( bool change = true; change; change = false ) - for ( std::pair < const common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & kv : subMap ) { - ext::set < common::ranked_symbol < SymbolType, RankType > > & substSet = kv.second; - - for ( auto eIter = substSet.begin ( ); eIter != substSet.end ( ); ) { - if ( alphabetK.count ( * eIter ) == 0 ) - ++ eIter; - else { - auto it = subMap.find ( * eIter ); - size_t oldSize = substSet.size ( ); - substSet.insert ( it->second.begin ( ), it->second.end ( ) ); - change = ( oldSize != substSet.size ( ) ); // something was added - eIter = substSet.erase ( eIter ); - } - } - } -} - template < class SymbolType, class RankType > -ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollow::replaceConstants ( const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, const ext::vector < common::ranked_symbol < SymbolType, RankType > > & follow, const ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subMap2 ) { - ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > subMap ( subMap2 ); - preprocessSubMap ( alphabetK, subMap ); - - ext::vector < ext::vector < common::ranked_symbol < SymbolType, RankType > > > input; +ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > GlushkovFollow::replaceConstants ( const TAlphabet < SymbolType, RankType > & alphabetK, const ext::vector < common::ranked_symbol < SymbolType, RankType > > & children, const TSubstMap < SymbolType, RankType > & subMap ) { + TFollowTuple < SymbolType, RankType > follow; - for ( const common::ranked_symbol < SymbolType, RankType > & e : follow ) { + for ( const common::ranked_symbol < SymbolType, RankType > & e : children ) { if ( alphabetK.count ( e ) > 0 ) - input.push_back ( ext::vector < common::ranked_symbol < SymbolType, RankType > > ( subMap.at ( e ).begin ( ), subMap.at ( e ).end ( ) ) ); + follow.push_back ( ext::set < common::ranked_symbol < SymbolType, RankType > > ( subMap.at ( e ).begin ( ), subMap.at ( e ).end ( ) ) ); else - input.push_back ( ext::vector < common::ranked_symbol < SymbolType, RankType > > { e } ); + follow.push_back ( ext::set < common::ranked_symbol < SymbolType, RankType > > { e } ); } - /* now do the cartesian product on "input" */ - ext::vector < ext::vector < common::ranked_symbol < SymbolType, RankType > > > followSet = cartesian ( input ); - return ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ( followSet.begin ( ), followSet.end ( ) ); + return ext::set < TFollowTuple < SymbolType, RankType > > { follow }; } // ----------------------------------------------------------------------------- template < class SymbolType, class RankType > -ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollow::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEAlternation < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subMap ) { - ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ret, tmp; +ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > GlushkovFollow::Formal < SymbolType, RankType > ::visit ( const rte::FormalRTEAlternation < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ) { + ext::set < TFollowTuple < SymbolType, RankType > > ret, tmp; - tmp = node.getLeftElement ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap ); + tmp = node.getLeftElement ( ).template accept < ext::set < TFollowTuple < SymbolType, RankType > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, substMapTree ); ret.insert ( tmp.begin ( ), tmp.end ( ) ); - tmp = node.getRightElement ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap ); + tmp = node.getRightElement ( ).template accept < ext::set < TFollowTuple < SymbolType, RankType > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, substMapTree ); ret.insert ( tmp.begin ( ), tmp.end ( ) ); return ret; } template < class SymbolType, class RankType > -ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollow::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESubstitution < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subMap ) { - - ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > subMap2 ( subMap ); - auto itMap = subMap2.find ( node.getSubstitutionSymbol ( ).getSymbol ( ) ); - - itMap->second.clear ( ); - - for ( const auto & s : node.getRightElement ( ).template accept < ext::set < common::ranked_symbol < SymbolType, RankType > >, GlushkovFirst::Formal < SymbolType, RankType > > ( ) ) - itMap->second.insert ( s ); - +ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > GlushkovFollow::Formal < SymbolType, RankType > ::visit ( const rte::FormalRTESubstitution < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ) { /* * E sub F * 1. if symbolF in F subtree, then Follow(F, symbolF); @@ -163,26 +114,20 @@ ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > Glus */ if ( node.getLeftElement ( ).template accept < bool, GlushkovPos::Formal < SymbolType, RankType > > ( symbolF ) ) - return node.getLeftElement ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap2 ); + return node.getLeftElement ( ).template accept < ext::set < TFollowTuple < SymbolType, RankType > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, substMapTree ); else - return node.getRightElement ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap ); + return node.getRightElement ( ).template accept < ext::set < TFollowTuple < SymbolType, RankType > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, substMapTree ); } template < class SymbolType, class RankType > -ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollow::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEIteration < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subMap ) { - - ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ret; - - for ( const auto & s : node.getElement ( ).template accept < ext::set < common::ranked_symbol < SymbolType, RankType > >, GlushkovFirst::Formal < SymbolType, RankType > > ( ) ) - subMap[node.getSubstitutionSymbol ( ).getSymbol ( )].insert ( s ); - - return node.getElement ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap ); +ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > GlushkovFollow::Formal < SymbolType, RankType > ::visit ( const rte::FormalRTEIteration < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ) { + return node.getElement ( ).template accept < ext::set < TFollowTuple < SymbolType, RankType > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, substMapTree ); } template < class SymbolType, class RankType > -ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollow::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESymbolAlphabet < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const ext::set < common::ranked_symbol < SymbolType, RankType > > & alphabetK, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & subMap ) { +ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > GlushkovFollow::Formal < SymbolType, RankType > ::visit ( const rte::FormalRTESymbolAlphabet < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const TAlphabet < SymbolType, RankType > & alphabetK, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & substMapTree ) { - ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ret, tmp; + ext::set < TFollowTuple < SymbolType, RankType > > ret, tmp; if ( symbolF == node.getSymbol ( ) ) { ext::vector < common::ranked_symbol < SymbolType, RankType > > children; @@ -190,11 +135,11 @@ ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > Glus for ( const ext::smart_ptr < const rte::FormalRTESymbol < SymbolType, RankType > > & c : node.getElements ( ) ) children.push_back ( c->getSymbol ( ) ); - return replaceConstants ( alphabetK, children, subMap ); + return replaceConstants ( alphabetK, children, substMapTree.at( & node )); } for ( const auto & c : node.getElements ( ) ) { - tmp = c->template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap ); + tmp = c->template accept < ext::set < TFollowTuple < SymbolType, RankType > >, GlushkovFollow::Formal < SymbolType, RankType > > ( symbolF, alphabetK, substMapTree ); ret.insert ( tmp.begin ( ), tmp.end ( ) ); } @@ -202,13 +147,13 @@ ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > Glus } template < class SymbolType, class RankType > -ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollow::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESymbolSubst < SymbolType, RankType > & /* node */, const common::ranked_symbol < SymbolType, RankType > & /* symbolF */, const ext::set < common::ranked_symbol < SymbolType, RankType > > & /* alphabetK */, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & /* subMap */ ) { - return ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ( ); +ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > GlushkovFollow::Formal < SymbolType, RankType > ::visit ( const rte::FormalRTESymbolSubst < SymbolType, RankType > & /* node */, const common::ranked_symbol < SymbolType, RankType > & /* symbol */, const TAlphabet < SymbolType, RankType > & /* alphabetK */, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & /* substMapTree */ ) { + return ext::set < TFollowTuple < SymbolType, RankType > > ( ); } template < class SymbolType, class RankType > -ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollow::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEEmpty < SymbolType, RankType > & /* node */, const common::ranked_symbol < SymbolType, RankType > & /* symbolF */, const ext::set < common::ranked_symbol < SymbolType, RankType > > & /* alphabetK */, ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > > & /* subMap */ ) { - return ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ( ); +ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > GlushkovFollow::Formal < SymbolType, RankType > ::visit ( const rte::FormalRTEEmpty < SymbolType, RankType > & /* node */, const common::ranked_symbol < SymbolType, RankType > & /* symbol */, const TAlphabet < SymbolType, RankType > & /* alphabetK */, const ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > & /* substMapTree */ ) { + return ext::set < TFollowTuple < SymbolType, RankType > > ( ); } } /* namespace rte */ diff --git a/alib2algo/src/rte/glushkov/GlushkovFollowNaive.h b/alib2algo/src/rte/glushkov/GlushkovFollowNaive.h new file mode 100644 index 0000000000000000000000000000000000000000..3e267510b9a4cd101c2d320cdfc31ed15652af16 --- /dev/null +++ b/alib2algo/src/rte/glushkov/GlushkovFollowNaive.h @@ -0,0 +1,236 @@ +/* + * GlushkovFollowNaive.h + * + * Created on: 14. 4. 2016 + * Author: Tomas Pecka + */ + +#ifndef RTE_GLUSHKOV_FOLLOW_NAIVE_H_ +#define RTE_GLUSHKOV_FOLLOW_NAIVE_H_ + +#include <map> +#include <set> +#include <vector> + +#include <rte/formal/FormalRTE.h> +#include <rte/formal/FormalRTEElements.h> + +#include <alphabet/RankedSymbol.h> + +#include "GlushkovFirst.h" +#include "GlushkovPos.h" +#include <iterator> +#include <vector> + +namespace rte { + +class GlushkovFollowNaive { + + // -------------------------------------------------------------------- + + template < class SymbolType, class RankType > + using TSubstMap = ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > >; + + template < class SymbolType, class RankType > + using TAlphabet = ext::set < common::ranked_symbol < SymbolType, RankType > >; + + // -------------------------------------------------------------------- + + template < class SymbolType, class RankType > + static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > replaceConstants ( const TAlphabet < SymbolType, RankType > & alphabetK, const ext::vector < common::ranked_symbol < SymbolType, RankType > > & follow, const TSubstMap < SymbolType, RankType > & subMap2 ); + + template < class SymbolType, class RankType > + static void preprocessSubMap ( const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap ); + + template < class T > + static ext::vector < ext::vector < T > > cartesian ( const ext::vector < ext::vector < T > > & input ); + + template < class T > + static void cartesian_rec ( const ext::vector < ext::vector < T > > & input, ext::vector < ext::vector < T > > & ret, ext::vector < T > & current, size_t depth ); + +public: + /** + * @param re rte to probe + * @param symbol FormalRTESymbol for which we need the follow(), i.e., Follow(RTE, symbol) + * @return all symbols that can follow specific symbol in word + */ + template < class SymbolType, class RankType > + static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > follow ( const rte::FormalRTE < SymbolType, RankType > & re, const common::ranked_symbol < SymbolType, RankType > & symbol ); + + template < class SymbolType, class RankType > + class Formal { + public: + static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTEElement < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM ); + static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTEAlternation < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM ); + static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTESubstitution < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM ); + static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTEIteration < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM ); + static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTESymbolAlphabet < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM ); + static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTESymbolSubst < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM ); + static ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > visit ( const rte::FormalRTEEmpty < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbol, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM ); + }; + +}; + +template < class SymbolType, class RankType > +ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollowNaive::follow ( const rte::FormalRTE < SymbolType, RankType > & rte, const common::ranked_symbol < SymbolType, RankType > & symbol ) { + TSubstMap < SymbolType, RankType > subMap; + + /* Init substitution map, ie \forall a \in K: sub[a] = \emptyset */ + for ( const common::ranked_symbol < SymbolType, RankType > & ssymb : rte.getSubstitutionAlphabet ( ) ) + subMap.insert ( std::make_pair ( ssymb, TAlphabet < SymbolType, RankType > { } ) ); + + /* recursively compute follow */ + return rte.getRTE ( ).getStructure ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollowNaive::Formal < SymbolType, RankType > > ( symbol, rte.getSubstitutionAlphabet ( ), subMap ); +} + +// ----------------------------------------------------------------------------- + +template < class T > +void GlushkovFollowNaive::cartesian_rec ( const ext::vector < ext::vector < T > > & input, ext::vector < ext::vector < T > > & ret, ext::vector < T > & current, size_t depth ) { + if ( depth == input.size ( ) ) + ret.push_back ( current ); + else + for ( size_t i = 0; i < input[depth].size ( ); i++ ) { + current[depth] = input[depth][i]; + cartesian_rec ( input, ret, current, depth + 1 ); + } +} + +template < class T > +ext::vector < ext::vector < T > > GlushkovFollowNaive::cartesian ( const ext::vector < ext::vector < T > > & input ) { + ext::vector < ext::vector < T > > ret; + ext::vector < T > current ( input.size ( ), T ( 0, 0 ) ); + + cartesian_rec ( input, ret, current, 0 ); + + return ret; +} + +/** + * Preprocessing: + * - Let k1, k2 be elements of alphabet K. + * - If k1 is an element of substMap[k2], then copy content of substMap[k1] into substMap[k2] + */ +template < class SymbolType, class RankType > +void GlushkovFollowNaive::preprocessSubMap ( const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap ) { + for ( bool change = true; change; change = false ) + for ( std::pair < const common::ranked_symbol < SymbolType, RankType >, TAlphabet < SymbolType, RankType > > & kv : subMap ) { + TAlphabet < SymbolType, RankType > & substSet = kv.second; + + for ( auto eIter = substSet.begin ( ); eIter != substSet.end ( ); ) { + if ( alphabetK.count ( * eIter ) == 0 ) { + ++eIter; + } else { + auto it = subMap.find ( * eIter ); + size_t oldSize = substSet.size ( ); + substSet.insert ( it->second.begin ( ), it->second.end ( ) ); + change = ( oldSize != substSet.size ( ) ); // something was added + eIter = substSet.erase ( eIter ); + } + } + } +} + +template < class SymbolType, class RankType > +ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollowNaive::replaceConstants ( const TAlphabet < SymbolType, RankType > & alphabetK, const ext::vector < common::ranked_symbol < SymbolType, RankType > > & follow, const TSubstMap < SymbolType, RankType > & subMap2 ) { + TSubstMap < SymbolType, RankType > subMap ( subMap2 ); + preprocessSubMap ( alphabetK, subMap ); + + ext::vector < ext::vector < common::ranked_symbol < SymbolType, RankType > > > input; + + for ( const common::ranked_symbol < SymbolType, RankType > & e : follow ) { + if ( alphabetK.count ( e ) > 0 ) + input.push_back ( ext::vector < common::ranked_symbol < SymbolType, RankType > > ( subMap.at ( e ).begin ( ), subMap.at ( e ).end ( ) ) ); + else + input.push_back ( ext::vector < common::ranked_symbol < SymbolType, RankType > > { e } ); + } + + /* now do the cartesian product on "input" */ + ext::vector < ext::vector < common::ranked_symbol < SymbolType, RankType > > > followSet = cartesian ( input ); + return ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ( followSet.begin ( ), followSet.end ( ) ); +} + +// ----------------------------------------------------------------------------- + +template < class SymbolType, class RankType > +ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollowNaive::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEAlternation < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap ) { + ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ret, tmp; + + tmp = node.getLeftElement ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollowNaive::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap ); + ret.insert ( tmp.begin ( ), tmp.end ( ) ); + + tmp = node.getRightElement ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollowNaive::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap ); + ret.insert ( tmp.begin ( ), tmp.end ( ) ); + + return ret; +} + +template < class SymbolType, class RankType > +ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollowNaive::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESubstitution < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap ) { + + TSubstMap < SymbolType, RankType > subMap2 ( subMap ); + auto itMap = subMap2.find ( node.getSubstitutionSymbol ( ).getSymbol ( ) ); + + itMap->second.clear ( ); + + for ( const auto & s : node.getRightElement ( ).template accept < TAlphabet < SymbolType, RankType >, GlushkovFirst::Formal < SymbolType, RankType > > ( ) ) + itMap->second.insert ( s ); + + /* + * E sub F + * 1. if symbolF in F subtree, then Follow(F, symbolF); + * 2. if symbolF in E subtree, then Follow(E, symbolF); + */ + + if ( node.getLeftElement ( ).template accept < bool, GlushkovPos::Formal < SymbolType, RankType > > ( symbolF ) ) + return node.getLeftElement ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollowNaive::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap2 ); + else + return node.getRightElement ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollowNaive::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap ); +} + +template < class SymbolType, class RankType > +ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollowNaive::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEIteration < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap ) { + + ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ret; + + for ( const auto & s : node.getElement ( ).template accept < TAlphabet < SymbolType, RankType >, GlushkovFirst::Formal < SymbolType, RankType > > ( ) ) + subMap[node.getSubstitutionSymbol ( ).getSymbol ( )].insert ( s ); + + return node.getElement ( ).template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollowNaive::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap ); +} + +template < class SymbolType, class RankType > +ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollowNaive::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESymbolAlphabet < SymbolType, RankType > & node, const common::ranked_symbol < SymbolType, RankType > & symbolF, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap ) { + + ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ret, tmp; + + if ( symbolF == node.getSymbol ( ) ) { + ext::vector < common::ranked_symbol < SymbolType, RankType > > children; + + for ( const ext::smart_ptr < const rte::FormalRTESymbol < SymbolType, RankType > > & c : node.getElements ( ) ) + children.push_back ( c->getSymbol ( ) ); + + return replaceConstants ( alphabetK, children, subMap ); + } + + for ( const auto & c : node.getElements ( ) ) { + tmp = c->template accept < ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > >, GlushkovFollowNaive::Formal < SymbolType, RankType > > ( symbolF, alphabetK, subMap ); + ret.insert ( tmp.begin ( ), tmp.end ( ) ); + } + + return ret; +} + +template < class SymbolType, class RankType > +ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollowNaive::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESymbolSubst < SymbolType, RankType > & /* node */, const common::ranked_symbol < SymbolType, RankType > & /* symbolF */, const TAlphabet < SymbolType, RankType > & /* alphabetK */, TSubstMap < SymbolType, RankType > & /* subMap */ ) { + return ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ( ); +} + +template < class SymbolType, class RankType > +ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > GlushkovFollowNaive::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEEmpty < SymbolType, RankType > & /* node */, const common::ranked_symbol < SymbolType, RankType > & /* symbolF */, const TAlphabet < SymbolType, RankType > & /* alphabetK */, TSubstMap < SymbolType, RankType > & /* subMap */ ) { + return ext::set < ext::vector < common::ranked_symbol < SymbolType, RankType > > > ( ); +} + +} /* namespace rte */ + +#endif /* RTE_GLUSHKOV_FOLLOW_NAIVE_H_ */ diff --git a/alib2algo/src/rte/glushkov/GlushkovIndexate.cpp b/alib2algo/src/rte/glushkov/GlushkovIndexate.cpp index 0bb192b59a5141840a2856f938c41433039bb704..4e07a7557619bdb6f045db257c3d478c58313af3 100644 --- a/alib2algo/src/rte/glushkov/GlushkovIndexate.cpp +++ b/alib2algo/src/rte/glushkov/GlushkovIndexate.cpp @@ -13,9 +13,10 @@ namespace rte { +// ---------------------------------------------------------------------------- + common::ranked_symbol < > GlushkovIndexate::getSymbolFromGlushkovPair ( const common::ranked_symbol < > & symbol ) { const ext::pair < DefaultSymbolType, DefaultSymbolType > & sps = ( ( const DefaultSymbolsPairType & ) symbol.getSymbol ( ).getData ( ) ); - return common::ranked_symbol < > ( sps.first, symbol.getRank ( ) ); } @@ -25,6 +26,8 @@ FormalRTE < > GlushkovIndexate::index ( const rte::FormalRTE < > & rte ) { return FormalRTE < > ( FormalRTEStructure < DefaultSymbolType, primitive::Unsigned > ( rte.getRTE ( ).getStructure ( ).accept < ext::rvalue_ref < rte::FormalRTEElement < DefaultSymbolType, primitive::Unsigned > >, GlushkovIndexate::Formal > ( i ) ) ); } +// ---------------------------------------------------------------------------- + ext::rvalue_ref < FormalRTEElement < DefaultSymbolType, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTESymbolAlphabet < DefaultSymbolType, primitive::Unsigned > & node, int & i ) { DefaultSymbolsPairType sps = DefaultSymbolsPairType ( ext::make_pair ( DefaultSymbolType ( node.getSymbol ( ).getSymbol ( ) ), DefaultSymbolType ( i++ ) ) ); FormalRTESymbolAlphabet < DefaultSymbolType, primitive::Unsigned > * ns = new FormalRTESymbolAlphabet < DefaultSymbolType, primitive::Unsigned > ( common::ranked_symbol < > ( DefaultSymbolType ( sps ), node.getSymbol ( ).getRank ( ) ) ); @@ -62,4 +65,6 @@ ext::rvalue_ref < FormalRTEElement < DefaultSymbolType, primitive::Unsigned > > return ext::rvalue_ref < FormalRTEElement < DefaultSymbolType, primitive::Unsigned > > ( new FormalRTEEmpty < DefaultSymbolType, primitive::Unsigned > ( ) ); } +// ---------------------------------------------------------------------------- + } /* namespace rte */ diff --git a/alib2algo/src/rte/glushkov/GlushkovIndexate.h b/alib2algo/src/rte/glushkov/GlushkovIndexate.h index 9a291afc88d091df88af2e3c0211bc64b1fac2b7..c96ead8a23987cd9dceb0ca79e2851d525b299c1 100644 --- a/alib2algo/src/rte/glushkov/GlushkovIndexate.h +++ b/alib2algo/src/rte/glushkov/GlushkovIndexate.h @@ -38,7 +38,6 @@ public: static ext::rvalue_ref < FormalRTEElement < DefaultSymbolType, primitive::Unsigned > > visit ( const rte::FormalRTESymbolSubst < DefaultSymbolType, primitive::Unsigned > & node, int & i ); static ext::rvalue_ref < FormalRTEElement < DefaultSymbolType, primitive::Unsigned > > visit ( const rte::FormalRTEEmpty < DefaultSymbolType, primitive::Unsigned > & node, int & i ); }; - }; } /* namespace rte */ diff --git a/alib2algo/src/rte/glushkov/GlushkovSubstitutionMap.h b/alib2algo/src/rte/glushkov/GlushkovSubstitutionMap.h new file mode 100644 index 0000000000000000000000000000000000000000..dfbabf1b0de8ea1f6b53451af3dece7ba5694f3b --- /dev/null +++ b/alib2algo/src/rte/glushkov/GlushkovSubstitutionMap.h @@ -0,0 +1,176 @@ +/* + * GlushkovSubstitutionMap.h + * + * Created on: 26. 7. 2017 + * Author: Tomas Pecka + */ + +#ifndef RTE_GLUSHKOV_SUBSTITUTION_MAP_H_ +#define RTE_GLUSHKOV_SUBSTITUTION_MAP_H_ + +#include <map> +#include <set> +#include <vector> + +#include <rte/formal/FormalRTE.h> +#include <rte/formal/FormalRTEElements.h> + +#include <alphabet/RankedSymbol.h> + +#include "GlushkovFirst.h" +#include "GlushkovPos.h" +#include <iterator> +#include <vector> + +namespace rte { + +class GlushkovSubstitutionMap { + +private: + template < class SymbolType, class RankType > + using TSubstMap = ext::map < common::ranked_symbol < SymbolType, RankType >, ext::set < common::ranked_symbol < SymbolType, RankType > > >; + + template < class SymbolType, class RankType > + using TSubstMapTree = ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > >; + + template < class SymbolType, class RankType > + using TAlphabet = ext::set < common::ranked_symbol < SymbolType, RankType > >; + + // -------------------------------------------------------------------- + + template < class SymbolType, class RankType > + static void preprocessSubMap ( TSubstMap < SymbolType, RankType > & subMap, const TAlphabet < SymbolType, RankType > & alphabetK ); + + template < class SymbolType, class RankType > + static void subst_symbol_replaces ( ext::map < const rte::FormalRTEElement < SymbolType, RankType > *, TSubstMap < SymbolType, RankType > > & substMapTree, const TAlphabet < SymbolType, RankType > & alphabetK ); + +public: + /** + * @param re rte to probe + * @return subst map for all RTE syntax tree elements + */ + template < class SymbolType, class RankType > + static ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > substMap ( const rte::FormalRTE < SymbolType, RankType > & re ); + + template < class SymbolType, class RankType > + class Formal { + public: + static void visit ( const rte::FormalRTEElement < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree ); + static void visit ( const rte::FormalRTEAlternation < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree ); + static void visit ( const rte::FormalRTESubstitution < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree ); + static void visit ( const rte::FormalRTEIteration < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree ); + static void visit ( const rte::FormalRTESymbolAlphabet < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree ); + static void visit ( const rte::FormalRTESymbolSubst < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree ); + static void visit ( const rte::FormalRTEEmpty < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree ); + }; + +}; + +// ----------------------------------------------------------------------------- + +template < class SymbolType, class RankType > +void GlushkovSubstitutionMap::subst_symbol_replaces ( ext::map < const rte::FormalRTEElement < SymbolType, RankType > *, TSubstMap < SymbolType, RankType > > & substMapTree, const TAlphabet < SymbolType, RankType > & alphabetK ) { + for ( std::pair < const rte::FormalRTEElement < SymbolType, RankType > * const, TSubstMap < SymbolType, RankType > > & kv : substMapTree ) { + preprocessSubMap ( kv.second, alphabetK ); + } +} + +/** + * Preprocessing: + * - Let k1, k2 be elements of alphabet K. + * - If k1 is an element of substMap[k2], then copy content of substMap[k1] into substMap[k2] + */ +template < class SymbolType, class RankType > +void GlushkovSubstitutionMap::preprocessSubMap ( TSubstMap < SymbolType, RankType > & subMap, const TAlphabet < SymbolType, RankType > & alphabetK ) { + for ( bool change = true; change; change = false ) + for ( std::pair < const common::ranked_symbol < SymbolType, RankType >, TAlphabet < SymbolType, RankType > > & kv : subMap ) { + TAlphabet < SymbolType, RankType > & substSet = kv.second; + + for ( auto eIter = substSet.begin ( ); eIter != substSet.end ( ); ) { + if ( alphabetK.count ( * eIter ) == 0 ) { + ++eIter; + } else { + auto it = subMap.find ( * eIter ); + size_t oldSize = substSet.size ( ); + substSet.insert ( it->second.begin ( ), it->second.end ( ) ); + change = ( oldSize != substSet.size ( ) ); // something was added + eIter = substSet.erase ( eIter ); + } + } + } +} + +// ----------------------------------------------------------------------------- + +template < class SymbolType, class RankType > +ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, GlushkovSubstitutionMap::TSubstMap < SymbolType, RankType > > GlushkovSubstitutionMap::substMap ( const rte::FormalRTE < SymbolType, RankType > & rte ) { + TSubstMap < SymbolType, RankType > subMap; + ext::map < const rte::FormalRTEElement < SymbolType, RankType >*, TSubstMap < SymbolType, RankType > > subMapTree; + + /* Init substitution map, ie \forall a \in K: sub[a] = \emptyset */ + for ( const common::ranked_symbol < SymbolType, RankType > & ssymb : rte.getSubstitutionAlphabet ( ) ) + subMap.insert ( std::make_pair ( ssymb, TAlphabet < SymbolType, RankType > { } ) ); + + /* recursively compute substMap */ + rte.getRTE ( ).getStructure ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree ); + subst_symbol_replaces ( subMapTree, rte.getSubstitutionAlphabet ( ) ); + return subMapTree; +} + +// ----------------------------------------------------------------------------- + +template < class SymbolType, class RankType > +void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEAlternation < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) { + subMapTree.insert ( std::make_pair ( & node, subMap ) ); + node.getLeftElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree ); + node.getRightElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree ); +} + +template < class SymbolType, class RankType > +void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESubstitution < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) { + subMapTree.insert ( std::make_pair ( & node, subMap ) ); + + // re-init left map + TSubstMap < SymbolType, RankType > subMapLeft ( subMap ); + auto itMap = subMapLeft.find ( node.getSubstitutionSymbol ( ).getSymbol ( ) ); + itMap->second.clear ( ); + + for ( const auto & s : node.getRightElement ( ).template accept < ext::set < common::ranked_symbol < > >, GlushkovFirst::Formal < SymbolType, RankType > > ( ) ) + itMap->second.insert ( s ); + + node.getLeftElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMapLeft, subMapTree ); + node.getRightElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree ); +} + +template < class SymbolType, class RankType > +void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEIteration < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) { + subMapTree.insert ( std::make_pair ( & node, subMap ) ); + + for ( const auto & s : node.getElement ( ).template accept < TAlphabet < SymbolType, RankType >, GlushkovFirst::Formal < SymbolType, RankType > > ( ) ) + subMap[node.getSubstitutionSymbol ( ).getSymbol ( )].insert ( s ); + + node.getElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree ); +} + +template < class SymbolType, class RankType > +void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESymbolAlphabet < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) { + subMapTree.insert ( std::make_pair ( & node, subMap ) ); + + for ( const auto & c : node.getElements ( ) ) { + c -> template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree ); + } +} + +template < class SymbolType, class RankType > +void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESymbolSubst < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) { + subMapTree.insert ( std::make_pair ( & node, subMap ) ); +} + +template < class SymbolType, class RankType > +void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEEmpty < SymbolType, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) { + subMapTree.insert ( std::make_pair ( & node, subMap ) ); +} + +} /* namespace rte */ + +#endif /* RTE_GLUSHKOV_SUBSTITUTION_MAP_H_ */ diff --git a/tests.glushkovrte_naive.sh b/tests.glushkovrte_naive.sh new file mode 100755 index 0000000000000000000000000000000000000000..3689c7fe0b0b73a3fc68184c942e47141d2e2919 --- /dev/null +++ b/tests.glushkovrte_naive.sh @@ -0,0 +1,179 @@ +#!/usr/bin/env bash + +# $1 test dir suffix (debug / release) + +# SETTINGS +TESTCASE_ITERATIONS=30 +TESTCASE_TIMEOUT=10 +LOGFILE="log_tests.txt" + +EXECUTABLES="arun2 adeterminize2 aconversions2" +TESTS_DIR="`pwd`/examples2/rte" + +RES_GOOD= +RES_FAIL= +RES_TIME= +RES_SEGV= +RES_UNKN= + + +# ---------------------------- + +for FILE in $EXECUTABLES; do + if [ ! -f bin-$1/$FILE ]; then + echo "Executable $FILE is required for testing. Make sure it is in bin-$1 folder." + exit 1 + fi +done + +cd bin-$1/ +rm -f $LOGFILE + +JOBS=$2 +if [ -z "$JOBS" ]; then + JOBS=1 +fi + +# ---------------------------- + +# $1 = str +# $2 = automaton +# $3 = rte +function log { + echo "----------------------------------------------------------" >> $LOGFILE + echo "str: " >> $LOGFILE + cat $1 >> $LOGFILE + echo "aut: " >> $LOGFILE + cat $2 >> $LOGFILE + echo "rte: " >> $LOGFILE + cat $3 >> $LOGFILE +} + +# $1 = genfile +# $2 = automaton +# $3 = rte +function runAcceptTest { + PATTERN=$(mktemp) + OUT=`timeout $TESTCASE_TIMEOUT bash -c "./arun2 -i <($1 | tee $PATTERN | ./aconvert2 --string_from_string) -a $2"` + RET=$? + if [ $RET == 0 ]; then # ok + echo $OUT | grep -q "<Bool>true</Bool>" + if [ $? != 0 ]; then + RET=1 + fi + fi + + if [ $RET != 0 ]; then # fail + log "$PATTERN" "$2" "$3" + echo -n "" + fi + + if [ $RET == 124 ]; then # timeout + registerResult 2 + return 2 + elif [ $RET -ge 124 ]; then #segv + registerResult 3 + return 3 + elif [ $RET != 0 ]; then # fail + registerResult 1 + return 1 + else + registerResult 0 + return 0 + fi +} + +function registerResult { + case $1 in + 0) + echo -n "." + echo -n "+1" >> $RES_GOOD + ;; + 1) + echo -n "x" + echo -n "+1" >> $RES_FAIL + ;; + 2) + echo -n "T" + echo -n "+1" >> $RES_TIME + ;; + 3) + echo -n "E" + echo -n "+1" >> $RES_SEGV + ;; + *) + echo -n "?" + echo -n "+1" >> $RES_UNKN + ;; + esac +} + +function initResults { + RES_GOOD=$(mktemp) + echo -n "0" > $RES_GOOD + RES_FAIL=$(mktemp) + echo -n "0" > $RES_FAIL + RES_TIME=$(mktemp) + echo -n "0" > $RES_TIME + RES_SEGV=$(mktemp) + echo -n "0" > $RES_SEGV + RES_UNKN=$(mktemp) + echo -n "0" > $RES_UNKN +} + +function clearResults { + rm $RES_GOOD + rm $RES_FAIL + rm $RES_TIME + rm $RES_SEGV + rm $RES_UNKN +} + +function outputResults { + echo "" >> $RES_GOOD + echo "" >> $RES_FAIL + echo "" >> $RES_TIME + echo "" >> $RES_SEGV + echo "" >> $RES_UNKN + + # summary + echo -ne "\n\t" + echo "RES: GOOD:" $(bc < $RES_GOOD) ", FAIL:" $(bc < $RES_FAIL) ", TIME:" $(bc < $RES_TIME) ", SEGV:" $(bc < $RES_SEGV), "UNKN:" $(bc < $RES_UNKN) + echo "" +} + +function run { + initResults + + # predefined tests first + TESTS="rte1 rte2 rte3 rte4 rte5 rte6 rte7 rte8 rte9" + for RTE in $TESTS; do + echo -n "$RTE: " + + RTE_FILE="$TESTS_DIR/$RTE.xml" + GEN_FILE="$TESTS_DIR/gen_$RTE.py" + AUTOMATON_FILE=$(mktemp) + + #echo $RTE_FILE + #echo $GEN_FILE + #echo $AUTOMATON_FILE + + ./aconversions2 -t pda -a glushkovrtenaive -i $RTE_FILE 2>/dev/null | ./adeterminize2 > $AUTOMATON_FILE + + for i in `seq 1 $TESTCASE_ITERATIONS`; do + runAcceptTest $GEN_FILE $AUTOMATON_FILE $RTE_FILE + done + + echo "" + done + + # random tests + # TODO + + wait + + outputResults + clearResults +} + +run