From 37af6d5127a533ecd222caa4fbc205fc98bd9351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sun, 22 May 2016 15:00:54 +0200 Subject: [PATCH] Made AxiomRule a proper class. --- include/plasp/sas/AxiomRule.h | 23 +++++++++--- src/plasp/sas/AxiomRule.cpp | 69 +++++++++++++++++++++++++++++++++++ src/plasp/sas/Debugging.cpp | 11 ++++-- src/plasp/sas/Description.cpp | 23 +----------- tests/TestSASParser.cpp | 18 ++++----- 5 files changed, 103 insertions(+), 41 deletions(-) create mode 100644 src/plasp/sas/AxiomRule.cpp diff --git a/include/plasp/sas/AxiomRule.h b/include/plasp/sas/AxiomRule.h index ff35e3c..c7b5810 100644 --- a/include/plasp/sas/AxiomRule.h +++ b/include/plasp/sas/AxiomRule.h @@ -17,18 +17,29 @@ namespace sas // //////////////////////////////////////////////////////////////////////////////////////////////////// -struct AxiomRule; +class AxiomRule; using AxiomRules = std::vector; //////////////////////////////////////////////////////////////////////////////////////////////////// -struct AxiomRule +class AxiomRule { - using Condition = AssignedVariable; - using Conditions = AssignedVariables; + 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; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/sas/AxiomRule.cpp b/src/plasp/sas/AxiomRule.cpp new file mode 100644 index 0000000..99a5d83 --- /dev/null +++ b/src/plasp/sas/AxiomRule.cpp @@ -0,0 +1,69 @@ +#include + +#include + +#include +#include + +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(istream, "begin_rule"); + + const auto numberOfConditions = utils::parse(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(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; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} diff --git a/src/plasp/sas/Debugging.cpp b/src/plasp/sas/Debugging.cpp index 9a5852e..7f03d4a 100644 --- a/src/plasp/sas/Debugging.cpp +++ b/src/plasp/sas/Debugging.cpp @@ -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; }); diff --git a/src/plasp/sas/Description.cpp b/src/plasp/sas/Description.cpp index 6b8d1e6..0cc637c 100644 --- a/src/plasp/sas/Description.cpp +++ b/src/plasp/sas/Description.cpp @@ -189,28 +189,7 @@ void Description::parseAxiomSection(std::istream &istream) m_axiomRules.reserve(numberOfAxiomRules); for (size_t i = 0; i < numberOfAxiomRules; i++) - { - utils::parseExpected(istream, "begin_rule"); - - const auto numberOfConditions = utils::parse(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(istream, "end_rule"); - } + m_axiomRules.emplace_back(AxiomRule::fromSAS(istream, m_variables)); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/tests/TestSASParser.cpp b/tests/TestSASParser.cpp index d7df800..7bb9e09 100644 --- a/tests/TestSASParser.cpp +++ b/tests/TestSASParser.cpp @@ -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) {