Made Goal a proper class.

This commit is contained in:
2016-05-22 14:04:58 +02:00
parent beef3aca60
commit 90dfa302a9
6 changed files with 105 additions and 28 deletions

View File

@@ -90,9 +90,9 @@ const InitialState &Description::initialState() const
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<AssignedVariable> &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<std::string>(istream, "begin_goal");
const auto numberOfGoalFacts = utils::parse<size_t>(istream);
m_goalFacts.reserve(numberOfGoalFacts);
for (size_t i = 0; i < numberOfGoalFacts; i++)
m_goalFacts.emplace_back(AssignedVariable::fromSAS(istream, m_variables));
utils::parseExpected<std::string>(istream, "end_goal");
m_goal = std::make_unique<Goal>(Goal::fromSAS(istream, m_variables));
}
////////////////////////////////////////////////////////////////////////////////////////////////////

45
src/plasp/sas/Goal.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include <plasp/sas/Goal.h>
#include <iostream>
#include <plasp/utils/Parsing.h>
namespace plasp
{
namespace sas
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Goal
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Goal Goal::fromSAS(std::istream &istream, const std::vector<Variable> &variables)
{
Goal goal;
utils::parseExpected<std::string>(istream, "begin_goal");
const auto numberOfGoalFacts = utils::parse<size_t>(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<std::string>(istream, "end_goal");
return goal;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const Goal::Facts &Goal::facts() const
{
return m_facts;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@@ -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;
});