Made Effect a proper class.

This commit is contained in:
Patrick Lühne 2016-05-22 15:46:41 +02:00
parent 342672063e
commit 6677ded33e
8 changed files with 103 additions and 40 deletions

View File

@ -30,7 +30,7 @@ class AssignedVariable
static AssignedVariable fromSAS(std::istream &istream, const Variable &variable); static AssignedVariable fromSAS(std::istream &istream, const Variable &variable);
public: public:
AssignedVariable(const Variable &variable, const Value &value); explicit AssignedVariable(const Variable &variable, const Value &value);
const Variable &variable() const; const Variable &variable() const;
const Value &value() const; const Value &value() const;

View File

@ -36,7 +36,7 @@ class AxiomRule
private: private:
AxiomRule() = default; AxiomRule() = default;
AxiomRule(Conditions conditions, Condition postcondition); explicit AxiomRule(Conditions conditions, Condition postcondition);
Conditions m_conditions; Conditions m_conditions;
Condition m_postcondition; Condition m_postcondition;

View File

@ -17,18 +17,29 @@ namespace sas
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
struct Effect; class Effect;
using Effects = std::vector<Effect>; using Effects = std::vector<Effect>;
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
struct Effect class Effect
{ {
using Condition = AssignedVariable; public:
using Conditions = AssignedVariables; using Condition = AssignedVariable;
using Conditions = AssignedVariables;
Conditions conditions; static Effect fromSAS(std::istream &istream, const Variables &variables, Conditions &preconditions);
Condition postcondition;
public:
const Conditions &conditions() const;
const Condition &postcondition() const;
private:
Effect() = default;
explicit Effect(Conditions conditions, Condition postcondition);
Conditions m_conditions;
Condition m_postcondition;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -117,9 +117,12 @@ std::ostream &operator >>(std::ostream &ostream, const Description &description)
[&](const auto &effect) [&](const auto &effect)
{ {
ostream << "\t\t\teffect:" << std::endl; ostream << "\t\t\teffect:" << std::endl;
ostream << "\t\t\t\tconditions: " << effect.conditions.size() << std::endl;
std::for_each(effect.conditions.cbegin(), effect.conditions.cend(), const auto &conditions = effect.conditions();
ostream << "\t\t\t\tconditions: " << conditions.size() << std::endl;
std::for_each(conditions.cbegin(), conditions.cend(),
[&](const auto &condition) [&](const auto &condition)
{ {
ostream << "\t\t\t\t\t" << condition.variable().name() << " = "; ostream << "\t\t\t\t\t" << condition.variable().name() << " = ";
@ -128,8 +131,8 @@ std::ostream &operator >>(std::ostream &ostream, const Description &description)
}); });
ostream << "\t\t\t\tpostcondition:" << std::endl; ostream << "\t\t\t\tpostcondition:" << std::endl;
ostream << "\t\t\t\t\t" << effect.postcondition.variable().name() << " = "; ostream << "\t\t\t\t\t" << effect.postcondition().variable().name() << " = ";
effect.postcondition.value().printAsSAS(ostream); effect.postcondition().value().printAsSAS(ostream);
ostream << std::endl; ostream << std::endl;
}); });

64
src/plasp/sas/Effect.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <plasp/sas/Effect.h>
#include <iostream>
#include <plasp/sas/VariableTransition.h>
#include <plasp/utils/Parsing.h>
namespace plasp
{
namespace sas
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Effect
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Effect::Effect(Conditions conditions, Condition postcondition)
: m_conditions(std::move(conditions)),
m_postcondition(std::move(postcondition))
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Effect Effect::fromSAS(std::istream &istream, const Variables &variables, Conditions &preconditions)
{
Effect::Conditions conditions;
const auto numberOfEffectConditions = utils::parse<size_t>(istream);
conditions.reserve(numberOfEffectConditions);
for (size_t k = 0; k < numberOfEffectConditions; k++)
conditions.emplace_back(Condition::fromSAS(istream, variables));
const auto variableTransition = VariableTransition::fromSAS(istream, variables);
if (&variableTransition.valueBefore() != &Value::Any)
preconditions.emplace_back(Condition(variableTransition.variable(), variableTransition.valueBefore()));
const Effect::Condition postcondition(variableTransition.variable(), variableTransition.valueAfter());
return Effect(std::move(conditions), std::move(postcondition));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const Effect::Conditions &Effect::conditions() const
{
return m_conditions;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const Effect::Condition &Effect::postcondition() const
{
return m_postcondition;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -50,30 +50,13 @@ Operator Operator::fromSAS(std::istream &istream, const Variables &variables)
operator_.m_preconditions.reserve(numberOfPrevailConditions); operator_.m_preconditions.reserve(numberOfPrevailConditions);
for (size_t j = 0; j < numberOfPrevailConditions; j++) for (size_t j = 0; j < numberOfPrevailConditions; j++)
operator_.m_preconditions.emplace_back(AssignedVariable::fromSAS(istream, variables)); operator_.m_preconditions.emplace_back(Condition::fromSAS(istream, variables));
const auto numberOfEffects = utils::parse<size_t>(istream); const auto numberOfEffects = utils::parse<size_t>(istream);
operator_.m_effects.reserve(numberOfEffects); operator_.m_effects.reserve(numberOfEffects);
for (size_t j = 0; j < numberOfEffects; j++) for (size_t j = 0; j < numberOfEffects; j++)
{ operator_.m_effects.emplace_back(Effect::fromSAS(istream, variables, operator_.m_preconditions));
Effect::Conditions conditions;
const auto numberOfEffectConditions = utils::parse<size_t>(istream);
conditions.reserve(numberOfEffectConditions);
for (size_t k = 0; k < numberOfEffectConditions; k++)
conditions.emplace_back(AssignedVariable::fromSAS(istream, variables));
const auto variableTransition = VariableTransition::fromSAS(istream, variables);
if (&variableTransition.valueBefore() != &Value::Any)
operator_.m_preconditions.emplace_back(AssignedVariable(variableTransition.variable(), variableTransition.valueBefore()));
const Effect::Condition postcondition = {variableTransition.variable(), variableTransition.valueAfter()};
const Effect effect = {std::move(conditions), std::move(postcondition)};
operator_.m_effects.push_back(std::move(effect));
}
operator_.m_costs = utils::parse<size_t>(istream); operator_.m_costs = utils::parse<size_t>(istream);

View File

@ -36,10 +36,12 @@ void TranslatorASP::checkSupport() const
std::for_each(operators.cbegin(), operators.cend(), std::for_each(operators.cbegin(), operators.cend(),
[&](const auto &operator_) [&](const auto &operator_)
{ {
std::for_each(operator_.effects().cbegin(), operator_.effects().cend(), const auto &effects = operator_.effects();
std::for_each(effects.cbegin(), effects.cend(),
[&](const auto &effect) [&](const auto &effect)
{ {
if (!effect.conditions.empty()) if (!effect.conditions().empty())
throw TranslatorException("Conditional effects are currently unsupported"); throw TranslatorException("Conditional effects are currently unsupported");
}); });
}); });
@ -142,8 +144,8 @@ void TranslatorASP::translate(std::ostream &ostream) const
[&](const auto &effect) [&](const auto &effect)
{ {
ostream << "postcondition(" << operator_.predicate() ostream << "postcondition(" << operator_.predicate()
<< ", " << effect.postcondition.value().name() << ", " << effect.postcondition().value().name()
<< ", " << (effect.postcondition.value().sign() == Value::Sign::Positive ? "true" : "false") << ", " << (effect.postcondition().value().sign() == Value::Sign::Positive ? "true" : "false")
<< ")." << std::endl; << ")." << std::endl;
}); });
}); });

View File

@ -76,8 +76,8 @@ TEST_F(SASParserTests, ParseValidSASFile)
ASSERT_EQ(&description.operators()[0].preconditions()[1].value(), &description.variables()[16].values()[1]); ASSERT_EQ(&description.operators()[0].preconditions()[1].value(), &description.variables()[16].values()[1]);
ASSERT_EQ(&description.operators()[0].preconditions()[2].value(), &description.variables()[0].values()[8]); ASSERT_EQ(&description.operators()[0].preconditions()[2].value(), &description.variables()[0].values()[8]);
ASSERT_EQ(description.operators()[0].effects().size(), 1); ASSERT_EQ(description.operators()[0].effects().size(), 1);
ASSERT_EQ(description.operators()[0].effects()[0].conditions.size(), 0); ASSERT_EQ(description.operators()[0].effects()[0].conditions().size(), 0);
ASSERT_EQ(&description.operators()[0].effects()[0].postcondition.value(), &description.variables()[0].values()[0]); ASSERT_EQ(&description.operators()[0].effects()[0].postcondition().value(), &description.variables()[0].values()[0]);
ASSERT_EQ(description.operators()[33].predicate().name, "queue-write"); ASSERT_EQ(description.operators()[33].predicate().name, "queue-write");
ASSERT_EQ(description.operators()[33].predicate().arguments.size(), 4); ASSERT_EQ(description.operators()[33].predicate().arguments.size(), 4);
ASSERT_EQ(description.operators()[33].predicate().arguments[0], "philosopher-1"); ASSERT_EQ(description.operators()[33].predicate().arguments[0], "philosopher-1");
@ -86,9 +86,9 @@ TEST_F(SASParserTests, ParseValidSASFile)
ASSERT_EQ(&description.operators()[33].preconditions()[0].value(), &description.variables()[1].values()[3]); ASSERT_EQ(&description.operators()[33].preconditions()[0].value(), &description.variables()[1].values()[3]);
ASSERT_EQ(&description.operators()[33].preconditions()[1].value(), &description.variables()[2].values()[2]); ASSERT_EQ(&description.operators()[33].preconditions()[1].value(), &description.variables()[2].values()[2]);
ASSERT_EQ(description.operators()[33].effects().size(), 3); ASSERT_EQ(description.operators()[33].effects().size(), 3);
ASSERT_EQ(description.operators()[33].effects()[0].conditions.size(), 0); ASSERT_EQ(description.operators()[33].effects()[0].conditions().size(), 0);
ASSERT_EQ(&description.operators()[33].effects()[0].postcondition.value(), &description.variables()[1].values()[7]); ASSERT_EQ(&description.operators()[33].effects()[0].postcondition().value(), &description.variables()[1].values()[7]);
ASSERT_EQ(&description.operators()[33].effects()[2].postcondition.value(), &description.variables()[35].values()[0]); ASSERT_EQ(&description.operators()[33].effects()[2].postcondition().value(), &description.variables()[35].values()[0]);
ASSERT_EQ(description.axiomRules().size(), 33); ASSERT_EQ(description.axiomRules().size(), 33);
ASSERT_EQ(description.axiomRules()[0].conditions().size(), 4); ASSERT_EQ(description.axiomRules()[0].conditions().size(), 4);