From d8b87c7bfae29bc14d1e4b5cbc8ec20a0a3a8754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sun, 22 May 2016 22:45:33 +0200 Subject: [PATCH] Finished SAS-to-ASP translation with limited support. --- include/plasp/sas/Value.h | 3 ++- src/plasp/sas/TranslatorASP.cpp | 43 ++++++++++++++++++--------------- src/plasp/sas/Value.cpp | 12 ++++++++- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/include/plasp/sas/Value.h b/include/plasp/sas/Value.h index 024c535..fdf2d3c 100644 --- a/include/plasp/sas/Value.h +++ b/include/plasp/sas/Value.h @@ -41,7 +41,8 @@ struct Value public: void printAsSAS(std::ostream &ostream) const; void printAsASP(std::ostream &ostream) const; - void printAsASPCommaSeparated(std::ostream &ostream) const; + void printAsASPPredicateBody(std::ostream &ostream) const; + void printAsASPHoldsPredicate(std::ostream &ostream) const; Sign sign() const; const std::string &name() const; diff --git a/src/plasp/sas/TranslatorASP.cpp b/src/plasp/sas/TranslatorASP.cpp index 492f2a7..715283d 100644 --- a/src/plasp/sas/TranslatorASP.cpp +++ b/src/plasp/sas/TranslatorASP.cpp @@ -112,11 +112,11 @@ void TranslatorASP::translate(std::ostream &ostream) const std::for_each(preconditions.cbegin(), preconditions.cend(), [&](const auto &precondition) - { + { ostream << "precondition("; operator_.predicate().printAsASP(ostream); ostream << ", "; - precondition.value().printAsASPCommaSeparated(ostream); + precondition.value().printAsASPPredicateBody(ostream); ostream << ")." << std::endl; }); @@ -128,7 +128,7 @@ void TranslatorASP::translate(std::ostream &ostream) const ostream << "postcondition("; operator_.predicate().printAsASP(ostream); ostream << ", "; - effect.postcondition().value().printAsASPCommaSeparated(ostream); + effect.postcondition().value().printAsASPPredicateBody(ostream); ostream << ")." << std::endl; }); @@ -142,8 +142,11 @@ void TranslatorASP::translate(std::ostream &ostream) const std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(), [&](const auto &fact) { - ostream << "init("; - fact.value().printAsASPCommaSeparated(ostream); + if (fact.value().sign() == Value::Sign::Negative) + return; + + ostream << "initialState("; + fact.value().printAsASP(ostream); ostream << ")." << std::endl; }); @@ -156,7 +159,7 @@ void TranslatorASP::translate(std::ostream &ostream) const [&](const auto &fact) { ostream << "goal("; - fact.value().printAsASPCommaSeparated(ostream); + fact.value().printAsASPPredicateBody(ostream); ostream << ")." << std::endl; }); @@ -175,14 +178,14 @@ void TranslatorASP::translate(std::ostream &ostream) const for (auto i = values.cbegin(); i != values.cend(); i++) for (auto j = i + 1; j != values.cend(); j++) { - const auto &value1 = *i; - const auto &value2 = *j; + const auto &value1 = *i; + const auto &value2 = *j; - ostream << ":- time(T), holds("; - value1.printAsASPCommaSeparated(ostream); - ostream << ", T), holds("; - value2.printAsASPCommaSeparated(ostream); - ostream << ", T)." << std::endl; + ostream << ":- "; + value1.printAsASPHoldsPredicate(ostream); + ostream << ", "; + value2.printAsASPHoldsPredicate(ostream); + ostream << "." << std::endl; } }); @@ -199,14 +202,14 @@ void TranslatorASP::translate(std::ostream &ostream) const for (auto i = facts.cbegin(); i != facts.cend(); i++) for (auto j = i + 1; j != facts.cend(); j++) { - const auto &value1 = i->value(); - const auto &value2 = j->value(); + const auto &value1 = i->value(); + const auto &value2 = j->value(); - ostream << ":- time(T), holds("; - value1.printAsASPCommaSeparated(ostream); - ostream << ", T), holds("; - value2.printAsASPCommaSeparated(ostream); - ostream << ", T)." << std::endl; + ostream << ":- "; + value1.printAsASPHoldsPredicate(ostream); + ostream << ", "; + value2.printAsASPHoldsPredicate(ostream); + ostream << "." << std::endl; } }); } diff --git a/src/plasp/sas/Value.cpp b/src/plasp/sas/Value.cpp index 2cf91c7..b87f8eb 100644 --- a/src/plasp/sas/Value.cpp +++ b/src/plasp/sas/Value.cpp @@ -113,13 +113,23 @@ void Value::printAsASP(std::ostream &ostream) const //////////////////////////////////////////////////////////////////////////////////////////////////// -void Value::printAsASPCommaSeparated(std::ostream &ostream) const +void Value::printAsASPPredicateBody(std::ostream &ostream) const { ostream << utils::escapeASP(m_name) << ", " << (m_sign == Sign::Positive ? "true" : "false"); } //////////////////////////////////////////////////////////////////////////////////////////////////// +void Value::printAsASPHoldsPredicate(std::ostream &ostream) const +{ + if (m_sign == Value::Sign::Negative) + ostream << "not "; + + ostream << "holds(" << utils::escapeASP(m_name) << ", t)"; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + void Value::printAsSAS(std::ostream &ostream) const { if (m_sign == Value::Sign::Positive)