2016-06-10 17:40:32 +02:00
# include <plasp/pddl/TranslatorASP.h>
2016-06-13 02:48:16 +02:00
# include <boost/assert.hpp>
2016-11-29 06:03:05 +01:00
# include <plasp/output/Formatting.h>
# include <plasp/output/TranslatorException.h>
2016-06-12 23:38:44 +02:00
# include <plasp/pddl/expressions/And.h>
# include <plasp/pddl/expressions/Not.h>
2016-06-13 02:38:56 +02:00
# include <plasp/pddl/expressions/Predicate.h>
2016-06-10 17:40:32 +02:00
namespace plasp
{
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TranslatorASP
//
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-12-07 01:56:06 +01:00
template < class T >
void translateVariablesHead ( output : : ColorStream & outputStream , const T & variables ) ;
template < class T >
void translateVariablesBody ( output : : ColorStream & outputStream , const T & variables ) ;
void translateLiteral ( output : : ColorStream & outputStream , const Expression & literal ) ;
void translatePredicate ( output : : ColorStream & outputStream , const expressions : : Predicate & predicate ) ;
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-11-29 06:03:05 +01:00
TranslatorASP : : TranslatorASP ( Description & description , output : : ColorStream & outputStream )
2016-06-12 22:31:18 +02:00
: m_description ( description ) ,
2016-06-14 16:14:39 +02:00
m_outputStream ( outputStream )
2016-06-10 17:40:32 +02:00
{
2016-09-02 16:39:42 +02:00
m_description . normalize ( ) ;
2016-06-10 17:40:32 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-12 22:31:18 +02:00
void TranslatorASP : : translate ( ) const
2016-06-10 17:40:32 +02:00
{
2016-06-12 22:31:18 +02:00
translateDomain ( ) ;
2016-06-10 17:40:32 +02:00
if ( m_description . containsProblem ( ) )
2016-06-12 22:25:12 +02:00
{
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-12 22:31:18 +02:00
translateProblem ( ) ;
2016-06-12 22:25:12 +02:00
}
2016-06-10 17:40:32 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-12 22:31:18 +02:00
void TranslatorASP : : translateDomain ( ) const
2016-06-10 17:40:32 +02:00
{
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Heading1 ( " domain " ) ;
2016-06-10 17:40:32 +02:00
const auto & domain = m_description . domain ( ) ;
// Types
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-14 13:53:09 +02:00
translateTypes ( ) ;
2016-06-10 17:52:19 +02:00
// Constants
2016-06-12 22:19:24 +02:00
if ( ! domain . constants ( ) . empty ( ) )
{
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-14 13:21:41 +02:00
translateConstants ( " constants " , domain . constants ( ) ) ;
2016-06-12 22:19:24 +02:00
}
2016-06-12 22:25:12 +02:00
// Predicates
if ( ! domain . predicates ( ) . empty ( ) )
{
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-12 22:39:04 +02:00
translatePredicates ( ) ;
}
2016-06-12 22:47:39 +02:00
2016-12-07 01:56:06 +01:00
// Derived predicates
if ( ! domain . derivedPredicates ( ) . empty ( ) )
{
m_outputStream < < std : : endl ;
translateDerivedPredicates ( ) ;
}
2016-06-12 22:47:39 +02:00
// Actions
if ( ! domain . actions ( ) . empty ( ) )
{
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-12 22:47:39 +02:00
translateActions ( ) ;
}
2016-06-12 22:39:04 +02:00
}
2016-06-12 22:25:12 +02:00
2016-06-12 22:39:04 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-12 22:25:12 +02:00
2016-06-12 22:39:04 +02:00
void TranslatorASP : : translateTypes ( ) const
{
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Heading2 ( " types " ) ;
2016-06-14 16:38:41 +02:00
m_outputStream < < std : : endl ;
2016-06-12 22:25:12 +02:00
2016-06-12 22:39:04 +02:00
const auto & types = m_description . domain ( ) . types ( ) ;
2016-06-12 22:25:12 +02:00
2016-06-14 13:53:09 +02:00
if ( types . empty ( ) )
{
2016-08-13 02:32:27 +02:00
m_outputStream
2016-11-29 06:03:05 +01:00
< < output : : Function ( " type " ) < < " ( "
< < output : : Keyword ( " type " ) < < " ( " < < output : : String ( " object " ) < < " )). " < < std : : endl ;
2016-08-13 02:32:27 +02:00
2016-06-14 13:53:09 +02:00
return ;
}
2016-06-12 22:39:04 +02:00
std : : for_each ( types . cbegin ( ) , types . cend ( ) ,
[ & ] ( const auto & type )
{
2016-08-13 02:32:27 +02:00
m_outputStream
2016-11-29 06:03:05 +01:00
< < output : : Function ( " type " ) < < " ( "
< < output : : Keyword ( " type " ) < < " ( "
< < output : : String ( type - > name ( ) . c_str ( ) )
2016-08-16 18:35:55 +02:00
< < " )). " < < std : : endl ;
2016-06-12 22:25:12 +02:00
2016-06-12 22:39:04 +02:00
const auto & parentTypes = type - > parentTypes ( ) ;
2016-06-12 22:25:12 +02:00
2016-06-12 22:39:04 +02:00
std : : for_each ( parentTypes . cbegin ( ) , parentTypes . cend ( ) ,
[ & ] ( const auto & parentType )
2016-06-12 22:25:12 +02:00
{
2016-06-14 17:05:41 +02:00
m_outputStream
2016-11-29 06:03:05 +01:00
< < output : : Function ( " inherits " ) < < " ( " < < output : : Keyword ( " type " )
< < " ( " < < output : : String ( type - > name ( ) . c_str ( ) ) < < " ), " < < output : : Keyword ( " type " )
< < " ( " < < output : : String ( parentType - > name ( ) . c_str ( ) ) < < " )). " < < std : : endl ;
2016-06-12 22:39:04 +02:00
} ) ;
} ) ;
2016-08-16 18:35:55 +02:00
m_outputStream
< < std : : endl
2016-11-29 06:03:05 +01:00
< < output : : Function ( " has " ) < < " ( "
< < output : : Variable ( " X " ) < < " , "
< < output : : Keyword ( " type " ) < < " ( " < < output : : Variable ( " T2 " ) < < " )) :- "
< < output : : Function ( " has " ) < < " ( "
< < output : : Variable ( " X " ) < < " , "
< < output : : Keyword ( " type " ) < < " ( " < < output : : Variable ( " T1 " ) < < " )), "
< < output : : Function ( " inherits " ) < < " ( "
< < output : : Keyword ( " type " ) < < " ( " < < output : : Variable ( " T1 " ) < < " ), "
< < output : : Keyword ( " type " ) < < " ( " < < output : : Variable ( " T2 " ) < < " )). "
2016-08-16 18:35:55 +02:00
< < std : : endl ;
2016-06-12 22:39:04 +02:00
}
2016-06-12 22:25:12 +02:00
2016-06-12 22:39:04 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-12 22:25:12 +02:00
2016-06-12 22:39:04 +02:00
void TranslatorASP : : translatePredicates ( ) const
{
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Heading2 ( " variables " ) ;
2016-06-12 22:39:04 +02:00
const auto & predicates = m_description . domain ( ) . predicates ( ) ;
2016-08-13 02:48:30 +02:00
const auto printPredicateName =
2016-06-12 22:39:04 +02:00
[ & ] ( const auto & predicate )
{
2016-08-16 18:35:55 +02:00
if ( predicate - > arguments ( ) . empty ( ) )
{
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : String ( predicate - > name ( ) . c_str ( ) ) ;
2016-06-12 22:39:04 +02:00
2016-08-16 18:35:55 +02:00
return ;
}
2016-11-29 06:03:05 +01:00
m_outputStream < < " ( " < < output : : String ( predicate - > name ( ) . c_str ( ) ) ;
2016-12-07 01:56:06 +01:00
translateVariablesHead ( m_outputStream , predicate - > arguments ( ) ) ;
2016-08-16 18:35:55 +02:00
m_outputStream < < " ) " ;
2016-08-13 02:48:30 +02:00
} ;
2016-08-17 23:31:31 +02:00
std : : for_each ( predicates . cbegin ( ) , predicates . cend ( ) ,
[ & ] ( const auto & predicate )
2016-08-13 02:48:30 +02:00
{
m_outputStream
2016-08-17 23:31:31 +02:00
< < std : : endl
2016-11-29 06:03:05 +01:00
< < output : : Function ( " variable " ) < < " ( "
< < output : : Keyword ( " variable " ) < < " ( " ;
2016-06-13 02:38:56 +02:00
2016-08-13 02:48:30 +02:00
printPredicateName ( predicate ) ;
2016-08-17 23:31:31 +02:00
m_outputStream < < " )) " ;
2016-06-13 02:38:56 +02:00
2016-12-07 01:56:06 +01:00
translateVariablesBody ( m_outputStream , predicate - > arguments ( ) ) ;
m_outputStream < < " . " ;
} ) ;
m_outputStream
< < std : : endl < < std : : endl
< < output : : Function ( " boolean " ) < < " ( " < < output : : Boolean ( " true " ) < < " ). " < < std : : endl
< < output : : Function ( " boolean " ) < < " ( " < < output : : Boolean ( " false " ) < < " ). " < < std : : endl
< < std : : endl
< < output : : Function ( " contains " ) < < " ( "
< < output : : Keyword ( " variable " ) < < " ( " < < output : : Variable ( " X " ) < < " ), "
< < output : : Keyword ( " value " ) < < " ( " < < output : : Variable ( " X " ) < < " , " < < output : : Variable ( " B " ) < < " )) :- "
< < output : : Function ( " variable " ) < < " ( " < < output : : Keyword ( " variable " ) < < " ( " < < output : : Variable ( " X " ) < < " )), "
< < output : : Function ( " boolean " ) < < " ( " < < output : : Variable ( " B " ) < < " ). "
< < std : : endl ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP : : translateDerivedPredicates ( ) const
{
m_outputStream < < output : : Heading2 ( " derived predicates " ) ;
const auto & derivedPredicates = m_description . domain ( ) . derivedPredicates ( ) ;
const auto printDerivedPredicateName =
[ & ] ( const auto & derivedPredicate )
{
if ( derivedPredicate - > parameters ( ) . empty ( ) )
{
// TODO: implement
//m_outputStream << output::String(derivedPredicate->name().c_str());
m_outputStream < < " <derived> " ;
return ;
}
// TODO: implement
//m_outputStream << output::String(derivedPredicate->name().c_str());
m_outputStream < < " <derived> " ;
translateVariablesHead ( m_outputStream , derivedPredicate - > parameters ( ) ) ;
m_outputStream < < " ) " ;
} ;
std : : for_each ( derivedPredicates . cbegin ( ) , derivedPredicates . cend ( ) ,
[ & ] ( const auto & predicate )
{
m_outputStream
< < std : : endl
< < output : : Function ( " variable " ) < < " ( "
< < output : : Keyword ( " variable " ) < < " ( " ;
printDerivedPredicateName ( predicate ) ;
m_outputStream < < " )) " ;
// TODO: implement
//translateVariablesBody(m_outputStream, predicate->arguments());
2016-06-12 22:39:04 +02:00
2016-08-18 00:00:50 +02:00
m_outputStream < < " . " ;
2016-08-13 02:48:30 +02:00
} ) ;
2016-08-18 00:00:50 +02:00
m_outputStream
< < std : : endl < < std : : endl
2016-11-29 06:03:05 +01:00
< < output : : Function ( " boolean " ) < < " ( " < < output : : Boolean ( " true " ) < < " ). " < < std : : endl
< < output : : Function ( " boolean " ) < < " ( " < < output : : Boolean ( " false " ) < < " ). " < < std : : endl
2016-08-18 00:00:50 +02:00
< < std : : endl
2016-11-29 06:03:05 +01:00
< < output : : Function ( " contains " ) < < " ( "
< < output : : Keyword ( " variable " ) < < " ( " < < output : : Variable ( " X " ) < < " ), "
< < output : : Keyword ( " value " ) < < " ( " < < output : : Variable ( " X " ) < < " , " < < output : : Variable ( " B " ) < < " )) :- "
< < output : : Function ( " variable " ) < < " ( " < < output : : Keyword ( " variable " ) < < " ( " < < output : : Variable ( " X " ) < < " )), "
< < output : : Function ( " boolean " ) < < " ( " < < output : : Variable ( " B " ) < < " ). "
2016-08-18 00:00:50 +02:00
< < std : : endl ;
2016-06-12 22:47:39 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP : : translateActions ( ) const
{
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Heading2 ( " actions " ) ;
2016-06-12 22:47:39 +02:00
const auto & actions = m_description . domain ( ) . actions ( ) ;
2016-06-13 02:38:56 +02:00
const auto printActionName =
[ & ] ( const auto & action )
{
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Keyword ( " action " ) < < " ( " ;
2016-06-13 02:38:56 +02:00
2016-08-16 18:35:55 +02:00
if ( action . parameters ( ) . empty ( ) )
{
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : String ( action . name ( ) . c_str ( ) ) < < " ) " ;
2016-06-13 02:38:56 +02:00
2016-08-16 18:35:55 +02:00
return ;
}
2016-11-29 06:03:05 +01:00
m_outputStream < < " ( " < < output : : String ( action . name ( ) . c_str ( ) ) ;
2016-12-07 01:56:06 +01:00
translateVariablesHead ( m_outputStream , action . parameters ( ) ) ;
2016-08-16 18:35:55 +02:00
m_outputStream < < " )) " ;
2016-06-13 02:38:56 +02:00
} ;
2016-06-12 22:47:39 +02:00
std : : for_each ( actions . cbegin ( ) , actions . cend ( ) ,
[ & ] ( const auto & action )
{
2016-12-07 01:56:06 +01:00
const auto translateExpression =
2016-08-13 03:17:48 +02:00
[ & ] ( const auto & ruleHead , const auto & literal , bool enumerateEffects = false )
2016-06-13 02:38:56 +02:00
{
2016-11-29 06:03:05 +01:00
m_outputStream < < std : : endl < < output : : Function ( ruleHead ) < < " ( " ;
2016-06-13 02:38:56 +02:00
printActionName ( * action ) ;
2016-08-14 17:21:41 +02:00
// TODO: implement conditional effects
2016-08-13 03:17:48 +02:00
if ( enumerateEffects )
2016-11-29 06:03:05 +01:00
m_outputStream < < " , " < < output : : Keyword ( " effect " ) < < " ( " < < output : : Reserved ( " unconditional " ) < < " ) " ;
2016-08-13 03:17:48 +02:00
2016-06-14 16:14:39 +02:00
m_outputStream < < " , " ;
2016-06-13 02:38:56 +02:00
2016-12-07 01:56:06 +01:00
translateLiteral ( m_outputStream , literal ) ;
2016-06-13 02:38:56 +02:00
2016-11-29 06:03:05 +01:00
m_outputStream < < " ) :- " < < output : : Function ( " action " ) < < " ( " ;
2016-06-13 02:38:56 +02:00
printActionName ( * action ) ;
2016-08-13 04:04:28 +02:00
m_outputStream < < " ). " ;
2016-06-13 02:38:56 +02:00
} ;
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-13 03:19:24 +02:00
// Name
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Function ( " action " ) < < " ( " ;
2016-06-13 03:19:24 +02:00
printActionName ( * action ) ;
2016-08-13 03:11:38 +02:00
m_outputStream < < " ) " ;
2016-06-13 03:19:24 +02:00
2016-12-07 01:56:06 +01:00
translateVariablesBody ( m_outputStream , action - > parameters ( ) ) ;
2016-06-13 03:19:24 +02:00
2016-06-14 16:14:39 +02:00
m_outputStream < < " . " ;
2016-06-13 03:19:24 +02:00
2016-06-13 02:38:56 +02:00
// Precondition
if ( action - > precondition ( ) )
{
const auto & precondition = * action - > precondition ( ) ;
2016-12-07 01:56:06 +01:00
switch ( precondition . expressionType ( ) )
2016-06-13 02:38:56 +02:00
{
2016-12-07 01:56:06 +01:00
case Expression : : Type : : And :
{
const auto & andExpression = dynamic_cast < const expressions : : And & > ( precondition ) ;
std : : for_each ( andExpression . arguments ( ) . cbegin ( ) , andExpression . arguments ( ) . cend ( ) ,
[ & ] ( const auto argument )
{
translateExpression ( " precondition " , * argument ) ;
} ) ;
break ;
}
case Expression : : Type : : Predicate :
case Expression : : Type : : Not :
case Expression : : Type : : DerivedPredicate :
{
translateExpression ( " precondition " , precondition ) ;
break ;
}
default :
throw output : : TranslatorException ( " only “and” expressions and (negated) predicates supported as action preconditions currently ( " + std : : to_string ( ( int ) precondition . expressionType ( ) ) + " ) " ) ;
2016-06-13 02:38:56 +02:00
}
}
2016-06-13 02:41:23 +02:00
// Effect
if ( action - > effect ( ) )
{
const auto & effect = * action - > effect ( ) ;
if ( effect . expressionType ( ) = = Expression : : Type : : Predicate
| | effect . expressionType ( ) = = Expression : : Type : : Not )
{
2016-12-07 01:56:06 +01:00
translateExpression ( " postcondition " , effect , true ) ;
2016-06-13 02:41:23 +02:00
}
// Assuming a conjunction
else
{
2016-06-13 19:20:00 +02:00
if ( effect . expressionType ( ) ! = Expression : : Type : : And )
2016-11-29 06:03:05 +01:00
throw output : : TranslatorException ( " only “and” expressions and (negated) predicates supported as action effects currently " ) ;
2016-06-13 19:20:00 +02:00
2016-06-13 02:41:23 +02:00
const auto & andExpression = dynamic_cast < const expressions : : And & > ( effect ) ;
std : : for_each ( andExpression . arguments ( ) . cbegin ( ) , andExpression . arguments ( ) . cend ( ) ,
2016-09-04 18:48:46 +02:00
[ & ] ( const auto argument )
2016-06-13 02:41:23 +02:00
{
2016-12-07 01:56:06 +01:00
translateExpression ( " postcondition " , * argument , true ) ;
2016-06-13 02:41:23 +02:00
} ) ;
}
}
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-13 02:38:56 +02:00
} ) ;
2016-06-12 22:55:35 +02:00
}
2016-06-12 22:47:39 +02:00
2016-06-12 22:55:35 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-12 22:47:39 +02:00
2016-06-14 16:38:41 +02:00
void TranslatorASP : : translateConstants ( const std : : string & heading , const expressions : : Constants & constants ) const
2016-06-14 13:21:41 +02:00
{
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Heading2 ( heading . c_str ( ) ) ;
2016-06-14 13:21:41 +02:00
std : : for_each ( constants . cbegin ( ) , constants . cend ( ) ,
[ & ] ( const auto & constant )
{
2016-08-13 03:09:14 +02:00
m_outputStream < < std : : endl
2016-11-29 06:03:05 +01:00
< < output : : Function ( " constant " ) < < " ( "
< < output : : Keyword ( " constant " ) < < " ( "
< < output : : String ( constant - > name ( ) . c_str ( ) )
2016-08-13 03:09:14 +02:00
< < " )). " < < std : : endl ;
2016-06-14 13:21:41 +02:00
2016-09-04 18:26:02 +02:00
const auto type = constant - > type ( ) ;
2016-06-14 13:21:41 +02:00
2016-06-14 13:53:09 +02:00
if ( type ! = nullptr )
2016-06-14 16:38:41 +02:00
{
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Function ( " has " ) < < " ( "
< < output : : Keyword ( " constant " ) < < " ( " < < output : : String ( constant - > name ( ) . c_str ( ) ) < < " ), "
< < output : : Keyword ( " type " ) < < " ( " < < output : : String ( type - > name ( ) . c_str ( ) ) < < " )). " < < std : : endl ;
2016-06-14 16:38:41 +02:00
}
2016-06-14 13:53:09 +02:00
else
2016-06-14 16:38:41 +02:00
{
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Function ( " has " ) < < " ( "
< < output : : Keyword ( " constant " ) < < " ( " < < output : : String ( constant - > name ( ) . c_str ( ) ) < < " ), "
< < output : : Keyword ( " type " ) < < " ( " < < output : : String ( " object " ) < < " )). " < < std : : endl ;
2016-06-14 16:38:41 +02:00
}
2016-06-14 13:21:41 +02:00
} ) ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-12-07 01:56:06 +01:00
template < class T >
void translateVariablesHead ( output : : ColorStream & outputStream , const T & variables )
2016-06-12 22:55:35 +02:00
{
if ( variables . empty ( ) )
return ;
2016-06-12 22:47:39 +02:00
2016-06-12 22:55:35 +02:00
for ( auto i = variables . cbegin ( ) ; i ! = variables . cend ( ) ; i + + )
{
2016-06-13 19:20:00 +02:00
const auto & variable = * * i ;
2016-06-12 22:47:39 +02:00
2016-12-07 01:56:06 +01:00
outputStream < < " , " < < output : : Variable ( variable . name ( ) . c_str ( ) ) ;
2016-06-12 22:55:35 +02:00
}
2016-06-13 02:38:56 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-12-07 01:56:06 +01:00
template < class T >
void translateVariablesBody ( output : : ColorStream & outputStream , const T & variables )
2016-06-13 02:38:56 +02:00
{
if ( variables . empty ( ) )
return ;
2016-12-07 01:56:06 +01:00
outputStream < < " :- " ;
2016-06-12 22:55:35 +02:00
for ( auto i = variables . cbegin ( ) ; i ! = variables . cend ( ) ; i + + )
{
2016-06-14 13:21:41 +02:00
const auto & variable = * * i ;
2016-06-12 22:55:35 +02:00
if ( i ! = variables . cbegin ( ) )
2016-12-07 01:56:06 +01:00
outputStream < < " , " ;
2016-06-12 22:55:35 +02:00
2016-06-13 16:37:35 +02:00
if ( variable . type ( ) ! = nullptr )
{
2016-06-13 19:20:00 +02:00
if ( variable . type ( ) - > expressionType ( ) ! = Expression : : Type : : PrimitiveType )
2016-11-29 06:03:05 +01:00
throw output : : TranslatorException ( " only primitive types supported currently " ) ;
2016-06-13 19:20:00 +02:00
2016-09-04 18:26:02 +02:00
const auto & type = dynamic_cast < const expressions : : PrimitiveType & > ( * variable . type ( ) ) ;
2016-06-13 16:37:35 +02:00
2016-12-07 01:56:06 +01:00
outputStream < < output : : Function ( " has " ) < < " ( "
2016-11-29 06:03:05 +01:00
< < output : : Variable ( variable . name ( ) . c_str ( ) ) < < " , "
< < output : : Keyword ( " type " ) < < " ( " < < output : : String ( type . name ( ) . c_str ( ) ) < < " )) " ;
2016-06-13 16:37:35 +02:00
}
2016-06-14 13:21:41 +02:00
else
2016-06-14 16:38:41 +02:00
{
2016-12-07 01:56:06 +01:00
outputStream < < output : : Function ( " has " ) < < " ( "
2016-11-29 06:03:05 +01:00
< < output : : Variable ( variable . name ( ) . c_str ( ) ) < < " , "
< < output : : Keyword ( " type " ) < < " ( " < < output : : String ( " object " ) < < " )) " ;
2016-06-14 16:38:41 +02:00
}
2016-06-12 22:55:35 +02:00
}
2016-06-10 17:40:32 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-12-07 01:56:06 +01:00
void translateLiteral ( output : : ColorStream & outputStream , const Expression & literal )
2016-06-13 03:19:24 +02:00
{
// Translate single predicate
if ( literal . expressionType ( ) = = Expression : : Type : : Predicate )
{
2016-08-13 03:08:05 +02:00
const auto & predicate = dynamic_cast < const expressions : : Predicate & > ( literal ) ;
2016-12-07 01:56:06 +01:00
outputStream < < output : : Keyword ( " variable " ) < < " ( " ;
translatePredicate ( outputStream , predicate ) ;
outputStream < < " ), " < < output : : Keyword ( " value " ) < < " ( " ;
translatePredicate ( outputStream , predicate ) ;
outputStream < < " , " < < output : : Boolean ( " true " ) < < " ) " ;
2016-06-13 03:19:24 +02:00
}
// Assuming that "not" expression may only contain a predicate
else if ( literal . expressionType ( ) = = Expression : : Type : : Not )
{
const auto & notExpression = dynamic_cast < const expressions : : Not & > ( literal ) ;
2016-08-16 18:43:37 +02:00
2016-08-16 18:58:30 +02:00
if ( notExpression . argument ( ) - > expressionType ( ) ! = Expression : : Type : : Predicate )
2016-11-29 06:03:05 +01:00
throw output : : TranslatorException ( " only negations of primitive predicates supported as literals currently " ) ;
2016-08-16 18:43:37 +02:00
2016-06-13 03:19:24 +02:00
const auto & predicate = dynamic_cast < const expressions : : Predicate & > ( * notExpression . argument ( ) ) ;
2016-12-07 01:56:06 +01:00
outputStream < < output : : Keyword ( " variable " ) < < " ( " ;
translatePredicate ( outputStream , predicate ) ;
outputStream < < " ), " < < output : : Keyword ( " value " ) < < " ( " ;
translatePredicate ( outputStream , predicate ) ;
outputStream < < " , " < < output : : Boolean ( " false " ) < < " ) " ;
}
else if ( literal . expressionType ( ) = = Expression : : Type : : DerivedPredicate )
{
const auto & derivedPredicate = dynamic_cast < const expressions : : DerivedPredicate & > ( literal ) ;
/*m_outputStream << output::Keyword("variable") << "(";
2016-06-13 03:19:24 +02:00
this - > translatePredicate ( predicate ) ;
2016-11-29 06:03:05 +01:00
m_outputStream < < " ), " < < output : : Keyword ( " value " ) < < " ( " ;
2016-08-13 03:08:05 +02:00
this - > translatePredicate ( predicate ) ;
2016-12-07 01:56:06 +01:00
m_outputStream < < " , " < < output : : Boolean ( " true " ) < < " ) " ; */
outputStream < < " (derived predicate) " ;
2016-06-13 03:19:24 +02:00
}
2016-06-13 19:20:00 +02:00
else
2016-11-29 06:03:05 +01:00
throw output : : TranslatorException ( " only primitive predicates and their negations supported as literals currently " ) ;
2016-06-13 03:19:24 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-12-07 01:56:06 +01:00
void translatePredicate ( output : : ColorStream & outputStream , const expressions : : Predicate & predicate )
2016-06-13 02:38:56 +02:00
{
const auto & arguments = predicate . arguments ( ) ;
if ( arguments . empty ( ) )
{
2016-12-07 01:56:06 +01:00
outputStream < < output : : String ( predicate . name ( ) . c_str ( ) ) ;
2016-08-16 18:35:55 +02:00
2016-06-13 02:38:56 +02:00
return ;
}
2016-12-07 01:56:06 +01:00
outputStream < < " ( " < < output : : String ( predicate . name ( ) . c_str ( ) ) ;
2016-06-13 02:38:56 +02:00
for ( auto i = arguments . cbegin ( ) ; i ! = arguments . cend ( ) ; i + + )
{
2016-12-07 01:56:06 +01:00
outputStream < < " , " ;
2016-06-13 02:38:56 +02:00
if ( ( * i ) - > expressionType ( ) = = Expression : : Type : : Constant )
{
const auto & constant = dynamic_cast < const expressions : : Constant & > ( * * i ) ;
2016-12-07 01:56:06 +01:00
outputStream < < output : : Keyword ( " constant " ) < < " ( " < < output : : String ( constant . name ( ) . c_str ( ) ) < < " ) " ;
2016-06-13 02:38:56 +02:00
}
else if ( ( * i ) - > expressionType ( ) = = Expression : : Type : : Variable )
{
const auto & variable = dynamic_cast < const expressions : : Variable & > ( * * i ) ;
2016-12-07 01:56:06 +01:00
outputStream < < output : : Variable ( variable . name ( ) . c_str ( ) ) ;
2016-06-13 02:38:56 +02:00
}
else
2016-11-29 06:03:05 +01:00
throw output : : TranslatorException ( " only variables and constants supported in predicates currently " ) ;
2016-06-13 02:38:56 +02:00
}
2016-12-07 01:56:06 +01:00
outputStream < < " ) " ;
2016-06-13 02:38:56 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-12 22:31:18 +02:00
void TranslatorASP : : translateProblem ( ) const
2016-06-10 17:40:32 +02:00
{
2016-06-13 03:19:24 +02:00
BOOST_ASSERT ( m_description . containsProblem ( ) ) ;
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Heading1 ( " problem " ) ;
2016-06-13 02:48:16 +02:00
const auto & problem = m_description . problem ( ) ;
// Objects
if ( ! problem . objects ( ) . empty ( ) )
{
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-14 13:21:41 +02:00
translateConstants ( " objects " , problem . objects ( ) ) ;
2016-06-13 02:48:16 +02:00
}
2016-06-13 02:59:43 +02:00
// Initial State
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-13 02:59:43 +02:00
translateInitialState ( ) ;
2016-06-13 03:19:24 +02:00
// Goal
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-13 03:19:24 +02:00
translateGoal ( ) ;
2016-06-13 02:48:16 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-13 02:59:43 +02:00
void TranslatorASP : : translateInitialState ( ) const
{
2016-06-13 03:19:24 +02:00
BOOST_ASSERT ( m_description . containsProblem ( ) ) ;
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Heading2 ( " initial state " ) ;
2016-06-13 02:59:43 +02:00
const auto & initialStateFacts = m_description . problem ( ) . initialState ( ) . facts ( ) ;
std : : for_each ( initialStateFacts . cbegin ( ) , initialStateFacts . cend ( ) ,
[ & ] ( const auto & fact )
{
2016-11-29 06:03:05 +01:00
m_outputStream < < std : : endl < < output : : Function ( " initialState " ) < < " ( " ;
2016-06-13 02:59:43 +02:00
// Translate single predicate
if ( fact - > expressionType ( ) = = Expression : : Type : : Predicate )
2016-08-13 03:22:25 +02:00
{
const auto & predicate = dynamic_cast < const expressions : : Predicate & > ( * fact ) ;
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Keyword ( " variable " ) < < " ( " ;
2016-12-07 01:56:06 +01:00
translatePredicate ( m_outputStream , predicate ) ;
2016-11-29 06:03:05 +01:00
m_outputStream < < " ), " < < output : : Keyword ( " value " ) < < " ( " ;
2016-12-07 01:56:06 +01:00
translatePredicate ( m_outputStream , predicate ) ;
2016-11-29 06:03:05 +01:00
m_outputStream < < " , " < < output : : Boolean ( " true " ) < < " ) " ;
2016-08-13 03:22:25 +02:00
}
2016-06-13 02:59:43 +02:00
// Assuming that "not" expression may only contain a predicate
else if ( fact - > expressionType ( ) = = Expression : : Type : : Not )
{
const auto & notExpression = dynamic_cast < const expressions : : Not & > ( * fact ) ;
if ( notExpression . argument ( ) - > expressionType ( ) ! = Expression : : Type : : Predicate )
2016-11-29 06:03:05 +01:00
throw output : : TranslatorException ( " only negations of simple predicates supported in initial state currently " ) ;
2016-06-13 02:59:43 +02:00
}
else
2016-11-29 06:03:05 +01:00
throw output : : TranslatorException ( " only predicates and their negations supported in initial state currently " ) ;
2016-06-13 02:59:43 +02:00
2016-06-14 16:14:39 +02:00
m_outputStream < < " ). " ;
2016-06-13 02:59:43 +02:00
} ) ;
2016-08-17 19:02:51 +02:00
m_outputStream
< < std : : endl < < std : : endl
2016-11-29 06:03:05 +01:00
< < output : : Function ( " initialState " ) < < " ( "
< < output : : Keyword ( " variable " ) < < " ( " < < output : : Variable ( " X " ) < < " ), "
< < output : : Keyword ( " value " ) < < " ( " < < output : : Variable ( " X " ) < < " , " < < output : : Boolean ( " false " ) < < " )) :- "
< < output : : Function ( " variable " ) < < " ( " < < output : : Keyword ( " variable " ) < < " ( " < < output : : Variable ( " X " ) < < " )), "
< < output : : Keyword ( " not " ) < < " "
< < output : : Function ( " initialState " ) < < " ( "
< < output : : Keyword ( " variable " ) < < " ( " < < output : : Variable ( " X " ) < < " ), "
< < output : : Keyword ( " value " ) < < " ( " < < output : : Variable ( " X " ) < < " , " < < output : : Boolean ( " true " ) < < " )). "
2016-08-17 19:02:51 +02:00
< < std : : endl ;
2016-06-13 02:59:43 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-13 03:19:24 +02:00
void TranslatorASP : : translateGoal ( ) const
{
BOOST_ASSERT ( m_description . containsProblem ( ) ) ;
2016-11-29 06:03:05 +01:00
m_outputStream < < output : : Heading2 ( " goal " ) ;
2016-06-13 03:19:24 +02:00
const auto & goal = m_description . problem ( ) . goal ( ) ;
if ( goal . expressionType ( ) = = Expression : : Type : : Predicate
| | goal . expressionType ( ) = = Expression : : Type : : Not )
{
2016-11-29 06:03:05 +01:00
m_outputStream < < std : : endl < < output : : Function ( " goal " ) < < " ( " ;
2016-06-13 03:19:24 +02:00
2016-12-07 01:56:06 +01:00
translateLiteral ( m_outputStream , goal ) ;
2016-06-13 03:19:24 +02:00
2016-06-14 16:14:39 +02:00
m_outputStream < < " ). " ;
2016-06-13 03:19:24 +02:00
}
else if ( goal . expressionType ( ) = = Expression : : Type : : And )
{
const auto & andExpression = dynamic_cast < const expressions : : And & > ( goal ) ;
std : : for_each ( andExpression . arguments ( ) . cbegin ( ) , andExpression . arguments ( ) . cend ( ) ,
2016-09-04 18:48:46 +02:00
[ & ] ( const auto argument )
2016-06-13 03:19:24 +02:00
{
2016-11-29 06:03:05 +01:00
m_outputStream < < std : : endl < < output : : Function ( " goal " ) < < " ( " ;
2016-06-13 03:19:24 +02:00
2016-12-07 01:56:06 +01:00
translateLiteral ( m_outputStream , * argument ) ;
2016-06-13 03:19:24 +02:00
2016-06-14 16:14:39 +02:00
m_outputStream < < " ). " ;
2016-06-13 03:19:24 +02:00
} ) ;
}
else
2016-11-29 06:03:05 +01:00
throw output : : TranslatorException ( " only single predicates, their negations, and conjunctions are currently supported in the goal " ) ;
2016-06-13 03:19:24 +02:00
2016-06-14 16:14:39 +02:00
m_outputStream < < std : : endl ;
2016-06-13 03:19:24 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-10 17:40:32 +02:00
}
}