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,
|
inline void translateConditionalEffect(colorlog::ColorStream &outputStream,
|
||||||
const ::pddl::normalizedAST::ConditionalEffect &conditionalEffect,
|
const ::pddl::normalizedAST::ConditionalEffect &conditionalEffect,
|
||||||
PrintObjectName printObjectName, VariableStack &variableStack,
|
PrintObjectName printObjectName, VariableStack &variableStack,
|
||||||
size_t &numberOfConditionalEffects)
|
size_t &numberOfConditionalEffects, VariableIDMap &variableIDs)
|
||||||
{
|
{
|
||||||
const auto handlePredicate =
|
const auto handlePredicate =
|
||||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||||
@ -38,7 +38,7 @@ inline void translateConditionalEffect(colorlog::ColorStream &outputStream,
|
|||||||
<< ", " << colorlog::Keyword("effect") << "("
|
<< ", " << colorlog::Keyword("effect") << "("
|
||||||
<< colorlog::Number<size_t>(numberOfConditionalEffects) << ")"
|
<< colorlog::Number<size_t>(numberOfConditionalEffects) << ")"
|
||||||
<< ", ";
|
<< ", ";
|
||||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||||
outputStream << ") :- " << colorlog::Function("action") << "(";
|
outputStream << ") :- " << colorlog::Function("action") << "(";
|
||||||
printObjectName();
|
printObjectName();
|
||||||
outputStream << ")";
|
outputStream << ")";
|
||||||
@ -49,7 +49,7 @@ inline void translateConditionalEffect(colorlog::ColorStream &outputStream,
|
|||||||
if (!layer->empty())
|
if (!layer->empty())
|
||||||
outputStream << ", ";
|
outputStream << ", ";
|
||||||
|
|
||||||
translateVariablesForRuleBody(outputStream, *layer);
|
translateVariablesForRuleBody(outputStream, *layer, variableIDs);
|
||||||
}
|
}
|
||||||
|
|
||||||
outputStream << ".";
|
outputStream << ".";
|
||||||
|
@ -20,13 +20,13 @@ namespace pddl
|
|||||||
//
|
//
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void translateDerivedPredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate);
|
void translateDerivedPredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate, VariableIDMap &variableIDs);
|
||||||
void translateDerivedPredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration);
|
void translateDerivedPredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration, VariableIDMap &variableIDs);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// TODO: avoid code duplication with translatePredicate
|
// 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;
|
const auto &arguments = derivedPredicate.arguments;
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ inline void translateDerivedPredicate(colorlog::ColorStream &outputStream, const
|
|||||||
const auto handleVariable =
|
const auto handleVariable =
|
||||||
[&](const ::pddl::normalizedAST::VariablePointer &variable)
|
[&](const ::pddl::normalizedAST::VariablePointer &variable)
|
||||||
{
|
{
|
||||||
outputStream << *variable;
|
translateVariable(outputStream, *variable, variableIDs);
|
||||||
};
|
};
|
||||||
|
|
||||||
argument.match(handleConstant, handleVariable);
|
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") << "(";
|
outputStream << colorlog::Keyword("derivedVariable") << "(";
|
||||||
|
|
||||||
@ -73,24 +73,24 @@ inline void translateDerivedPredicateDeclaration(colorlog::ColorStream &outputSt
|
|||||||
}
|
}
|
||||||
|
|
||||||
outputStream << "(" << derivedPredicateDeclaration;
|
outputStream << "(" << derivedPredicateDeclaration;
|
||||||
translateVariablesForRuleHead(outputStream, derivedPredicateDeclaration.parameters);
|
translateVariablesForRuleHead(outputStream, derivedPredicateDeclaration.parameters, variableIDs);
|
||||||
outputStream << "))";
|
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") << "(";
|
outputStream << colorlog::Keyword("derivedVariable") << "(";
|
||||||
|
|
||||||
translateDerivedPredicate(outputStream, derivedPredicate);
|
translateDerivedPredicate(outputStream, derivedPredicate, variableIDs);
|
||||||
|
|
||||||
outputStream
|
outputStream
|
||||||
<< "), "
|
<< "), "
|
||||||
<< colorlog::Keyword("value") << "("
|
<< colorlog::Keyword("value") << "("
|
||||||
<< colorlog::Keyword("derivedVariable") << "(";
|
<< colorlog::Keyword("derivedVariable") << "(";
|
||||||
|
|
||||||
translateDerivedPredicate(outputStream, derivedPredicate);
|
translateDerivedPredicate(outputStream, derivedPredicate, variableIDs);
|
||||||
|
|
||||||
outputStream << "), ";
|
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") << "(";
|
outputStream << ", " << colorlog::Keyword("value") << "(";
|
||||||
translateDerivedPredicateDeclaration(outputStream, derivedPredicateDeclaration);
|
translateDerivedPredicateDeclaration(outputStream, derivedPredicateDeclaration, variableIDs);
|
||||||
outputStream << ", ";
|
outputStream << ", ";
|
||||||
|
|
||||||
if (isPositive)
|
if (isPositive)
|
||||||
|
@ -23,7 +23,9 @@ namespace pddl
|
|||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
template<typename PrintObjectName>
|
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 auto handlePredicate =
|
||||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||||
@ -31,7 +33,7 @@ inline void translateDerivedPredicatePrecondition(colorlog::ColorStream &outputS
|
|||||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||||
printObjectName();
|
printObjectName();
|
||||||
outputStream << ", ";
|
outputStream << ", ";
|
||||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||||
outputStream << ") :- " << colorlog::Function(objectType.c_str()) << "(";
|
outputStream << ") :- " << colorlog::Function(objectType.c_str()) << "(";
|
||||||
printObjectName();
|
printObjectName();
|
||||||
outputStream << ").";
|
outputStream << ").";
|
||||||
@ -49,7 +51,7 @@ inline void translateDerivedPredicatePrecondition(colorlog::ColorStream &outputS
|
|||||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||||
printObjectName();
|
printObjectName();
|
||||||
outputStream << ", ";
|
outputStream << ", ";
|
||||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, isPositive);
|
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, variableIDs, isPositive);
|
||||||
outputStream << ") :- " << colorlog::Function(objectType.c_str()) << "(";
|
outputStream << ") :- " << colorlog::Function(objectType.c_str()) << "(";
|
||||||
printObjectName();
|
printObjectName();
|
||||||
outputStream << ").";
|
outputStream << ").";
|
||||||
|
@ -28,7 +28,7 @@ namespace pddl
|
|||||||
template<typename PrintObjectName>
|
template<typename PrintObjectName>
|
||||||
inline void translateEffect(colorlog::ColorStream &outputStream,
|
inline void translateEffect(colorlog::ColorStream &outputStream,
|
||||||
const ::pddl::normalizedAST::Effect &effect, PrintObjectName printObjectName,
|
const ::pddl::normalizedAST::Effect &effect, PrintObjectName printObjectName,
|
||||||
VariableStack &variableStack, size_t &numberOfConditionalEffects)
|
VariableStack &variableStack, size_t &numberOfConditionalEffects, VariableIDMap &variableIDs)
|
||||||
{
|
{
|
||||||
const auto handlePredicate =
|
const auto handlePredicate =
|
||||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||||
@ -39,7 +39,7 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
|||||||
<< ", " << colorlog::Keyword("effect") << "("
|
<< ", " << colorlog::Keyword("effect") << "("
|
||||||
<< colorlog::Reserved("unconditional") << ")"
|
<< colorlog::Reserved("unconditional") << ")"
|
||||||
<< ", ";
|
<< ", ";
|
||||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||||
outputStream << ") :- " << colorlog::Function("action") << "(";
|
outputStream << ") :- " << colorlog::Function("action") << "(";
|
||||||
printObjectName();
|
printObjectName();
|
||||||
outputStream << ")";
|
outputStream << ")";
|
||||||
@ -50,7 +50,7 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
|||||||
if (!layer->empty())
|
if (!layer->empty())
|
||||||
outputStream << ", ";
|
outputStream << ", ";
|
||||||
|
|
||||||
translateVariablesForRuleBody(outputStream, *layer);
|
translateVariablesForRuleBody(outputStream, *layer, variableIDs);
|
||||||
}
|
}
|
||||||
|
|
||||||
outputStream << ".";
|
outputStream << ".";
|
||||||
@ -98,7 +98,7 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
|||||||
{
|
{
|
||||||
for (const auto &argument : and_->arguments)
|
for (const auto &argument : and_->arguments)
|
||||||
translateEffect(outputStream, argument, printObjectName, variableStack,
|
translateEffect(outputStream, argument, printObjectName, variableStack,
|
||||||
numberOfConditionalEffects);
|
numberOfConditionalEffects, variableIDs);
|
||||||
};
|
};
|
||||||
|
|
||||||
const auto handleForAll =
|
const auto handleForAll =
|
||||||
@ -107,7 +107,7 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
|||||||
variableStack.push(&forAll->parameters);
|
variableStack.push(&forAll->parameters);
|
||||||
|
|
||||||
translateEffect(outputStream, forAll->argument, printObjectName,
|
translateEffect(outputStream, forAll->argument, printObjectName,
|
||||||
variableStack, numberOfConditionalEffects);
|
variableStack, numberOfConditionalEffects, variableIDs);
|
||||||
|
|
||||||
variableStack.pop();
|
variableStack.pop();
|
||||||
};
|
};
|
||||||
@ -137,15 +137,15 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
|||||||
if (!layer->empty())
|
if (!layer->empty())
|
||||||
outputStream << ", ";
|
outputStream << ", ";
|
||||||
|
|
||||||
translateVariablesForRuleBody(outputStream, *layer);
|
translateVariablesForRuleBody(outputStream, *layer, variableIDs);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
translatePrecondition(outputStream, when->argumentLeft,
|
translatePrecondition(outputStream, when->argumentLeft,
|
||||||
printConditionalEffectIdentifier, printPreconditionRuleBody);
|
printConditionalEffectIdentifier, printPreconditionRuleBody, variableIDs);
|
||||||
|
|
||||||
translateConditionalEffect(outputStream, when->argumentRight, printObjectName,
|
translateConditionalEffect(outputStream, when->argumentRight, printObjectName,
|
||||||
variableStack, numberOfConditionalEffects);
|
variableStack, numberOfConditionalEffects, variableIDs);
|
||||||
};
|
};
|
||||||
|
|
||||||
effect.match(handleAnd, handleForAll, handleLiteral, handleWhen);
|
effect.match(handleAnd, handleForAll, handleLiteral, handleWhen);
|
||||||
@ -156,12 +156,12 @@ inline void translateEffect(colorlog::ColorStream &outputStream,
|
|||||||
template<typename PrintObjectName>
|
template<typename PrintObjectName>
|
||||||
inline void translateEffect(colorlog::ColorStream &outputStream,
|
inline void translateEffect(colorlog::ColorStream &outputStream,
|
||||||
const ::pddl::normalizedAST::Effect &effect, PrintObjectName printObjectName,
|
const ::pddl::normalizedAST::Effect &effect, PrintObjectName printObjectName,
|
||||||
size_t &numberOfConditionalEffects)
|
size_t &numberOfConditionalEffects, VariableIDMap &variableIDs)
|
||||||
{
|
{
|
||||||
VariableStack variableStack;
|
VariableStack variableStack;
|
||||||
|
|
||||||
translateEffect(outputStream, effect, printObjectName, 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") << "(";
|
outputStream << std::endl << colorlog::Function("initialState") << "(";
|
||||||
|
|
||||||
|
VariableIDMap variableIDs;
|
||||||
|
|
||||||
const auto handlePredicate =
|
const auto handlePredicate =
|
||||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||||
{
|
{
|
||||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||||
};
|
};
|
||||||
|
|
||||||
const auto handleNegatedPredicate =
|
const auto handleNegatedPredicate =
|
||||||
|
@ -24,6 +24,8 @@ namespace pddl
|
|||||||
|
|
||||||
inline void translateGoal(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Goal &goal)
|
inline void translateGoal(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Goal &goal)
|
||||||
{
|
{
|
||||||
|
VariableIDMap variableIDs;
|
||||||
|
|
||||||
const auto ensureNoVariables =
|
const auto ensureNoVariables =
|
||||||
[](const auto &predicate)
|
[](const auto &predicate)
|
||||||
{
|
{
|
||||||
@ -38,7 +40,7 @@ inline void translateGoal(colorlog::ColorStream &outputStream, const ::pddl::nor
|
|||||||
ensureNoVariables(predicate);
|
ensureNoVariables(predicate);
|
||||||
|
|
||||||
outputStream << std::endl << colorlog::Function("goal") << "(";
|
outputStream << std::endl << colorlog::Function("goal") << "(";
|
||||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||||
outputStream << ").";
|
outputStream << ").";
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -54,7 +56,7 @@ inline void translateGoal(colorlog::ColorStream &outputStream, const ::pddl::nor
|
|||||||
ensureNoVariables(derivedPredicate);
|
ensureNoVariables(derivedPredicate);
|
||||||
|
|
||||||
outputStream << std::endl << colorlog::Function("goal") << "(";
|
outputStream << std::endl << colorlog::Function("goal") << "(";
|
||||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, isPositive);
|
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, variableIDs, isPositive);
|
||||||
outputStream << ").";
|
outputStream << ").";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ namespace pddl
|
|||||||
template<typename PrintObjectName, typename PrintRuleBody>
|
template<typename PrintObjectName, typename PrintRuleBody>
|
||||||
inline void translatePrecondition(colorlog::ColorStream &outputStream,
|
inline void translatePrecondition(colorlog::ColorStream &outputStream,
|
||||||
const ::pddl::normalizedAST::Precondition &precondition, PrintObjectName printObjectName,
|
const ::pddl::normalizedAST::Precondition &precondition, PrintObjectName printObjectName,
|
||||||
PrintRuleBody printRuleBody)
|
PrintRuleBody printRuleBody, VariableIDMap &variableIDs)
|
||||||
{
|
{
|
||||||
const auto handlePredicate =
|
const auto handlePredicate =
|
||||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||||
@ -33,7 +33,7 @@ inline void translatePrecondition(colorlog::ColorStream &outputStream,
|
|||||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||||
printObjectName();
|
printObjectName();
|
||||||
outputStream << ", ";
|
outputStream << ", ";
|
||||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
translatePredicateToVariable(outputStream, *predicate, variableIDs, isPositive);
|
||||||
outputStream << ")";
|
outputStream << ")";
|
||||||
printRuleBody();
|
printRuleBody();
|
||||||
outputStream << ".";
|
outputStream << ".";
|
||||||
@ -51,7 +51,7 @@ inline void translatePrecondition(colorlog::ColorStream &outputStream,
|
|||||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||||
printObjectName();
|
printObjectName();
|
||||||
outputStream << ", ";
|
outputStream << ", ";
|
||||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, isPositive);
|
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, variableIDs, isPositive);
|
||||||
outputStream << ")";
|
outputStream << ")";
|
||||||
printRuleBody();
|
printRuleBody();
|
||||||
outputStream << ".";
|
outputStream << ".";
|
||||||
|
@ -20,12 +20,12 @@ namespace pddl
|
|||||||
//
|
//
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate);
|
void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate, VariableIDMap &variableIDs);
|
||||||
void translatePredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::PredicateDeclaration &predicateDeclaration);
|
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;
|
const auto &arguments = predicate.arguments;
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ inline void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl
|
|||||||
const auto handleVariable =
|
const auto handleVariable =
|
||||||
[&](const ::pddl::normalizedAST::VariablePointer &variable)
|
[&](const ::pddl::normalizedAST::VariablePointer &variable)
|
||||||
{
|
{
|
||||||
outputStream << *variable;
|
translateVariable(outputStream, *variable, variableIDs);
|
||||||
};
|
};
|
||||||
|
|
||||||
argument.match(handleConstant, handleVariable);
|
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") << "(";
|
outputStream << colorlog::Keyword("variable") << "(";
|
||||||
|
|
||||||
@ -72,24 +72,24 @@ inline void translatePredicateDeclaration(colorlog::ColorStream &outputStream, c
|
|||||||
}
|
}
|
||||||
|
|
||||||
outputStream << "(" << predicateDeclaration;
|
outputStream << "(" << predicateDeclaration;
|
||||||
translateVariablesForRuleHead(outputStream, predicateDeclaration.parameters);
|
translateVariablesForRuleHead(outputStream, predicateDeclaration.parameters, variableIDs);
|
||||||
outputStream << "))";
|
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") << "(";
|
outputStream << colorlog::Keyword("variable") << "(";
|
||||||
|
|
||||||
translatePredicate(outputStream, predicate);
|
translatePredicate(outputStream, predicate, variableIDs);
|
||||||
|
|
||||||
outputStream
|
outputStream
|
||||||
<< "), "
|
<< "), "
|
||||||
<< colorlog::Keyword("value") << "("
|
<< colorlog::Keyword("value") << "("
|
||||||
<< colorlog::Keyword("variable") << "(";
|
<< colorlog::Keyword("variable") << "(";
|
||||||
|
|
||||||
translatePredicate(outputStream, predicate);
|
translatePredicate(outputStream, predicate, variableIDs);
|
||||||
|
|
||||||
outputStream << "), ";
|
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
|
// TODO: move to appropriate header
|
||||||
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::Action &action)
|
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 <pddl/Parse.h>
|
||||||
|
|
||||||
#include <plasp/TranslatorException.h>
|
#include <plasp/TranslatorException.h>
|
||||||
|
#include <plasp/pddl/translation/Variable.h>
|
||||||
|
|
||||||
namespace plasp
|
namespace plasp
|
||||||
{
|
{
|
||||||
@ -20,23 +21,26 @@ namespace pddl
|
|||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
void translateVariablesForRuleHead(colorlog::ColorStream &outputStream, const T &variables);
|
void translateVariablesForRuleHead(colorlog::ColorStream &outputStream, const T &variables, VariableIDMap &variableIDs);
|
||||||
template<class T>
|
template<class T>
|
||||||
void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, const T &variables);
|
void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, const T &variables, VariableIDMap &variableIDs);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
template<class T>
|
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)
|
for (const auto &variable : variables)
|
||||||
outputStream << ", " << *variable;
|
{
|
||||||
|
outputStream << ", ";
|
||||||
|
translateVariableDeclaration(outputStream, *variable, variableIDs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
template<class T>
|
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)
|
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>();
|
const auto &type = variable->type.value().template get<::pddl::normalizedAST::PrimitiveTypePointer>();
|
||||||
|
|
||||||
outputStream << colorlog::Function("has") << "("
|
outputStream << colorlog::Function("has") << "(";
|
||||||
<< *variable << ", " << colorlog::Keyword("type") << "(" << *type << "))";
|
translateVariableDeclaration(outputStream, *variable, variableIDs);
|
||||||
|
outputStream << ", " << colorlog::Keyword("type") << "(" << *type << "))";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
outputStream << colorlog::Function("has") << "("
|
outputStream << colorlog::Function("has") << "(";
|
||||||
<< *variable << ", "
|
translateVariableDeclaration(outputStream, *variable, variableIDs);
|
||||||
<< colorlog::Keyword("type") << "(" << colorlog::String("object") << "))";
|
outputStream << ", " << colorlog::Keyword("type") << "(" << colorlog::String("object") << "))";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,16 +168,21 @@ void TranslatorASP::translatePredicates() const
|
|||||||
|
|
||||||
for (const auto &predicate : predicates)
|
for (const auto &predicate : predicates)
|
||||||
{
|
{
|
||||||
|
VariableIDMap variableIDs;
|
||||||
|
|
||||||
m_outputStream << std::endl << colorlog::Function("variable") << "(";
|
m_outputStream << std::endl << colorlog::Function("variable") << "(";
|
||||||
|
|
||||||
translatePredicateDeclaration(m_outputStream, *predicate);
|
translatePredicateDeclaration(m_outputStream, *predicate, variableIDs);
|
||||||
|
|
||||||
m_outputStream << ")";
|
m_outputStream << ")";
|
||||||
|
|
||||||
if (!predicate->parameters.empty())
|
if (!predicate->parameters.empty())
|
||||||
{
|
{
|
||||||
m_outputStream << " :- ";
|
m_outputStream << " :- ";
|
||||||
translateVariablesForRuleBody(m_outputStream, predicate->parameters);
|
|
||||||
|
VariableIDMap variableIDs;
|
||||||
|
|
||||||
|
translateVariablesForRuleBody(m_outputStream, predicate->parameters, variableIDs);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_outputStream << ".";
|
m_outputStream << ".";
|
||||||
@ -201,16 +206,18 @@ void TranslatorASP::translateDerivedPredicates(const ::pddl::normalizedAST::Deri
|
|||||||
|
|
||||||
for (const auto &derivedPredicate : derivedPredicates)
|
for (const auto &derivedPredicate : derivedPredicates)
|
||||||
{
|
{
|
||||||
|
VariableIDMap variableIDs;
|
||||||
|
|
||||||
m_outputStream << std::endl << colorlog::Function("derivedVariable") << "(";
|
m_outputStream << std::endl << colorlog::Function("derivedVariable") << "(";
|
||||||
|
|
||||||
translateDerivedPredicateDeclaration(m_outputStream, *derivedPredicate);
|
translateDerivedPredicateDeclaration(m_outputStream, *derivedPredicate, variableIDs);
|
||||||
|
|
||||||
m_outputStream << ")";
|
m_outputStream << ")";
|
||||||
|
|
||||||
if (!derivedPredicate->parameters.empty())
|
if (!derivedPredicate->parameters.empty())
|
||||||
m_outputStream << " :- ";
|
m_outputStream << " :- ";
|
||||||
|
|
||||||
translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters);
|
translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters, variableIDs);
|
||||||
|
|
||||||
m_outputStream << ".";
|
m_outputStream << ".";
|
||||||
}
|
}
|
||||||
@ -226,6 +233,8 @@ void TranslatorASP::translateDerivedPredicates(const ::pddl::normalizedAST::Deri
|
|||||||
|
|
||||||
for (const auto &derivedPredicate : derivedPredicates)
|
for (const auto &derivedPredicate : derivedPredicates)
|
||||||
{
|
{
|
||||||
|
VariableIDMap variableIDs;
|
||||||
|
|
||||||
const auto printDerivedPredicateName =
|
const auto printDerivedPredicateName =
|
||||||
[&]()
|
[&]()
|
||||||
{
|
{
|
||||||
@ -239,8 +248,8 @@ void TranslatorASP::translateDerivedPredicates(const ::pddl::normalizedAST::Deri
|
|||||||
|
|
||||||
m_outputStream << "(" << *derivedPredicate;
|
m_outputStream << "(" << *derivedPredicate;
|
||||||
|
|
||||||
translateVariablesForRuleHead(m_outputStream, derivedPredicate->parameters);
|
translateVariablesForRuleHead(m_outputStream, derivedPredicate->parameters, variableIDs);
|
||||||
translateVariablesForRuleHead(m_outputStream, derivedPredicate->existentialParameters);
|
translateVariablesForRuleHead(m_outputStream, derivedPredicate->existentialParameters, variableIDs);
|
||||||
|
|
||||||
m_outputStream << ")), " << colorlog::Keyword("type") << "(";
|
m_outputStream << ")), " << colorlog::Keyword("type") << "(";
|
||||||
|
|
||||||
@ -262,18 +271,18 @@ void TranslatorASP::translateDerivedPredicates(const ::pddl::normalizedAST::Deri
|
|||||||
if (!derivedPredicate->parameters.empty() || !derivedPredicate->existentialParameters.empty())
|
if (!derivedPredicate->parameters.empty() || !derivedPredicate->existentialParameters.empty())
|
||||||
m_outputStream << " :- ";
|
m_outputStream << " :- ";
|
||||||
|
|
||||||
translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters);
|
translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters, variableIDs);
|
||||||
|
|
||||||
if (!derivedPredicate->existentialParameters.empty() && !derivedPredicate->parameters.empty())
|
if (!derivedPredicate->existentialParameters.empty() && !derivedPredicate->parameters.empty())
|
||||||
m_outputStream << ", ";
|
m_outputStream << ", ";
|
||||||
|
|
||||||
translateVariablesForRuleBody(m_outputStream, derivedPredicate->existentialParameters);
|
translateVariablesForRuleBody(m_outputStream, derivedPredicate->existentialParameters, variableIDs);
|
||||||
|
|
||||||
m_outputStream << ".";
|
m_outputStream << ".";
|
||||||
|
|
||||||
// Precondition
|
// Precondition
|
||||||
if (derivedPredicate->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") << "(";
|
m_outputStream << std::endl << colorlog::Function("postcondition") << "(";
|
||||||
printDerivedPredicateName();
|
printDerivedPredicateName();
|
||||||
@ -281,7 +290,7 @@ void TranslatorASP::translateDerivedPredicates(const ::pddl::normalizedAST::Deri
|
|||||||
<< ", " << colorlog::Keyword("effect") << "("
|
<< ", " << colorlog::Keyword("effect") << "("
|
||||||
<< colorlog::Reserved("unconditional") << ")"
|
<< colorlog::Reserved("unconditional") << ")"
|
||||||
<< ", ";
|
<< ", ";
|
||||||
translateDerivedPredicateDeclarationToVariable(m_outputStream, *derivedPredicate, true);
|
translateDerivedPredicateDeclarationToVariable(m_outputStream, *derivedPredicate, variableIDs, true);
|
||||||
m_outputStream << ") :- " << colorlog::Function("derivedPredicate") << "(";
|
m_outputStream << ") :- " << colorlog::Function("derivedPredicate") << "(";
|
||||||
printDerivedPredicateName();
|
printDerivedPredicateName();
|
||||||
m_outputStream << ").";
|
m_outputStream << ").";
|
||||||
@ -302,6 +311,8 @@ void TranslatorASP::translateActions() const
|
|||||||
|
|
||||||
for (const auto &action : actions)
|
for (const auto &action : actions)
|
||||||
{
|
{
|
||||||
|
VariableIDMap variableIDs;
|
||||||
|
|
||||||
const auto printActionName =
|
const auto printActionName =
|
||||||
[&]()
|
[&]()
|
||||||
{
|
{
|
||||||
@ -314,7 +325,7 @@ void TranslatorASP::translateActions() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_outputStream << "(" << *action;
|
m_outputStream << "(" << *action;
|
||||||
translateVariablesForRuleHead(m_outputStream, action->parameters);
|
translateVariablesForRuleHead(m_outputStream, action->parameters, variableIDs);
|
||||||
m_outputStream << "))";
|
m_outputStream << "))";
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -336,7 +347,7 @@ void TranslatorASP::translateActions() const
|
|||||||
if (!action->parameters.empty())
|
if (!action->parameters.empty())
|
||||||
{
|
{
|
||||||
m_outputStream << " :- ";
|
m_outputStream << " :- ";
|
||||||
translateVariablesForRuleBody(m_outputStream, action->parameters);
|
translateVariablesForRuleBody(m_outputStream, action->parameters, variableIDs);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_outputStream << ".";
|
m_outputStream << ".";
|
||||||
@ -344,12 +355,12 @@ void TranslatorASP::translateActions() const
|
|||||||
// Precondition
|
// Precondition
|
||||||
if (action->precondition)
|
if (action->precondition)
|
||||||
translatePrecondition(m_outputStream, action->precondition.value(), printActionName,
|
translatePrecondition(m_outputStream, action->precondition.value(), printActionName,
|
||||||
printPreconditionRuleBody);
|
printPreconditionRuleBody, variableIDs);
|
||||||
|
|
||||||
// Effect
|
// Effect
|
||||||
if (action->effect)
|
if (action->effect)
|
||||||
translateEffect(m_outputStream, action->effect.value(), printActionName,
|
translateEffect(m_outputStream, action->effect.value(), printActionName,
|
||||||
numberOfConditionalEffects);
|
numberOfConditionalEffects, variableIDs);
|
||||||
|
|
||||||
m_outputStream << std::endl;
|
m_outputStream << std::endl;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user