Move arithmetic check to separate header
Checking whether terms are arithmetic will be used not just in integer variable detection but also in simplifying formulas with integer variables. For this purpose, the arithmetic check is moved to a commonly accessible header file.
This commit is contained in:
parent
b1ca027de5
commit
d6a811e363
154
include/anthem/Arithmetics.h
Normal file
154
include/anthem/Arithmetics.h
Normal file
@ -0,0 +1,154 @@
|
||||
#ifndef __ANTHEM__ARITHMETICS_H
|
||||
#define __ANTHEM__ARITHMETICS_H
|
||||
|
||||
#include <anthem/Utils.h>
|
||||
|
||||
namespace anthem
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Arithmetics
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DefaultVariableDomainAccessor
|
||||
{
|
||||
ast::Domain operator()(const ast::Variable &variable)
|
||||
{
|
||||
return variable.declaration->domain;
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <class VariableDomainAccessor = DefaultVariableDomainAccessor, class... Arguments>
|
||||
EvaluationResult isArithmetic(const ast::Term &term, Arguments &&...);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <class VariableDomainAccessor = DefaultVariableDomainAccessor>
|
||||
struct IsTermArithmeticVisitor
|
||||
{
|
||||
template <class... Arguments>
|
||||
static EvaluationResult visit(const ast::BinaryOperation &binaryOperation, Arguments &&... arguments)
|
||||
{
|
||||
const auto isLeftArithemtic = isArithmetic<VariableDomainAccessor>(binaryOperation.left, std::forward<Arguments>(arguments)...);
|
||||
const auto isRightArithmetic = isArithmetic<VariableDomainAccessor>(binaryOperation.right, std::forward<Arguments>(arguments)...);
|
||||
|
||||
if (isLeftArithemtic == EvaluationResult::Error || isRightArithmetic == EvaluationResult::Error)
|
||||
return EvaluationResult::Error;
|
||||
|
||||
if (isLeftArithemtic == EvaluationResult::False || isRightArithmetic == EvaluationResult::False)
|
||||
return EvaluationResult::Error;
|
||||
|
||||
if (isLeftArithemtic == EvaluationResult::Unknown || isRightArithmetic == EvaluationResult::Unknown)
|
||||
return EvaluationResult::Unknown;
|
||||
|
||||
return EvaluationResult::True;
|
||||
}
|
||||
|
||||
template <class... Arguments>
|
||||
static EvaluationResult visit(const ast::Boolean &, Arguments &&...)
|
||||
{
|
||||
return EvaluationResult::False;
|
||||
}
|
||||
|
||||
template <class... Arguments>
|
||||
static EvaluationResult visit(const ast::Function &function, Arguments &&...)
|
||||
{
|
||||
switch (function.declaration->domain)
|
||||
{
|
||||
case ast::Domain::General:
|
||||
return EvaluationResult::False;
|
||||
case ast::Domain::Integer:
|
||||
return EvaluationResult::True;
|
||||
case ast::Domain::Unknown:
|
||||
return EvaluationResult::Unknown;
|
||||
}
|
||||
|
||||
return EvaluationResult::Unknown;
|
||||
}
|
||||
|
||||
template <class... Arguments>
|
||||
static EvaluationResult visit(const ast::Integer &, Arguments &&...)
|
||||
{
|
||||
return EvaluationResult::True;
|
||||
}
|
||||
|
||||
template <class... Arguments>
|
||||
static EvaluationResult visit(const ast::Interval &interval, Arguments &&... arguments)
|
||||
{
|
||||
const auto isFromArithmetic = isArithmetic<VariableDomainAccessor>(interval.from, std::forward<Arguments>(arguments)...);
|
||||
const auto isToArithmetic = isArithmetic<VariableDomainAccessor>(interval.to, std::forward<Arguments>(arguments)...);
|
||||
|
||||
if (isFromArithmetic == EvaluationResult::Error || isToArithmetic == EvaluationResult::Error)
|
||||
return EvaluationResult::Error;
|
||||
|
||||
if (isFromArithmetic == EvaluationResult::False || isToArithmetic == EvaluationResult::False)
|
||||
return EvaluationResult::Error;
|
||||
|
||||
if (isFromArithmetic == EvaluationResult::Unknown || isToArithmetic == EvaluationResult::Unknown)
|
||||
return EvaluationResult::Unknown;
|
||||
|
||||
return EvaluationResult::True;
|
||||
}
|
||||
|
||||
template <class... Arguments>
|
||||
static EvaluationResult visit(const ast::SpecialInteger &, Arguments &&...)
|
||||
{
|
||||
return EvaluationResult::False;
|
||||
}
|
||||
|
||||
template <class... Arguments>
|
||||
static EvaluationResult visit(const ast::String &, Arguments &&...)
|
||||
{
|
||||
return EvaluationResult::False;
|
||||
}
|
||||
|
||||
template <class... Arguments>
|
||||
static EvaluationResult visit(const ast::UnaryOperation &unaryOperation, Arguments &&... arguments)
|
||||
{
|
||||
const auto isArgumentArithmetic = isArithmetic<VariableDomainAccessor>(unaryOperation.argument, std::forward<Arguments>(arguments)...);
|
||||
|
||||
switch (unaryOperation.operator_)
|
||||
{
|
||||
case ast::UnaryOperation::Operator::Absolute:
|
||||
return (isArgumentArithmetic == EvaluationResult::False ? EvaluationResult::Error : isArgumentArithmetic);
|
||||
}
|
||||
|
||||
return EvaluationResult::Unknown;
|
||||
}
|
||||
|
||||
template <class... Arguments>
|
||||
static EvaluationResult visit(const ast::Variable &variable, Arguments &&... arguments)
|
||||
{
|
||||
const auto domain = VariableDomainAccessor()(variable, std::forward<Arguments>(arguments)...);
|
||||
|
||||
switch (domain)
|
||||
{
|
||||
case ast::Domain::General:
|
||||
return EvaluationResult::False;
|
||||
case ast::Domain::Integer:
|
||||
return EvaluationResult::True;
|
||||
case ast::Domain::Unknown:
|
||||
return EvaluationResult::Unknown;
|
||||
}
|
||||
|
||||
return EvaluationResult::Unknown;
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <class VariableDomainAccessor, class... Arguments>
|
||||
EvaluationResult isArithmetic(const ast::Term &term, Arguments &&... arguments)
|
||||
{
|
||||
return term.accept(IsTermArithmeticVisitor<VariableDomainAccessor>(), std::forward<Arguments>(arguments)...);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -1,5 +1,6 @@
|
||||
#include <anthem/IntegerVariableDetection.h>
|
||||
|
||||
#include <anthem/Arithmetics.h>
|
||||
#include <anthem/ASTCopy.h>
|
||||
#include <anthem/ASTUtils.h>
|
||||
#include <anthem/ASTVisitors.h>
|
||||
@ -44,107 +45,11 @@ void clearVariableDomainMap(VariableDomainMap &variableDomainMap)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
EvaluationResult isArithmetic(const ast::Term &term, VariableDomainMap &variableDomainMap);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct IsTermArithmeticVisitor
|
||||
struct VariableDomainMapAccessor
|
||||
{
|
||||
static EvaluationResult visit(const ast::BinaryOperation &binaryOperation, VariableDomainMap &variableDomainMap)
|
||||
ast::Domain operator()(const ast::Variable &variable, VariableDomainMap &variableDomainMap)
|
||||
{
|
||||
const auto isLeftArithemtic = isArithmetic(binaryOperation.left, variableDomainMap);
|
||||
const auto isRightArithmetic = isArithmetic(binaryOperation.right, variableDomainMap);
|
||||
|
||||
if (isLeftArithemtic == EvaluationResult::Error || isRightArithmetic == EvaluationResult::Error)
|
||||
return EvaluationResult::Error;
|
||||
|
||||
if (isLeftArithemtic == EvaluationResult::False || isRightArithmetic == EvaluationResult::False)
|
||||
return EvaluationResult::Error;
|
||||
|
||||
if (isLeftArithemtic == EvaluationResult::Unknown || isRightArithmetic == EvaluationResult::Unknown)
|
||||
return EvaluationResult::Unknown;
|
||||
|
||||
return EvaluationResult::True;
|
||||
}
|
||||
|
||||
static EvaluationResult visit(const ast::Boolean &, VariableDomainMap &)
|
||||
{
|
||||
return EvaluationResult::False;
|
||||
}
|
||||
|
||||
static EvaluationResult visit(const ast::Function &function, VariableDomainMap &)
|
||||
{
|
||||
switch (function.declaration->domain)
|
||||
{
|
||||
case ast::Domain::General:
|
||||
return EvaluationResult::False;
|
||||
case ast::Domain::Integer:
|
||||
return EvaluationResult::True;
|
||||
case ast::Domain::Unknown:
|
||||
return EvaluationResult::Unknown;
|
||||
}
|
||||
|
||||
return EvaluationResult::Unknown;
|
||||
}
|
||||
|
||||
static EvaluationResult visit(const ast::Integer &, VariableDomainMap &)
|
||||
{
|
||||
return EvaluationResult::True;
|
||||
}
|
||||
|
||||
static EvaluationResult visit(const ast::Interval &interval, VariableDomainMap &variableDomainMap)
|
||||
{
|
||||
const auto isFromArithmetic = isArithmetic(interval.from, variableDomainMap);
|
||||
const auto isToArithmetic = isArithmetic(interval.to, variableDomainMap);
|
||||
|
||||
if (isFromArithmetic == EvaluationResult::Error || isToArithmetic == EvaluationResult::Error)
|
||||
return EvaluationResult::Error;
|
||||
|
||||
if (isFromArithmetic == EvaluationResult::False || isToArithmetic == EvaluationResult::False)
|
||||
return EvaluationResult::Error;
|
||||
|
||||
if (isFromArithmetic == EvaluationResult::Unknown || isToArithmetic == EvaluationResult::Unknown)
|
||||
return EvaluationResult::Unknown;
|
||||
|
||||
return EvaluationResult::True;
|
||||
}
|
||||
|
||||
static EvaluationResult visit(const ast::SpecialInteger &, VariableDomainMap &)
|
||||
{
|
||||
return EvaluationResult::False;
|
||||
}
|
||||
|
||||
static EvaluationResult visit(const ast::String &, VariableDomainMap &)
|
||||
{
|
||||
return EvaluationResult::False;
|
||||
}
|
||||
|
||||
static EvaluationResult visit(const ast::UnaryOperation &unaryOperation, VariableDomainMap &variableDomainMap)
|
||||
{
|
||||
const auto isArgumentArithmetic = isArithmetic(unaryOperation.argument, variableDomainMap);
|
||||
|
||||
switch (unaryOperation.operator_)
|
||||
{
|
||||
case ast::UnaryOperation::Operator::Absolute:
|
||||
return (isArgumentArithmetic == EvaluationResult::False ? EvaluationResult::Error : isArgumentArithmetic);
|
||||
}
|
||||
|
||||
return EvaluationResult::Unknown;
|
||||
}
|
||||
|
||||
static EvaluationResult visit(const ast::Variable &variable, VariableDomainMap &variableDomainMap)
|
||||
{
|
||||
switch (domain(variable, variableDomainMap))
|
||||
{
|
||||
case ast::Domain::General:
|
||||
return EvaluationResult::False;
|
||||
case ast::Domain::Integer:
|
||||
return EvaluationResult::True;
|
||||
case ast::Domain::Unknown:
|
||||
return EvaluationResult::Unknown;
|
||||
}
|
||||
|
||||
return EvaluationResult::Unknown;
|
||||
return domain(variable, variableDomainMap);
|
||||
}
|
||||
};
|
||||
|
||||
@ -152,7 +57,7 @@ struct IsTermArithmeticVisitor
|
||||
|
||||
EvaluationResult isArithmetic(const ast::Term &term, VariableDomainMap &variableDomainMap)
|
||||
{
|
||||
return term.accept(IsTermArithmeticVisitor(), variableDomainMap);
|
||||
return isArithmetic<VariableDomainMapAccessor>(term, variableDomainMap);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
Loading…
Reference in New Issue
Block a user