From 6d07fcbd6c514146f60cc6a4ebb886e609d2234d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 27 Oct 2017 16:36:01 +0200 Subject: [PATCH] Moved initial state translation to new header. --- include/plasp/pddl/translation/Fact.h | 69 +++++++++++++++++++++++++++ src/plasp/pddl/TranslatorASP.cpp | 46 +----------------- 2 files changed, 71 insertions(+), 44 deletions(-) create mode 100644 include/plasp/pddl/translation/Fact.h diff --git a/include/plasp/pddl/translation/Fact.h b/include/plasp/pddl/translation/Fact.h new file mode 100644 index 0000000..98d2712 --- /dev/null +++ b/include/plasp/pddl/translation/Fact.h @@ -0,0 +1,69 @@ +#ifndef __PLASP__PDDL__TRANSLATION__FACT_H +#define __PLASP__PDDL__TRANSLATION__FACT_H + +#include + +#include + +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Fact +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline void translateFact(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Fact &fact) +{ + outputStream << std::endl << colorlog::Function("initialState") << "("; + + const auto handlePredicate = + [&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true) + { + translatePredicateToVariable(outputStream, *predicate, isPositive); + }; + + const auto handleNegatedPredicate = + [&](const ::pddl::normalizedAST::PredicatePointer &predicate) + { + return handlePredicate(predicate, false); + }; + + const auto handleDerivedPredicate = + [&](const ::pddl::normalizedAST::DerivedPredicatePointer &, bool = true) + { + throw TranslatorException("derived predicates should not occur in initial state"); + }; + + const auto handleNegatedDerivedPredicate = + [&](const ::pddl::normalizedAST::DerivedPredicatePointer &derivedPredicate) + { + return handleDerivedPredicate(derivedPredicate, false); + }; + + const auto handleAtomicFormula = + [&](const ::pddl::normalizedAST::AtomicFormula &atomicFormula) + { + atomicFormula.match(handlePredicate, handleDerivedPredicate); + }; + + const auto handleNot = + [&](const ::pddl::normalizedAST::NotPointer<::pddl::normalizedAST::AtomicFormula> ¬_) + { + not_->argument.match(handleNegatedPredicate, handleNegatedDerivedPredicate); + }; + + fact.match(handleAtomicFormula, handleNot); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 4a0e79f..3db9582 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -436,51 +437,8 @@ void TranslatorASP::translateInitialState() const const auto &facts = m_description.problem.value()->initialState.facts; - // TODO: move to separate header for (const auto &fact : facts) - { - m_outputStream << std::endl << colorlog::Function("initialState") << "("; - - const auto handlePredicate = - [&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true) - { - translatePredicateToVariable(m_outputStream, *predicate, isPositive); - }; - - const auto handleNegatedPredicate = - [&](const ::pddl::normalizedAST::PredicatePointer &predicate) - { - return handlePredicate(predicate, false); - }; - - const auto handleDerivedPredicate = - [&](const ::pddl::normalizedAST::DerivedPredicatePointer &, bool = true) - { - throw TranslatorException("derived predicates should not occur in initial state"); - }; - - const auto handleNegatedDerivedPredicate = - [&](const ::pddl::normalizedAST::DerivedPredicatePointer &derivedPredicate) - { - return handleDerivedPredicate(derivedPredicate, false); - }; - - const auto handleAtomicFormula = - [&](const ::pddl::normalizedAST::AtomicFormula &atomicFormula) - { - atomicFormula.match(handlePredicate, handleDerivedPredicate); - }; - - const auto handleNot = - [&](const ::pddl::normalizedAST::NotPointer<::pddl::normalizedAST::AtomicFormula> ¬_) - { - not_->argument.match(handleNegatedPredicate, handleNegatedDerivedPredicate); - }; - - fact.match(handleAtomicFormula, handleNot); - - m_outputStream << ")."; - } + ::plasp::pddl::translateFact(m_outputStream, fact); m_outputStream << std::endl << std::endl