diff --git a/include/plasp/pddl/Context.h b/include/plasp/pddl/Context.h index def3ba1..5a3cd52 100644 --- a/include/plasp/pddl/Context.h +++ b/include/plasp/pddl/Context.h @@ -1,6 +1,7 @@ #ifndef __PLASP__PDDL__CONTEXT_H #define __PLASP__PDDL__CONTEXT_H +#include #include namespace plasp @@ -14,9 +15,11 @@ namespace pddl // //////////////////////////////////////////////////////////////////////////////////////////////////// -struct Context +class Context { - TypeHashMap types; + public: + TypeHashMap types; + PredicateHashMap predicates; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/Domain.h b/include/plasp/pddl/Domain.h index a882cce..61f6519 100644 --- a/include/plasp/pddl/Domain.h +++ b/include/plasp/pddl/Domain.h @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -28,6 +29,7 @@ class Domain const std::string &name() const; const Requirements &requirements() const; const TypeHashMap &types() const; + const PredicateHashMap &predicates() const; private: Domain(Context &context); @@ -40,6 +42,8 @@ class Domain void parseTypingSection(utils::Parser &parser); + void parsePredicateSection(utils::Parser &parser); + void checkConsistency(); Context &m_context; diff --git a/include/plasp/pddl/Predicate.h b/include/plasp/pddl/Predicate.h new file mode 100644 index 0000000..17daa79 --- /dev/null +++ b/include/plasp/pddl/Predicate.h @@ -0,0 +1,87 @@ +#ifndef __PLASP__PDDL__PREDICATE_H +#define __PLASP__PDDL__PREDICATE_H + +#include +#include + +#include + +#include +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Predicate +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Context; + +struct PredicateHashMapKey +{ + std::string name; + size_t arity; + + bool operator==(const PredicateHashMapKey &other) const + { + return arity == other.arity && name == other.name; + } +}; + +class Predicate; +using PredicateHashMap = std::unordered_map; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Predicate +{ + public: + static Predicate &parseDeclaration(utils::Parser &parser, Context &context); + + public: + const std::string &name() const; + const Variables &arguments() const; + + bool isDeclared() const; + + private: + Predicate(std::string name); + + void setDeclared(); + + bool m_isDeclared; + + std::string m_name; + Variables m_arguments; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace std +{ + template<> + struct hash + { + std::size_t operator()(const plasp::pddl::PredicateHashMapKey &key) const + { + std::size_t seed = 0; + + boost::hash_combine(seed, key.name); + boost::hash_combine(seed, key.arity); + + return seed; + } + }; +} + +#endif diff --git a/include/plasp/pddl/Type.h b/include/plasp/pddl/Type.h index f59377b..ca3dd9f 100644 --- a/include/plasp/pddl/Type.h +++ b/include/plasp/pddl/Type.h @@ -30,7 +30,7 @@ class Type { public: static Type &parse(utils::Parser &parser, Context &context); - static Type &parseWithInheritance(utils::Parser &parser, Context &context); + static Type &parseDeclaration(utils::Parser &parser, Context &context); public: const std::string &name() const; diff --git a/include/plasp/pddl/Variable.h b/include/plasp/pddl/Variable.h new file mode 100644 index 0000000..c820d51 --- /dev/null +++ b/include/plasp/pddl/Variable.h @@ -0,0 +1,56 @@ +#ifndef __PLASP__PDDL__VARIABLE_H +#define __PLASP__PDDL__VARIABLE_H + +#include + +#include +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Variable +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Context; + +class Variable; +using Variables = std::vector; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Variable +{ + public: + static Variable parse(utils::Parser &parser, Context &context); + + public: + const std::string &name() const; + const Type &type() const; + + void setDirty(bool isDirty = true); + bool isDirty() const; + + void setType(const Type &type); + + private: + Variable(std::string name); + + bool m_isDirty; + + std::string m_name; + + const Type *m_type; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/src/plasp/pddl/Domain.cpp b/src/plasp/pddl/Domain.cpp index 69f889d..86f2350 100644 --- a/src/plasp/pddl/Domain.cpp +++ b/src/plasp/pddl/Domain.cpp @@ -74,6 +74,13 @@ const TypeHashMap &Domain::types() const //////////////////////////////////////////////////////////////////////////////////////////////////// +const PredicateHashMap &Domain::predicates() const +{ + return m_context.predicates; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + void Domain::parseSection(utils::Parser &parser) { parser.expect("(:"); @@ -111,7 +118,7 @@ void Domain::parseSection(utils::Parser &parser) else if (sectionIdentifier == "constants") skipSection(); else if (sectionIdentifier == "predicates") - skipSection(); + parsePredicateSection(parser); else if (sectionIdentifier == "functions") skipSection(); else if (sectionIdentifier == "constraints") @@ -124,17 +131,16 @@ void Domain::parseSection(utils::Parser &parser) void Domain::parseRequirementsSection(utils::Parser &parser) { - while (true) + parser.skipWhiteSpace(); + + while (parser.currentCharacter() != ')') { - parser.skipWhiteSpace(); - - if (parser.currentCharacter() == ')') - break; - if (parser.currentCharacter() == ':') parser.advance(); m_requirements.emplace_back(Requirement::parse(parser)); + + parser.skipWhiteSpace(); } if (m_requirements.empty()) @@ -204,10 +210,29 @@ void Domain::computeDerivedRequirements() void Domain::parseTypingSection(utils::Parser &parser) { + parser.skipWhiteSpace(); + // Store types and their parent types while (parser.currentCharacter() != ')') { - Type::parseWithInheritance(parser, m_context); + Type::parseDeclaration(parser, m_context); + + parser.skipWhiteSpace(); + } + + parser.expect(")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void Domain::parsePredicateSection(utils::Parser &parser) +{ + parser.skipWhiteSpace(); + + // Store predicates and their arguments + while (parser.currentCharacter() != ')') + { + Predicate::parseDeclaration(parser, m_context); parser.skipWhiteSpace(); } @@ -234,6 +259,16 @@ void Domain::checkConsistency() if (!type.second.isDeclared()) throw ConsistencyException("Type \"" + type.second.name() + "\" used but never declared"); }); + + // Verify that all used predicates have been declared + std::for_each(m_context.predicates.cbegin(), m_context.predicates.cend(), + [&](const auto &predicate) + { + if (!predicate.second.isDeclared()) + throw ConsistencyException("Predicate \"" + predicate.second.name() + "\" used but never declared"); + }); + + // Verify that all variables have types } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/pddl/Predicate.cpp b/src/plasp/pddl/Predicate.cpp new file mode 100644 index 0000000..a394850 --- /dev/null +++ b/src/plasp/pddl/Predicate.cpp @@ -0,0 +1,111 @@ +#include + +#include + +#include +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Predicate +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Predicate::Predicate(std::string name) +: m_isDeclared{false}, + m_name{name} +{ +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context) +{ + parser.expect("("); + + const auto predicateName = parser.parseIdentifier(isIdentifier); + + Predicate predicate(predicateName); + + // Flag predicate as correctly declared in the types section + predicate.setDeclared(); + + parser.skipWhiteSpace(); + + // Parse arguments + while (parser.currentCharacter() != ')') + { + predicate.m_arguments.emplace_back(Variable::parse(parser, context)); + + parser.skipWhiteSpace(); + + // Check if the variable has a type declaration + if (!parser.advanceIf('-')) + continue; + + // Parse argument type + const auto &type = Type::parse(parser, context); + + // Set the argument type for all previously flagged arguments + std::for_each(predicate.m_arguments.begin(), predicate.m_arguments.end(), + [&](auto &argument) + { + if (!argument.isDirty()) + return; + + argument.setType(type); + argument.setDirty(false); + }); + + parser.skipWhiteSpace(); + } + + parser.expect(")"); + + const auto predicateArity = predicate.m_arguments.size(); + const PredicateHashMapKey key = {predicateName, predicateArity}; + + const auto insertionResult = context.predicates.emplace(std::make_pair(key, std::move(predicate))); + + std::cout << "Emplaced " << insertionResult.first->second.name() << std::endl; + + return insertionResult.first->second; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void Predicate::setDeclared() +{ + m_isDeclared = true; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +bool Predicate::isDeclared() const +{ + return m_isDeclared; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const std::string &Predicate::name() const +{ + return m_name; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Variables &Predicate::arguments() const +{ + return m_arguments; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} diff --git a/src/plasp/pddl/Type.cpp b/src/plasp/pddl/Type.cpp index e5adadb..fdc36f4 100644 --- a/src/plasp/pddl/Type.cpp +++ b/src/plasp/pddl/Type.cpp @@ -48,19 +48,19 @@ Type &Type::parse(utils::Parser &parser, Context &context) // Flag type for potentially upcoming parent type declaration type.setDirty(); - // Flag type as correctly declared in the types section - type.setDeclared(); - return type; } //////////////////////////////////////////////////////////////////////////////////////////////////// -Type &Type::parseWithInheritance(utils::Parser &parser, Context &context) +Type &Type::parseDeclaration(utils::Parser &parser, Context &context) { // Parse and store type auto &type = parse(parser, context); + // Flag type as correctly declared in the types section + type.setDeclared(); + parser.skipWhiteSpace(); // Check for type inheritance @@ -72,6 +72,10 @@ Type &Type::parseWithInheritance(utils::Parser &parser, Context &context) parentType.setDirty(false); + // Type object is an implicit primitive type + if (parentType.name() == "object") + parentType.setDeclared(); + // Assign parent type to all types that were previously flagged std::for_each(context.types.begin(), context.types.end(), [&](auto &childType) diff --git a/src/plasp/pddl/Variable.cpp b/src/plasp/pddl/Variable.cpp new file mode 100644 index 0000000..af566df --- /dev/null +++ b/src/plasp/pddl/Variable.cpp @@ -0,0 +1,81 @@ +#include + +#include + +#include +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Variable +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Variable::Variable(std::string name) +: m_isDirty{false}, + m_name(name) +{ +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Variable Variable::parse(utils::Parser &parser, Context &context) +{ + parser.skipWhiteSpace(); + + parser.expect("?"); + + const auto variableName = parser.parseIdentifier(isIdentifier); + + Variable variable(variableName); + variable.setDirty(); + + return variable; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void Variable::setDirty(bool isDirty) +{ + m_isDirty = isDirty; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +bool Variable::isDirty() const +{ + return m_isDirty; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const std::string &Variable::name() const +{ + return m_name; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void Variable::setType(const Type &type) +{ + m_type = &type; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Type &Variable::type() const +{ + BOOST_ASSERT(m_type != nullptr); + + return *m_type; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} diff --git a/tests/TestPDDLParser.cpp b/tests/TestPDDLParser.cpp index 419e1eb..ae76db5 100644 --- a/tests/TestPDDLParser.cpp +++ b/tests/TestPDDLParser.cpp @@ -56,11 +56,15 @@ TEST_F(PDDLParserTests, ParseBlocksWorldDomain) ASSERT_EQ(domain.types().size(), 1u); - const auto blockType = domain.types().find("block"); - ASSERT_NE(blockType, domain.types().cend()); + const auto block = domain.types().find("block"); + ASSERT_NE(block, domain.types().cend()); - ASSERT_EQ(blockType->second.name(), "block"); - ASSERT_EQ(blockType->second.parentTypes().size(), 0u); + ASSERT_EQ(block->second.name(), "block"); + ASSERT_EQ(block->second.parentTypes().size(), 0u); + + ASSERT_EQ(domain.predicates().size(), 5u); + + const auto on = domain.predicates().find({"on", 2}); } catch (const std::exception &e) {