From 5e016d2d79d3538f5246b55f401724e244d5cd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sat, 21 May 2016 15:54:03 +0200 Subject: [PATCH] Made Variable a proper class. --- include/plasp/sas/Value.h | 5 +++ include/plasp/sas/Variable.h | 19 ++++++++---- src/plasp/sas/Description.cpp | 36 +++++++++++----------- src/plasp/sas/TranslatorASP.cpp | 4 +-- src/plasp/sas/Variable.cpp | 38 ++++++++++++++++++++--- tests/TestSASParser.cpp | 54 ++++++++++++++++----------------- 6 files changed, 99 insertions(+), 57 deletions(-) diff --git a/include/plasp/sas/Value.h b/include/plasp/sas/Value.h index 9613678..2c0d95a 100644 --- a/include/plasp/sas/Value.h +++ b/include/plasp/sas/Value.h @@ -3,6 +3,7 @@ #include #include +#include namespace plasp { @@ -31,6 +32,10 @@ struct Value //////////////////////////////////////////////////////////////////////////////////////////////////// +using Values = std::vector; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + std::ostream &operator <<(std::ostream &ostream, const Value &value); //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/sas/Variable.h b/include/plasp/sas/Variable.h index 18ccd15..b25db1a 100644 --- a/include/plasp/sas/Variable.h +++ b/include/plasp/sas/Variable.h @@ -18,15 +18,22 @@ namespace sas // //////////////////////////////////////////////////////////////////////////////////////////////////// -struct Variable +class Variable { - static Variable fromSAS(std::istream &istream); + public: + static Variable fromSAS(std::istream &istream); - using Values = std::vector; + public: + const std::string &name() const; + int axiomLayer() const; + const Values &values() const; - std::string name; - int axiomLayer; - Values values; + private: + Variable(); + + std::string m_name; + int m_axiomLayer; + Values m_values; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/sas/Description.cpp b/src/plasp/sas/Description.cpp index a15b9c3..ddbfedd 100644 --- a/src/plasp/sas/Description.cpp +++ b/src/plasp/sas/Description.cpp @@ -121,16 +121,18 @@ void Description::print(std::ostream &ostream) const std::for_each(m_variables.cbegin(), m_variables.cend(), [&](const auto &variable) { - ostream << "\t" << variable.name << ":" << std::endl; - ostream << "\t\tvalues: " << variable.values.size() << std::endl; + const auto &values = variable.values(); - std::for_each(variable.values.cbegin(), variable.values.cend(), + ostream << "\t" << variable.name() << ":" << std::endl; + ostream << "\t\tvalues: " << values.size() << std::endl; + + std::for_each(values.cbegin(), values.cend(), [&](const auto &value) { ostream << "\t\t\t" << value << std::endl; }); - ostream << "\t\taxiom layer: " << variable.axiomLayer << std::endl; + ostream << "\t\taxiom layer: " << variable.axiomLayer() << std::endl; }); // Mutex section @@ -144,7 +146,7 @@ void Description::print(std::ostream &ostream) const std::for_each(mutexGroup.facts.cbegin(), mutexGroup.facts.cend(), [&](const auto &fact) { - ostream << "\t\t" << fact.variable.name << " = " << fact.value << std::endl; + ostream << "\t\t" << fact.variable.name() << " = " << fact.value << std::endl; }); }); @@ -154,7 +156,7 @@ 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 << std::endl; + ostream << "\t" << initialStateFact.variable.name() << " = " << initialStateFact.value << std::endl; }); // Goal section @@ -163,7 +165,7 @@ 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 << std::endl; + ostream << "\t" << goalFact.variable.name() << " = " << goalFact.value << std::endl; }); // Operator section @@ -178,7 +180,7 @@ 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 << std::endl; + std::cout << "\t\t\t" << precondition.variable.name() << " = " << precondition.value << std::endl; }); ostream << "\t\teffects: " << operator_.effects.size() << std::endl; @@ -192,11 +194,11 @@ 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 << std::endl; + ostream << "\t\t\t\t\t" << condition.variable.name() << " = " << condition.value << std::endl; }); ostream << "\t\t\t\tpostcondition:" << std::endl; - ostream << "\t\t\t\t\t" << effect.postcondition.variable.name << " = " << effect.postcondition.value << std::endl; + ostream << "\t\t\t\t\t" << effect.postcondition.variable.name() << " = " << effect.postcondition.value << std::endl; }); ostream << "\t\tcosts: " << operator_.costs << std::endl; @@ -214,11 +216,11 @@ 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 << std::endl; + ostream << "\t\t\t" << condition.variable.name() << " = " << condition.value << std::endl; }); ostream << "\t\tpostcondition:" << std::endl; - ostream << "\t\t\t" << axiomRule.postcondition.variable.name << " = " << axiomRule.postcondition.value << std::endl; + ostream << "\t\t\t" << axiomRule.postcondition.variable.name() << " = " << axiomRule.postcondition.value << std::endl; }); } @@ -243,10 +245,10 @@ const Value &Description::parseValue(std::istream &istream, const Variable &vari 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) + ")"); + 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]; + return variable.values()[valueID]; } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -301,10 +303,10 @@ void Description::parseMetricSection(std::istream &istream) void Description::parseVariablesSection(std::istream &istream) { const auto numberOfVariables = utils::parse(istream); - m_variables.resize(numberOfVariables); + m_variables.reserve(numberOfVariables); for (size_t i = 0; i < numberOfVariables; i++) - m_variables[i] = Variable::fromSAS(istream); + m_variables.emplace_back(Variable::fromSAS(istream)); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/sas/TranslatorASP.cpp b/src/plasp/sas/TranslatorASP.cpp index e688078..58574d0 100644 --- a/src/plasp/sas/TranslatorASP.cpp +++ b/src/plasp/sas/TranslatorASP.cpp @@ -27,7 +27,7 @@ void TranslatorASP::checkSupport() const std::for_each(variables.cbegin(), variables.cend(), [&](const auto &variable) { - if (variable.axiomLayer != -1) + if (variable.axiomLayer() != -1) throw TranslatorException("Axiom layers are currently unsupported"); }); @@ -61,7 +61,7 @@ void TranslatorASP::translate(std::ostream &ostream) const std::for_each(variables.cbegin(), variables.cend(), [&](const auto &variable) { - const auto &values = variable.values; + const auto &values = variable.values(); std::for_each(values.cbegin(), values.cend(), [&](const auto &value) diff --git a/src/plasp/sas/Variable.cpp b/src/plasp/sas/Variable.cpp index 97a95f8..afb6d5e 100644 --- a/src/plasp/sas/Variable.cpp +++ b/src/plasp/sas/Variable.cpp @@ -15,23 +15,30 @@ namespace sas // //////////////////////////////////////////////////////////////////////////////////////////////////// +Variable::Variable() +: m_axiomLayer(-1) +{ +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + Variable Variable::fromSAS(std::istream &istream) { Variable variable; utils::parseExpected(istream, "begin_variable"); - variable.name = utils::parse(istream); - variable.axiomLayer = utils::parse(istream); + variable.m_name = utils::parse(istream); + variable.m_axiomLayer = utils::parse(istream); const auto numberOfValues = utils::parse(istream); - variable.values.resize(numberOfValues); + variable.m_values.resize(numberOfValues); try { for (size_t j = 0; j < numberOfValues; j++) { - auto &value = variable.values[j]; + auto &value = variable.m_values[j]; const auto sasSign = utils::parse(istream); @@ -49,7 +56,7 @@ Variable Variable::fromSAS(std::istream &istream) } catch (const std::exception &e) { - throw utils::ParserException("Could not parse variable " + variable.name + " (" + e.what() + ")"); + throw utils::ParserException("Could not parse variable " + variable.m_name + " (" + e.what() + ")"); } utils::parseExpected(istream, "end_variable"); @@ -59,5 +66,26 @@ Variable Variable::fromSAS(std::istream &istream) //////////////////////////////////////////////////////////////////////////////////////////////////// +const std::string &Variable::name() const +{ + return m_name; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +int Variable::axiomLayer() const +{ + return m_axiomLayer; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Values &Variable::values() const +{ + return m_values; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + } } diff --git a/tests/TestSASParser.cpp b/tests/TestSASParser.cpp index 3607ffb..4a5b457 100644 --- a/tests/TestSASParser.cpp +++ b/tests/TestSASParser.cpp @@ -45,26 +45,26 @@ TEST_F(SASParserTests, ParseValidSASFile) ASSERT_FALSE(description.usesActionCosts()); ASSERT_EQ(description.variables().size(), 37); - ASSERT_EQ(description.variables()[0].axiomLayer, -1); - ASSERT_EQ(description.variables()[0].values[0].sign, plasp::sas::Value::Sign::Positive); - ASSERT_EQ(description.variables()[0].values[0].name, "activate(philosopher-0, forks--pid-rfork)"); - ASSERT_EQ(description.variables()[36].axiomLayer, -1); - ASSERT_EQ(description.variables()[36].values[1].sign, plasp::sas::Value::Sign::Negative); - ASSERT_EQ(description.variables()[36].values[1].name, "queue-tail-msg(forks-1-, fork)"); + ASSERT_EQ(description.variables()[0].axiomLayer(), -1); + ASSERT_EQ(description.variables()[0].values()[0].sign, plasp::sas::Value::Sign::Positive); + ASSERT_EQ(description.variables()[0].values()[0].name, "activate(philosopher-0, forks--pid-rfork)"); + ASSERT_EQ(description.variables()[36].axiomLayer(), -1); + ASSERT_EQ(description.variables()[36].values()[1].sign, plasp::sas::Value::Sign::Negative); + 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()[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()[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) {