Made AxiomRule a proper class.

This commit is contained in:
Patrick Lühne 2016-05-22 15:00:54 +02:00
parent 9fce86ffaf
commit 37af6d5127
5 changed files with 103 additions and 41 deletions

View File

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

View File

@ -0,0 +1,69 @@
#include <plasp/sas/AxiomRule.h>
#include <iostream>
#include <plasp/sas/VariableTransition.h>
#include <plasp/utils/Parsing.h>
namespace plasp
{
namespace sas
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// AxiomRule
//
////////////////////////////////////////////////////////////////////////////////////////////////////
AxiomRule::AxiomRule(AxiomRule::Conditions conditions, AxiomRule::Condition postcondition)
: m_conditions(std::move(conditions)),
m_postcondition(std::move(postcondition))
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
AxiomRule AxiomRule::fromSAS(std::istream &istream, const Variables &variables)
{
utils::parseExpected<std::string>(istream, "begin_rule");
const auto numberOfConditions = utils::parse<size_t>(istream);
Conditions conditions;
conditions.reserve(numberOfConditions);
for (size_t j = 0; j < numberOfConditions; j++)
conditions.emplace_back(AssignedVariable::fromSAS(istream, variables));
const auto variableTransition = VariableTransition::fromSAS(istream, variables);
if (&variableTransition.valueBefore() != &Value::Any)
conditions.emplace_back(AssignedVariable(variableTransition.variable(), variableTransition.valueBefore()));
utils::parseExpected<std::string>(istream, "end_rule");
const Condition postcondition(variableTransition.variable(), variableTransition.valueAfter());
const AxiomRule axiomRule(std::move(conditions), std::move(postcondition));
return axiomRule;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const AxiomRule::Conditions &AxiomRule::conditions() const
{
return m_conditions;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const AxiomRule::Condition &AxiomRule::postcondition() const
{
return m_postcondition;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

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

View File

@ -189,28 +189,7 @@ void Description::parseAxiomSection(std::istream &istream)
m_axiomRules.reserve(numberOfAxiomRules);
for (size_t i = 0; i < numberOfAxiomRules; i++)
{
utils::parseExpected<std::string>(istream, "begin_rule");
const auto numberOfConditions = utils::parse<size_t>(istream);
AxiomRule::Conditions conditions;
conditions.reserve(numberOfConditions);
for (size_t j = 0; j < numberOfConditions; j++)
conditions.emplace_back(AssignedVariable::fromSAS(istream, m_variables));
const auto variableTransition = VariableTransition::fromSAS(istream, m_variables);
if (&variableTransition.valueBefore() != &Value::Any)
conditions.emplace_back(AssignedVariable(variableTransition.variable(), variableTransition.valueBefore()));
const AxiomRule::Condition postcondition = {variableTransition.variable(), variableTransition.valueAfter()};
const AxiomRule axiomRule = {std::move(conditions), std::move(postcondition)};
m_axiomRules.push_back(std::move(axiomRule));
utils::parseExpected<std::string>(istream, "end_rule");
}
m_axiomRules.emplace_back(AxiomRule::fromSAS(istream, m_variables));
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -91,15 +91,15 @@ TEST_F(SASParserTests, ParseValidSASFile)
ASSERT_EQ(&description.operators()[33].effects()[2].postcondition.value(), &description.variables()[35].values()[0]);
ASSERT_EQ(description.axiomRules().size(), 33);
ASSERT_EQ(description.axiomRules()[0].conditions.size(), 4);
ASSERT_EQ(&description.axiomRules()[0].conditions[0].value(), &description.variables()[0].values()[0]);
ASSERT_EQ(&description.axiomRules()[0].conditions[2].value(), &description.variables()[27].values()[0]);
ASSERT_EQ(&description.axiomRules()[0].conditions[3].value(), &description.variables()[8].values()[1]);
ASSERT_EQ(&description.axiomRules()[0].postcondition.value(), &description.variables()[8].values()[0]);
ASSERT_EQ(description.axiomRules()[32].conditions.size(), 2);
ASSERT_EQ(&description.axiomRules()[32].conditions[0].value(), &description.variables()[15].values()[0]);
ASSERT_EQ(&description.axiomRules()[32].conditions[1].value(), &description.variables()[25].values()[0]);
ASSERT_EQ(&description.axiomRules()[32].postcondition.value(), &description.variables()[25].values()[1]);
ASSERT_EQ(description.axiomRules()[0].conditions().size(), 4);
ASSERT_EQ(&description.axiomRules()[0].conditions()[0].value(), &description.variables()[0].values()[0]);
ASSERT_EQ(&description.axiomRules()[0].conditions()[2].value(), &description.variables()[27].values()[0]);
ASSERT_EQ(&description.axiomRules()[0].conditions()[3].value(), &description.variables()[8].values()[1]);
ASSERT_EQ(&description.axiomRules()[0].postcondition().value(), &description.variables()[8].values()[0]);
ASSERT_EQ(description.axiomRules()[32].conditions().size(), 2);
ASSERT_EQ(&description.axiomRules()[32].conditions()[0].value(), &description.variables()[15].values()[0]);
ASSERT_EQ(&description.axiomRules()[32].conditions()[1].value(), &description.variables()[25].values()[0]);
ASSERT_EQ(&description.axiomRules()[32].postcondition().value(), &description.variables()[25].values()[1]);
}
catch (const std::exception &e)
{