Started implementing PDDL domain parser.

This commit is contained in:
2016-05-29 16:27:11 +02:00
parent 42fda5925d
commit 40547691a0
9 changed files with 347 additions and 12 deletions

View File

@@ -3,6 +3,9 @@ set(target plasp)
file(GLOB core_sources "plasp/*.cpp")
file(GLOB core_headers "../include/plasp/*.h")
file(GLOB pddl_sources "plasp/pddl/*.cpp")
file(GLOB pddl_headers "../include/plasp/pddl/*.h")
file(GLOB sas_sources "plasp/sas/*.cpp")
file(GLOB sas_headers "../include/plasp/sas/*.h")
@@ -21,6 +24,9 @@ set(sources
${core_sources}
${core_headers}
${pddl_sources}
${pddl_headers}
${sas_sources}
${sas_headers}

View File

@@ -0,0 +1,87 @@
#include <plasp/pddl/Description.h>
#include <fstream>
#include <sstream>
#include <boost/filesystem.hpp>
#include <plasp/utils/ParserException.h>
namespace plasp
{
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Description
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Description Description::fromStream(std::istream &istream)
{
Description description;
utils::Parser parser(istream);
while (true)
{
parser.skipWhiteSpace();
if (parser.atEndOfFile())
break;
description.parseContent(parser);
}
return description;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Description Description::fromFile(const boost::filesystem::path &path)
{
if (!boost::filesystem::is_regular_file(path))
throw std::runtime_error("File does not exist: \"" + path.string() + "\"");
std::ifstream fileStream(path.string(), std::ios::in);
return Description::fromStream(fileStream);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Description::parseContent(utils::Parser &parser)
{
std::cout << "Parsing file content" << std::endl;
parser.expect<std::string>("(define");
parseSection(parser);
parser.expect<std::string>(")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Description::parseSection(utils::Parser &parser)
{
// Parse domain/problem identifier
parser.expect<std::string>("(");
const auto sectionIdentifier = parser.parse<std::string>();
std::cout << "Parsing section " << sectionIdentifier << std::endl;
if (sectionIdentifier == "domain")
m_domain = std::make_unique<Domain>(Domain::fromPDDL(parser));
//else if (sectionIdentifier == "problem")
// m_problem = std::make_unique<Problem>(Problem::fromPDDL(parser));
else
throw utils::ParserException(parser.row(), parser.column(), "Unknown PDDL section \"" + sectionIdentifier + "\"");
parser.expect<std::string>(")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

92
src/plasp/pddl/Domain.cpp Normal file
View File

@@ -0,0 +1,92 @@
#include <plasp/pddl/Domain.h>
#include <plasp/pddl/Identifier.h>
namespace plasp
{
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Domain
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Domain Domain::fromPDDL(utils::Parser &parser)
{
Domain domain;
domain.m_name = parser.parseIdentifier(isIdentifier);
std::cout << "Parsing domain " << domain.m_name << std::endl;
parser.expect<std::string>(")");
while (true)
{
parser.skipWhiteSpace();
std::cout << parser.currentCharacter() << std::endl;
if (parser.currentCharacter() == ')')
break;
domain.parseSection(parser);
}
return domain;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Domain::parseSection(utils::Parser &parser)
{
parser.expect<std::string>("(:");
const auto sectionIdentifier = parser.parseIdentifier(isIdentifier);
const auto skipSection =
[&]()
{
std::cout << "Skipping section " << sectionIdentifier << std::endl;
size_t openParentheses = 1;
while (true)
{
const auto character = parser.currentCharacter();
parser.advance();
if (character == '(')
openParentheses++;
else if (character == ')')
{
openParentheses--;
if (openParentheses == 0)
return;
}
}
};
if (sectionIdentifier == "requirements")
skipSection();
else if (sectionIdentifier == "types")
skipSection();
else if (sectionIdentifier == "constants")
skipSection();
else if (sectionIdentifier == "predicates")
skipSection();
else if (sectionIdentifier == "functions")
skipSection();
else if (sectionIdentifier == "constraints")
skipSection();
else if (sectionIdentifier == "action")
skipSection();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@@ -63,6 +63,13 @@ Parser::CharacterType Parser::currentCharacter() const
////////////////////////////////////////////////////////////////////////////////////////////////////
bool Parser::atEndOfFile() const
{
return m_position == EndOfFile;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Parser::checkStream() const
{
if (m_position == EndOfFile)