From 73edebdeb38f7ecbc6c258bfd09ffff3761dde41 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= <peckato1@fit.cvut.cz>
Date: Tue, 4 Mar 2014 22:49:43 +0100
Subject: [PATCH] aconversions fixes and allow to call RG->RE

---
 .../src/aconversion/ConversionHandler.cpp     | 44 ++++++++++++++++---
 .../src/aconversion/ConversionHandler.h       |  4 ++
 2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/aconversions/src/aconversion/ConversionHandler.cpp b/aconversions/src/aconversion/ConversionHandler.cpp
index a17dd35f05..c425143ea4 100644
--- a/aconversions/src/aconversion/ConversionHandler.cpp
+++ b/aconversions/src/aconversion/ConversionHandler.cpp
@@ -21,7 +21,14 @@ ConversionHandler::ConversionHandler( list<Token> & tokens, const string & targe
 {
     m_source = parseFormalismFromString( m_tokens.front( ).getData( ) );
     m_target = parseFormalismFromString( target );
+
+    if( m_source == m_target )
+        throw AlibException( "Cannot convert between same formalisms." );
+
     m_algorithm = parseAlgorithmFromString( algorithm );
+    if( m_algorithm == DEFAULT )
+        m_algorithm = getDefaultAlgorithm( );
+
     if( ! checkAlgorithm( ) )
         throw AlibException( "Invalid conversion. See help." );
 }
@@ -73,7 +80,34 @@ void ConversionHandler::convertGrammar( ostream & out )
     }
     else if( m_target == REGULAR_EXPRESSION )
     {
-        // TODO:
+        AbstractRGtoREConverter* conv;
+        RightRegularGrammar rrg;
+        LeftRegularGrammar lrg;
+        bool rightGrammar = true;
+
+        try
+        {
+            GrammarFactory::buildRightRegularGrammar( ug );
+        }
+        catch( AlibException & e )
+        {
+            rightGrammar = false;
+        }
+
+        if( m_algorithm == BRZOZOWSKI_ALGEBRAIC && rightGrammar )
+        {
+            rrg = GrammarFactory::buildRightRegularGrammar( ug );
+            conv = new RRGAlgebraic( rrg );
+        }
+        else if( m_algorithm == BRZOZOWSKI_ALGEBRAIC && ! rightGrammar )
+        {
+            lrg = GrammarFactory::buildLeftRegularGrammar( ug );
+            conv = new LRGAlgebraic( lrg );
+        }
+        else throw AlibException( "wtf?" );
+
+        conv->convert( ).toXML( out );
+        delete conv;
     }
 }
 void ConversionHandler::convertRegExp( ostream & out )
@@ -158,6 +192,7 @@ ConversionHandler::TAlgorithm ConversionHandler::parseAlgorithmFromString( const
     if( algorithm == "" ) return DEFAULT;
 
     /* FA to RE */
+    /* RG to RE */
     if( algorithm == "elimination" ) return STATE_ELIMINATION;
     if( algorithm == "algebraic" ) return BRZOZOWSKI_ALGEBRAIC;
 
@@ -175,8 +210,6 @@ ConversionHandler::TAlgorithm ConversionHandler::parseAlgorithmFromString( const
     /* RG to FA */
     if( algorithm == "rg" ) return RG_FA;
 
-    /* RG to RE */
-
     throw AlibException( "Invalid conversion. See help." );
 }
 
@@ -187,7 +220,7 @@ ConversionHandler::TAlgorithm ConversionHandler::getDefaultAlgorithm( void ) con
     if( m_source == REGULAR_EXPRESSION && m_target == FINITE_AUTOMATA ) return GLUSHKOV;
     if( m_source == REGULAR_EXPRESSION && m_target == REGULAR_GRAMMAR ) return DEFAULT;
     if( m_source == REGULAR_GRAMMAR && m_target == FINITE_AUTOMATA )    return RG_FA;
-    if( m_source == REGULAR_GRAMMAR && m_target == REGULAR_EXPRESSION ) return DEFAULT;
+    if( m_source == REGULAR_GRAMMAR && m_target == REGULAR_EXPRESSION ) return BRZOZOWSKI_ALGEBRAIC;
 
     throw AlibException( "ConversionHandler::getDefaultAlgorithm - unreachable code" );
 }
@@ -206,7 +239,6 @@ bool ConversionHandler::checkAlgorithm( void ) const
 
     /* RE to FA */
     /* RE to RG */
-
     if( m_source == REGULAR_EXPRESSION )
         return ( m_target == FINITE_AUTOMATA &&
                     ( m_algorithm == BRZOZOWSKI_DERIVATION || m_algorithm == GLUSHKOV || m_algorithm == THOMPSON ) ) ||
@@ -219,7 +251,7 @@ bool ConversionHandler::checkAlgorithm( void ) const
         return ( m_target == FINITE_AUTOMATA &&
                     ( m_algorithm == RG_FA ) ) ||
                ( m_target == REGULAR_EXPRESSION &&
-                    ( false ) );
+                    ( m_algorithm == BRZOZOWSKI_ALGEBRAIC ) );
 
     return false;
 }
diff --git a/aconversions/src/aconversion/ConversionHandler.h b/aconversions/src/aconversion/ConversionHandler.h
index 26fd073dbb..94e13d136e 100644
--- a/aconversions/src/aconversion/ConversionHandler.h
+++ b/aconversions/src/aconversion/ConversionHandler.h
@@ -26,6 +26,8 @@
 #include "../fa2rg/fa2rrg/FAtoRRGConverter.h"
 #include "../rg2fa/LRGtoFAConverter.h"
 #include "../rg2fa/RRGtoFAConverter.h"
+#include "../rg2re/RRGAlgebraic.h"
+#include "../rg2re/LRGAlgebraic.h"
 
 namespace conversions
 {
@@ -56,6 +58,8 @@ public:
         RG_FA,
 
         /* RG to RE */
+        // BRZOZOWSKI_ALGEBRAIC,
+        // STATE_ELIMINATION,
     };
 
     enum TFormalism
-- 
GitLab