diff --git a/include/plasp/sas/Description.h b/include/plasp/sas/Description.h index 69e27f0..65f05bb 100644 --- a/include/plasp/sas/Description.h +++ b/include/plasp/sas/Description.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -35,7 +36,7 @@ class Description const std::vector &variables() const; const std::vector &mutexGroups() const; const InitialState &initialState() const; - const std::vector &goalFacts() const; + const Goal &goal() const; const std::vector &operators() const; const std::vector &axiomRules() const; @@ -58,7 +59,7 @@ class Description std::vector m_variables; std::vector m_mutexGroups; std::unique_ptr m_initialState; - std::vector m_goalFacts; + std::unique_ptr m_goal; std::vector m_operators; std::vector m_axiomRules; }; diff --git a/include/plasp/sas/Goal.h b/include/plasp/sas/Goal.h new file mode 100644 index 0000000..062db36 --- /dev/null +++ b/include/plasp/sas/Goal.h @@ -0,0 +1,39 @@ +#ifndef __SAS__GOAL_H +#define __SAS__GOAL_H + +#include + +namespace plasp +{ +namespace sas +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Goal +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Goal +{ + public: + using Fact = AssignedVariable; + using Facts = std::vector; + + static Goal fromSAS(std::istream &istream, const std::vector &variables); + + public: + const Facts &facts() const; + + private: + Goal() = default; + + Facts m_facts; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/src/plasp/sas/Description.cpp b/src/plasp/sas/Description.cpp index 044d53e..8d1df82 100644 --- a/src/plasp/sas/Description.cpp +++ b/src/plasp/sas/Description.cpp @@ -90,9 +90,9 @@ const InitialState &Description::initialState() const //////////////////////////////////////////////////////////////////////////////////////////////////// -const std::vector &Description::goalFacts() const +const Goal &Description::goal() const { - return m_goalFacts; + return *m_goal; } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -159,21 +159,21 @@ void Description::print(std::ostream &ostream) const ostream << "initial state:" << std::endl; std::for_each(m_initialState->facts().cbegin(), m_initialState->facts().cend(), - [&](const auto &initialStateFact) + [&](const auto &fact) { - ostream << "\t" << initialStateFact.variable().name() << " = "; - initialStateFact.value().printAsSAS(ostream); + ostream << "\t" << fact.variable().name() << " = "; + fact.value().printAsSAS(ostream); ostream << std::endl; }); // Goal section ostream << "goal:" << std::endl; - std::for_each(m_goalFacts.cbegin(), m_goalFacts.cend(), - [&](const auto &goalFact) + std::for_each(m_goal->facts().cbegin(), m_goal->facts().cend(), + [&](const auto &fact) { - ostream << "\t" << goalFact.variable().name() << " = "; - goalFact.value().printAsSAS(ostream); + ostream << "\t" << fact.variable().name() << " = "; + fact.value().printAsSAS(ostream); ostream << std::endl; }); @@ -302,15 +302,7 @@ void Description::parseInitialStateSection(std::istream &istream) void Description::parseGoalSection(std::istream &istream) { - utils::parseExpected(istream, "begin_goal"); - - const auto numberOfGoalFacts = utils::parse(istream); - m_goalFacts.reserve(numberOfGoalFacts); - - for (size_t i = 0; i < numberOfGoalFacts; i++) - m_goalFacts.emplace_back(AssignedVariable::fromSAS(istream, m_variables)); - - utils::parseExpected(istream, "end_goal"); + m_goal = std::make_unique(Goal::fromSAS(istream, m_variables)); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/sas/Goal.cpp b/src/plasp/sas/Goal.cpp new file mode 100644 index 0000000..a056a5f --- /dev/null +++ b/src/plasp/sas/Goal.cpp @@ -0,0 +1,45 @@ +#include + +#include + +#include + +namespace plasp +{ +namespace sas +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Goal +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Goal Goal::fromSAS(std::istream &istream, const std::vector &variables) +{ + Goal goal; + + utils::parseExpected(istream, "begin_goal"); + + const auto numberOfGoalFacts = utils::parse(istream); + goal.m_facts.reserve(numberOfGoalFacts); + + for (size_t i = 0; i < numberOfGoalFacts; i++) + goal.m_facts.emplace_back(AssignedVariable::fromSAS(istream, variables)); + + utils::parseExpected(istream, "end_goal"); + + return goal; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Goal::Facts &Goal::facts() const +{ + return m_facts; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} diff --git a/src/plasp/sas/TranslatorASP.cpp b/src/plasp/sas/TranslatorASP.cpp index 70fc647..2d29e4d 100644 --- a/src/plasp/sas/TranslatorASP.cpp +++ b/src/plasp/sas/TranslatorASP.cpp @@ -95,23 +95,23 @@ void TranslatorASP::translate(std::ostream &ostream) const const auto &initialStateFacts = m_description.initialState().facts(); std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(), - [&](const auto &initialStateFact) + [&](const auto &fact) { ostream << "init("; - initialStateFact.value().printAsASP(ostream); + fact.value().printAsASP(ostream); ostream << ")." << std::endl; }); ostream << std::endl; ostream << "% goal" << std::endl; - const auto &goalFacts = m_description.goalFacts(); + const auto &goalFacts = m_description.goal().facts(); std::for_each(goalFacts.cbegin(), goalFacts.cend(), - [&](const auto &goalFact) + [&](const auto &fact) { ostream << "goal("; - goalFact.value().printAsASP(ostream); + fact.value().printAsASP(ostream); ostream << ")." << std::endl; }); diff --git a/tests/TestSASParser.cpp b/tests/TestSASParser.cpp index ce7f556..c8bdd1c 100644 --- a/tests/TestSASParser.cpp +++ b/tests/TestSASParser.cpp @@ -62,9 +62,9 @@ TEST_F(SASParserTests, ParseValidSASFile) ASSERT_EQ(&description.initialState().facts()[0].value(), &description.variables()[0].values()[8]); ASSERT_EQ(&description.initialState().facts()[36].value(), &description.variables()[36].values()[1]); - ASSERT_EQ(description.goalFacts().size(), 2); - ASSERT_EQ(&description.goalFacts()[0].value(), &description.variables()[6].values()[0]); - ASSERT_EQ(&description.goalFacts()[1].value(), &description.variables()[7].values()[0]); + ASSERT_EQ(description.goal().facts().size(), 2); + ASSERT_EQ(&description.goal().facts()[0].value(), &description.variables()[6].values()[0]); + ASSERT_EQ(&description.goal().facts()[1].value(), &description.variables()[7].values()[0]); ASSERT_EQ(description.operators().size(), 34); ASSERT_EQ(description.operators()[0].predicate.name, "activate-trans");