Refactoring to avoid checking expression types via type enum.

This commit is contained in:
Patrick Lühne 2016-12-07 02:11:54 +01:00
parent 3b110c0b8a
commit 692d3fe83a
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
5 changed files with 137 additions and 133 deletions

View File

@ -130,6 +130,9 @@ class Expression
virtual Type expressionType() const = 0;
template<class T>
bool is() const;
virtual ExpressionPointer copy();
// Transform into a normal form as used for the translation to ASP
@ -161,6 +164,14 @@ class Expression
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
bool Expression::is() const
{
return expressionType() == T::ExpressionType;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void intrusive_ptr_add_ref(Expression *expression)
{
expression->m_referenceCount++;

View File

@ -61,7 +61,7 @@ ExpressionPointer Expression::moveUpQuantifiers(ExpressionPointer parent, Expres
{
BOOST_ASSERT(child);
if (child->expressionType() == Expression::Type::Exists)
if (child->is<expressions::Exists>())
{
auto quantifiedExpression = expressions::ExistsPointer(dynamic_cast<expressions::Exists *>(child.get()));
@ -74,7 +74,7 @@ ExpressionPointer Expression::moveUpQuantifiers(ExpressionPointer parent, Expres
// Make parent point to the quantifier that has been moved up
return quantifiedExpression;
}
else if (child->expressionType() == Expression::Type::ForAll)
else if (child->is<expressions::ForAll>())
{
auto quantifiedExpression = expressions::ForAllPointer(dynamic_cast<expressions::ForAll *>(child.get()));
@ -109,7 +109,7 @@ ExpressionPointer Expression::simplified()
ExpressionPointer Expression::negated()
{
if (expressionType() == Type::Not)
if (is<expressions::Not>())
{
auto &notExpression = dynamic_cast<expressions::Not &>(*this);

View File

@ -279,8 +279,7 @@ void TranslatorASP::translateActions() const
m_outputStream << "))";
};
std::for_each(actions.cbegin(), actions.cend(),
[&](const auto &action)
for (const auto &action : actions)
{
const auto translateExpression =
[&](const auto &ruleHead, const auto &literal, bool enumerateEffects = false)
@ -351,8 +350,7 @@ void TranslatorASP::translateActions() const
{
const auto &effect = *action->effect();
if (effect.expressionType() == Expression::Type::Predicate
|| effect.expressionType() == Expression::Type::Not)
if (effect.is<expressions::Predicate>() || effect.is<expressions::Not>())
{
translateExpression("postcondition", effect, true);
}
@ -373,7 +371,7 @@ void TranslatorASP::translateActions() const
}
m_outputStream << std::endl;
});
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -466,7 +464,7 @@ void translateVariablesBody(output::ColorStream &outputStream, const T &variable
void translateLiteral(output::ColorStream &outputStream, const Expression &literal)
{
// Translate single predicate
if (literal.expressionType() == Expression::Type::Predicate)
if (literal.is<expressions::Predicate>())
{
const auto &predicate = dynamic_cast<const expressions::Predicate &>(literal);
@ -477,7 +475,7 @@ void translateLiteral(output::ColorStream &outputStream, const Expression &liter
outputStream << ", " << output::Boolean("true") << ")";
}
// Assuming that "not" expression may only contain a predicate
else if (literal.expressionType() == Expression::Type::Not)
else if (literal.is<expressions::Not>())
{
const auto &notExpression = dynamic_cast<const expressions::Not &>(literal);
@ -492,7 +490,7 @@ void translateLiteral(output::ColorStream &outputStream, const Expression &liter
translatePredicate(outputStream, predicate);
outputStream << ", " << output::Boolean("false") << ")";
}
else if (literal.expressionType() == Expression::Type::DerivedPredicate)
else if (literal.is<expressions::DerivedPredicate>())
{
const auto &derivedPredicate = dynamic_cast<const expressions::DerivedPredicate &>(literal);
@ -527,13 +525,13 @@ void translatePredicate(output::ColorStream &outputStream, const expressions::Pr
{
outputStream << ", ";
if ((*i)->expressionType() == Expression::Type::Constant)
if ((*i)->is<expressions::Constant>())
{
const auto &constant = dynamic_cast<const expressions::Constant &>(**i);
outputStream << output::Keyword("constant") << "(" << output::String(constant.name().c_str()) << ")";
}
else if ((*i)->expressionType() == Expression::Type::Variable)
else if ((*i)->is<expressions::Variable>())
{
const auto &variable = dynamic_cast<const expressions::Variable &>(**i);
@ -582,13 +580,12 @@ void TranslatorASP::translateInitialState() const
const auto &initialStateFacts = m_description.problem().initialState().facts();
std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(),
[&](const auto &fact)
for (const auto &fact : initialStateFacts)
{
m_outputStream << std::endl << output::Function("initialState") << "(";
// Translate single predicate
if (fact->expressionType() == Expression::Type::Predicate)
if (fact->is<expressions::Predicate>())
{
const auto &predicate = dynamic_cast<const expressions::Predicate &>(*fact);
@ -599,7 +596,7 @@ void TranslatorASP::translateInitialState() const
m_outputStream << ", " << output::Boolean("true") << ")";
}
// Assuming that "not" expression may only contain a predicate
else if (fact->expressionType() == Expression::Type::Not)
else if (fact->is<expressions::Not>())
{
const auto &notExpression = dynamic_cast<const expressions::Not &>(*fact);
@ -610,7 +607,7 @@ void TranslatorASP::translateInitialState() const
throw output::TranslatorException("only predicates and their negations supported in initial state currently");
m_outputStream << ").";
});
}
m_outputStream
<< std::endl << std::endl
@ -635,8 +632,7 @@ void TranslatorASP::translateGoal() const
const auto &goal = m_description.problem().goal();
if (goal.expressionType() == Expression::Type::Predicate
|| goal.expressionType() == Expression::Type::Not)
if (goal.is<expressions::Predicate>() || goal.is<expressions::Not>())
{
m_outputStream << std::endl << output::Function("goal") << "(";
@ -644,7 +640,7 @@ void TranslatorASP::translateGoal() const
m_outputStream << ").";
}
else if (goal.expressionType() == Expression::Type::And)
else if (goal.is<expressions::And>())
{
const auto &andExpression = dynamic_cast<const expressions::And &>(goal);

View File

@ -2,9 +2,11 @@
#include <plasp/output/TranslatorException.h>
#include <plasp/pddl/expressions/And.h>
#include <plasp/pddl/expressions/DerivedPredicate.h>
#include <plasp/pddl/expressions/Exists.h>
#include <plasp/pddl/expressions/ForAll.h>
#include <plasp/pddl/expressions/Or.h>
#include <plasp/pddl/expressions/Predicate.h>
namespace plasp
{
@ -79,17 +81,15 @@ ExpressionPointer Not::simplified()
m_argument = m_argument->simplified();
if (!m_argument->is<expressions::Not>())
return this;
// Remove double negations
if (m_argument->expressionType() == Expression::Type::Not)
{
const auto &notExpression = dynamic_cast<expressions::Not &>(*m_argument);
return notExpression.argument();
}
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Not::collectParameters(std::set<VariablePointer> &parameters)
@ -104,11 +104,8 @@ ExpressionPointer Not::decomposed(DerivedPredicates &derivedPredicates)
m_argument = m_argument->decomposed(derivedPredicates);
// Predicates and derived predicates can be directly negated
if (m_argument->expressionType() == Expression::Type::Predicate
|| m_argument->expressionType() == Expression::Type::DerivedPredicate)
{
if (m_argument->is<expressions::Predicate>() || m_argument->is<expressions::DerivedPredicate>())
return this;
}
derivedPredicates.emplace_back(new DerivedPredicate());
auto &derivedPredicate = derivedPredicates.back();

View File

@ -33,7 +33,7 @@ ExpressionPointer Or::decomposed(DerivedPredicates &derivedPredicates)
Expressions conjunction;
// “and” expressions can directly be inlined into the derived predicate
if (argument->expressionType() == Expression::Type::And)
if (argument->is<expressions::And>())
{
const auto &andExpression = dynamic_cast<expressions::And &>(*argument);