Added conditional effect support for SAS translator output.
This commit is contained in:
parent
bc5f8edb67
commit
3a2dfd97b8
@ -1,8 +1,9 @@
|
|||||||
#include <incmode>.
|
#include <incmode>.
|
||||||
|
|
||||||
% Check feature requirements
|
% Check feature requirements
|
||||||
:- requiresFeature(actionCosts, true).
|
:- requiresFeature(actionCosts).
|
||||||
:- requiresFeature(axiomRules, true).
|
:- requiresFeature(axiomRules).
|
||||||
|
:- requiresFeature(conditionalEffects).
|
||||||
|
|
||||||
#program base.
|
#program base.
|
||||||
|
|
||||||
@ -18,18 +19,21 @@ holds(Var, Val, 0) :- initialState(Var, Val).
|
|||||||
:- occurs(A, t), precondition(A, Var, Val), not holds(Var, Val, t - 1).
|
:- occurs(A, t), precondition(A, Var, Val), not holds(Var, Val, t - 1).
|
||||||
|
|
||||||
% Apply effects
|
% Apply effects
|
||||||
caused(Var, Val, t) :- occurs(A, t), postcondition(A, Var, Val).
|
caused(Var, Val, t) :- occurs(A, t), contains(A, E), postcondition(E, Var, Val).
|
||||||
modified(Var, t) :- caused(Var, Val, t).
|
modified(Var, t) :- caused(Var, Val, t).
|
||||||
|
|
||||||
holds(Var, Val, t) :- caused(Var, Val, t).
|
holds(Var, Val, t) :- caused(Var, Val, t).
|
||||||
holds(Var, Val, t) :- holds(Var, Val, t - 1), not modified(Var, t).
|
holds(Var, Val, t) :- holds(Var, Val, t - 1), not modified(Var, t).
|
||||||
|
|
||||||
% Check that variables without <none of those> values are unique
|
% Check that variables without <none of those> values are unique
|
||||||
:- variable(V), Var = variable(V), not contains(Var, noneValue), not 1 {holds(Var, Val, t) : contains(Var, Val)} 1.
|
:- variable(X), Var = variable(X), not contains(Var, noneValue), not 1 {holds(Var, Val, t) : contains(Var, Val)} 1.
|
||||||
:- contains(Var, noneValue), not {holds(Var, Val, t) : contains(Var, Val)} 1.
|
:- contains(Var, noneValue), not {holds(Var, Val, t) : contains(Var, Val)} 1.
|
||||||
|
|
||||||
% Check mutexes
|
% Check mutexes
|
||||||
:- mutexGroup(M), not {holds(Var, Val, t) : contains(M, Var, Val)} 1.
|
:- mutexGroup(X), M = mutexGroup(X), not {holds(Var, Val, t) : contains(M, Var, Val)} 1.
|
||||||
|
|
||||||
|
% Output predicate
|
||||||
|
do(L, t) :- occurs(A, t), label(A, action(L)).
|
||||||
|
|
||||||
#program check(t).
|
#program check(t).
|
||||||
|
|
||||||
@ -37,4 +41,4 @@ holds(Var, Val, t) :- holds(Var, Val, t - 1), not modified(Var, t).
|
|||||||
:- query(t), goal(Var, Val), not holds(Var, Val, t).
|
:- query(t), goal(Var, Val), not holds(Var, Val, t).
|
||||||
|
|
||||||
#show query/1.
|
#show query/1.
|
||||||
#show occurs/2.
|
#show do/2.
|
||||||
|
@ -41,6 +41,7 @@ class Description
|
|||||||
const AxiomRules &axiomRules() const;
|
const AxiomRules &axiomRules() const;
|
||||||
|
|
||||||
bool usesAxiomRules() const;
|
bool usesAxiomRules() const;
|
||||||
|
bool usesConditionalEffects() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Description();
|
Description();
|
||||||
|
@ -128,6 +128,27 @@ bool Description::usesAxiomRules() const
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
bool Description::usesConditionalEffects() const
|
||||||
|
{
|
||||||
|
const auto match = std::find_if(m_operators.cbegin(), m_operators.cend(),
|
||||||
|
[&](const auto &operator_)
|
||||||
|
{
|
||||||
|
const auto &effects = operator_.effects();
|
||||||
|
|
||||||
|
const auto match = std::find_if(effects.cbegin(), effects.cend(),
|
||||||
|
[&](const auto &effect)
|
||||||
|
{
|
||||||
|
return !effect.conditions().empty();
|
||||||
|
});
|
||||||
|
|
||||||
|
return match != effects.cend();
|
||||||
|
});
|
||||||
|
|
||||||
|
return match != m_operators.cend();
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void Description::parseVersionSection(std::istream &istream) const
|
void Description::parseVersionSection(std::istream &istream) const
|
||||||
{
|
{
|
||||||
utils::parseExpected<std::string>(istream, "begin_version");
|
utils::parseExpected<std::string>(istream, "begin_version");
|
||||||
|
@ -47,6 +47,7 @@ void TranslatorASP::translate(std::ostream &ostream) const
|
|||||||
|
|
||||||
const auto usesActionCosts = m_description.usesActionCosts();
|
const auto usesActionCosts = m_description.usesActionCosts();
|
||||||
const auto usesAxiomRules = m_description.usesAxiomRules();
|
const auto usesAxiomRules = m_description.usesAxiomRules();
|
||||||
|
const auto usesConditionalEffects = m_description.usesConditionalEffects();
|
||||||
|
|
||||||
ostream << "% feature requirements" << std::endl;
|
ostream << "% feature requirements" << std::endl;
|
||||||
|
|
||||||
@ -56,6 +57,9 @@ void TranslatorASP::translate(std::ostream &ostream) const
|
|||||||
if (usesAxiomRules)
|
if (usesAxiomRules)
|
||||||
ostream << "requiresFeature(axiomRules)." << std::endl;
|
ostream << "requiresFeature(axiomRules)." << std::endl;
|
||||||
|
|
||||||
|
if (usesConditionalEffects)
|
||||||
|
ostream << "requiresFeature(conditionalEffects)." << std::endl;
|
||||||
|
|
||||||
ostream << std::endl;
|
ostream << std::endl;
|
||||||
ostream << "% initial state" << std::endl;
|
ostream << "% initial state" << std::endl;
|
||||||
|
|
||||||
@ -129,12 +133,20 @@ void TranslatorASP::translate(std::ostream &ostream) const
|
|||||||
|
|
||||||
const auto &operators = m_description.operators();
|
const auto &operators = m_description.operators();
|
||||||
|
|
||||||
|
size_t currentActionID = 0;
|
||||||
|
size_t currentEffectID = 0;
|
||||||
|
|
||||||
std::for_each(operators.cbegin(), operators.cend(),
|
std::for_each(operators.cbegin(), operators.cend(),
|
||||||
[&](const auto &operator_)
|
[&](const auto &operator_)
|
||||||
{
|
{
|
||||||
ostream << std::endl;
|
const auto actionID = std::to_string(currentActionID);
|
||||||
|
currentActionID++;
|
||||||
|
|
||||||
|
ostream << std::endl << "action(" << actionID << ")." << std::endl;
|
||||||
|
|
||||||
|
ostream << "label(action(" << actionID << "), ";
|
||||||
operator_.printPredicateAsASP(ostream);
|
operator_.printPredicateAsASP(ostream);
|
||||||
ostream << "." << std::endl;
|
ostream << ")." << std::endl;
|
||||||
|
|
||||||
const auto &preconditions = operator_.preconditions();
|
const auto &preconditions = operator_.preconditions();
|
||||||
|
|
||||||
@ -144,9 +156,7 @@ void TranslatorASP::translate(std::ostream &ostream) const
|
|||||||
if (precondition.value() == Value::None)
|
if (precondition.value() == Value::None)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ostream << "precondition(";
|
ostream << "precondition(action(" << actionID << "), ";
|
||||||
operator_.printPredicateAsASP(ostream);
|
|
||||||
ostream << ", ";
|
|
||||||
precondition.variable().printNameAsASPPredicate(ostream);
|
precondition.variable().printNameAsASPPredicate(ostream);
|
||||||
ostream << ", ";
|
ostream << ", ";
|
||||||
precondition.value().printAsASPPredicate(ostream);
|
precondition.value().printAsASPPredicate(ostream);
|
||||||
@ -161,9 +171,28 @@ void TranslatorASP::translate(std::ostream &ostream) const
|
|||||||
if (effect.postcondition().value() == Value::None)
|
if (effect.postcondition().value() == Value::None)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ostream << "postcondition(";
|
const auto effectID = std::to_string(currentEffectID);
|
||||||
operator_.printPredicateAsASP(ostream);
|
currentEffectID++;
|
||||||
ostream << ", ";
|
|
||||||
|
ostream << "effect(" << effectID << ")." << std::endl;
|
||||||
|
ostream << "contains(action(" << actionID << "), effect(" << effectID << "))." << std::endl;
|
||||||
|
|
||||||
|
if (usesConditionalEffects)
|
||||||
|
{
|
||||||
|
const auto &conditions = effect.conditions();
|
||||||
|
|
||||||
|
std::for_each(conditions.cbegin(), conditions.cend(),
|
||||||
|
[&](const auto &condition)
|
||||||
|
{
|
||||||
|
ostream << "condition(effect(" << effectID << "), ";
|
||||||
|
condition.variable().printNameAsASPPredicate(ostream);
|
||||||
|
ostream << ", ";
|
||||||
|
condition.value().printAsASPPredicate(ostream);
|
||||||
|
ostream << ")." << std::endl;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
ostream << "postcondition(effect(" << effectID << "), ";
|
||||||
effect.postcondition().variable().printNameAsASPPredicate(ostream);
|
effect.postcondition().variable().printNameAsASPPredicate(ostream);
|
||||||
ostream << ", ";
|
ostream << ", ";
|
||||||
effect.postcondition().value().printAsASPPredicate(ostream);
|
effect.postcondition().value().printAsASPPredicate(ostream);
|
||||||
@ -183,24 +212,28 @@ void TranslatorASP::translate(std::ostream &ostream) const
|
|||||||
|
|
||||||
const auto &mutexGroups = m_description.mutexGroups();
|
const auto &mutexGroups = m_description.mutexGroups();
|
||||||
|
|
||||||
for (size_t i = 0; i < mutexGroups.size(); i++)
|
size_t currentMutexGroupID = 0;
|
||||||
{
|
|
||||||
const auto &mutexGroup = mutexGroups[i];
|
|
||||||
|
|
||||||
ostream << std::endl << "mutexGroup(mutexGroup" << i << ")." << std::endl;
|
std::for_each(mutexGroups.cbegin(), mutexGroups.cend(),
|
||||||
|
[&](const auto &mutexGroup)
|
||||||
|
{
|
||||||
|
const auto mutexGroupID = std::to_string(currentMutexGroupID);
|
||||||
|
currentMutexGroupID++;
|
||||||
|
|
||||||
const auto &facts = mutexGroup.facts();
|
ostream << std::endl << "mutexGroup(" << mutexGroupID << ")." << std::endl;
|
||||||
|
|
||||||
std::for_each(facts.cbegin(), facts.cend(),
|
const auto &facts = mutexGroup.facts();
|
||||||
[&](const auto &fact)
|
|
||||||
{
|
std::for_each(facts.cbegin(), facts.cend(),
|
||||||
ostream << "contains(mutexGroup(mutexGroup" << i << "), ";
|
[&](const auto &fact)
|
||||||
fact.variable().printNameAsASPPredicate(ostream);
|
{
|
||||||
ostream << ", ";
|
ostream << "contains(mutexGroup(" << mutexGroupID << "), ";
|
||||||
fact.value().printAsASPPredicate(ostream);
|
fact.variable().printNameAsASPPredicate(ostream);
|
||||||
ostream << ")." << std::endl;
|
ostream << ", ";
|
||||||
});
|
fact.value().printAsASPPredicate(ostream);
|
||||||
}
|
ostream << ")." << std::endl;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if (usesAxiomRules)
|
if (usesAxiomRules)
|
||||||
{
|
{
|
||||||
@ -209,32 +242,36 @@ void TranslatorASP::translate(std::ostream &ostream) const
|
|||||||
|
|
||||||
const auto &axiomRules = m_description.axiomRules();
|
const auto &axiomRules = m_description.axiomRules();
|
||||||
|
|
||||||
for (size_t i = 0; i < axiomRules.size(); i++)
|
size_t currentAxiomRuleID = 0;
|
||||||
{
|
|
||||||
const auto &axiomRule = axiomRules[i];
|
|
||||||
|
|
||||||
ostream << std::endl << "axiomRule(axiomRule" << i << ")." << std::endl;
|
std::for_each(axiomRules.cbegin(), axiomRules.cend(),
|
||||||
|
[&](const auto &axiomRule)
|
||||||
|
{
|
||||||
|
const auto axiomRuleID = std::to_string(currentAxiomRuleID);
|
||||||
|
currentAxiomRuleID++;
|
||||||
|
|
||||||
const auto &conditions = axiomRule.conditions();
|
ostream << std::endl << "axiomRule(" << axiomRuleID << ")." << std::endl;
|
||||||
|
|
||||||
std::for_each(conditions.cbegin(), conditions.cend(),
|
const auto &conditions = axiomRule.conditions();
|
||||||
[&](const auto &condition)
|
|
||||||
{
|
|
||||||
ostream << "condition(axiomRule(axiomRule" << i << "), ";
|
|
||||||
condition.variable().printNameAsASPPredicate(ostream);
|
|
||||||
ostream << ", ";
|
|
||||||
condition.value().printAsASPPredicate(ostream);
|
|
||||||
ostream << ")." << std::endl;
|
|
||||||
});
|
|
||||||
|
|
||||||
const auto &postcondition = axiomRule.postcondition();
|
std::for_each(conditions.cbegin(), conditions.cend(),
|
||||||
|
[&](const auto &condition)
|
||||||
|
{
|
||||||
|
ostream << "condition(axiomRule(" << axiomRuleID << "), ";
|
||||||
|
condition.variable().printNameAsASPPredicate(ostream);
|
||||||
|
ostream << ", ";
|
||||||
|
condition.value().printAsASPPredicate(ostream);
|
||||||
|
ostream << ")." << std::endl;
|
||||||
|
});
|
||||||
|
|
||||||
ostream << "postcondition(axiomRule(axiomRule" << i << "), ";
|
const auto &postcondition = axiomRule.postcondition();
|
||||||
postcondition.variable().printNameAsASPPredicate(ostream);
|
|
||||||
ostream << ", ";
|
ostream << "postcondition(axiomRule(axiomRule" << axiomRuleID << "), ";
|
||||||
postcondition.value().printAsASPPredicate(ostream);
|
postcondition.variable().printNameAsASPPredicate(ostream);
|
||||||
ostream << ")." << std::endl;
|
ostream << ", ";
|
||||||
}
|
postcondition.value().printAsASPPredicate(ostream);
|
||||||
|
ostream << ")." << std::endl;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user