diff --git a/include/plasp/pddl/Problem.h b/include/plasp/pddl/Problem.h index 945eed0..e761b86 100644 --- a/include/plasp/pddl/Problem.h +++ b/include/plasp/pddl/Problem.h @@ -40,6 +40,8 @@ class Problem InitialState &initialState(); const InitialState &initialState() const; + const Expression &goal() const; + void checkConsistency(); private: @@ -52,6 +54,8 @@ class Problem void parseInitialStateSection(); + void parseGoalSection(); + Context &m_context; Domain &m_domain; @@ -67,6 +71,9 @@ class Problem utils::Parser::Position m_initialStatePosition; std::unique_ptr m_initialState; + + utils::Parser::Position m_goalPosition; + ExpressionPointer m_goal; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/pddl/Problem.cpp b/src/plasp/pddl/Problem.cpp index e419c70..f450307 100644 --- a/src/plasp/pddl/Problem.cpp +++ b/src/plasp/pddl/Problem.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -26,7 +27,8 @@ Problem::Problem(Context &context, Domain &domain) m_domainPosition{-1}, m_requirementsPosition{-1}, m_objectsPosition{-1}, - m_initialStatePosition{-1} + m_initialStatePosition{-1}, + m_goalPosition{-1} { } @@ -79,8 +81,9 @@ void Problem::findSections() setSectionPosition("objects", m_objectsPosition, position, true); else if (parser.probeIdentifier("init", isIdentifier)) setSectionPosition("init", m_initialStatePosition, position, true); - else if (parser.probeIdentifier("goal", isIdentifier) - || parser.probeIdentifier("constraints", isIdentifier) + else if (parser.probeIdentifier("goal", isIdentifier)) + setSectionPosition("goal", m_goalPosition, position, true); + else if (parser.probeIdentifier("constraints", isIdentifier) || parser.probeIdentifier("metric", isIdentifier) || parser.probeIdentifier("length", isIdentifier)) { @@ -138,6 +141,12 @@ void Problem::parse() parser.seek(m_initialStatePosition); parseInitialStateSection(); + + if (m_goalPosition == -1) + throw ConsistencyException("Problem description does not specify a goal"); + + parser.seek(m_goalPosition); + parseGoalSection(); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -332,6 +341,25 @@ void Problem::parseInitialStateSection() //////////////////////////////////////////////////////////////////////////////////////////////////// +void Problem::parseGoalSection() +{ + auto &parser = m_context.parser; + + parser.expect("("); + parser.expect(":"); + parser.expect("goal"); + + expressions::Variables noParameters; + + ExpressionContext expressionContext(m_domain, this, noParameters); + + m_goal = parsePreconditionExpression(m_context, expressionContext); + + parser.expect(")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + InitialState &Problem::initialState() { BOOST_ASSERT(m_initialState); @@ -350,6 +378,15 @@ const InitialState &Problem::initialState() const //////////////////////////////////////////////////////////////////////////////////////////////////// +const Expression &Problem::goal() const +{ + BOOST_ASSERT(m_goal); + + return *m_goal; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + void Problem::checkConsistency() { }