Made Predicate a proper class.

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

View File

@@ -95,7 +95,9 @@ std::ostream &operator >>(std::ostream &ostream, const Description &description)
std::for_each(operators.cbegin(), operators.cend(),
[&](const auto &operator_)
{
ostream << "\t" << operator_.predicate() << ":" << std::endl;
ostream << "\t";
operator_.predicate().printAsSAS(ostream);
ostream << ":" << std::endl;
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");
try
{
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");
}
operator_.m_predicate = Predicate::fromSAS(istream);
const auto numberOfPrevailConditions = utils::parse<size_t>(istream);
operator_.m_preconditions.reserve(numberOfPrevailConditions);

View File

@@ -1,6 +1,10 @@
#include <plasp/sas/Predicate.h>
#include <iostream>
#include <limits>
#include <sstream>
#include <plasp/utils/Parsing.h>
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())
return (ostream << predicate.name);
Predicate predicate;
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)
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(),
[&](const auto &operator_)
{
ostream << "action(" << operator_.predicate() << ")." << std::endl;
ostream << "action(";
operator_.predicate().printAsASP(ostream);
ostream << ")." << std::endl;
const auto &preconditions = operator_.preconditions();
std::for_each(preconditions.cbegin(), preconditions.cend(),
[&](const auto &precondition)
{
ostream << "precondition(" << operator_.predicate()
<< ", " << precondition.value().name()
ostream << "precondition(";
operator_.predicate().printAsASP(ostream);
ostream << ", " << precondition.value().name()
<< ", " << (precondition.value().sign() == Value::Sign::Positive ? "true" : "false")
<< ")." << std::endl;
});
@@ -143,8 +146,9 @@ void TranslatorASP::translate(std::ostream &ostream) const
std::for_each(effects.cbegin(), effects.cend(),
[&](const auto &effect)
{
ostream << "postcondition(" << operator_.predicate()
<< ", " << effect.postcondition().value().name()
ostream << "postcondition(";
operator_.predicate().printAsASP(ostream);
ostream << ", " << effect.postcondition().value().name()
<< ", " << (effect.postcondition().value().sign() == Value::Sign::Positive ? "true" : "false")
<< ")." << std::endl;
});