119 lines
3.1 KiB
C++
119 lines
3.1 KiB
C++
#include <plasp/pddl/Action.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <plasp/pddl/Context.h>
|
|
#include <plasp/pddl/Domain.h>
|
|
#include <plasp/pddl/ExpressionContext.h>
|
|
#include <plasp/pddl/expressions/Type.h>
|
|
|
|
namespace plasp
|
|
{
|
|
namespace pddl
|
|
{
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Action
|
|
//
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void Action::parseDeclaration(Context &context, Domain &domain)
|
|
{
|
|
auto &tokenizer = context.tokenizer;
|
|
|
|
auto action = std::make_unique<Action>(Action());
|
|
|
|
action->m_name = tokenizer.getIdentifier();
|
|
|
|
tokenizer.expect<std::string>(":parameters");
|
|
tokenizer.expect<std::string>("(");
|
|
|
|
ExpressionContext expressionContext(domain);
|
|
expressionContext.variables.push(&action->m_parameters);
|
|
|
|
// Read parameters
|
|
expressions::Variable::parseTypedDeclarations(context, expressionContext, action->m_parameters);
|
|
|
|
tokenizer.expect<std::string>(")");
|
|
|
|
// Parse preconditions and effects
|
|
while (!tokenizer.testAndReturn(')'))
|
|
{
|
|
tokenizer.expect<std::string>(":");
|
|
|
|
if (tokenizer.testIdentifierAndSkip("precondition"))
|
|
action->m_precondition = parsePreconditionExpression(context, expressionContext);
|
|
else if (tokenizer.testIdentifierAndSkip("effect"))
|
|
action->m_effect = parseEffectExpression(context, expressionContext);
|
|
|
|
tokenizer.skipWhiteSpace();
|
|
}
|
|
|
|
// Store new action
|
|
expressionContext.domain.actions().emplace_back(std::move(action));
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
const std::string &Action::name() const
|
|
{
|
|
return m_name;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
const expressions::Variables &Action::parameters() const
|
|
{
|
|
return m_parameters;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
const Expression *Action::precondition() const
|
|
{
|
|
BOOST_ASSERT(m_precondition);
|
|
|
|
return m_precondition.get();
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
const Expression *Action::effect() const
|
|
{
|
|
BOOST_ASSERT(m_effect);
|
|
|
|
return m_effect.get();
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void Action::normalize(expressions::DerivedPredicates &derivedPredicates)
|
|
{
|
|
// Normalize preconditions and effects
|
|
m_precondition = m_precondition->normalized()->decomposed(derivedPredicates);
|
|
{
|
|
m_precondition->print(std::cout);
|
|
|
|
// TODO: refactor
|
|
std::set<expressions::VariablePointer> parameters;
|
|
m_precondition->collectParameters(parameters);
|
|
}
|
|
|
|
m_effect = m_effect->normalized();
|
|
{
|
|
// TODO: refactor
|
|
std::set<expressions::VariablePointer> parameters;
|
|
m_effect->collectParameters(parameters);
|
|
}
|
|
|
|
// Normalize parameter names
|
|
for (size_t i = 0; i < m_parameters.size(); i++)
|
|
m_parameters[i]->setName("X" + std::to_string(i));
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
}
|
|
}
|