Made Predicate a proper class.

This commit is contained in:
Patrick Lühne 2016-05-22 16:00:58 +02:00
parent 6677ded33e
commit 1e47173d17
6 changed files with 115 additions and 50 deletions

View File

@ -16,18 +16,27 @@ namespace sas
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
struct Predicate class Predicate
{ {
std::string name; public:
std::vector<std::string> arguments; static Predicate fromSAS(std::istream &istream);
using Arguments = std::vector<std::string>;
public:
void printAsSAS(std::ostream &ostream) const;
void printAsASP(std::ostream &ostream) const;
const std::string &name() const;
const Arguments &arguments() const;
private:
std::string m_name;
std::vector<std::string> m_arguments;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
std::ostream &operator <<(std::ostream &ostream, const Predicate &predicate);
////////////////////////////////////////////////////////////////////////////////////////////////////
} }
} }

View File

@ -95,7 +95,9 @@ std::ostream &operator >>(std::ostream &ostream, const Description &description)
std::for_each(operators.cbegin(), operators.cend(), std::for_each(operators.cbegin(), operators.cend(),
[&](const auto &operator_) [&](const auto &operator_)
{ {
ostream << "\t" << operator_.predicate() << ":" << std::endl; ostream << "\t";
operator_.predicate().printAsSAS(ostream);
ostream << ":" << std::endl;
const auto &preconditions = operator_.preconditions(); const auto &preconditions = operator_.preconditions();

View File

@ -23,28 +23,7 @@ Operator Operator::fromSAS(std::istream &istream, const Variables &variables)
utils::parseExpected<std::string>(istream, "begin_operator"); utils::parseExpected<std::string>(istream, "begin_operator");
try operator_.m_predicate = Predicate::fromSAS(istream);
{
istream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// TODO: Inefficient, reimplement in one pass
std::string line;
std::getline(istream, line);
std::stringstream lineStream(line);
operator_.m_predicate.name = utils::parse<std::string>(lineStream);
while (lineStream.peek() == ' ')
lineStream.ignore(1);
for (std::string argument; std::getline(lineStream, argument, ' ');)
operator_.m_predicate.arguments.push_back(std::move(argument));
}
catch (const std::exception &e)
{
throw utils::ParserException("Could not parse operator predicate");
}
const auto numberOfPrevailConditions = utils::parse<size_t>(istream); const auto numberOfPrevailConditions = utils::parse<size_t>(istream);
operator_.m_preconditions.reserve(numberOfPrevailConditions); operator_.m_preconditions.reserve(numberOfPrevailConditions);

View File

@ -1,6 +1,10 @@
#include <plasp/sas/Predicate.h> #include <plasp/sas/Predicate.h>
#include <iostream> #include <iostream>
#include <limits>
#include <sstream>
#include <plasp/utils/Parsing.h>
namespace plasp namespace plasp
{ {
@ -13,22 +17,89 @@ namespace sas
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
std::ostream &operator <<(std::ostream &ostream, const Predicate &predicate) Predicate Predicate::fromSAS(std::istream &istream)
{ {
if (predicate.arguments.empty()) Predicate predicate;
return (ostream << predicate.name);
ostream << predicate.name << "("; try
{
istream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
for (size_t i = 0; i < predicate.arguments.size(); i++) // TODO: Inefficient, reimplement in one pass
std::string line;
std::getline(istream, line);
std::stringstream lineStream(line);
predicate.m_name = utils::parse<std::string>(lineStream);
while (lineStream.peek() == ' ')
lineStream.ignore(1);
for (std::string argument; std::getline(lineStream, argument, ' ');)
predicate.m_arguments.push_back(std::move(argument));
}
catch (const std::exception &e)
{
throw utils::ParserException("Could not parse operator predicate");
}
return predicate;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::string &Predicate::name() const
{
return m_name;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const Predicate::Arguments &Predicate::arguments() const
{
return m_arguments;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Predicate::printAsSAS(std::ostream &ostream) const
{
if (m_arguments.empty())
{
ostream << m_name;
return;
}
for (size_t i = 0; i < m_arguments.size(); i++)
{
if (i > 0)
ostream << " ";
ostream << m_arguments[i];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Predicate::printAsASP(std::ostream &ostream) const
{
if (m_arguments.empty())
{
ostream << m_name;
return;
}
ostream << m_name << "(";
for (size_t i = 0; i < m_arguments.size(); i++)
{ {
if (i > 0) if (i > 0)
ostream << ", "; ostream << ", ";
ostream << predicate.arguments[i]; ostream << m_arguments[i];
} }
return (ostream << ")"); ostream << ")";
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -125,15 +125,18 @@ void TranslatorASP::translate(std::ostream &ostream) const
std::for_each(operators.cbegin(), operators.cend(), std::for_each(operators.cbegin(), operators.cend(),
[&](const auto &operator_) [&](const auto &operator_)
{ {
ostream << "action(" << operator_.predicate() << ")." << std::endl; ostream << "action(";
operator_.predicate().printAsASP(ostream);
ostream << ")." << std::endl;
const auto &preconditions = operator_.preconditions(); const auto &preconditions = operator_.preconditions();
std::for_each(preconditions.cbegin(), preconditions.cend(), std::for_each(preconditions.cbegin(), preconditions.cend(),
[&](const auto &precondition) [&](const auto &precondition)
{ {
ostream << "precondition(" << operator_.predicate() ostream << "precondition(";
<< ", " << precondition.value().name() operator_.predicate().printAsASP(ostream);
ostream << ", " << precondition.value().name()
<< ", " << (precondition.value().sign() == Value::Sign::Positive ? "true" : "false") << ", " << (precondition.value().sign() == Value::Sign::Positive ? "true" : "false")
<< ")." << std::endl; << ")." << std::endl;
}); });
@ -143,8 +146,9 @@ void TranslatorASP::translate(std::ostream &ostream) const
std::for_each(effects.cbegin(), effects.cend(), std::for_each(effects.cbegin(), effects.cend(),
[&](const auto &effect) [&](const auto &effect)
{ {
ostream << "postcondition(" << operator_.predicate() ostream << "postcondition(";
<< ", " << effect.postcondition().value().name() operator_.predicate().printAsASP(ostream);
ostream << ", " << effect.postcondition().value().name()
<< ", " << (effect.postcondition().value().sign() == Value::Sign::Positive ? "true" : "false") << ", " << (effect.postcondition().value().sign() == Value::Sign::Positive ? "true" : "false")
<< ")." << std::endl; << ")." << std::endl;
}); });

View File

@ -67,10 +67,10 @@ TEST_F(SASParserTests, ParseValidSASFile)
ASSERT_EQ(&description.goal().facts()[1].value(), &description.variables()[7].values()[0]); ASSERT_EQ(&description.goal().facts()[1].value(), &description.variables()[7].values()[0]);
ASSERT_EQ(description.operators().size(), 34); ASSERT_EQ(description.operators().size(), 34);
ASSERT_EQ(description.operators()[0].predicate().name, "activate-trans"); ASSERT_EQ(description.operators()[0].predicate().name(), "activate-trans");
ASSERT_EQ(description.operators()[0].predicate().arguments.size(), 5); ASSERT_EQ(description.operators()[0].predicate().arguments().size(), 5);
ASSERT_EQ(description.operators()[0].predicate().arguments[0], "philosopher-0"); 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].predicate().arguments()[4], "state-3");
ASSERT_EQ(description.operators()[0].preconditions().size(), 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()[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()[1].value(), &description.variables()[16].values()[1]);
@ -78,10 +78,10 @@ TEST_F(SASParserTests, ParseValidSASFile)
ASSERT_EQ(description.operators()[0].effects().size(), 1); 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].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().name(), "queue-write");
ASSERT_EQ(description.operators()[33].predicate().arguments.size(), 4); 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()[0], "philosopher-1");
ASSERT_EQ(description.operators()[33].predicate().arguments[3], "fork"); ASSERT_EQ(description.operators()[33].predicate().arguments()[3], "fork");
ASSERT_EQ(description.operators()[33].preconditions().size(), 2); 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()[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()[1].value(), &description.variables()[2].values()[2]);