From a7a2ef1f8cc231b07d8d4cde9f3957b7abe644a5 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Fri, 4 Nov 2016 14:23:34 +0100 Subject: [PATCH] use visitors in indexation of rtes for glushkov --- .../ToPostfixPushdownAutomatonGlushkov.cpp | 7 +- .../src/rte/glushkov/GlushkovIndexate.cpp | 65 +++++++++++++++++++ alib2algo/src/rte/glushkov/GlushkovIndexate.h | 46 +++++++++++++ .../src/rte/glushkov/GlushkovTraversal.cpp | 49 -------------- .../src/rte/glushkov/GlushkovTraversal.h | 14 ---- 5 files changed, 115 insertions(+), 66 deletions(-) create mode 100644 alib2algo/src/rte/glushkov/GlushkovIndexate.cpp create mode 100644 alib2algo/src/rte/glushkov/GlushkovIndexate.h diff --git a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp index 3497420874..9fb502a18a 100644 --- a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp +++ b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp @@ -17,6 +17,7 @@ #include "global/GlobalData.h" #include "../glushkov/GlushkovTraversal.h" +#include "../glushkov/GlushkovIndexate.h" #include <iterator> @@ -41,7 +42,7 @@ bool isSubstSymbolPresent ( const std::set < std::ranked_symbol < > > & containe automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::FormalRTE < > & rte ) { // step 1; index RTE - rte::FormalRTE < > indexedRTE = rte::GlushkovTraversal::index ( rte ); + rte::FormalRTE < > indexedRTE = rte::GlushkovIndexate::index ( rte ); // step 2; compute: // - first set @@ -117,12 +118,12 @@ automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::For for ( const std::ranked_symbol < > & symb : indexedRTE.getAlphabet ( ) ) { if ( symb.getRank ( ) == primitive::Unsigned ( 0 ) ) - automaton.addTransition ( q, rte::GlushkovTraversal::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), { }, q, { alphabet::Symbol ( alphabet::RankedSymbol < > ( symb ) ) } ); + automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), { }, q, { alphabet::Symbol ( alphabet::RankedSymbol < > ( symb ) ) } ); else for ( const std::vector < std::ranked_symbol < > > & f : followSet[symb] ) { std::vector < alphabet::Symbol > fstring = phi ( f ); std::reverse ( fstring.begin ( ), fstring.end ( ) ); - automaton.addTransition ( q, rte::GlushkovTraversal::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), fstring, q, { alphabet::Symbol ( alphabet::RankedSymbol < > ( symb ) ) } ); + automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), fstring, q, { alphabet::Symbol ( alphabet::RankedSymbol < > ( symb ) ) } ); } } diff --git a/alib2algo/src/rte/glushkov/GlushkovIndexate.cpp b/alib2algo/src/rte/glushkov/GlushkovIndexate.cpp new file mode 100644 index 0000000000..f50ce4f3b4 --- /dev/null +++ b/alib2algo/src/rte/glushkov/GlushkovIndexate.cpp @@ -0,0 +1,65 @@ +/* + * GlushkovIndexate.cpp + * + * Created on: 14. 4. 2016 + * Author: Tomas Pecka + */ + +#include "GlushkovIndexate.h" + +#include <rte/formal/FormalRTEElements.h> + +#include <alphabet/SymbolPairSymbol.h> + +namespace rte { + +std::ranked_symbol < > GlushkovIndexate::getSymbolFromGlushkovPair ( const std::ranked_symbol < > & symbol ) { + const std::pair < alphabet::Symbol, alphabet::Symbol > & sps = ( ( const alphabet::SymbolPairSymbol & ) symbol.getSymbol ( ).getData ( ) ).getData ( ); + + return std::ranked_symbol < > ( sps.first, symbol.getRank ( ) ); +} + +FormalRTE < > GlushkovIndexate::index ( const rte::FormalRTE < > & rte ) { + int i = 1; + + return FormalRTE < > ( FormalRTEStructure < alphabet::Symbol, primitive::Unsigned > ( rte.getRTE ( ).getStructure ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal > ( i ) ) ); +} + +std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > & node, int & i ) { + alphabet::SymbolPairSymbol sps = alphabet::SymbolPairSymbol ( std::make_pair ( alphabet::Symbol ( node.getSymbol ( ).getSymbol ( ) ), alphabet::symbolFrom ( i++ ) ) ); + FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > * ns = new FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > ( std::ranked_symbol < > ( alphabet::Symbol ( sps ), node.getSymbol ( ).getRank ( ) ) ); + + for ( const std::smart_ptr < const rte::FormalRTESymbol < alphabet::Symbol, primitive::Unsigned > > & e : node.getElements ( ) ) { + std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > child = e->accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i ); + ns->appendElement ( * static_cast < FormalRTESymbol < alphabet::Symbol, primitive::Unsigned > * > ( child->clone ( ) ) ); // FIXME typecast + } + + return std::rvalue_ref < FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > > ( ns ); +} + +std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > & node, int & ) { + return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > ( node ) ); +} + +std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > & node, int & i ) { + std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > left = node.getLeftElement ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i ); + std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > right = node.getRightElement ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i ); + return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > ( left, right ) ); +} + +std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > & node, int & i ) { + std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > left = node.getLeftElement ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i ); + std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > right = node.getRightElement ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i ); + return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > ( left, right, node.getSubstitutionSymbol ( ) ) ); +} + +std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > & node, int & i ) { + std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > element = node.getElement ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i ); + return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > ( element, node.getSubstitutionSymbol ( ) ) ); +} + +std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > &, int & ) { + return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > ( ) ); +} + +} /* namespace rte */ diff --git a/alib2algo/src/rte/glushkov/GlushkovIndexate.h b/alib2algo/src/rte/glushkov/GlushkovIndexate.h new file mode 100644 index 0000000000..c0043d8edf --- /dev/null +++ b/alib2algo/src/rte/glushkov/GlushkovIndexate.h @@ -0,0 +1,46 @@ +/* + * GlushkovIndexate.h + * + * Created on: 14. 4. 2016 + * Author: Tomas Pecka + */ + +#ifndef RTE_GLUSHKOV_INDEXATE_H_ +#define RTE_GLUSHKOV_INDEXATE_H_ + +#include <rte/formal/FormalRTE.h> +#include <rte/RTEFeatures.h> + +#include <alphabet/RankedSymbol.h> + +namespace rte { + +class GlushkovIndexate { +public: + /** + * @param re rte to index + * @return FormalRTE with indexed elements + */ + static FormalRTE < > index ( const rte::FormalRTE < > & re ); + + /** + * @param symbol Glushkov Pair symbol, i.e., SymbolPair of RankedSymbol < > and integer index + * @return RankedSymbol < > from the pair on input + */ + static std::ranked_symbol < > getSymbolFromGlushkovPair ( const std::ranked_symbol < > & symbol ); + + class Formal { + public: + static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > & node, int & i ); + static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > & node, int & i ); + static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > & node, int & i ); + static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > & node, int & i ); + static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > & node, int & i ); + static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > & node, int & i ); + }; + +}; + +} /* namespace rte */ + +#endif /* RTE_GLUSHKOV_INDEXATE_H_ */ diff --git a/alib2algo/src/rte/glushkov/GlushkovTraversal.cpp b/alib2algo/src/rte/glushkov/GlushkovTraversal.cpp index f931f866fc..9c00b73141 100644 --- a/alib2algo/src/rte/glushkov/GlushkovTraversal.cpp +++ b/alib2algo/src/rte/glushkov/GlushkovTraversal.cpp @@ -12,14 +12,6 @@ namespace rte { -std::ranked_symbol < > GlushkovTraversal::getSymbolFromGlushkovPair ( const std::ranked_symbol < > & symbol ) { - const std::pair < alphabet::Symbol, alphabet::Symbol > sps = ( ( const alphabet::SymbolPairSymbol & ) symbol.getSymbol ( ).getData ( ) ).getData ( ); - - return std::ranked_symbol < > ( sps.first, symbol.getRank ( ) ); -} - -// ----------------------------------------------------------------------------- - bool GlushkovTraversal::pos ( const std::ranked_symbol < > & symbol, const rte::FormalRTE < > & rte ) { return pos ( rte.getRTE ( ).getStructure ( ), symbol ); } @@ -351,45 +343,4 @@ bool GlushkovTraversal::pos ( const rte::FormalRTEEmpty < alphabet::Symbol, prim return false; } -// ---------------------------------------------------------------------------- - -FormalRTE < > GlushkovTraversal::index ( const rte::FormalRTE < > & rte ) { - int i = 1; - - return FormalRTE < > ( FormalRTEStructure < alphabet::Symbol, primitive::Unsigned > ( index ( rte, rte.getRTE ( ).getStructure ( ), i ) ) ); -} - -std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovTraversal::index ( const rte::FormalRTE < > & rte, const rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > & node, int & i ) { - const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > * alternation = dynamic_cast < const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > * > ( & node ); - const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > * substitution = dynamic_cast < const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > * > ( & node ); - const rte::FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > * iteration = dynamic_cast < const rte::FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > * > ( & node ); - const rte::FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > * symbol = dynamic_cast < const rte::FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > * > ( & node ); - const rte::FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > * substSymbol = dynamic_cast < const rte::FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > * > ( & node ); - const rte::FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > * empty = dynamic_cast < const rte::FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > * > ( & node ); - - if ( symbol ) { - alphabet::SymbolPairSymbol sps = alphabet::SymbolPairSymbol ( std::make_pair ( alphabet::Symbol ( symbol->getSymbol ( ).getSymbol ( ) ), alphabet::symbolFrom ( i++ ) ) ); - FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > * ns = new FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > ( std::ranked_symbol < > ( alphabet::Symbol ( sps ), symbol->getSymbol ( ).getRank ( ) ) ); - - for ( const std::smart_ptr < const rte::FormalRTESymbol < alphabet::Symbol, primitive::Unsigned > > & e : symbol->getElements ( ) ) { - std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > child = index ( rte, * e.get ( ), i ); - ns->appendElement ( * static_cast < FormalRTESymbol < alphabet::Symbol, primitive::Unsigned > * > ( child->clone ( ) ) ); // FIXME typecast - } - - return std::rvalue_ref < FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > > ( ns ); - } else if ( substSymbol ) { - return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > ( * substSymbol ) ); - } else if ( alternation ) { - return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > ( index ( rte, alternation->getLeftElement ( ), i ), index ( rte, alternation->getRightElement ( ), i ) ) ); - } else if ( substitution ) { - return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > ( index ( rte, substitution->getLeftElement ( ), i ), index ( rte, substitution->getRightElement ( ), i ), substitution->getSubstitutionSymbol ( ) ) ); - } else if ( iteration ) { - return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > ( index ( rte, iteration->getElement ( ), i ), iteration->getSubstitutionSymbol ( ) ) ); - } else if ( empty ) { - return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > ( ) ); - } else { - throw exception::CommonException ( "GlushkovTraversal::index() - unknown rteElement node" ); - } -} - } /* namespace rte */ diff --git a/alib2algo/src/rte/glushkov/GlushkovTraversal.h b/alib2algo/src/rte/glushkov/GlushkovTraversal.h index 538224eac8..05c642714b 100644 --- a/alib2algo/src/rte/glushkov/GlushkovTraversal.h +++ b/alib2algo/src/rte/glushkov/GlushkovTraversal.h @@ -37,26 +37,12 @@ public: */ static std::set < std::vector < std::ranked_symbol < > > > follow ( const rte::FormalRTE < > & re, const std::ranked_symbol < > & symbol ); - /** - * @param re rte to index - * @return FormalRTE with indexed elements - */ - static FormalRTE < > index ( const rte::FormalRTE < > & re ); - - /** - * @param symbol Glushkov Pair symbol, i.e., SymbolPair of RankedSymbol < > and integer index - * @return RankedSymbol < > from the pair on input - */ - static std::ranked_symbol < > getSymbolFromGlushkovPair ( const std::ranked_symbol < > & symbol ); - private: /** * @return bool true if symbol pointer is in this subtree */ static bool pos ( const std::ranked_symbol < > & symbol, const rte::FormalRTE < > & node ); - static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > index ( const rte::FormalRTE < > & rte, const rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > & node, int & i ); - static std::set < std::ranked_symbol < > > first ( const rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > & node ); static std::set < std::ranked_symbol < > > first ( const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > & node ); static std::set < std::ranked_symbol < > > first ( const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > & node ); -- GitLab