Made Goal a proper class.

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

View File

@ -8,6 +8,7 @@
#include <boost/filesystem/path.hpp>
#include <plasp/sas/AxiomRule.h>
#include <plasp/sas/Goal.h>
#include <plasp/sas/InitialState.h>
#include <plasp/sas/MutexGroup.h>
#include <plasp/sas/Operator.h>
@ -35,7 +36,7 @@ class Description
const std::vector<Variable> &variables() const;
const std::vector<MutexGroup> &mutexGroups() const;
const InitialState &initialState() const;
const std::vector<AssignedVariable> &goalFacts() const;
const Goal &goal() const;
const std::vector<Operator> &operators() const;
const std::vector<AxiomRule> &axiomRules() const;
@ -58,7 +59,7 @@ class Description
std::vector<Variable> m_variables;
std::vector<MutexGroup> m_mutexGroups;
std::unique_ptr<InitialState> m_initialState;
std::vector<AssignedVariable> m_goalFacts;
std::unique_ptr<Goal> m_goal;
std::vector<Operator> m_operators;
std::vector<AxiomRule> m_axiomRules;
};

39
include/plasp/sas/Goal.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef __SAS__GOAL_H
#define __SAS__GOAL_H
#include <plasp/sas/AssignedVariable.h>
namespace plasp
{
namespace sas
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Goal
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Goal
{
public:
using Fact = AssignedVariable;
using Facts = std::vector<Fact>;
static Goal fromSAS(std::istream &istream, const std::vector<Variable> &variables);
public:
const Facts &facts() const;
private:
Goal() = default;
Facts m_facts;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

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

View File

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