Made Variable a proper class.

This commit is contained in:
Patrick Lühne 2016-05-21 15:54:03 +02:00
parent bac8d5c842
commit 5e016d2d79
6 changed files with 99 additions and 57 deletions

View File

@ -3,6 +3,7 @@
#include <iosfwd>
#include <string>
#include <vector>
namespace plasp
{
@ -31,6 +32,10 @@ struct Value
////////////////////////////////////////////////////////////////////////////////////////////////////
using Values = std::vector<Value>;
////////////////////////////////////////////////////////////////////////////////////////////////////
std::ostream &operator <<(std::ostream &ostream, const Value &value);
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -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<Value>;
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;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -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<size_t>(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<size_t>(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<size_t>(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));
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -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)

View File

@ -15,23 +15,30 @@ namespace sas
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Variable::Variable()
: m_axiomLayer(-1)
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Variable Variable::fromSAS(std::istream &istream)
{
Variable variable;
utils::parseExpected<std::string>(istream, "begin_variable");
variable.name = utils::parse<std::string>(istream);
variable.axiomLayer = utils::parse<int>(istream);
variable.m_name = utils::parse<std::string>(istream);
variable.m_axiomLayer = utils::parse<int>(istream);
const auto numberOfValues = utils::parse<size_t>(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<std::string>(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<std::string>(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;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -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)
{