diff --git a/alib2algo/src/rte/glushkov/GlushkovTraversal.cpp b/alib2algo/src/rte/glushkov/GlushkovTraversal.cpp index 991ba88b1db40fa66c2edc832678ee311516ebb8..196657b917d2ece9edf616f84c50b29cba2a4a7f 100644 --- a/alib2algo/src/rte/glushkov/GlushkovTraversal.cpp +++ b/alib2algo/src/rte/glushkov/GlushkovTraversal.cpp @@ -21,11 +21,11 @@ std::ranked_symbol < > GlushkovTraversal::getSymbolFromGlushkovPair ( const std: // ----------------------------------------------------------------------------- bool GlushkovTraversal::pos ( const std::ranked_symbol < > & symbol, const rte::FormalRTE & rte ) { - return pos ( rte.getRTE ( ), symbol ); + return pos ( rte.getRTE ( ).getStructure ( ), symbol ); } std::set < std::ranked_symbol < > > GlushkovTraversal::first ( const rte::FormalRTE & rte ) { - return first ( rte.getRTE ( ) ); + return first ( rte.getRTE ( ).getStructure ( ) ); } std::set < std::vector < std::ranked_symbol < > > > GlushkovTraversal::follow ( const rte::FormalRTE & rte, const std::ranked_symbol < > & symbol ) { @@ -36,7 +36,7 @@ std::set < std::vector < std::ranked_symbol < > > > GlushkovTraversal::follow ( subMap.insert ( std::make_pair ( ssymb, std::set < std::ranked_symbol < > > { } ) ); /* recursively compute follow */ - return follow ( rte.getRTE ( ), symbol, rte.getSubstitutionAlphabet ( ), subMap ); + return follow ( rte.getRTE ( ).getStructure ( ), symbol, rte.getSubstitutionAlphabet ( ), subMap ); } // ----------------------------------------------------------------------------- @@ -356,7 +356,7 @@ bool GlushkovTraversal::pos ( const rte::FormalRTEEmpty & /* node */, const std: FormalRTE GlushkovTraversal::index ( const rte::FormalRTE & rte ) { int i = 1; - return FormalRTE ( index ( rte, rte.getRTE ( ), i ) ); + return FormalRTE ( FormalRTEStructure ( index ( rte, rte.getRTE ( ).getStructure ( ), i ) ) ); } std::rvalue_ref < FormalRTEElement > GlushkovTraversal::index ( const rte::FormalRTE & rte, const rte::FormalRTEElement & node, int & i ) { diff --git a/alib2data/src/rte/formal/FormalRTE.cpp b/alib2data/src/rte/formal/FormalRTE.cpp index 929ac86934988c717d656e18c3ce20cc8f8fc2fd..ae0dd72e97f35d1516eb583f966a4b483bb5dbd2 100644 --- a/alib2data/src/rte/formal/FormalRTE.cpp +++ b/alib2data/src/rte/formal/FormalRTE.cpp @@ -17,26 +17,18 @@ namespace rte { -FormalRTE::FormalRTE ( std::set < std::ranked_symbol < > > alphabetF, std::set < std::ranked_symbol < > > alphabetK, FormalRTEElement && rte ) : std::Components < FormalRTE, std::ranked_symbol < >, std::tuple < GeneralAlphabet, ConstantAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( alphabetF ), std::move ( alphabetK ) ), std::tuple < > ( ) ), rte ( NULL ) { - setRTE ( std::move ( rte ) ); +FormalRTE::FormalRTE ( std::set < std::ranked_symbol < > > alphabetF, std::set < std::ranked_symbol < > > alphabetK, FormalRTEStructure rte ) : std::Components < FormalRTE, std::ranked_symbol < >, std::tuple < GeneralAlphabet, ConstantAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( alphabetF ), std::move ( alphabetK ) ), std::tuple < > ( ) ), rte ( std::move ( rte ) ) { + if ( !this->rte.getStructure ( ).checkAlphabet ( getAlphabet ( ), getSubstitutionAlphabet ( ) ) ) + throw exception::CommonException ( "Input symbols not in the alphabet." ); } -FormalRTE::FormalRTE ( std::pair < std::set < std::ranked_symbol < > >, std::set < std::ranked_symbol < > > > alphabets, FormalRTEElement && rte ) : FormalRTE ( std::move ( alphabets.first ), std::move ( alphabets.second ), std::move ( rte ) ) { +FormalRTE::FormalRTE ( std::pair < std::set < std::ranked_symbol < > >, std::set < std::ranked_symbol < > > > alphabets, FormalRTEStructure rte ) : FormalRTE ( std::move ( alphabets.first ), std::move ( alphabets.second ), std::move ( rte ) ) { } -FormalRTE::FormalRTE ( std::pair < std::set < std::ranked_symbol < > >, std::set < std::ranked_symbol < > > > alphabets, const FormalRTEElement & rte ) : FormalRTE ( std::move ( alphabets.first ), std::move ( alphabets.second ), std::move_copy ( rte ) ) { +FormalRTE::FormalRTE ( ) : FormalRTE ( std::set < std::ranked_symbol < > > ( ), std::set < std::ranked_symbol < > > ( ), FormalRTEStructure ( ) ) { } -FormalRTE::FormalRTE ( ) : FormalRTE ( std::set < std::ranked_symbol < > > ( ), std::set < std::ranked_symbol < > > ( ), FormalRTEEmpty ( ) ) { -} - -FormalRTE::FormalRTE ( std::set < std::ranked_symbol < > > alphabetF, std::set < std::ranked_symbol < > > alphabetK, const FormalRTEElement & rte ) : FormalRTE ( std::move ( alphabetF ), std::move ( alphabetK ), std::move_copy ( rte ) ) { -} - -FormalRTE::FormalRTE ( const FormalRTEElement & rte ) : FormalRTE ( rte.computeMinimalAlphabets ( ), std::move_copy ( rte ) ) { -} - -FormalRTE::FormalRTE ( FormalRTEElement && rte ) : FormalRTE ( rte.computeMinimalAlphabets ( ), rte ) { +FormalRTE::FormalRTE ( FormalRTEStructure rte ) : FormalRTE ( rte.getStructure ( ).computeMinimalAlphabets ( ), rte ) { } RTEBase * FormalRTE::clone ( ) const { @@ -47,27 +39,23 @@ RTEBase * FormalRTE::plunder ( ) && { return new FormalRTE ( std::move ( * this ) ); } -const FormalRTEElement & FormalRTE::getRTE ( ) const { - return * rte; -} - -FormalRTEElement & FormalRTE::getRTE ( ) { - return * rte; +const FormalRTEStructure & FormalRTE::getRTE ( ) const { + return rte; } -void FormalRTE::setRTE ( const FormalRTEElement & rte ) { - setRTE ( std::move_copy ( rte ) ); +FormalRTEStructure & FormalRTE::getRTE ( ) { + return rte; } -void FormalRTE::setRTE ( FormalRTEElement && param ) { - this->rte = std::smart_ptr < FormalRTEElement > ( std::move ( param ).plunder ( ) ); - - if ( !this->rte->checkAlphabet ( getAlphabet ( ), getSubstitutionAlphabet ( ) ) ) +void FormalRTE::setRTE ( FormalRTEStructure param ) { + if ( !param.getStructure ( ).checkAlphabet ( getAlphabet ( ), getSubstitutionAlphabet ( ) ) ) throw exception::CommonException ( "Symbols not matching alphabets." ); + + this->rte = std::move ( param ); } void FormalRTE::operator >>( std::ostream & out ) const { - out << "(FormalRTE " << * ( this->rte ) << ")"; + out << "(FormalRTE " << this->rte << ")"; } int FormalRTE::compare ( const FormalRTE & other ) const { @@ -89,7 +77,7 @@ FormalRTE FormalRTE::parse ( std::deque < sax::Token >::iterator & input ) { sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, FormalRTE::getXmlTagName ( ) ); std::pair < std::set < std::ranked_symbol < > >, std::set < std::ranked_symbol < > > > alphabets = RTEFromXMLParser::parseAlphabet ( input ); - std::rvalue_ref < FormalRTEElement > element = alib::xmlApi < rte::FormalRTEElement >::parse ( input ); + FormalRTEStructure element ( alib::xmlApi < rte::FormalRTEStructure >::parse ( input ) ); FormalRTE rte ( std::move ( alphabets.first ), std::move ( alphabets.second ), std::move ( element ) ); sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, FormalRTE::getXmlTagName ( ) ); @@ -99,7 +87,7 @@ FormalRTE FormalRTE::parse ( std::deque < sax::Token >::iterator & input ) { void FormalRTE::compose ( std::deque < sax::Token > & out ) const { out.emplace_back ( FormalRTE::getXmlTagName ( ), sax::Token::TokenType::START_ELEMENT ); RTEToXMLComposer::composeAlphabet ( out, this->getAlphabet ( ), this->getSubstitutionAlphabet ( ) ); - alib::xmlApi < FormalRTEElement >::compose ( out, this->getRTE ( ) ); + alib::xmlApi < FormalRTEStructure >::compose ( out, this->getRTE ( ) ); out.emplace_back ( FormalRTE::getXmlTagName ( ), sax::Token::TokenType::END_ELEMENT ); } diff --git a/alib2data/src/rte/formal/FormalRTE.h b/alib2data/src/rte/formal/FormalRTE.h index ec6d8bc05d8cbd601f0795d42eaa56271a7691c9..86f2bbd9888684ec88e83eca56cb7294f621ed67 100644 --- a/alib2data/src/rte/formal/FormalRTE.h +++ b/alib2data/src/rte/formal/FormalRTE.h @@ -3,14 +3,14 @@ #include "../RTEBase.h" #include <exception/CommonException.h> -#include "FormalRTEElement.h" +#include "FormalRTEStructure.h" #include <core/components.hpp> #include <set> #include <string> namespace rte { -class FormalRTEElement; +class FormalRTEStructure; class GeneralAlphabet; class ConstantAlphabet; @@ -20,25 +20,22 @@ class ConstantAlphabet; */ class FormalRTE : public RTEBase, public std::Components < FormalRTE, std::ranked_symbol < >, std::tuple < GeneralAlphabet, ConstantAlphabet >, std::tuple < > > { protected: - std::smart_ptr < FormalRTEElement > rte; + FormalRTEStructure rte; public: explicit FormalRTE ( ); - explicit FormalRTE ( std::set < std::ranked_symbol < > > alphabetF, std::set < std::ranked_symbol < > > alphabetK, const FormalRTEElement & rte ); - explicit FormalRTE ( std::set < std::ranked_symbol < > > alphabetF, std::set < std::ranked_symbol < > > alphabetK, FormalRTEElement && rte ); - explicit FormalRTE ( std::pair < std::set < std::ranked_symbol < > >, std::set < std::ranked_symbol < > > > alphabets, const FormalRTEElement & rte ); - explicit FormalRTE ( std::pair < std::set < std::ranked_symbol < > >, std::set < std::ranked_symbol < > > > alphabets, FormalRTEElement && rte ); - explicit FormalRTE ( const FormalRTEElement & rte ); - explicit FormalRTE ( FormalRTEElement && rte ); + explicit FormalRTE ( std::set < std::ranked_symbol < > > alphabetF, std::set < std::ranked_symbol < > > alphabetK, FormalRTEStructure rte ); + explicit FormalRTE ( std::pair < std::set < std::ranked_symbol < > >, std::set < std::ranked_symbol < > > > alphabets, FormalRTEStructure rte ); + explicit FormalRTE ( FormalRTEStructure rte ); /** - * @copydoc FormalRTEElement::clone() const + * @copydoc FormalRTEStructure::clone() const */ virtual RTEBase * clone ( ) const; /** - * @copydoc FormalRTEElement::plunder() const + * @copydoc FormalRTEStructure::plunder() const */ virtual RTEBase * plunder ( ) &&; @@ -85,24 +82,18 @@ public: /** * @return Root node of the regular expression tree */ - const FormalRTEElement & getRTE ( ) const; + const FormalRTEStructure & getRTE ( ) const; /** * @return Root node of the regular expression tree */ - FormalRTEElement & getRTE ( ); + FormalRTEStructure & getRTE ( ); /** * Sets the root node of the regular expression tree * @param regExp root node to set */ - void setRTE ( const FormalRTEElement & regExp ); - - /** - * Sets the root node of the regular expression tree - * @param regExp root node to set - */ - void setRTE ( FormalRTEElement && regExp ); + void setRTE ( FormalRTEStructure regExp ); /** * Prints XML representation of the RTE to the output stream. @@ -140,7 +131,7 @@ template < > class ComponentConstraint< rte::FormalRTE, std::ranked_symbol < >, rte::GeneralAlphabet > { public: static bool used ( const rte::FormalRTE & rte, const std::ranked_symbol < > & symbol ) { - return rte.getRTE ( ).testSymbol ( symbol ); + return rte.getRTE ( ).getStructure ( ).testSymbol ( symbol ); } static bool available ( const rte::FormalRTE &, const std::ranked_symbol < > & ) { @@ -149,7 +140,7 @@ public: static void valid ( const rte::FormalRTE & rte, const std::ranked_symbol < > & symbol ) { if ( rte.accessComponent < rte::ConstantAlphabet > ( ).get ( ).count ( symbol ) ) - throw ::exception::CommonException ( "Symbol " + std::to_string ( symbol ) + "cannot be in general alphabet since it is already in constant alphabet" ); + throw ::exception::CommonException ( "Symbol " + std::to_string ( symbol ) + " cannot be in general alphabet since it is already in constant alphabet" ); } }; @@ -157,7 +148,7 @@ template < > class ComponentConstraint< rte::FormalRTE, std::ranked_symbol < >, rte::ConstantAlphabet > { public: static bool used ( const rte::FormalRTE & rte, const std::ranked_symbol < > & symbol ) { - return rte.getRTE ( ).testSymbol ( symbol ); + return rte.getRTE ( ).getStructure ( ).testSymbol ( symbol ); } static bool available ( const rte::FormalRTE &, const std::ranked_symbol < > & ) { @@ -166,7 +157,7 @@ public: static void valid ( const rte::FormalRTE & rte, const std::ranked_symbol < > & symbol ) { if ( rte.accessComponent < rte::GeneralAlphabet > ( ).get ( ).count ( symbol ) ) - throw ::exception::CommonException ( "Symbol " + std::to_string ( symbol ) + "cannot be in constant alphabet since it is already in general alphabet" ); + throw ::exception::CommonException ( "Symbol " + std::to_string ( symbol ) + " cannot be in constant alphabet since it is already in general alphabet" ); } }; diff --git a/alib2data/src/rte/formal/FormalRTEElement.cpp b/alib2data/src/rte/formal/FormalRTEElement.cpp index 202fa2b1f53a7e1e3617e92495e957c9bdee33eb..8889648d46d442e99632729b675f204590fc29eb 100644 --- a/alib2data/src/rte/formal/FormalRTEElement.cpp +++ b/alib2data/src/rte/formal/FormalRTEElement.cpp @@ -18,19 +18,3 @@ std::pair < std::set < std::ranked_symbol < > >, std::set < std::ranked_symbol < } } /* namespace rte */ - -namespace alib { - -std::rvalue_ref < rte::FormalRTEElement > xmlApi < rte::FormalRTEElement >::parse ( std::deque < sax::Token >::iterator & input ) { - return rte::RTEFromXMLParser::parseFormalRTEElement ( input ); -} - -bool xmlApi < rte::FormalRTEElement >::first ( const std::deque < sax::Token >::const_iterator & ) { - throw exception::CommonException ( "Unimplemented" ); -} - -void xmlApi < rte::FormalRTEElement >::compose ( std::deque < sax::Token > & output, const rte::FormalRTEElement & data ) { - data.Accept ( ( void * ) & output, rte::RTEToXMLComposer::RTE_TO_XML_COMPOSER ); -} - -} /* namespace rte */ diff --git a/alib2data/src/rte/formal/FormalRTEElement.h b/alib2data/src/rte/formal/FormalRTEElement.h index 184ae8379ee5496ea31e94c44f3400c7eaf8fabf..7a86a1e7eb569b86370c6c204faf3debceb42f94 100644 --- a/alib2data/src/rte/formal/FormalRTEElement.h +++ b/alib2data/src/rte/formal/FormalRTEElement.h @@ -65,15 +65,4 @@ public: } /* namespace rte */ -namespace alib { - -template < > -struct xmlApi < rte::FormalRTEElement > { - static std::rvalue_ref < rte::FormalRTEElement > parse ( std::deque < sax::Token >::iterator & input ); - static bool first ( const std::deque < sax::Token >::const_iterator & input ); - static void compose ( std::deque < sax::Token > & output, const rte::FormalRTEElement & data ); -}; - -} /* namespace alib */ - #endif /* FORMAL_RTE_ELEMENT_H_ */ diff --git a/alib2data/src/rte/formal/FormalRTEStructure.cpp b/alib2data/src/rte/formal/FormalRTEStructure.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd234379b50b579447c47e93bd409d54c561da1f --- /dev/null +++ b/alib2data/src/rte/formal/FormalRTEStructure.cpp @@ -0,0 +1,59 @@ +/* + * FormalRTEStructure.cpp + * + * Created on: Oct 20, 2016 + * Author: Jan Travnicek + */ + +#include "FormalRTEStructure.h" +#include "FormalRTEEmpty.h" + +#include "../common/RTEFromXMLParser.h" +#include "../common/RTEToXMLComposer.h" +#include <exception/CommonException.h> + +namespace rte { + +FormalRTEStructure::FormalRTEStructure ( FormalRTEElement && structure ) : structure ( NULL ) { + setStructure ( std::move ( structure ) ); +} + +FormalRTEStructure::FormalRTEStructure ( const FormalRTEElement & structure ) : FormalRTEStructure ( std::move_copy ( structure ) ) { +} + +FormalRTEStructure::FormalRTEStructure ( ) : FormalRTEStructure ( FormalRTEEmpty ( ) ) { +} + +const FormalRTEElement & FormalRTEStructure::getStructure ( ) const { + return * structure; +} + +FormalRTEElement & FormalRTEStructure::getStructure ( ) { + return * structure; +} + +void FormalRTEStructure::setStructure ( const FormalRTEElement & structure ) { + setStructure ( std::move_copy ( structure ) ); +} + +void FormalRTEStructure::setStructure ( FormalRTEElement && param ) { + this->structure = std::smart_ptr < FormalRTEElement > ( std::move ( param ).plunder ( ) ); +} + +} /* namespace rte */ + +namespace alib { + +rte::FormalRTEStructure xmlApi < rte::FormalRTEStructure >::parse ( std::deque < sax::Token >::iterator & input ) { + return rte::FormalRTEStructure ( rte::RTEFromXMLParser::parseFormalRTEElement ( input ) ); +} + +bool xmlApi < rte::FormalRTEStructure >::first ( const std::deque < sax::Token >::const_iterator & ) { + throw exception::CommonException ( "Unimplemented" ); +} + +void xmlApi < rte::FormalRTEStructure >::compose ( std::deque < sax::Token > & output, const rte::FormalRTEStructure & data ) { + data.getStructure ( ).Accept ( ( void * ) & output, rte::RTEToXMLComposer::RTE_TO_XML_COMPOSER ); +} + +} /* namespace alib */ diff --git a/alib2data/src/rte/formal/FormalRTEStructure.h b/alib2data/src/rte/formal/FormalRTEStructure.h new file mode 100644 index 0000000000000000000000000000000000000000..657d404470584bfe312be4a880c8ea7e110f47ae --- /dev/null +++ b/alib2data/src/rte/formal/FormalRTEStructure.h @@ -0,0 +1,86 @@ +/* + * FormalRTEStructure.h + * + * Created on: Oct 20, 2016 + * Author: Jan Travnicek + */ + +#ifndef FORMAL_RTE_STRUCTURE_H_ +#define FORMAL_RTE_STRUCTURE_H_ + +#include <memory> +#include "FormalRTEElement.h" + +namespace rte { + +/** + * Represents formal regular expression structure as parsed from the XML. Regular expression is stored + * as a tree of FormalRTEElement. + */ +class FormalRTEStructure { +protected: + std::smart_ptr < FormalRTEElement > structure; + +public: + explicit FormalRTEStructure ( ); + + explicit FormalRTEStructure ( FormalRTEElement && structure ); + explicit FormalRTEStructure ( const FormalRTEElement & structure ); + + /** + * @return Root node of the formal regular expression tree + */ + const FormalRTEElement & getStructure ( ) const; + + /** + * @return Root node of the formal regular expression tree + */ + FormalRTEElement & getStructure ( ); + + /** + * Sets the root node of the formal regular expression tree + * @param structure root node to set + */ + void setStructure ( FormalRTEElement && structure ); + void setStructure ( const FormalRTEElement & structure ); + + friend std::ostream & operator << ( std::ostream & out, const FormalRTEStructure & structure ) { + out << structure.getStructure ( ); + return out; + } + + friend bool operator < ( const FormalRTEStructure & first, const FormalRTEStructure & second ) { + return first.getStructure().compare(second.getStructure()) < 0; + } + + friend bool operator == ( const FormalRTEStructure & first, const FormalRTEStructure & second ) { + return first.getStructure().compare(second.getStructure()) == 0; + } + +}; + +} /* namespace rte */ + +namespace alib { + +template < > +struct xmlApi < rte::FormalRTEStructure > { + static rte::FormalRTEStructure parse ( std::deque < sax::Token >::iterator & input ); + static bool first ( const std::deque < sax::Token >::const_iterator & input ); + static void compose ( std::deque < sax::Token > & output, const rte::FormalRTEStructure & data ); +}; + +} /* namespace alib */ + +namespace std { + +template < > +struct compare < rte::FormalRTEStructure > { + int operator()(const rte::FormalRTEStructure& first, const rte::FormalRTEStructure& second) const { + return first.getStructure().compare(second.getStructure()); + } +}; + +} /* namespace std */ + +#endif /* FORMAL_RTE_STRUCTURE_H_ */ diff --git a/alib2data/test-src/rte/RTETest.cpp b/alib2data/test-src/rte/RTETest.cpp index 58137e7d17f7c09153d8aa8df13b7f4f7912e7d0..fb2c88cde8ce4aca788add66cb95042104b11548 100644 --- a/alib2data/test-src/rte/RTETest.cpp +++ b/alib2data/test-src/rte/RTETest.cpp @@ -78,7 +78,7 @@ void RTETest::testCopyConstruct ( ) { rte::FormalRTEIteration iter = rte::FormalRTEIteration ( a, rte::FormalRTESymbolSubst ( symb_y0 ) ); rte::FormalRTESubstitution subst = rte::FormalRTESubstitution ( iter, alt, rte::FormalRTESymbolSubst ( symb_z0 ) ); - frte.setRTE ( subst ); + frte.setRTE ( rte::FormalRTEStructure ( subst ) ); rte::FormalRTE frte2 ( frte ); CPPUNIT_ASSERT ( frte == frte2 ); @@ -117,7 +117,7 @@ void RTETest::testXMLParser ( ) { rte::FormalRTEIteration iter = rte::FormalRTEIteration ( d, rte::FormalRTESymbolSubst ( symb_y0 ) ); rte::FormalRTESubstitution subst = rte::FormalRTESubstitution ( iter, alt, rte::FormalRTESymbolSubst ( symb_z0 ) ); - frte.setRTE ( subst ); + frte.setRTE ( rte::FormalRTEStructure ( subst ) ); rte::RTE rte ( frte ); alib::XmlDataFactory::toStdout ( rte );