From 796380134c76ee594f39d133003da6d7e8e60e75 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 23 Jun 2016 07:36:57 +0200
Subject: [PATCH] formal rte use components and general refactoring

---
 alib2data/src/rte/formal/FormalRTE.cpp        | 132 +++++++++---------
 alib2data/src/rte/formal/FormalRTE.h          |  91 +++++++-----
 .../src/rte/formal/FormalRTEAlternation.cpp   |   2 +-
 .../src/rte/formal/FormalRTEAlternation.h     |  22 +--
 alib2data/src/rte/formal/FormalRTEElement.cpp |   8 ++
 alib2data/src/rte/formal/FormalRTEElement.h   |  29 ++--
 alib2data/src/rte/formal/FormalRTEEmpty.cpp   |   2 +-
 alib2data/src/rte/formal/FormalRTEEmpty.h     |  22 +--
 .../src/rte/formal/FormalRTEIteration.cpp     |   2 +-
 alib2data/src/rte/formal/FormalRTEIteration.h |  23 +--
 .../src/rte/formal/FormalRTESubstitution.cpp  |   2 +-
 .../src/rte/formal/FormalRTESubstitution.h    |  23 +--
 alib2data/src/rte/formal/FormalRTESymbol.cpp  |   2 +-
 alib2data/src/rte/formal/FormalRTESymbol.h    |  23 +--
 alib2data/test-src/rte/RTETest.cpp            |   8 +-
 15 files changed, 211 insertions(+), 180 deletions(-)

diff --git a/alib2data/src/rte/formal/FormalRTE.cpp b/alib2data/src/rte/formal/FormalRTE.cpp
index 60dfc704ca..dcbe114412 100644
--- a/alib2data/src/rte/formal/FormalRTE.cpp
+++ b/alib2data/src/rte/formal/FormalRTE.cpp
@@ -19,8 +19,17 @@
 
 namespace rte {
 
-FormalRTE::FormalRTE ( ) {
-	this->rte = new FormalRTEEmpty ( );
+FormalRTE::FormalRTE ( std::set < alphabet::RankedSymbol > alphabetF, std::set < alphabet::RankedSymbol > alphabetK, FormalRTEElement && rte ) : std::Components < FormalRTE, alphabet::RankedSymbol, 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::pair < std::set < alphabet::RankedSymbol >, std::set < alphabet::RankedSymbol > > alphabets, FormalRTEElement && rte ) : FormalRTE ( std::move ( alphabets.first ), std::move ( alphabets.second ), std::move ( rte ) ) {
+}
+
+FormalRTE::FormalRTE ( std::pair < std::set < alphabet::RankedSymbol >, std::set < alphabet::RankedSymbol > > alphabets, const FormalRTEElement & rte ) : FormalRTE ( std::move ( alphabets.first ), std::move ( alphabets.second ), std::move_copy ( rte ) ) {
+}
+
+FormalRTE::FormalRTE ( ) : FormalRTE ( std::set < alphabet::RankedSymbol > ( ), std::set < alphabet::RankedSymbol > ( ), FormalRTEEmpty ( ) ) {
 }
 
 /*
@@ -33,43 +42,21 @@ FormalRTE::FormalRTE ( ) {
  * }
  */
 
-FormalRTE::FormalRTE ( std::set < alphabet::RankedSymbol > alphabetF, std::set < alphabet::RankedSymbol > alphabetK, const FormalRTEElement & rte ) {
-	this->alphabetF = std::move ( alphabetF );
-	this->alphabetK = std::move ( alphabetK );
-	this->rte = NULL;
-	setRTE ( rte );
+FormalRTE::FormalRTE ( std::set < alphabet::RankedSymbol > alphabetF, std::set < alphabet::RankedSymbol > alphabetK, const FormalRTEElement & rte ) : FormalRTE ( std::move ( alphabetF ), std::move ( alphabetK ), std::move_copy ( rte ) ) {
 }
 
-FormalRTE::FormalRTE ( std::set < alphabet::RankedSymbol > alphabetF, std::set < alphabet::RankedSymbol > alphabetK, FormalRTEElement && rte ) {
-	this->alphabetF = std::move ( alphabetF );
-	this->alphabetK = std::move ( alphabetK );
-	this->rte = NULL;
-	setRTE ( std::move ( rte ) );
+FormalRTE::FormalRTE ( const FormalRTEElement & rte ) : FormalRTE ( rte.computeMinimalAlphabets ( ), std::move_copy ( rte ) ) {
 }
 
-FormalRTE::FormalRTE ( const FormalRTEElement & rte ) {
-	rte.computeMinimalAlphabet ( alphabetF, alphabetK );
-	this->rte = NULL;
-	setRTE ( rte );
+FormalRTE::FormalRTE ( FormalRTEElement && rte ) : FormalRTE ( rte.computeMinimalAlphabets ( ), rte ) {
 }
 
-FormalRTE::FormalRTE ( FormalRTEElement && rte ) {
-	rte.computeMinimalAlphabet ( alphabetF, alphabetK );
-	this->rte = NULL;
-	setRTE ( std::move ( rte ) );
-}
-
-FormalRTE::FormalRTE ( const FormalRTE & other ) : rte ( other.rte->clone ( ) ) {
-	alphabetF = other.alphabetF;
-	alphabetK = other.alphabetK;
+FormalRTE::FormalRTE ( const FormalRTE & other ) : std::Components < FormalRTE, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, ConstantAlphabet >, std::tuple < > > ( std::make_tuple ( other.getAlphabet ( ), other.getConstantAlphabet ( ) ), std::tuple < > ( ) ), rte ( other.rte ) {
 	this->rte->attachRTE ( this );
 }
 
-FormalRTE::FormalRTE ( FormalRTE && other ) noexcept : rte ( other.rte ) {
-	alphabetF = std::move ( other.alphabetF );
-	alphabetK = std::move ( other.alphabetK );
+FormalRTE::FormalRTE ( FormalRTE && other ) noexcept : std::Components < FormalRTE, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, ConstantAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( other.accessComponent < GeneralAlphabet > ( ).get ( ) ), std::move ( other.accessComponent < ConstantAlphabet > ( ).get ( ) ) ), std::tuple < > ( ) ), rte ( std::move ( other.rte ) ) {
 	this->rte->attachRTE ( this );
-	other.rte = NULL;
 }
 
 RTEBase * FormalRTE::clone ( ) const {
@@ -91,14 +78,13 @@ FormalRTE & FormalRTE::operator =( const FormalRTE & other ) {
 
 FormalRTE & FormalRTE::operator =( FormalRTE && other ) noexcept {
 	std::swap ( this->rte, other.rte );
-	std::swap ( this->alphabetF, other.alphabetF );
-	std::swap ( this->alphabetK, other.alphabetK );
+	std::swap ( accessComponent < GeneralAlphabet > ( ).get ( ), other.accessComponent < GeneralAlphabet > ( ).get ( ) );
+	std::swap ( accessComponent < ConstantAlphabet > ( ).get ( ), other.accessComponent < ConstantAlphabet > ( ).get ( ) );
 
 	return * this;
 }
 
 FormalRTE::~FormalRTE ( ) noexcept {
-	delete rte;
 }
 
 const FormalRTEElement & FormalRTE::getRTE ( ) const {
@@ -110,35 +96,14 @@ FormalRTEElement & FormalRTE::getRTE ( ) {
 }
 
 void FormalRTE::setRTE ( const FormalRTEElement & rte ) {
-	delete this->rte;
-	this->rte = rte.clone ( );
-
-	if ( !this->rte->attachRTE ( this ) )
-		throw exception::CommonException ( "Input symbols not in the alphabet." );
+	setRTE ( std::move_copy ( rte ) );
 }
 
-void FormalRTE::setRTE ( FormalRTEElement && rte ) {
-	delete this->rte;
-	this->rte = std::move ( rte ).plunder ( );
+void FormalRTE::setRTE ( FormalRTEElement && param ) {
+	this->rte = std::smart_ptr < FormalRTEElement > ( std::move ( param ).plunder ( ) );
 
-	if ( !this->rte->attachRTE ( this ) ) {
-		delete this->rte;
+	if ( !this->rte->attachRTE ( this ) )
 		throw exception::CommonException ( "Input symbols not in the alphabet." );
-	}
-}
-
-bool FormalRTE::removeSymbolFromAlphabet ( const alphabet::RankedSymbol & symbol ) {
-	if ( this->rte->testSymbol ( symbol ) )
-		throw exception::CommonException ( "Input symbol \"" + ( std::string ) symbol + "\" is used." );
-
-	return alphabetF.erase ( symbol );
-}
-
-bool FormalRTE::removeConstantSymbolFromAlphabet ( const alphabet::RankedSymbol & symbol ) {
-	if ( this->rte->testSymbol ( symbol ) )
-		throw exception::CommonException ( "Input substitution symbol \"" + ( std::string ) symbol + "\" is used." );
-
-	return alphabetK.erase ( symbol );
 }
 
 void FormalRTE::operator >>( std::ostream & out ) const {
@@ -146,17 +111,12 @@ void FormalRTE::operator >>( std::ostream & out ) const {
 }
 
 int FormalRTE::compare ( const FormalRTE & other ) const {
-	int res = rte->compare ( * other.rte );
-
-	if ( res == 0 ) {
-		std::compare < std::set < alphabet::RankedSymbol > > comp;
-		res = comp ( alphabetF, other.alphabetF );
+	auto first = std::tie ( rte, getAlphabet ( ), getConstantAlphabet ( ) );
+	auto second = std::tie ( rte, other.getAlphabet ( ), getConstantAlphabet ( ) );
 
-		if ( res == 0 )
-			res = comp ( alphabetK, other.alphabetK );
-	}
+	std::compare < decltype ( first ) > comp;
 
-	return res;
+	return comp ( first, second );
 }
 
 FormalRTE::operator std::string ( ) const {
@@ -185,6 +145,46 @@ void FormalRTE::compose ( std::deque < sax::Token > & out ) const {
 
 } /* namespace rte */
 
+namespace std {
+
+template < >
+bool rte::FormalRTE::Component < rte::FormalRTE, alphabet::RankedSymbol, rte::GeneralAlphabet >::used ( const alphabet::RankedSymbol & symbol ) const {
+	return static_cast < const rte::FormalRTE * > ( this )->getRTE ( ).testSymbol ( symbol );
+}
+
+template < >
+bool rte::FormalRTE::Component < rte::FormalRTE, alphabet::RankedSymbol, rte::GeneralAlphabet >::available ( const alphabet::RankedSymbol & ) const {
+	return true;
+}
+
+template < >
+void rte::FormalRTE::Component < rte::FormalRTE, alphabet::RankedSymbol, rte::GeneralAlphabet >::valid ( const alphabet::RankedSymbol & symbol ) const {
+	const rte::FormalRTE * rte = static_cast < const rte::FormalRTE * > ( this );
+
+	if ( rte->accessComponent < rte::ConstantAlphabet > ( ).get ( ).count ( symbol ) )
+		throw ::exception::CommonException ( "Symbol " + ( std::string ) symbol + "cannot be in general alphabet since it is already in constant alphabet" );
+}
+
+template < >
+bool rte::FormalRTE::Component < rte::FormalRTE, alphabet::RankedSymbol, rte::ConstantAlphabet >::used ( const alphabet::RankedSymbol & symbol ) const {
+	return static_cast < const rte::FormalRTE * > ( this )->getRTE ( ).testSymbol ( symbol );
+}
+
+template < >
+bool rte::FormalRTE::Component < rte::FormalRTE, alphabet::RankedSymbol, rte::ConstantAlphabet >::available ( const alphabet::RankedSymbol & ) const {
+	return true;
+}
+
+template < >
+void rte::FormalRTE::Component < rte::FormalRTE, alphabet::RankedSymbol, rte::ConstantAlphabet >::valid ( const alphabet::RankedSymbol & symbol ) const {
+	const rte::FormalRTE * rte = static_cast < const rte::FormalRTE * > ( this );
+
+	if ( rte->accessComponent < rte::GeneralAlphabet > ( ).get ( ).count ( symbol ) )
+		throw ::exception::CommonException ( "Symbol " + ( std::string ) symbol + "cannot be in constant alphabet since it is already in general alphabet" );
+}
+
+} /* namespace std */
+
 namespace alib {
 
 auto formalRTEParserRegister  = xmlApi < rte::RTE >::ParserRegister < rte::FormalRTE > ( );
diff --git a/alib2data/src/rte/formal/FormalRTE.h b/alib2data/src/rte/formal/FormalRTE.h
index 2c41e47c49..5e4ae0ad86 100644
--- a/alib2data/src/rte/formal/FormalRTE.h
+++ b/alib2data/src/rte/formal/FormalRTE.h
@@ -1,17 +1,17 @@
 #ifndef FORMAL_RTE_H_
 #define FORMAL_RTE_H_
 
-#include <vector>
-#include <list>
 #include <string>
 #include <set>
 #include "FormalRTEElement.h"
 #include "../RTEBase.h"
-#include "../common/RTEAlphabet.h"
+#include <core/components.hpp>
 
 namespace rte {
 
 class FormalRTEElement;
+class GeneralAlphabet;
+class ConstantAlphabet;
 
 // class UnboundedRTE;
 
@@ -19,26 +19,18 @@ class FormalRTEElement;
  * Represents regular tree expression parsed from the XML. Regular tree expression is stored
  * as a tree of RTEElement elements.
  */
-class FormalRTE : public RTEBase, public RTEAlphabet {
+class FormalRTE : public RTEBase, public std::Components < FormalRTE, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, ConstantAlphabet >, std::tuple < > > {
 protected:
-	FormalRTEElement * rte;
+	std::smart_ptr < FormalRTEElement > rte;
 
 public:
-	/**
-	 * @copydoc FormalRTEElement::clone() const
-	 */
-	virtual RTEBase * clone ( ) const;
-
-	/**
-	 * @copydoc FormalRTEElement::plunder() const
-	 */
-	virtual RTEBase * plunder ( ) &&;
-
 	explicit FormalRTE ( );
 
 	 // explicit FormalRTE ( const UnboundedRTE & other );
 	explicit FormalRTE ( std::set < alphabet::RankedSymbol > alphabetF, std::set < alphabet::RankedSymbol > alphabetK, const FormalRTEElement & rte );
 	explicit FormalRTE ( std::set < alphabet::RankedSymbol > alphabetF, std::set < alphabet::RankedSymbol > alphabetK, FormalRTEElement && rte );
+	explicit FormalRTE ( std::pair < std::set < alphabet::RankedSymbol >, std::set < alphabet::RankedSymbol > > alphabets, const FormalRTEElement & rte );
+	explicit FormalRTE ( std::pair < std::set < alphabet::RankedSymbol >, std::set < alphabet::RankedSymbol > > alphabets, FormalRTEElement && rte );
 	explicit FormalRTE ( const FormalRTEElement & rte );
 	explicit FormalRTE ( FormalRTEElement && rte );
 
@@ -52,12 +44,54 @@ public:
 	FormalRTE & operator =( FormalRTE && other ) noexcept;
 	~FormalRTE ( ) noexcept;
 
-	virtual const std::set < alphabet::RankedSymbol > & getAlphabet ( ) const {
-		return RTEAlphabet::getAlphabet ( );
+	/**
+	 * @copydoc FormalRTEElement::clone() const
+	 */
+	virtual RTEBase * clone ( ) const;
+
+	/**
+	 * @copydoc FormalRTEElement::plunder() const
+	 */
+	virtual RTEBase * plunder ( ) &&;
+
+	const std::set < alphabet::RankedSymbol > & getAlphabet ( ) const {
+		return accessComponent < GeneralAlphabet > ( ).get ( );
 	}
 
-	virtual const std::set < alphabet::RankedSymbol > & getConstantAlphabet ( ) const {
-		return RTEAlphabet::getConstantAlphabet ( );
+	void addAlphabetSymbols ( alphabet::RankedSymbol symbol ) {
+		accessComponent < GeneralAlphabet > ( ).add ( std::move ( symbol ) );
+	}
+
+	void addAlphabetSymbols ( std::set < alphabet::RankedSymbol > symbols ) {
+		accessComponent < GeneralAlphabet > ( ).add ( std::move ( symbols ) );
+	}
+
+	void setAlphabetSymbols ( std::set < alphabet::RankedSymbol > symbols ) {
+		accessComponent < GeneralAlphabet > ( ).set ( std::move ( symbols ) );
+	}
+
+	void removeAlphabetSymbol ( const alphabet::RankedSymbol & symbol ) {
+		accessComponent < GeneralAlphabet > ( ).remove ( symbol );
+	}
+
+	const std::set < alphabet::RankedSymbol > & getConstantAlphabet ( ) const {
+		return accessComponent < ConstantAlphabet > ( ).get ( );
+	}
+
+	void addConstantSymbol ( alphabet::RankedSymbol symbol ) {
+		accessComponent < ConstantAlphabet > ( ).add ( std::move ( symbol ) );
+	}
+
+	void addConstantSymbols ( std::set < alphabet::RankedSymbol > symbols ) {
+		accessComponent < ConstantAlphabet > ( ).add ( std::move ( symbols ) );
+	}
+
+	void setConstantSymbols ( std::set < alphabet::RankedSymbol > symbols ) {
+		accessComponent < ConstantAlphabet > ( ).set ( std::move ( symbols ) );
+	}
+
+	void removeConstantSymbol ( const alphabet::RankedSymbol & symbol ) {
+		accessComponent < ConstantAlphabet > ( ).remove ( symbol );
 	}
 
 	/**
@@ -82,13 +116,6 @@ public:
 	 */
 	void setRTE ( FormalRTEElement && regExp );
 
-	/**
-	 * Removes symbol from the alphabet of symbol available in the regular tree expression
-	 * @param symbol removed symbol from the alphabet
-	 */
-	bool removeSymbolFromAlphabet ( const alphabet::RankedSymbol & symbol );
-	bool removeConstantSymbolFromAlphabet ( const alphabet::RankedSymbol & symbol );
-
 	/**
 	 * Prints XML representation of the RTE to the output stream.
 	 * @param out output stream to which print the RTE
@@ -119,16 +146,4 @@ public:
 
 } /* namespace rte */
 
-namespace std {
-
-template < >
-struct compare < rte::FormalRTE > {
-	int operator ()( const rte::FormalRTE & first, const rte::FormalRTE & second ) const {
-		return first.compare ( second );
-	}
-
-};
-
-} /* namespace std */
-
 #endif /* FORMAL_RTE_H_ */
diff --git a/alib2data/src/rte/formal/FormalRTEAlternation.cpp b/alib2data/src/rte/formal/FormalRTEAlternation.cpp
index 030fc53510..31b5de9617 100644
--- a/alib2data/src/rte/formal/FormalRTEAlternation.cpp
+++ b/alib2data/src/rte/formal/FormalRTEAlternation.cpp
@@ -153,7 +153,7 @@ bool FormalRTEAlternation::testSymbol ( const alphabet::RankedSymbol & symbol )
 	return false;
 }
 
-bool FormalRTEAlternation::attachRTE ( const RTEAlphabet * regexp ) {
+bool FormalRTEAlternation::attachRTE ( const FormalRTE * regexp ) {
 	if ( this->parentRTE == regexp ) return true;
 
 	this->parentRTE = regexp;
diff --git a/alib2data/src/rte/formal/FormalRTEAlternation.h b/alib2data/src/rte/formal/FormalRTEAlternation.h
index 02b48c6328..fb1f555557 100644
--- a/alib2data/src/rte/formal/FormalRTEAlternation.h
+++ b/alib2data/src/rte/formal/FormalRTEAlternation.h
@@ -20,20 +20,10 @@ protected:
 	FormalRTEElement * left;
 	FormalRTEElement * right;
 
-	/**
-	 * @copydoc FormalRTEElement::testSymbol() const
-	 */
-	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
-
 	/**
 	 * @copydoc FormalRTEElement::attachRTE()
 	 */
-	virtual bool attachRTE ( const RTEAlphabet * regexp );
-
-	/**
-	 * @copydoc FormalRTEElement::computeMinimalAlphabet()
-	 */
-	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const;
+	virtual bool attachRTE ( const FormalRTE * regexp );
 
 public:
 	void Accept ( void * userData, const FormalRTEElementVisitor & visitor ) const {
@@ -60,6 +50,16 @@ public:
 	 */
 	virtual FormalRTEAlternation * plunder ( ) &&;
 
+	/**
+	 * @copydoc FormalRTEElement::testSymbol() const
+	 */
+	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
+
+	/**
+	 * @copydoc FormalRTEElement::computeMinimalAlphabet()
+	 */
+	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const;
+
 	/**
 	 * @return elements
 	 */
diff --git a/alib2data/src/rte/formal/FormalRTEElement.cpp b/alib2data/src/rte/formal/FormalRTEElement.cpp
index 7cfdffc521..d36f226153 100644
--- a/alib2data/src/rte/formal/FormalRTEElement.cpp
+++ b/alib2data/src/rte/formal/FormalRTEElement.cpp
@@ -12,6 +12,14 @@ namespace rte {
 FormalRTEElement::FormalRTEElement ( ) : parentRTE ( NULL ) {
 }
 
+std::pair < std::set < alphabet::RankedSymbol >, std::set < alphabet::RankedSymbol > > FormalRTEElement::computeMinimalAlphabets ( ) const {
+	std::set < alphabet::RankedSymbol > alphabetF;
+	std::set < alphabet::RankedSymbol > alphabetK;
+
+	computeMinimalAlphabet ( alphabetF, alphabetK );
+	return std::make_pair ( std::move ( alphabetF), std::move ( alphabetK ) );
+}
+
 } /* namespace rte */
 
 namespace alib {
diff --git a/alib2data/src/rte/formal/FormalRTEElement.h b/alib2data/src/rte/formal/FormalRTEElement.h
index d1b57396c7..4d84bcdde2 100644
--- a/alib2data/src/rte/formal/FormalRTEElement.h
+++ b/alib2data/src/rte/formal/FormalRTEElement.h
@@ -33,7 +33,19 @@ protected:
 	/*
 	 * Parent regexp contanining this instance of RTEElement
 	 */
-	const RTEAlphabet * parentRTE;
+	const FormalRTE * parentRTE;
+
+	/**
+	 * Attaches the regexp to this instance and all its childs
+	 * @param regexp parent regexp to attach as parent
+	 * @return true if symbols used in regexp element are in the regexp's alphabet
+	 */
+	virtual bool attachRTE ( const FormalRTE * regexp ) = 0;
+
+public:
+	virtual void Accept ( void * userData, const FormalRTEElementVisitor & visitor ) const = 0;
+
+	explicit FormalRTEElement ( );
 
 	/**
 	 * Traverses the regexp tree looking if particular Symbol is used in the regexp.
@@ -43,13 +55,6 @@ protected:
 	 */
 	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const = 0;
 
-	/**
-	 * Attaches the regexp to this instance and all its childs
-	 * @param regexp parent regexp to attach as parent
-	 * @return true if symbols used in regexp element are in the regexp's alphabet
-	 */
-	virtual bool attachRTE ( const RTEAlphabet * regexp ) = 0;
-
 	/**
 	 * Traverses the regexp tree computing minimal alphabet needed by regexp
 	 *
@@ -58,10 +63,10 @@ protected:
 	 */
 	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const = 0;
 
-public:
-	virtual void Accept ( void * userData, const FormalRTEElementVisitor & visitor ) const = 0;
-
-	explicit FormalRTEElement ( );
+	/**
+	 * @copydoc RankedNode::computeMinimalAlphabet()
+	 */
+	std::pair < std::set < alphabet::RankedSymbol >, std::set < alphabet::RankedSymbol > > computeMinimalAlphabets ( ) const;
 
 	/**
 	 * Creates copy of the element.
diff --git a/alib2data/src/rte/formal/FormalRTEEmpty.cpp b/alib2data/src/rte/formal/FormalRTEEmpty.cpp
index b8437c65ae..8438131d8c 100644
--- a/alib2data/src/rte/formal/FormalRTEEmpty.cpp
+++ b/alib2data/src/rte/formal/FormalRTEEmpty.cpp
@@ -51,7 +51,7 @@ bool FormalRTEEmpty::testSymbol ( const alphabet::RankedSymbol & ) const {
 	return false;
 }
 
-bool FormalRTEEmpty::attachRTE ( const RTEAlphabet * regexp ) {
+bool FormalRTEEmpty::attachRTE ( const FormalRTE * regexp ) {
 	if ( this->parentRTE == regexp ) return true;
 
 	this->parentRTE = regexp;
diff --git a/alib2data/src/rte/formal/FormalRTEEmpty.h b/alib2data/src/rte/formal/FormalRTEEmpty.h
index 107da0fb68..c8b05c2d53 100644
--- a/alib2data/src/rte/formal/FormalRTEEmpty.h
+++ b/alib2data/src/rte/formal/FormalRTEEmpty.h
@@ -17,20 +17,10 @@ protected:
 
 	// virtual UnboundedRTEElement * cloneAsUnbounded ( ) const;
 
-	/**
-	 * @copydoc FormalRTEElement::testSymbol() const
-	 */
-	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
-
 	/**
 	 * @copydoc FormalRTEElement::attachRTE()
 	 */
-	virtual bool attachRTE ( const RTEAlphabet * regexp );
-
-	/**
-	 * @copydoc RTEElement::computeMinimalAlphabet()
-	 */
-	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const;
+	virtual bool attachRTE ( const FormalRTE * regexp );
 
 public:
 	void Accept ( void * userData, const FormalRTEElementVisitor & visitor ) const {
@@ -53,6 +43,16 @@ public:
 	 */
 	virtual FormalRTEEmpty * plunder ( ) &&;
 
+	/**
+	 * @copydoc FormalRTEElement::testSymbol() const
+	 */
+	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
+
+	/**
+	 * @copydoc RTEElement::computeMinimalAlphabet()
+	 */
+	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const;
+
 	virtual int compare ( const FormalRTEElement & other ) const {
 		if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other );
 
diff --git a/alib2data/src/rte/formal/FormalRTEIteration.cpp b/alib2data/src/rte/formal/FormalRTEIteration.cpp
index 05720a241c..72f6b50a01 100644
--- a/alib2data/src/rte/formal/FormalRTEIteration.cpp
+++ b/alib2data/src/rte/formal/FormalRTEIteration.cpp
@@ -119,7 +119,7 @@ bool FormalRTEIteration::testSymbol ( const alphabet::RankedSymbol & symbol ) co
 	return element->testSymbol ( symbol );
 }
 
-bool FormalRTEIteration::attachRTE ( const RTEAlphabet * regexp ) {
+bool FormalRTEIteration::attachRTE ( const FormalRTE * regexp ) {
 	if ( this->parentRTE == regexp ) return true;
 
 	this->parentRTE = regexp;
diff --git a/alib2data/src/rte/formal/FormalRTEIteration.h b/alib2data/src/rte/formal/FormalRTEIteration.h
index 21d5965fea..4b228bfe2a 100644
--- a/alib2data/src/rte/formal/FormalRTEIteration.h
+++ b/alib2data/src/rte/formal/FormalRTEIteration.h
@@ -3,6 +3,7 @@
 
 #include "FormalRTEElement.h"
 #include "FormalRTESymbol.h"
+#include "FormalRTE.h"
 
 namespace rte {
 
@@ -27,20 +28,10 @@ protected:
 
 	// virtual UnboundedRTEElement * cloneAsUnbounded ( ) const;
 
-	/**
-	 * @copydoc FormalRTEElement::testSymbol() const
-	 */
-	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
-
 	/**
 	 * @copydoc FormalRTEElement::attachRTE()
 	 */
-	virtual bool attachRTE ( const RTEAlphabet * regexp );
-
-	/**
-	 * @copydoc FormalRTEElement::computeMinimalAlphabet()
-	 */
-	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const;
+	virtual bool attachRTE ( const FormalRTE * regexp );
 
 public:
 	void Accept ( void * userData, const FormalRTEElementVisitor & visitor ) const {
@@ -66,6 +57,16 @@ public:
 	 */
 	virtual FormalRTEIteration * plunder ( ) &&;
 
+	/**
+	 * @copydoc FormalRTEElement::testSymbol() const
+	 */
+	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
+
+	/**
+	 * @copydoc FormalRTEElement::computeMinimalAlphabet()
+	 */
+	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const;
+
 	/**
 	 * @return element
 	 */
diff --git a/alib2data/src/rte/formal/FormalRTESubstitution.cpp b/alib2data/src/rte/formal/FormalRTESubstitution.cpp
index a51a82293a..16151201fc 100644
--- a/alib2data/src/rte/formal/FormalRTESubstitution.cpp
+++ b/alib2data/src/rte/formal/FormalRTESubstitution.cpp
@@ -174,7 +174,7 @@ bool FormalRTESubstitution::testSymbol ( const alphabet::RankedSymbol & symbol )
 	return false;
 }
 
-bool FormalRTESubstitution::attachRTE ( const RTEAlphabet * regexp ) {
+bool FormalRTESubstitution::attachRTE ( const FormalRTE * regexp ) {
 	if ( this->parentRTE == regexp ) return true;
 
 	this->parentRTE = regexp;
diff --git a/alib2data/src/rte/formal/FormalRTESubstitution.h b/alib2data/src/rte/formal/FormalRTESubstitution.h
index 0d4de57e20..c8cee7c513 100644
--- a/alib2data/src/rte/formal/FormalRTESubstitution.h
+++ b/alib2data/src/rte/formal/FormalRTESubstitution.h
@@ -3,6 +3,7 @@
 
 #include "FormalRTEElement.h"
 #include "FormalRTESymbol.h"
+#include "FormalRTE.h"
 
 namespace rte {
 
@@ -22,20 +23,10 @@ protected:
 	FormalRTEElement * right;
 	FormalRTESymbol * substitutionSymbol; // substite this in left by right
 
-	/**
-	 * @copydoc FormalRTEElement::testSymbol() const
-	 */
-	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
-
 	/**
 	 * @copydoc FormalRTEElement::attachRTE()
 	 */
-	virtual bool attachRTE ( const RTEAlphabet * regexp );
-
-	/**
-	 * @copydoc FormalRTEElement::computeMinimalAlphabet()
-	 */
-	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const;
+	virtual bool attachRTE ( const FormalRTE * regexp );
 
 public:
 	void Accept ( void * userData, const FormalRTEElementVisitor & visitor ) const {
@@ -61,6 +52,16 @@ public:
 	 */
 	virtual FormalRTESubstitution * plunder ( ) &&;
 
+	/**
+	 * @copydoc FormalRTEElement::testSymbol() const
+	 */
+	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
+
+	/**
+	 * @copydoc FormalRTEElement::computeMinimalAlphabet()
+	 */
+	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const;
+
 	/**
 	 * @return elements
 	 */
diff --git a/alib2data/src/rte/formal/FormalRTESymbol.cpp b/alib2data/src/rte/formal/FormalRTESymbol.cpp
index 5baaf329fb..c7955055f4 100644
--- a/alib2data/src/rte/formal/FormalRTESymbol.cpp
+++ b/alib2data/src/rte/formal/FormalRTESymbol.cpp
@@ -124,7 +124,7 @@ bool FormalRTESymbol::testSymbol ( const alphabet::RankedSymbol & symbol ) const
 	return symbol == this->symbol;
 }
 
-bool FormalRTESymbol::attachRTE ( const RTEAlphabet * regexp ) {
+bool FormalRTESymbol::attachRTE ( const FormalRTE * regexp ) {
 	if ( this->parentRTE == regexp ) return true;
 
 	this->parentRTE = regexp;
diff --git a/alib2data/src/rte/formal/FormalRTESymbol.h b/alib2data/src/rte/formal/FormalRTESymbol.h
index 0242a00a7a..4dec3933f5 100644
--- a/alib2data/src/rte/formal/FormalRTESymbol.h
+++ b/alib2data/src/rte/formal/FormalRTESymbol.h
@@ -4,6 +4,7 @@
 #include "../../label/Label.h"
 #include "../../alphabet/LabeledSymbol.h"
 #include "FormalRTEElement.h"
+#include "FormalRTE.h"
 
 namespace rte {
 
@@ -21,20 +22,10 @@ protected:
 
 	// virtual UnboundedRTEElement * cloneAsUnbounded ( ) const;
 
-	/**
-	 * @copydoc FormalRTEElement::testSymbol() const
-	 */
-	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
-
 	/**
 	 * @copydoc FormalRTEElement::attachRTE()
 	 */
-	virtual bool attachRTE ( const RTEAlphabet * regexp );
-
-	/**
-	 * @copydoc FormalRTEElement::computeMinimalAlphabet()
-	 */
-	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const;
+	virtual bool attachRTE ( const FormalRTE * regexp );
 
 public:
 	void Accept ( void * userData, const FormalRTEElementVisitor & visitor ) const {
@@ -59,6 +50,16 @@ public:
 	 */
 	virtual FormalRTESymbol * plunder ( ) &&;
 
+	/**
+	 * @copydoc FormalRTEElement::testSymbol() const
+	 */
+	virtual bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
+
+	/**
+	 * @copydoc FormalRTEElement::computeMinimalAlphabet()
+	 */
+	virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const;
+
 	const std::vector < const FormalRTESymbol * > & getElements ( ) const;
 	std::vector < FormalRTESymbol * > & getElements ( );
 
diff --git a/alib2data/test-src/rte/RTETest.cpp b/alib2data/test-src/rte/RTETest.cpp
index 62f98b4f09..0e515c6fd9 100644
--- a/alib2data/test-src/rte/RTETest.cpp
+++ b/alib2data/test-src/rte/RTETest.cpp
@@ -62,8 +62,8 @@ void RTETest::testCopyConstruct ( ) {
 		const alphabet::RankedSymbol symb_z0 ( 'z', 0 );
 
 		rte::FormalRTE frte;
-		frte.setAlphabet ( { symb_a2, symb_b0, symb_c0 } );
-		frte.setConstantAlphabet ( { symb_y0, symb_z0 } );
+		frte.setAlphabetSymbols ( { symb_a2, symb_b0, symb_c0 } );
+		frte.setConstantSymbols ( { symb_y0, symb_z0 } );
 
 		rte::FormalRTESymbol b = rte::FormalRTESymbol ( symb_b0 );
 		rte::FormalRTESymbol c = rte::FormalRTESymbol ( symb_c0 );
@@ -98,8 +98,8 @@ void RTETest::testXMLParser ( ) {
 
 	rte::FormalRTE frte;
 
-	frte.setAlphabet ( { symb_a2, symb_b0, symb_c0, symb_d1 } );
-	frte.setConstantAlphabet ( { symb_y0, symb_z0 } );
+	frte.setAlphabetSymbols ( { symb_a2, symb_b0, symb_c0, symb_d1 } );
+	frte.setConstantSymbols ( { symb_y0, symb_z0 } );
 
 	rte::FormalRTESymbol b = rte::FormalRTESymbol ( symb_b0 );
 	rte::FormalRTESymbol c = rte::FormalRTESymbol ( symb_c0 );
-- 
GitLab