Implemented parsing the operator predicates.

This commit is contained in:
Patrick Lühne 2016-05-21 02:03:45 +02:00
parent 0e4233cc65
commit 6e36fc128e
4 changed files with 96 additions and 6 deletions

View File

@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include <plasp/sas/Effect.h> #include <plasp/sas/Effect.h>
#include <plasp/sas/Predicate.h>
#include <plasp/sas/Variable.h> #include <plasp/sas/Variable.h>
namespace plasp namespace plasp
@ -24,7 +25,7 @@ struct Operator
using Conditions = std::vector<Condition>; using Conditions = std::vector<Condition>;
using Effects = std::vector<Effect>; using Effects = std::vector<Effect>;
std::string name; Predicate predicate;
Conditions preconditions; Conditions preconditions;
Effects effects; Effects effects;
size_t costs; size_t costs;

View File

@ -0,0 +1,34 @@
#ifndef __SAS__PREDICATE_H
#define __SAS__PREDICATE_H
#include <iosfwd>
#include <string>
#include <vector>
namespace plasp
{
namespace sas
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Predicate
//
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Predicate
{
std::string name;
std::vector<std::string> arguments;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
std::ostream &operator <<(std::ostream &ostream, const Predicate &predicate);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -2,6 +2,7 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <sstream>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
@ -29,7 +30,7 @@ Description Description::fromStream(std::istream &istream)
{ {
Description description; Description description;
setlocale(LC_NUMERIC, "C"); std::setlocale(LC_NUMERIC, "C");
istream.exceptions(std::ifstream::failbit | std::ifstream::badbit); istream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
@ -170,7 +171,7 @@ void Description::print(std::ostream &ostream) const
std::for_each(m_operators.cbegin(), m_operators.cend(), std::for_each(m_operators.cbegin(), m_operators.cend(),
[&](const auto &operator_) [&](const auto &operator_)
{ {
ostream << "\t" << operator_.name << ":" << std::endl; ostream << "\t" << operator_.predicate << ":" << std::endl;
ostream << "\t\tpreconditions: " << operator_.preconditions.size() << std::endl; ostream << "\t\tpreconditions: " << operator_.preconditions.size() << std::endl;
std::for_each(operator_.preconditions.cbegin(), operator_.preconditions.cend(), std::for_each(operator_.preconditions.cbegin(), operator_.preconditions.cend(),
@ -445,10 +446,30 @@ void Description::parseOperatorSection(std::istream &istream)
{ {
parseSectionIdentifier(istream, "begin_operator"); parseSectionIdentifier(istream, "begin_operator");
auto &operator_ = m_operators[i];
try
{
istream.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); istream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
auto &operator_ = m_operators[i]; // TODO: Inefficient, reimplement in one pass
std::getline(istream, operator_.name); std::string line;
std::getline(istream, line);
std::stringstream lineStream(line);
operator_.predicate.name = parse<std::string>(lineStream);
while (lineStream.peek() == ' ')
lineStream.ignore(1);
for (std::string argument; std::getline(lineStream, argument, ' ');)
operator_.predicate.arguments.push_back(std::move(argument));
}
catch (const std::exception &e)
{
throw ParserException("Could not parse operator predicate");
}
const auto numberOfPrevailConditions = parse<size_t>(istream); const auto numberOfPrevailConditions = parse<size_t>(istream);
operator_.preconditions.reserve(numberOfPrevailConditions); operator_.preconditions.reserve(numberOfPrevailConditions);

View File

@ -0,0 +1,34 @@
#include <plasp/sas/Predicate.h>
#include <iostream>
namespace plasp
{
namespace sas
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Predicate
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::ostream &operator <<(std::ostream &ostream, const Predicate &predicate)
{
ostream << predicate.name << "(";
for (size_t i = 0; i < predicate.arguments.size(); i++)
{
if (i > 0)
ostream << ", ";
ostream << predicate.arguments[i];
}
return (ostream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}