patrick
/
plasp
Archived
1
0
Fork 0

Added axiom rule support for SAS translator output.

This commit is contained in:
Patrick Lühne 2016-05-23 17:13:11 +02:00
parent a5fd698888
commit 05058c149e
6 changed files with 66 additions and 14 deletions

View File

@ -2,6 +2,7 @@
% Check feature requirements % Check feature requirements
:- requiresFeature(actionCosts, true). :- requiresFeature(actionCosts, true).
:- requiresFeature(axiomRules, true).
#program base. #program base.

View File

@ -40,6 +40,8 @@ class Description
const Operators &operators() const; const Operators &operators() const;
const AxiomRules &axiomRules() const; const AxiomRules &axiomRules() const;
bool usesAxiomRules() const;
private: private:
Description(); Description();

View File

@ -65,6 +65,7 @@ inline std::string escapeASP(const std::string &string)
boost::replace_all(escaped, "_", "__"); boost::replace_all(escaped, "_", "__");
boost::replace_all(escaped, "-", "_h"); boost::replace_all(escaped, "-", "_h");
boost::replace_all(escaped, "@", "_a");
return escaped; return escaped;
} }
@ -75,6 +76,7 @@ inline std::string unescapeASP(const std::string &string)
{ {
auto unescaped = string; auto unescaped = string;
boost::replace_all(unescaped, "_a", "@");
boost::replace_all(unescaped, "_h", "-"); boost::replace_all(unescaped, "_h", "-");
boost::replace_all(unescaped, "__", "_"); boost::replace_all(unescaped, "__", "_");

View File

@ -1,5 +1,6 @@
#include <plasp/sas/Description.h> #include <plasp/sas/Description.h>
#include <algorithm>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
@ -111,6 +112,22 @@ const AxiomRules &Description::axiomRules() const
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
bool Description::usesAxiomRules() const
{
if (!m_axiomRules.empty())
return true;
const auto match = std::find_if(m_variables.cbegin(), m_variables.cend(),
[&](const auto &variable)
{
return variable.axiomLayer() != -1;
});
return match != m_variables.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");

View File

@ -23,15 +23,6 @@ TranslatorASP::TranslatorASP(const Description &description)
void TranslatorASP::checkSupport() const void TranslatorASP::checkSupport() const
{ {
const auto &variables = m_description.variables();
std::for_each(variables.cbegin(), variables.cend(),
[&](const auto &variable)
{
if (variable.axiomLayer() != -1)
throw TranslatorException("Axiom layers are currently unsupported");
});
const auto &operators = m_description.operators(); const auto &operators = m_description.operators();
std::for_each(operators.cbegin(), operators.cend(), std::for_each(operators.cbegin(), operators.cend(),
@ -46,9 +37,6 @@ void TranslatorASP::checkSupport() const
throw TranslatorException("Conditional effects are currently unsupported"); throw TranslatorException("Conditional effects are currently unsupported");
}); });
}); });
if (!m_description.axiomRules().empty())
throw TranslatorException("Axiom rules are currently unsupported");
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -57,11 +45,17 @@ void TranslatorASP::translate(std::ostream &ostream) const
{ {
checkSupport(); checkSupport();
const auto usesActionCosts = m_description.usesActionCosts();
const auto usesAxiomRules = m_description.usesAxiomRules();
ostream << "% feature requirements" << std::endl; ostream << "% feature requirements" << std::endl;
if (m_description.usesActionCosts()) if (usesActionCosts)
ostream << "requiresFeature(actionCosts)." << std::endl; ostream << "requiresFeature(actionCosts)." << std::endl;
if (usesAxiomRules)
ostream << "requiresFeature(axiomRules)." << std::endl;
ostream << std::endl; ostream << std::endl;
ostream << "% initial state" << std::endl; ostream << "% initial state" << std::endl;
@ -204,6 +198,41 @@ void TranslatorASP::translate(std::ostream &ostream) const
ostream << ")." << std::endl; ostream << ")." << std::endl;
}); });
} }
if (usesAxiomRules)
{
ostream << std::endl;
ostream << "% axiom rules";
const auto &axiomRules = m_description.axiomRules();
for (size_t i = 0; i < axiomRules.size(); i++)
{
const auto &axiomRule = axiomRules[i];
ostream << std::endl << "axiomRule(axiomRule" << i << ")." << std::endl;
const auto &conditions = axiomRule.conditions();
std::for_each(conditions.cbegin(), conditions.cend(),
[&](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();
ostream << "postcondition(axiomRule(axiomRule" << i << "), ";
postcondition.variable().printNameAsASPPredicate(ostream);
ostream << ", ";
postcondition.value().printAsASPPredicate(ostream);
ostream << ")." << std::endl;
}
}
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -6,11 +6,12 @@
TEST(UtilsTests, EscapeASP) TEST(UtilsTests, EscapeASP)
{ {
const std::string predicate = "action(stack_on(block-1, block-2))"; const std::string predicate = "action(stack_on(block-1, block-2, value@3, value@4))";
const auto escaped = plasp::utils::escapeASP(predicate); const auto escaped = plasp::utils::escapeASP(predicate);
const auto unescaped = plasp::utils::unescapeASP(escaped); const auto unescaped = plasp::utils::unescapeASP(escaped);
ASSERT_EQ(escaped.find("-"), std::string::npos); ASSERT_EQ(escaped.find("-"), std::string::npos);
ASSERT_EQ(escaped.find("@"), std::string::npos);
ASSERT_EQ(predicate, unescaped); ASSERT_EQ(predicate, unescaped);
} }