From c7c7a93eba57890d74aeae6129952b82823a82e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sat, 21 May 2016 17:09:55 +0200 Subject: [PATCH] Outsourced SAS value parsing. --- include/plasp/sas/AssignedVariable.h | 44 ++++++++ include/plasp/sas/Description.h | 5 - include/plasp/sas/MutexGroup.h | 20 +++- include/plasp/sas/Value.h | 4 + include/plasp/sas/Variable.h | 18 +-- include/plasp/sas/VariableTransition.h | 43 ++++++++ src/plasp/sas/AssignedVariable.cpp | 79 ++++++++++++++ src/plasp/sas/Description.cpp | 145 ++++++------------------- src/plasp/sas/MutexGroup.cpp | 47 ++++++++ src/plasp/sas/TranslatorASP.cpp | 16 +-- src/plasp/sas/Value.cpp | 16 +++ src/plasp/sas/Variable.cpp | 12 ++ src/plasp/sas/VariableTransition.cpp | 70 ++++++++++++ tests/TestSASParser.cpp | 46 ++++---- 14 files changed, 394 insertions(+), 171 deletions(-) create mode 100644 include/plasp/sas/AssignedVariable.h create mode 100644 include/plasp/sas/VariableTransition.h create mode 100644 src/plasp/sas/AssignedVariable.cpp create mode 100644 src/plasp/sas/MutexGroup.cpp create mode 100644 src/plasp/sas/VariableTransition.cpp diff --git a/include/plasp/sas/AssignedVariable.h b/include/plasp/sas/AssignedVariable.h new file mode 100644 index 0000000..3f9d63f --- /dev/null +++ b/include/plasp/sas/AssignedVariable.h @@ -0,0 +1,44 @@ +#ifndef __SAS__ASSIGNED_VARIABLE_H +#define __SAS__ASSIGNED_VARIABLE_H + +#include + +#include +#include + +namespace plasp +{ +namespace sas +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// AssignedVariable +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class AssignedVariable +{ + public: + static AssignedVariable fromSAS(std::istream &istream, const std::vector &variables); + static AssignedVariable fromSAS(std::istream &istream, const Variable &variable); + + public: + AssignedVariable(const Variable &variable, const Value &value); + + const Variable &variable() const; + const Value &value() const; + + private: + AssignedVariable(); + + const Variable *m_variable; + const Value *m_value; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/include/plasp/sas/Description.h b/include/plasp/sas/Description.h index 9524ad1..206e6e4 100644 --- a/include/plasp/sas/Description.h +++ b/include/plasp/sas/Description.h @@ -42,11 +42,6 @@ class Description private: Description(); - const Variable &parseVariable(std::istream &istream) const; - const Value &parseValue(std::istream &istream, const Variable &variable) const; - AssignedVariable parseAssignedVariable(std::istream &istream) const; - VariableTransition parseVariableTransition(std::istream &istream) const; - void parseVersionSection(std::istream &istream) const; void parseMetricSection(std::istream &istream); void parseVariablesSection(std::istream &istream); diff --git a/include/plasp/sas/MutexGroup.h b/include/plasp/sas/MutexGroup.h index ddda781..0d19c26 100644 --- a/include/plasp/sas/MutexGroup.h +++ b/include/plasp/sas/MutexGroup.h @@ -1,7 +1,7 @@ #ifndef __SAS__MUTEX_GROUP_H #define __SAS__MUTEX_GROUP_H -#include +#include namespace plasp { @@ -14,13 +14,21 @@ namespace sas // //////////////////////////////////////////////////////////////////////////////////////////////////// -// TODO: make consistent with initial state/goal -struct MutexGroup +class MutexGroup { - using Fact = AssignedVariable; - using Facts = std::vector; + public: + using Fact = AssignedVariable; + using Facts = std::vector; - Facts facts; + static MutexGroup fromSAS(std::istream &istream, const std::vector &variables); + + public: + const Facts &facts() const; + + private: + MutexGroup() = default; + + Facts m_facts; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/sas/Value.h b/include/plasp/sas/Value.h index de66b04..668db0d 100644 --- a/include/plasp/sas/Value.h +++ b/include/plasp/sas/Value.h @@ -10,6 +10,9 @@ namespace plasp namespace sas { +// Forward declarations +class Variable; + //////////////////////////////////////////////////////////////////////////////////////////////////// // // Value @@ -28,6 +31,7 @@ struct Value static const Value Any; static Value fromSAS(std::istream &istream); + static const Value &referenceFromSAS(std::istream &istream, const Variable &variable); public: void printAsSAS(std::ostream &ostream) const; diff --git a/include/plasp/sas/Variable.h b/include/plasp/sas/Variable.h index b25db1a..d02bf98 100644 --- a/include/plasp/sas/Variable.h +++ b/include/plasp/sas/Variable.h @@ -22,6 +22,7 @@ class Variable { public: static Variable fromSAS(std::istream &istream); + static const Variable &referenceFromSAS(std::istream &istream, const std::vector &variables); public: const std::string &name() const; @@ -38,23 +39,6 @@ class Variable //////////////////////////////////////////////////////////////////////////////////////////////////// -struct AssignedVariable -{ - const Variable &variable; - const Value &value; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -struct VariableTransition -{ - const Variable &variable; - const Value &valueBefore; - const Value &valueAfter; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - } } diff --git a/include/plasp/sas/VariableTransition.h b/include/plasp/sas/VariableTransition.h new file mode 100644 index 0000000..57ee00c --- /dev/null +++ b/include/plasp/sas/VariableTransition.h @@ -0,0 +1,43 @@ +#ifndef __SAS__VARIABLE_TRANSITION_H +#define __SAS__VARIABLE_TRANSITION_H + +#include + +#include +#include + +namespace plasp +{ +namespace sas +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// VariableTransition +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class VariableTransition +{ + public: + static VariableTransition fromSAS(std::istream &istream, const std::vector &variables); + + public: + const Variable &variable() const; + const Value &valueBefore() const; + const Value &valueAfter() const; + + private: + VariableTransition(); + + const Variable *m_variable; + const Value *m_valueBefore; + const Value *m_valueAfter; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/src/plasp/sas/AssignedVariable.cpp b/src/plasp/sas/AssignedVariable.cpp new file mode 100644 index 0000000..e22fcd8 --- /dev/null +++ b/src/plasp/sas/AssignedVariable.cpp @@ -0,0 +1,79 @@ +#include + +#include + +#include + +#include + +namespace plasp +{ +namespace sas +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// AssignedVariable +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +AssignedVariable::AssignedVariable() +: m_variable{nullptr}, + m_value{nullptr} +{ +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +AssignedVariable::AssignedVariable(const Variable &variable, const Value &value) +: m_variable{&variable}, + m_value{&value} +{ +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +AssignedVariable AssignedVariable::fromSAS(std::istream &istream, const std::vector &variables) +{ + AssignedVariable assignedVariable; + + assignedVariable.m_variable = &Variable::referenceFromSAS(istream, variables); + assignedVariable.m_value = &Value::referenceFromSAS(istream, *assignedVariable.m_variable); + + return assignedVariable; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +AssignedVariable AssignedVariable::fromSAS(std::istream &istream, const Variable &variable) +{ + AssignedVariable assignedVariable; + + assignedVariable.m_variable = &variable; + assignedVariable.m_value = &Value::referenceFromSAS(istream, *assignedVariable.m_variable); + + return assignedVariable; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Variable &AssignedVariable::variable() const +{ + BOOST_ASSERT(m_variable != nullptr); + + return *m_variable; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Value &AssignedVariable::value() const +{ + BOOST_ASSERT(m_value != nullptr); + + return *m_value; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} diff --git a/src/plasp/sas/Description.cpp b/src/plasp/sas/Description.cpp index 78c9d87..6f26dd7 100644 --- a/src/plasp/sas/Description.cpp +++ b/src/plasp/sas/Description.cpp @@ -8,6 +8,7 @@ #include #include +#include namespace plasp { @@ -145,11 +146,11 @@ void Description::print(std::ostream &ostream) const { ostream << "\tmutex group:" << std::endl; - std::for_each(mutexGroup.facts.cbegin(), mutexGroup.facts.cend(), + std::for_each(mutexGroup.facts().cbegin(), mutexGroup.facts().cend(), [&](const auto &fact) { - ostream << "\t\t" << fact.variable.name() << " = "; - fact.value.printAsSAS(ostream); + ostream << "\t\t" << fact.variable().name() << " = "; + fact.value().printAsSAS(ostream); ostream << std::endl; }); }); @@ -160,8 +161,8 @@ void Description::print(std::ostream &ostream) const std::for_each(m_initialStateFacts.cbegin(), m_initialStateFacts.cend(), [&](const auto &initialStateFact) { - ostream << "\t" << initialStateFact.variable.name() << " = "; - initialStateFact.value.printAsSAS(ostream); + ostream << "\t" << initialStateFact.variable().name() << " = "; + initialStateFact.value().printAsSAS(ostream); ostream << std::endl; }); @@ -171,8 +172,8 @@ void Description::print(std::ostream &ostream) const std::for_each(m_goalFacts.cbegin(), m_goalFacts.cend(), [&](const auto &goalFact) { - ostream << "\t" << goalFact.variable.name() << " = "; - goalFact.value.printAsSAS(ostream); + ostream << "\t" << goalFact.variable().name() << " = "; + goalFact.value().printAsSAS(ostream); ostream << std::endl; }); @@ -188,8 +189,8 @@ void Description::print(std::ostream &ostream) const std::for_each(operator_.preconditions.cbegin(), operator_.preconditions.cend(), [&](const auto &precondition) { - std::cout << "\t\t\t" << precondition.variable.name() << " = "; - precondition.value.printAsSAS(ostream); + std::cout << "\t\t\t" << precondition.variable().name() << " = "; + precondition.value().printAsSAS(ostream); ostream << std::endl; }); @@ -204,14 +205,14 @@ void Description::print(std::ostream &ostream) const std::for_each(effect.conditions.cbegin(), effect.conditions.cend(), [&](const auto &condition) { - ostream << "\t\t\t\t\t" << condition.variable.name() << " = "; - condition.value.printAsSAS(ostream); + ostream << "\t\t\t\t\t" << condition.variable().name() << " = "; + condition.value().printAsSAS(ostream); ostream << std::endl; }); ostream << "\t\t\t\tpostcondition:" << std::endl; - ostream << "\t\t\t\t\t" << effect.postcondition.variable.name() << " = "; - effect.postcondition.value.printAsSAS(ostream); + ostream << "\t\t\t\t\t" << effect.postcondition.variable().name() << " = "; + effect.postcondition.value().printAsSAS(ostream); ostream << std::endl; }); @@ -230,68 +231,20 @@ void Description::print(std::ostream &ostream) const std::for_each(axiomRule.conditions.cbegin(), axiomRule.conditions.cend(), [&](const auto &condition) { - ostream << "\t\t\t" << condition.variable.name() << " = "; - condition.value.printAsSAS(ostream); + ostream << "\t\t\t" << condition.variable().name() << " = "; + condition.value().printAsSAS(ostream); ostream << std::endl; }); 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; }); } //////////////////////////////////////////////////////////////////////////////////////////////////// -const Variable &Description::parseVariable(std::istream &istream) const -{ - const auto variableID = utils::parse(istream); - - if (variableID >= m_variables.size()) - throw utils::ParserException("Variable index out of range (index " + std::to_string(variableID) + ")"); - - return m_variables[variableID]; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Value &Description::parseValue(std::istream &istream, const Variable &variable) const -{ - const auto valueID = utils::parse(istream); - - if (valueID == -1) - return Value::Any; - - if (valueID < 0 || static_cast(valueID) >= variable.values().size()) - throw utils::ParserException("Value index out of range (variable " + variable.name() + ", index " + std::to_string(valueID) + ")"); - - return variable.values()[valueID]; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -AssignedVariable Description::parseAssignedVariable(std::istream &istream) const -{ - const auto &variable = parseVariable(istream); - const auto &value = parseValue(istream, variable); - - return {variable, value}; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -VariableTransition Description::parseVariableTransition(std::istream &istream) const -{ - const auto &variable = parseVariable(istream); - const auto &valueBefore = parseValue(istream, variable); - const auto &valueAfter = parseValue(istream, variable); - - return {variable, valueBefore, valueAfter}; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - void Description::parseVersionSection(std::istream &istream) const { // Version section @@ -332,25 +285,10 @@ void Description::parseVariablesSection(std::istream &istream) void Description::parseMutexSection(std::istream &istream) { const auto numberOfMutexGroups = utils::parse(istream); - m_mutexGroups.resize(numberOfMutexGroups); + m_mutexGroups.reserve(numberOfMutexGroups); for (size_t i = 0; i < numberOfMutexGroups; i++) - { - utils::parseExpected(istream, "begin_mutex_group"); - - auto &mutexGroup = m_mutexGroups[i]; - - const auto numberOfFacts = utils::parse(istream); - mutexGroup.facts.reserve(numberOfFacts); - - for (size_t j = 0; j < numberOfFacts; j++) - { - const auto fact = parseAssignedVariable(istream); - mutexGroup.facts.push_back(std::move(fact)); - } - - utils::parseExpected(istream, "end_mutex_group"); - } + m_mutexGroups.emplace_back(MutexGroup::fromSAS(istream, m_variables)); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -362,12 +300,7 @@ void Description::parseInitialStateSection(std::istream &istream) m_initialStateFacts.reserve(m_variables.size()); for (size_t i = 0; i < m_variables.size(); i++) - { - const auto &variable = m_variables[i]; - const auto &value = parseValue(istream, variable); - - m_initialStateFacts.push_back({variable, value}); - } + m_initialStateFacts.emplace_back(AssignedVariable::fromSAS(istream, m_variables[i])); utils::parseExpected(istream, "end_state"); } @@ -382,10 +315,7 @@ void Description::parseGoalSection(std::istream &istream) m_goalFacts.reserve(numberOfGoalFacts); for (size_t i = 0; i < numberOfGoalFacts; i++) - { - const auto goalFact = parseAssignedVariable(istream); - m_goalFacts.push_back(std::move(goalFact)); - } + m_goalFacts.emplace_back(AssignedVariable::fromSAS(istream, m_variables)); utils::parseExpected(istream, "end_goal"); } @@ -430,10 +360,7 @@ void Description::parseOperatorSection(std::istream &istream) operator_.preconditions.reserve(numberOfPrevailConditions); for (size_t j = 0; j < numberOfPrevailConditions; j++) - { - const auto precondition = parseAssignedVariable(istream); - operator_.preconditions.push_back(std::move(precondition)); - } + operator_.preconditions.emplace_back(AssignedVariable::fromSAS(istream, m_variables)); const auto numberOfEffects = utils::parse(istream); operator_.effects.reserve(numberOfEffects); @@ -446,17 +373,14 @@ void Description::parseOperatorSection(std::istream &istream) conditions.reserve(numberOfEffectConditions); for (size_t k = 0; k < numberOfEffectConditions; k++) - { - const auto condition = parseAssignedVariable(istream); - conditions.push_back(std::move(condition)); - } + conditions.emplace_back(AssignedVariable::fromSAS(istream, m_variables)); - const auto variableTransition = parseVariableTransition(istream); + const auto variableTransition = VariableTransition::fromSAS(istream, m_variables); - if (&variableTransition.valueBefore != &Value::Any) - operator_.preconditions.push_back({variableTransition.variable, variableTransition.valueBefore}); + if (&variableTransition.valueBefore() != &Value::Any) + operator_.preconditions.emplace_back(AssignedVariable(variableTransition.variable(), variableTransition.valueBefore())); - const Effect::Condition postcondition = {variableTransition.variable, variableTransition.valueAfter}; + const Effect::Condition postcondition = {variableTransition.variable(), variableTransition.valueAfter()}; const Effect effect = {std::move(conditions), std::move(postcondition)}; operator_.effects.push_back(std::move(effect)); } @@ -484,17 +408,14 @@ void Description::parseAxiomSection(std::istream &istream) conditions.reserve(numberOfConditions); for (size_t j = 0; j < numberOfConditions; j++) - { - const auto condition = parseAssignedVariable(istream); - conditions.push_back(std::move(condition)); - } + conditions.emplace_back(AssignedVariable::fromSAS(istream, m_variables)); - const auto variableTransition = parseVariableTransition(istream); + const auto variableTransition = VariableTransition::fromSAS(istream, m_variables); - if (&variableTransition.valueBefore != &Value::Any) - conditions.push_back({variableTransition.variable, variableTransition.valueBefore}); + if (&variableTransition.valueBefore() != &Value::Any) + conditions.emplace_back(AssignedVariable(variableTransition.variable(), variableTransition.valueBefore())); - const AxiomRule::Condition postcondition = {variableTransition.variable, variableTransition.valueAfter}; + const AxiomRule::Condition postcondition = {variableTransition.variable(), variableTransition.valueAfter()}; const AxiomRule axiomRule = {std::move(conditions), std::move(postcondition)}; m_axiomRules.push_back(std::move(axiomRule)); diff --git a/src/plasp/sas/MutexGroup.cpp b/src/plasp/sas/MutexGroup.cpp new file mode 100644 index 0000000..701f771 --- /dev/null +++ b/src/plasp/sas/MutexGroup.cpp @@ -0,0 +1,47 @@ +#include + +#include + +#include + +namespace plasp +{ +namespace sas +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// MutexGroup +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +MutexGroup MutexGroup::fromSAS(std::istream &istream, const std::vector &variables) +{ + MutexGroup mutexGroup; + + utils::parseExpected(istream, "begin_mutex_group"); + + const auto numberOfFacts = utils::parse(istream); + mutexGroup.m_facts.reserve(numberOfFacts); + + for (size_t j = 0; j < numberOfFacts; j++) + { + mutexGroup.m_facts.emplace_back(AssignedVariable::fromSAS(istream, variables)); + } + + utils::parseExpected(istream, "end_mutex_group"); + + return mutexGroup; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const MutexGroup::Facts &MutexGroup::facts() const +{ + return m_facts; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} diff --git a/src/plasp/sas/TranslatorASP.cpp b/src/plasp/sas/TranslatorASP.cpp index 4aab003..cf027da 100644 --- a/src/plasp/sas/TranslatorASP.cpp +++ b/src/plasp/sas/TranslatorASP.cpp @@ -98,7 +98,7 @@ void TranslatorASP::translate(std::ostream &ostream) const [&](const auto &initialStateFact) { ostream << "init("; - initialStateFact.value.printAsASP(ostream); + initialStateFact.value().printAsASP(ostream); ostream << ")." << std::endl; }); @@ -111,7 +111,7 @@ void TranslatorASP::translate(std::ostream &ostream) const [&](const auto &goalFact) { ostream << "goal("; - goalFact.value.printAsASP(ostream); + goalFact.value().printAsASP(ostream); ostream << ")." << std::endl; }); @@ -129,8 +129,8 @@ void TranslatorASP::translate(std::ostream &ostream) const [&](const auto &precondition) { ostream << "precondition(" << operator_.predicate - << ", " << precondition.value.name() - << ", " << (precondition.value.sign() == Value::Sign::Positive ? "true" : "false") + << ", " << precondition.value().name() + << ", " << (precondition.value().sign() == Value::Sign::Positive ? "true" : "false") << ")." << std::endl; }); @@ -138,8 +138,8 @@ void TranslatorASP::translate(std::ostream &ostream) const [&](const auto &effect) { ostream << "postcondition(" << operator_.predicate - << ", " << effect.postcondition.value.name() - << ", " << (effect.postcondition.value.sign() == Value::Sign::Positive ? "true" : "false") + << ", " << effect.postcondition.value().name() + << ", " << (effect.postcondition.value().sign() == Value::Sign::Positive ? "true" : "false") << ")." << std::endl; }); }); @@ -154,11 +154,11 @@ void TranslatorASP::translate(std::ostream &ostream) const { ostream << ":- time(T)"; - std::for_each(mutexGroup.facts.cbegin(), mutexGroup.facts.cend(), + std::for_each(mutexGroup.facts().cbegin(), mutexGroup.facts().cend(), [&](const auto &fact) { ostream << ", holds("; - fact.value.printAsASP(ostream); + fact.value().printAsASP(ostream); ostream << ", T)"; }); diff --git a/src/plasp/sas/Value.cpp b/src/plasp/sas/Value.cpp index 0dc52fc..35e62e7 100644 --- a/src/plasp/sas/Value.cpp +++ b/src/plasp/sas/Value.cpp @@ -2,6 +2,7 @@ #include +#include #include namespace plasp @@ -73,6 +74,21 @@ Value Value::fromSAS(std::istream &istream) //////////////////////////////////////////////////////////////////////////////////////////////////// +const Value &Value::referenceFromSAS(std::istream &istream, const Variable &variable) +{ + const auto valueID = utils::parse(istream); + + if (valueID == -1) + return Value::Any; + + if (valueID < 0 || static_cast(valueID) >= variable.values().size()) + throw utils::ParserException("Value index out of range (variable " + variable.name() + ", index " + std::to_string(valueID) + ")"); + + return variable.values()[valueID]; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + Value::Sign Value::sign() const { return m_sign; diff --git a/src/plasp/sas/Variable.cpp b/src/plasp/sas/Variable.cpp index 4d44c08..bfdf656 100644 --- a/src/plasp/sas/Variable.cpp +++ b/src/plasp/sas/Variable.cpp @@ -44,6 +44,18 @@ Variable Variable::fromSAS(std::istream &istream) //////////////////////////////////////////////////////////////////////////////////////////////////// +const Variable &Variable::referenceFromSAS(std::istream &istream, const std::vector &variables) +{ + const auto variableID = utils::parse(istream); + + if (variableID >= variables.size()) + throw utils::ParserException("Variable index out of range (index " + std::to_string(variableID) + ")"); + + return variables[variableID]; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + const std::string &Variable::name() const { return m_name; diff --git a/src/plasp/sas/VariableTransition.cpp b/src/plasp/sas/VariableTransition.cpp new file mode 100644 index 0000000..a530dea --- /dev/null +++ b/src/plasp/sas/VariableTransition.cpp @@ -0,0 +1,70 @@ +#include + +#include + +#include + +#include + +namespace plasp +{ +namespace sas +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// VariableTransition +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +VariableTransition::VariableTransition() +: m_variable{nullptr}, + m_valueBefore{nullptr}, + m_valueAfter{nullptr} +{ +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +VariableTransition VariableTransition::fromSAS(std::istream &istream, const std::vector &variables) +{ + VariableTransition variableTransition; + + variableTransition.m_variable = &Variable::referenceFromSAS(istream, variables); + variableTransition.m_valueBefore = &Value::referenceFromSAS(istream, *variableTransition.m_variable); + variableTransition.m_valueAfter = &Value::referenceFromSAS(istream, *variableTransition.m_variable); + + return variableTransition; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Variable &VariableTransition::variable() const +{ + BOOST_ASSERT(m_variable != nullptr); + + return *m_variable; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Value &VariableTransition::valueBefore() const +{ + BOOST_ASSERT(m_valueBefore != nullptr); + + return *m_valueBefore; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Value &VariableTransition::valueAfter() const +{ + BOOST_ASSERT(m_valueAfter != nullptr); + + return *m_valueAfter; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} diff --git a/tests/TestSASParser.cpp b/tests/TestSASParser.cpp index 5e774a2..2cd23fe 100644 --- a/tests/TestSASParser.cpp +++ b/tests/TestSASParser.cpp @@ -53,18 +53,18 @@ TEST_F(SASParserTests, ParseValidSASFile) ASSERT_EQ(description.variables()[36].values()[1].name(), "queue-tail-msg(forks-1-, fork)"); ASSERT_EQ(description.mutexGroups().size(), 8); - ASSERT_EQ(description.mutexGroups()[0].facts.size(), 9); - ASSERT_EQ(&description.mutexGroups()[0].facts[0].value, &description.variables()[0].values()[0]); - ASSERT_EQ(description.mutexGroups()[7].facts.size(), 2); - ASSERT_EQ(&description.mutexGroups()[7].facts[1].value, &description.variables()[34].values()[1]); + ASSERT_EQ(description.mutexGroups()[0].facts().size(), 9); + ASSERT_EQ(&description.mutexGroups()[0].facts()[0].value(), &description.variables()[0].values()[0]); + ASSERT_EQ(description.mutexGroups()[7].facts().size(), 2); + ASSERT_EQ(&description.mutexGroups()[7].facts()[1].value(), &description.variables()[34].values()[1]); ASSERT_EQ(description.initialStateFacts().size(), 37); - ASSERT_EQ(&description.initialStateFacts()[0].value, &description.variables()[0].values()[8]); - ASSERT_EQ(&description.initialStateFacts()[36].value, &description.variables()[36].values()[1]); + ASSERT_EQ(&description.initialStateFacts()[0].value(), &description.variables()[0].values()[8]); + ASSERT_EQ(&description.initialStateFacts()[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.goalFacts()[0].value(), &description.variables()[6].values()[0]); + ASSERT_EQ(&description.goalFacts()[1].value(), &description.variables()[7].values()[0]); ASSERT_EQ(description.operators().size(), 34); ASSERT_EQ(description.operators()[0].predicate.name, "activate-trans"); @@ -72,34 +72,34 @@ TEST_F(SASParserTests, ParseValidSASFile) ASSERT_EQ(description.operators()[0].predicate.arguments[0], "philosopher-0"); ASSERT_EQ(description.operators()[0].predicate.arguments[4], "state-3"); ASSERT_EQ(description.operators()[0].preconditions.size(), 3); - ASSERT_EQ(&description.operators()[0].preconditions[0].value, &description.variables()[4].values()[4]); - 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[0].value(), &description.variables()[4].values()[4]); + 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].effects.size(), 1); 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.arguments.size(), 4); ASSERT_EQ(description.operators()[33].predicate.arguments[0], "philosopher-1"); ASSERT_EQ(description.operators()[33].predicate.arguments[3], "fork"); ASSERT_EQ(description.operators()[33].preconditions.size(), 2); - 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[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].effects.size(), 3); 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[2].postcondition.value, &description.variables()[35].values()[0]); + 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.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()[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()[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) {