Replacing user-defined variable names.
This replaces all user-defined variable names with continuously numbered ones so that they don’t lead to syntax problems in ASP, while still unique and distinguishable. For instance, this avoids problems when variable names contain hyphens, which are allowed in PDDL identifiers but not in ASP variables.
This commit is contained in:
parent
7c02b46f7c
commit
2288fa891e
@ -27,7 +27,7 @@ template<typename PrintObjectName>
|
||||
inline void translateConditionalEffect(colorlog::ColorStream &outputStream,
|
||||
const ::pddl::normalizedAST::ConditionalEffect &conditionalEffect,
|
||||
PrintObjectName printObjectName, VariableStack &variableStack,
|
||||
size_t &numberOfConditionalEffects)
|
||||
size_t &numberOfConditionalEffects, VariableIDMap &variableIDs)
|
||||
{
|
||||
const auto handlePredicate =
|
||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||
@ -38,7 +38,7 @@ inline void translateConditionalEffect(colorlog::ColorStream &outputStream,
|
||||
<< ", " << colorlog::Keyword("effect") << "("
|
||||
<< colorlog::Number<size_t>(numberOfConditionalEffects) << ")"
|
||||
<< ", ";
|
||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
||||
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||
outputStream << ") :- " << colorlog::Function("action") << "(";
|
||||
printObjectName();
|
||||
outputStream << ")";
|
||||
@ -49,7 +49,7 @@ inline void translateConditionalEffect(colorlog::ColorStream &outputStream,
|
||||
if (!layer->empty())
|
||||
outputStream << ", ";
|
||||
|
||||
translateVariablesForRuleBody(outputStream, *layer);
|
||||
translateVariablesForRuleBody(outputStream, *layer, variableIDs);
|
||||
}
|
||||
|
||||
outputStream << ".";
|
||||
|
@ -20,13 +20,13 @@ namespace pddl
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void translateDerivedPredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate);
|
||||
void translateDerivedPredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration);
|
||||
void translateDerivedPredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate, VariableIDMap &variableIDs);
|
||||
void translateDerivedPredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration, VariableIDMap &variableIDs);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// TODO: avoid code duplication with translatePredicate
|
||||
inline void translateDerivedPredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate)
|
||||
inline void translateDerivedPredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate, VariableIDMap &variableIDs)
|
||||
{
|
||||
const auto &arguments = derivedPredicate.arguments;
|
||||
|
||||
@ -51,7 +51,7 @@ inline void translateDerivedPredicate(colorlog::ColorStream &outputStream, const
|
||||
const auto handleVariable =
|
||||
[&](const ::pddl::normalizedAST::VariablePointer &variable)
|
||||
{
|
||||
outputStream << *variable;
|
||||
translateVariable(outputStream, *variable, variableIDs);
|
||||
};
|
||||
|
||||
argument.match(handleConstant, handleVariable);
|
||||
@ -62,7 +62,7 @@ inline void translateDerivedPredicate(colorlog::ColorStream &outputStream, const
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translateDerivedPredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration)
|
||||
inline void translateDerivedPredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration, VariableIDMap &variableIDs)
|
||||
{
|
||||
outputStream << colorlog::Keyword("derivedVariable") << "(";
|
||||
|
||||
@ -73,24 +73,24 @@ inline void translateDerivedPredicateDeclaration(colorlog::ColorStream &outputSt
|
||||
}
|
||||
|
||||
outputStream << "(" << derivedPredicateDeclaration;
|
||||
translateVariablesForRuleHead(outputStream, derivedPredicateDeclaration.parameters);
|
||||
translateVariablesForRuleHead(outputStream, derivedPredicateDeclaration.parameters, variableIDs);
|
||||
outputStream << "))";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translateDerivedPredicateToVariable(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate, bool isPositive = true)
|
||||
inline void translateDerivedPredicateToVariable(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate, VariableIDMap &variableIDs, bool isPositive = true)
|
||||
{
|
||||
outputStream << colorlog::Keyword("derivedVariable") << "(";
|
||||
|
||||
translateDerivedPredicate(outputStream, derivedPredicate);
|
||||
translateDerivedPredicate(outputStream, derivedPredicate, variableIDs);
|
||||
|
||||
outputStream
|
||||
<< "), "
|
||||
<< colorlog::Keyword("value") << "("
|
||||
<< colorlog::Keyword("derivedVariable") << "(";
|
||||
|
||||
translateDerivedPredicate(outputStream, derivedPredicate);
|
||||
translateDerivedPredicate(outputStream, derivedPredicate, variableIDs);
|
||||
|
||||
outputStream << "), ";
|
||||
|
||||
@ -104,11 +104,11 @@ inline void translateDerivedPredicateToVariable(colorlog::ColorStream &outputStr
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translateDerivedPredicateDeclarationToVariable(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration, bool isPositive = true)
|
||||
inline void translateDerivedPredicateDeclarationToVariable(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration, VariableIDMap &variableIDs, bool isPositive = true)
|
||||
{
|
||||
translateDerivedPredicateDeclaration(outputStream, derivedPredicateDeclaration);
|
||||
translateDerivedPredicateDeclaration(outputStream, derivedPredicateDeclaration, variableIDs);
|
||||
outputStream << ", " << colorlog::Keyword("value") << "(";
|
||||
translateDerivedPredicateDeclaration(outputStream, derivedPredicateDeclaration);
|
||||
translateDerivedPredicateDeclaration(outputStream, derivedPredicateDeclaration, variableIDs);
|
||||
outputStream << ", ";
|
||||
|
||||
if (isPositive)
|
||||
|
@ -23,7 +23,9 @@ namespace pddl
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<typename PrintObjectName>
|
||||
inline void translateDerivedPredicatePrecondition(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicatePrecondition &derivedPredicatePrecondition, const std::string &objectType, PrintObjectName printObjectName)
|
||||
inline void translateDerivedPredicatePrecondition(colorlog::ColorStream &outputStream,
|
||||
const ::pddl::normalizedAST::DerivedPredicatePrecondition &derivedPredicatePrecondition,
|
||||
const std::string &objectType, PrintObjectName printObjectName, VariableIDMap &variableIDs)
|
||||
{
|
||||
const auto handlePredicate =
|
||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||
@ -31,7 +33,7 @@ inline void translateDerivedPredicatePrecondition(colorlog::ColorStream &outputS
|
||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||
printObjectName();
|
||||
outputStream << ", ";
|
||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
||||
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||
outputStream << ") :- " << colorlog::Function(objectType.c_str()) << "(";
|
||||
printObjectName();
|
||||
outputStream << ").";
|
||||
@ -49,7 +51,7 @@ inline void translateDerivedPredicatePrecondition(colorlog::ColorStream &outputS
|
||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||
printObjectName();
|
||||
outputStream << ", ";
|
||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, isPositive);
|
||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, variableIDs, isPositive);
|
||||
outputStream << ") :- " << colorlog::Function(objectType.c_str()) << "(";
|
||||
printObjectName();
|
||||
outputStream << ").";
|
||||
|
@ -28,7 +28,7 @@ namespace pddl
|
||||
template<typename PrintObjectName>
|
||||
inline void translateEffect(colorlog::ColorStream &outputStream,
|
||||
const ::pddl::normalizedAST::Effect &effect, PrintObjectName printObjectName,
|
||||
VariableStack &variableStack, size_t &numberOfConditionalEffects)
|
||||
VariableStack &variableStack, size_t &numberOfConditionalEffects, VariableIDMap &variableIDs)
|
||||
{
|
||||
const auto handlePredicate =
|
||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||
@ -39,7 +39,7 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
||||
<< ", " << colorlog::Keyword("effect") << "("
|
||||
<< colorlog::Reserved("unconditional") << ")"
|
||||
<< ", ";
|
||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
||||
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||
outputStream << ") :- " << colorlog::Function("action") << "(";
|
||||
printObjectName();
|
||||
outputStream << ")";
|
||||
@ -50,7 +50,7 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
||||
if (!layer->empty())
|
||||
outputStream << ", ";
|
||||
|
||||
translateVariablesForRuleBody(outputStream, *layer);
|
||||
translateVariablesForRuleBody(outputStream, *layer, variableIDs);
|
||||
}
|
||||
|
||||
outputStream << ".";
|
||||
@ -98,7 +98,7 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
||||
{
|
||||
for (const auto &argument : and_->arguments)
|
||||
translateEffect(outputStream, argument, printObjectName, variableStack,
|
||||
numberOfConditionalEffects);
|
||||
numberOfConditionalEffects, variableIDs);
|
||||
};
|
||||
|
||||
const auto handleForAll =
|
||||
@ -107,7 +107,7 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
||||
variableStack.push(&forAll->parameters);
|
||||
|
||||
translateEffect(outputStream, forAll->argument, printObjectName,
|
||||
variableStack, numberOfConditionalEffects);
|
||||
variableStack, numberOfConditionalEffects, variableIDs);
|
||||
|
||||
variableStack.pop();
|
||||
};
|
||||
@ -137,15 +137,15 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
||||
if (!layer->empty())
|
||||
outputStream << ", ";
|
||||
|
||||
translateVariablesForRuleBody(outputStream, *layer);
|
||||
translateVariablesForRuleBody(outputStream, *layer, variableIDs);
|
||||
}
|
||||
};
|
||||
|
||||
translatePrecondition(outputStream, when->argumentLeft,
|
||||
printConditionalEffectIdentifier, printPreconditionRuleBody);
|
||||
printConditionalEffectIdentifier, printPreconditionRuleBody, variableIDs);
|
||||
|
||||
translateConditionalEffect(outputStream, when->argumentRight, printObjectName,
|
||||
variableStack, numberOfConditionalEffects);
|
||||
variableStack, numberOfConditionalEffects, variableIDs);
|
||||
};
|
||||
|
||||
effect.match(handleAnd, handleForAll, handleLiteral, handleWhen);
|
||||
@ -156,12 +156,12 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
||||
template<typename PrintObjectName>
|
||||
inline void translateEffect(colorlog::ColorStream &outputStream,
|
||||
const ::pddl::normalizedAST::Effect &effect, PrintObjectName printObjectName,
|
||||
size_t &numberOfConditionalEffects)
|
||||
size_t &numberOfConditionalEffects, VariableIDMap &variableIDs)
|
||||
{
|
||||
VariableStack variableStack;
|
||||
|
||||
translateEffect(outputStream, effect, printObjectName, variableStack,
|
||||
numberOfConditionalEffects);
|
||||
numberOfConditionalEffects, variableIDs);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -22,10 +22,12 @@ inline void translateFact(colorlog::ColorStream &outputStream, const ::pddl::nor
|
||||
{
|
||||
outputStream << std::endl << colorlog::Function("initialState") << "(";
|
||||
|
||||
VariableIDMap variableIDs;
|
||||
|
||||
const auto handlePredicate =
|
||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||
{
|
||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
||||
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||
};
|
||||
|
||||
const auto handleNegatedPredicate =
|
||||
|
@ -24,6 +24,8 @@ namespace pddl
|
||||
|
||||
inline void translateGoal(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Goal &goal)
|
||||
{
|
||||
VariableIDMap variableIDs;
|
||||
|
||||
const auto ensureNoVariables =
|
||||
[](const auto &predicate)
|
||||
{
|
||||
@ -38,7 +40,7 @@ inline void translateGoal(colorlog::ColorStream &outputStream, const ::pddl::nor
|
||||
ensureNoVariables(predicate);
|
||||
|
||||
outputStream << std::endl << colorlog::Function("goal") << "(";
|
||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
||||
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||
outputStream << ").";
|
||||
};
|
||||
|
||||
@ -54,7 +56,7 @@ inline void translateGoal(colorlog::ColorStream &outputStream, const ::pddl::nor
|
||||
ensureNoVariables(derivedPredicate);
|
||||
|
||||
outputStream << std::endl << colorlog::Function("goal") << "(";
|
||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, isPositive);
|
||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, variableIDs, isPositive);
|
||||
outputStream << ").";
|
||||
};
|
||||
|
||||
|
@ -25,7 +25,7 @@ namespace pddl
|
||||
template<typename PrintObjectName, typename PrintRuleBody>
|
||||
inline void translatePrecondition(colorlog::ColorStream &outputStream,
|
||||
const ::pddl::normalizedAST::Precondition &precondition, PrintObjectName printObjectName,
|
||||
PrintRuleBody printRuleBody)
|
||||
PrintRuleBody printRuleBody, VariableIDMap &variableIDs)
|
||||
{
|
||||
const auto handlePredicate =
|
||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||
@ -33,7 +33,7 @@ inline void translatePrecondition(colorlog::ColorStream &outputStream,
|
||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||
printObjectName();
|
||||
outputStream << ", ";
|
||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
||||
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||
outputStream << ")";
|
||||
printRuleBody();
|
||||
outputStream << ".";
|
||||
@ -51,7 +51,7 @@ inline void translatePrecondition(colorlog::ColorStream &outputStream,
|
||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||
printObjectName();
|
||||
outputStream << ", ";
|
||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, isPositive);
|
||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, variableIDs, isPositive);
|
||||
outputStream << ")";
|
||||
printRuleBody();
|
||||
outputStream << ".";
|
||||
|
@ -20,12 +20,12 @@ namespace pddl
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate);
|
||||
void translatePredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::PredicateDeclaration &predicateDeclaration);
|
||||
void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate, VariableIDMap &variableIDs);
|
||||
void translatePredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::PredicateDeclaration &predicateDeclaration, VariableIDMap &variableIDs);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate)
|
||||
inline void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate, VariableIDMap &variableIDs)
|
||||
{
|
||||
const auto &arguments = predicate.arguments;
|
||||
|
||||
@ -50,7 +50,7 @@ inline void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl
|
||||
const auto handleVariable =
|
||||
[&](const ::pddl::normalizedAST::VariablePointer &variable)
|
||||
{
|
||||
outputStream << *variable;
|
||||
translateVariable(outputStream, *variable, variableIDs);
|
||||
};
|
||||
|
||||
argument.match(handleConstant, handleVariable);
|
||||
@ -61,7 +61,7 @@ inline void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translatePredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::PredicateDeclaration &predicateDeclaration)
|
||||
inline void translatePredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::PredicateDeclaration &predicateDeclaration, VariableIDMap &variableIDs)
|
||||
{
|
||||
outputStream << colorlog::Keyword("variable") << "(";
|
||||
|
||||
@ -72,24 +72,24 @@ inline void translatePredicateDeclaration(colorlog::ColorStream &outputStream, c
|
||||
}
|
||||
|
||||
outputStream << "(" << predicateDeclaration;
|
||||
translateVariablesForRuleHead(outputStream, predicateDeclaration.parameters);
|
||||
translateVariablesForRuleHead(outputStream, predicateDeclaration.parameters, variableIDs);
|
||||
outputStream << "))";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void translatePredicateToVariable(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate, bool isPositive = true)
|
||||
void translatePredicateToVariable(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate, VariableIDMap &variableIDs, bool isPositive = true)
|
||||
{
|
||||
outputStream << colorlog::Keyword("variable") << "(";
|
||||
|
||||
translatePredicate(outputStream, predicate);
|
||||
translatePredicate(outputStream, predicate, variableIDs);
|
||||
|
||||
outputStream
|
||||
<< "), "
|
||||
<< colorlog::Keyword("value") << "("
|
||||
<< colorlog::Keyword("variable") << "(";
|
||||
|
||||
translatePredicate(outputStream, predicate);
|
||||
translatePredicate(outputStream, predicate, variableIDs);
|
||||
|
||||
outputStream << "), ";
|
||||
|
||||
|
@ -54,31 +54,6 @@ inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, ::pddl::normalizedAST::VariableDeclaration &variableDeclaration)
|
||||
{
|
||||
assert(!variableDeclaration.name.empty());
|
||||
assert(std::isalpha(variableDeclaration.name[0]));
|
||||
|
||||
if (!std::isupper(variableDeclaration.name[0]))
|
||||
variableDeclaration.name[0] = std::toupper(variableDeclaration.name[0]);
|
||||
|
||||
return (stream
|
||||
<< colorlog::Format({colorlog::Color::Green, colorlog::FontWeight::Bold})
|
||||
<< variableDeclaration.name
|
||||
<< colorlog::ResetFormat());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, ::pddl::normalizedAST::Variable &variable)
|
||||
{
|
||||
assert(variable.declaration != nullptr);
|
||||
|
||||
return (stream << *variable.declaration);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// TODO: move to appropriate header
|
||||
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::Action &action)
|
||||
{
|
||||
|
64
include/plasp/pddl/translation/Variable.h
Normal file
64
include/plasp/pddl/translation/Variable.h
Normal file
@ -0,0 +1,64 @@
|
||||
#ifndef __PLASP__PDDL__TRANSLATION__VARIABLE_H
|
||||
#define __PLASP__PDDL__TRANSLATION__VARIABLE_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <colorlog/Formatting.h>
|
||||
|
||||
#include <pddl/NormalizedAST.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Variable
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using VariableIDMap = std::map<const ::pddl::normalizedAST::VariableDeclaration *, size_t>;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translateVariableDeclaration(colorlog::ColorStream &stream, ::pddl::normalizedAST::VariableDeclaration &variableDeclaration, VariableIDMap &variableIDs)
|
||||
{
|
||||
assert(!variableDeclaration.name.empty());
|
||||
|
||||
const auto variableID =
|
||||
[&]()
|
||||
{
|
||||
const auto matchingVariableID = variableIDs.find(&variableDeclaration);
|
||||
|
||||
if (matchingVariableID != variableIDs.cend())
|
||||
return matchingVariableID->second;
|
||||
|
||||
const auto variableID = variableIDs.size() + 1;
|
||||
|
||||
variableIDs.insert({&variableDeclaration, variableID});
|
||||
|
||||
return variableID;
|
||||
};
|
||||
|
||||
stream
|
||||
<< colorlog::Format({colorlog::Color::Green, colorlog::FontWeight::Bold})
|
||||
<< "X" << variableID()
|
||||
<< colorlog::ResetFormat();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translateVariable(colorlog::ColorStream &stream, ::pddl::normalizedAST::Variable &variable, VariableIDMap &variableIDs)
|
||||
{
|
||||
assert(variable.declaration != nullptr);
|
||||
|
||||
translateVariableDeclaration(stream, *variable.declaration, variableIDs);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -7,6 +7,7 @@
|
||||
#include <pddl/Parse.h>
|
||||
|
||||
#include <plasp/TranslatorException.h>
|
||||
#include <plasp/pddl/translation/Variable.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@ -20,23 +21,26 @@ namespace pddl
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class T>
|
||||
void translateVariablesForRuleHead(colorlog::ColorStream &outputStream, const T &variables);
|
||||
void translateVariablesForRuleHead(colorlog::ColorStream &outputStream, const T &variables, VariableIDMap &variableIDs);
|
||||
template<class T>
|
||||
void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, const T &variables);
|
||||
void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, const T &variables, VariableIDMap &variableIDs);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class T>
|
||||
inline void translateVariablesForRuleHead(colorlog::ColorStream &outputStream, const T &variables)
|
||||
inline void translateVariablesForRuleHead(colorlog::ColorStream &outputStream, const T &variables, VariableIDMap &variableIDs)
|
||||
{
|
||||
for (const auto &variable : variables)
|
||||
outputStream << ", " << *variable;
|
||||
{
|
||||
outputStream << ", ";
|
||||
translateVariableDeclaration(outputStream, *variable, variableIDs);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class T>
|
||||
void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, const T &variables)
|
||||
void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, const T &variables, VariableIDMap &variableIDs)
|
||||
{
|
||||
for (const auto &variable : variables)
|
||||
{
|
||||
@ -50,14 +54,15 @@ void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, const T
|
||||
|
||||
const auto &type = variable->type.value().template get<::pddl::normalizedAST::PrimitiveTypePointer>();
|
||||
|
||||
outputStream << colorlog::Function("has") << "("
|
||||
<< *variable << ", " << colorlog::Keyword("type") << "(" << *type << "))";
|
||||
outputStream << colorlog::Function("has") << "(";
|
||||
translateVariableDeclaration(outputStream, *variable, variableIDs);
|
||||
outputStream << ", " << colorlog::Keyword("type") << "(" << *type << "))";
|
||||
}
|
||||
else
|
||||
{
|
||||
outputStream << colorlog::Function("has") << "("
|
||||
<< *variable << ", "
|
||||
<< colorlog::Keyword("type") << "(" << colorlog::String("object") << "))";
|
||||
outputStream << colorlog::Function("has") << "(";
|
||||
translateVariableDeclaration(outputStream, *variable, variableIDs);
|
||||
outputStream << ", " << colorlog::Keyword("type") << "(" << colorlog::String("object") << "))";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -168,16 +168,21 @@ void TranslatorASP::translatePredicates() const
|
||||
|
||||
for (const auto &predicate : predicates)
|
||||
{
|
||||
VariableIDMap variableIDs;
|
||||
|
||||
m_outputStream << std::endl << colorlog::Function("variable") << "(";
|
||||
|
||||
translatePredicateDeclaration(m_outputStream, *predicate);
|
||||
translatePredicateDeclaration(m_outputStream, *predicate, variableIDs);
|
||||
|
||||
m_outputStream << ")";
|
||||
|
||||
if (!predicate->parameters.empty())
|
||||
{
|
||||
m_outputStream << " :- ";
|
||||
translateVariablesForRuleBody(m_outputStream, predicate->parameters);
|
||||
|
||||
VariableIDMap variableIDs;
|
||||
|
||||
translateVariablesForRuleBody(m_outputStream, predicate->parameters, variableIDs);
|
||||
}
|
||||
|
||||
m_outputStream << ".";
|
||||
@ -201,16 +206,18 @@ void TranslatorASP::translateDerivedPredicates(const ::pddl::normalizedAST::Deri
|
||||
|
||||
for (const auto &derivedPredicate : derivedPredicates)
|
||||
{
|
||||
VariableIDMap variableIDs;
|
||||
|
||||
m_outputStream << std::endl << colorlog::Function("derivedVariable") << "(";
|
||||
|
||||
translateDerivedPredicateDeclaration(m_outputStream, *derivedPredicate);
|
||||
translateDerivedPredicateDeclaration(m_outputStream, *derivedPredicate, variableIDs);
|
||||
|
||||
m_outputStream << ")";
|
||||
|
||||
if (!derivedPredicate->parameters.empty())
|
||||
m_outputStream << " :- ";
|
||||
|
||||
translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters);
|
||||
translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters, variableIDs);
|
||||
|
||||
m_outputStream << ".";
|
||||
}
|
||||
@ -226,6 +233,8 @@ void TranslatorASP::translateDerivedPredicates(const ::pddl::normalizedAST::Deri
|
||||
|
||||
for (const auto &derivedPredicate : derivedPredicates)
|
||||
{
|
||||
VariableIDMap variableIDs;
|
||||
|
||||
const auto printDerivedPredicateName =
|
||||
[&]()
|
||||
{
|
||||
@ -239,8 +248,8 @@ void TranslatorASP::translateDerivedPredicates(const ::pddl::normalizedAST::Deri
|
||||
|
||||
m_outputStream << "(" << *derivedPredicate;
|
||||
|
||||
translateVariablesForRuleHead(m_outputStream, derivedPredicate->parameters);
|
||||
translateVariablesForRuleHead(m_outputStream, derivedPredicate->existentialParameters);
|
||||
translateVariablesForRuleHead(m_outputStream, derivedPredicate->parameters, variableIDs);
|
||||
translateVariablesForRuleHead(m_outputStream, derivedPredicate->existentialParameters, variableIDs);
|
||||
|
||||
m_outputStream << ")), " << colorlog::Keyword("type") << "(";
|
||||
|
||||
@ -262,18 +271,18 @@ void TranslatorASP::translateDerivedPredicates(const ::pddl::normalizedAST::Deri
|
||||
if (!derivedPredicate->parameters.empty() || !derivedPredicate->existentialParameters.empty())
|
||||
m_outputStream << " :- ";
|
||||
|
||||
translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters);
|
||||
translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters, variableIDs);
|
||||
|
||||
if (!derivedPredicate->existentialParameters.empty() && !derivedPredicate->parameters.empty())
|
||||
m_outputStream << ", ";
|
||||
|
||||
translateVariablesForRuleBody(m_outputStream, derivedPredicate->existentialParameters);
|
||||
translateVariablesForRuleBody(m_outputStream, derivedPredicate->existentialParameters, variableIDs);
|
||||
|
||||
m_outputStream << ".";
|
||||
|
||||
// Precondition
|
||||
if (derivedPredicate->precondition)
|
||||
translateDerivedPredicatePrecondition(m_outputStream, derivedPredicate->precondition.value(), "derivedPredicate", printDerivedPredicateName);
|
||||
translateDerivedPredicatePrecondition(m_outputStream, derivedPredicate->precondition.value(), "derivedPredicate", printDerivedPredicateName, variableIDs);
|
||||
|
||||
m_outputStream << std::endl << colorlog::Function("postcondition") << "(";
|
||||
printDerivedPredicateName();
|
||||
@ -281,7 +290,7 @@ void TranslatorASP::translateDerivedPredicates(const ::pddl::normalizedAST::Deri
|
||||
<< ", " << colorlog::Keyword("effect") << "("
|
||||
<< colorlog::Reserved("unconditional") << ")"
|
||||
<< ", ";
|
||||
translateDerivedPredicateDeclarationToVariable(m_outputStream, *derivedPredicate, true);
|
||||
translateDerivedPredicateDeclarationToVariable(m_outputStream, *derivedPredicate, variableIDs, true);
|
||||
m_outputStream << ") :- " << colorlog::Function("derivedPredicate") << "(";
|
||||
printDerivedPredicateName();
|
||||
m_outputStream << ").";
|
||||
@ -302,6 +311,8 @@ void TranslatorASP::translateActions() const
|
||||
|
||||
for (const auto &action : actions)
|
||||
{
|
||||
VariableIDMap variableIDs;
|
||||
|
||||
const auto printActionName =
|
||||
[&]()
|
||||
{
|
||||
@ -314,7 +325,7 @@ void TranslatorASP::translateActions() const
|
||||
}
|
||||
|
||||
m_outputStream << "(" << *action;
|
||||
translateVariablesForRuleHead(m_outputStream, action->parameters);
|
||||
translateVariablesForRuleHead(m_outputStream, action->parameters, variableIDs);
|
||||
m_outputStream << "))";
|
||||
};
|
||||
|
||||
@ -336,7 +347,7 @@ void TranslatorASP::translateActions() const
|
||||
if (!action->parameters.empty())
|
||||
{
|
||||
m_outputStream << " :- ";
|
||||
translateVariablesForRuleBody(m_outputStream, action->parameters);
|
||||
translateVariablesForRuleBody(m_outputStream, action->parameters, variableIDs);
|
||||
}
|
||||
|
||||
m_outputStream << ".";
|
||||
@ -344,12 +355,12 @@ void TranslatorASP::translateActions() const
|
||||
// Precondition
|
||||
if (action->precondition)
|
||||
translatePrecondition(m_outputStream, action->precondition.value(), printActionName,
|
||||
printPreconditionRuleBody);
|
||||
printPreconditionRuleBody, variableIDs);
|
||||
|
||||
// Effect
|
||||
if (action->effect)
|
||||
translateEffect(m_outputStream, action->effect.value(), printActionName,
|
||||
numberOfConditionalEffects);
|
||||
numberOfConditionalEffects, variableIDs);
|
||||
|
||||
m_outputStream << std::endl;
|
||||
}
|
||||
|
Reference in New Issue
Block a user