2016-05-29 16:27:11 +02:00
# include <algorithm>
2016-05-20 15:29:24 +02:00
# include <iostream>
2016-05-29 16:27:11 +02:00
# include <string>
2016-05-20 15:29:24 +02:00
# include <boost/program_options.hpp>
2016-06-10 01:23:41 +02:00
# include <plasp/LanguageDetection.h>
2016-05-29 16:27:11 +02:00
# include <plasp/pddl/Description.h>
2016-06-10 17:40:32 +02:00
# include <plasp/pddl/TranslatorASP.h>
2016-05-20 15:29:24 +02:00
# include <plasp/sas/Description.h>
2016-05-21 02:43:07 +02:00
# include <plasp/sas/TranslatorASP.h>
2016-06-14 17:35:32 +02:00
# include <plasp/utils/LogStream.h>
2016-06-12 22:09:47 +02:00
# include <plasp/utils/TranslatorException.h>
2016-05-20 15:29:24 +02:00
int main ( int argc , char * * argv )
{
namespace po = boost : : program_options ;
po : : options_description description ( " Allowed options " ) ;
description . add_options ( )
2016-05-24 16:24:37 +02:00
( " help,h " , " Display this help message. " )
( " version,v " , " Display version information. " )
2016-06-13 15:01:23 +02:00
( " input,i " , po : : value < std : : vector < std : : string > > ( ) , " Specify the PDDL or SAS input file. " )
2016-06-14 18:42:29 +02:00
( " language,l " , po : : value < std : : string > ( ) , " Specify the input language (sas or pddl). Omit for automatic detection. " )
2016-06-14 18:02:59 +02:00
( " warning-level " , po : : value < std : : string > ( ) - > default_value ( " normal " ) , " Specify whether to output warnings normally (normal), to treat them as critical errors (error), or to ignore them (ignore). " )
( " color " , po : : value < std : : string > ( ) - > default_value ( " auto " ) , " Specify whether to colorize the output (always, never, or auto). " ) ;
2016-05-20 15:29:24 +02:00
po : : positional_options_description positionalOptionsDescription ;
positionalOptionsDescription . add ( " input " , - 1 ) ;
po : : variables_map variablesMap ;
2016-05-24 16:24:37 +02:00
const auto printHelp =
[ & ] ( )
{
2016-06-14 18:28:45 +02:00
std : : cout < < " Usage: plasp [files] [options] " < < std : : endl ;
2016-05-24 16:24:37 +02:00
std : : cout < < " Translate PDDL instances to ASP facts. " < < std : : endl < < std : : endl ;
std : : cout < < description ;
} ;
2016-06-14 18:02:59 +02:00
plasp : : utils : : Logger logger ;
2016-05-24 16:24:37 +02:00
try
{
po : : store ( po : : command_line_parser ( argc , argv )
. options ( description )
. positional ( positionalOptionsDescription )
. run ( ) ,
variablesMap ) ;
po : : notify ( variablesMap ) ;
}
catch ( const po : : error & e )
{
2016-06-14 18:02:59 +02:00
logger . logError ( e . what ( ) ) ;
std : : cout < < std : : endl ;
2016-05-24 16:24:37 +02:00
printHelp ( ) ;
return EXIT_FAILURE ;
}
2016-05-20 15:29:24 +02:00
if ( variablesMap . count ( " help " ) )
{
2016-05-24 16:24:37 +02:00
printHelp ( ) ;
return EXIT_SUCCESS ;
}
if ( variablesMap . count ( " version " ) )
{
2016-08-14 17:35:23 +02:00
std : : cout < < " plasp version 3.0.2-rc.1 " < < std : : endl ;
2016-05-20 15:29:24 +02:00
return EXIT_SUCCESS ;
}
2016-06-14 18:02:59 +02:00
const auto warningLevel = variablesMap [ " warning-level " ] . as < std : : string > ( ) ;
if ( warningLevel = = " error " )
logger . setWarningLevel ( plasp : : utils : : Logger : : WarningLevel : : Error ) ;
else if ( warningLevel = = " ignore " )
logger . setWarningLevel ( plasp : : utils : : Logger : : WarningLevel : : Ignore ) ;
2016-06-14 18:47:02 +02:00
else if ( warningLevel = = " normal " )
logger . setWarningLevel ( plasp : : utils : : Logger : : WarningLevel : : Normal ) ;
else
{
logger . logError ( " unknown warning level “ " + warningLevel + " ” " ) ;
std : : cout < < std : : endl ;
printHelp ( ) ;
return EXIT_FAILURE ;
}
2016-06-14 18:02:59 +02:00
const auto colorPolicy = variablesMap [ " color " ] . as < std : : string > ( ) ;
if ( colorPolicy = = " auto " )
logger . setColorPolicy ( plasp : : utils : : LogStream : : ColorPolicy : : Auto ) ;
else if ( colorPolicy = = " never " )
logger . setColorPolicy ( plasp : : utils : : LogStream : : ColorPolicy : : Never ) ;
else if ( colorPolicy = = " always " )
logger . setColorPolicy ( plasp : : utils : : LogStream : : ColorPolicy : : Always ) ;
2016-06-14 18:47:02 +02:00
else
{
logger . logError ( " unknown color policy “ " + colorPolicy + " ” " ) ;
std : : cout < < std : : endl ;
printHelp ( ) ;
return EXIT_FAILURE ;
}
2016-06-14 18:02:59 +02:00
2016-06-10 17:12:23 +02:00
try
2016-06-10 00:59:44 +02:00
{
2016-08-02 19:58:54 +02:00
plasp : : utils : : Parser < plasp : : utils : : CaseInsensitiveParserPolicy > parser ;
2016-06-10 00:59:44 +02:00
2016-06-10 17:12:23 +02:00
if ( variablesMap . count ( " input " ) )
2016-06-10 01:23:41 +02:00
{
2016-06-10 17:12:23 +02:00
const auto & inputFiles = variablesMap [ " input " ] . as < std : : vector < std : : string > > ( ) ;
std : : for_each ( inputFiles . cbegin ( ) , inputFiles . cend ( ) ,
[ & ] ( const auto & inputFile )
{
2016-08-02 19:58:54 +02:00
parser . read ( inputFile ) ;
2016-06-10 17:12:23 +02:00
} ) ;
}
else
2016-08-02 19:58:54 +02:00
parser . read ( " std::cin " , std : : cin ) ;
2016-06-10 17:12:23 +02:00
const auto detectLanguage =
[ & ] ( )
{
if ( variablesMap . count ( " language " ) = = 0 )
return plasp : : detectLanguage ( parser ) ;
2016-05-29 16:27:11 +02:00
2016-06-10 17:12:23 +02:00
const auto languageName = variablesMap [ " language " ] . as < std : : string > ( ) ;
2016-06-10 01:23:41 +02:00
2016-06-10 17:12:23 +02:00
return plasp : : Language : : fromString ( languageName ) ;
} ;
2016-06-10 01:23:41 +02:00
2016-06-10 17:12:23 +02:00
const auto language = detectLanguage ( ) ;
2016-06-10 01:23:41 +02:00
2016-06-10 17:12:23 +02:00
if ( language = = plasp : : Language : : Type : : Unknown )
{
2016-06-14 15:06:43 +02:00
logger . logError ( " unknown input language " ) ;
2016-06-14 12:47:39 +02:00
std : : cout < < std : : endl ;
2016-06-10 17:12:23 +02:00
printHelp ( ) ;
return EXIT_FAILURE ;
}
if ( language = = plasp : : Language : : Type : : PDDL )
2016-06-10 17:40:32 +02:00
{
2016-06-14 19:14:26 +02:00
auto pddlLogger = logger ;
auto context = plasp : : pddl : : Context ( std : : move ( parser ) , std : : move ( pddlLogger ) ) ;
2016-06-14 16:14:39 +02:00
auto description = plasp : : pddl : : Description : : fromContext ( std : : move ( context ) ) ;
const auto translator = plasp : : pddl : : TranslatorASP ( description , description . context ( ) . logger . outputStream ( ) ) ;
2016-06-12 22:31:18 +02:00
translator . translate ( ) ;
2016-06-10 17:40:32 +02:00
}
2016-06-10 17:12:23 +02:00
else if ( language = = plasp : : Language : : Type : : SAS )
{
2016-06-10 17:40:32 +02:00
const auto description = plasp : : sas : : Description : : fromParser ( std : : move ( parser ) ) ;
2016-06-14 18:02:59 +02:00
const auto translator = plasp : : sas : : TranslatorASP ( description , logger . outputStream ( ) ) ;
2016-06-12 22:35:31 +02:00
translator . translate ( ) ;
2016-06-10 17:12:23 +02:00
}
}
2016-06-12 22:09:47 +02:00
catch ( const plasp : : utils : : ParserException & e )
{
2016-06-14 15:06:43 +02:00
logger . logError ( e . coordinate ( ) , e . message ( ) ) ;
2016-06-14 12:47:39 +02:00
return EXIT_FAILURE ;
2016-06-13 14:45:31 +02:00
}
2016-06-12 22:09:47 +02:00
catch ( const plasp : : utils : : TranslatorException & e )
{
2016-06-14 15:06:43 +02:00
logger . logError ( e . what ( ) ) ;
2016-06-14 12:47:39 +02:00
return EXIT_FAILURE ;
2016-06-12 22:09:47 +02:00
}
2016-06-10 17:12:23 +02:00
catch ( const std : : exception & e )
2016-06-10 01:23:41 +02:00
{
2016-06-14 15:06:43 +02:00
logger . logError ( e . what ( ) ) ;
2016-06-14 12:47:39 +02:00
return EXIT_FAILURE ;
2016-06-10 01:23:41 +02:00
}
2016-05-20 15:29:24 +02:00
return EXIT_SUCCESS ;
}