#ifndef RULE_OPERATORS_H_
#define RULE_OPERATORS_H_
#include <cmath>
#include <algorithm>
#include <cctype>
#include <boost/operators.hpp>
#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "rdf_rule_core.h"
#include "rdf_date_time.h"
#include "rule_session.h"
namespace rule {
namespace internal {
using rdf::internal::no_value;
using rdf::literal;
using rdf::int_type;
using rdf::u_int_type;
using rdf::real_type;
using rdf::date_type;
using rdf::time_type;
using rdf::duration_type;
// --VISITORS-->>>-----------------------------------------------------------------------------------------------------------------
// --operator-FLOOR-----------------------------------------------------------------------------------------------------------------
struct u_floor
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "floor";};
template<class T>return_type const operator()(T const&)const{return no_value();};
return_type operator()(int_type const lhs)const{return lhs;};
return_type operator()(u_int_type const lhs)const{return lhs;};
return_type operator()(real_type const lhs)const{return boost::numeric_cast<real_type>(floor(lhs));};
u_floor(): m_rule_session_p(NULL) {};
u_floor(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-CEIL-----------------------------------------------------------------------------------------------------------------
struct u_ceil
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "ceil";};
template<class T>return_type const operator()(T const&)const{return no_value();};
return_type operator()(int_type const lhs)const{return lhs;};
return_type operator()(u_int_type const lhs)const{return lhs;};
return_type operator()(real_type const lhs)const{return boost::numeric_cast<real_type>(ceil(lhs));};
u_ceil(): m_rule_session_p(NULL) {};
u_ceil(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-NOT-----------------------------------------------------------------------------------------------------------------
struct u_not
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "not";};
template<class T>return_type const operator()(T const&)const{return no_value();};
return_type operator()(int_type const lhs)const{return not lhs;};
return_type operator()(u_int_type const lhs)const{return not lhs;};
return_type operator()(real_type const lhs)const{return not lhs;};
return_type operator()(date_type const lhs)const{return not lhs.d;};
return_type operator()(time_type const lhs)const{return not lhs.d;};
return_type operator()(duration_type const lhs)const{return not lhs.d;};
u_not(): m_rule_session_p(NULL) {};
u_not(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-ABS-----------------------------------------------------------------------------------------------------------------
struct u_abs
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "abs";};
template<class T>return_type const operator()(T const&)const{return no_value();};
return_type operator()(int_type const lhs)const{return abs(lhs);};
return_type operator()(u_int_type const lhs)const{return lhs;};
return_type operator()(real_type const lhs)const{return boost::numeric_cast<real_type>(fabs(lhs));};
u_abs(): m_rule_session_p(NULL) {};
u_abs(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-LT-----------------------------------------------------------------------------------------------------------------
struct f_lt
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "<";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs<rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs<rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs<rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs<rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs<rhs;};
return_type operator()(date_type const lhs, date_type const rhs)const{return rdf::date_adaptor(lhs) < rdf::date_adaptor(rhs);};
return_type operator()(date_type const lhs, time_type const rhs)const{return rdf::date_adaptor(lhs) < rdf::as_ptime(rhs).date();};
return_type operator()(time_type const lhs, date_type const rhs)const{return rdf::as_ptime(lhs).date() < rdf::date_adaptor(rhs);};
return_type operator()(time_type const lhs, time_type const rhs)const{return rdf::as_ptime(lhs) < rdf::as_ptime(rhs);};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return rdf::as_time_duration(lhs) < rdf::as_time_duration(rhs);};
f_lt(): m_rule_session_p(NULL) {};
f_lt(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-LE-----------------------------------------------------------------------------------------------------------------
struct f_le
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "<=";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs<=rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs<=rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs<=rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs<=rhs;};
return_type operator()(date_type const lhs, date_type const rhs)const{return rdf::date_adaptor(lhs) <= rdf::date_adaptor(rhs);};
return_type operator()(date_type const lhs, time_type const rhs)const{return rdf::date_adaptor(lhs) <= rdf::as_ptime(rhs).date();};
return_type operator()(time_type const lhs, date_type const rhs)const{return rdf::as_ptime(lhs).date() <= rdf::date_adaptor(rhs);};
return_type operator()(time_type const lhs, time_type const rhs)const{return rdf::as_ptime(lhs) <= rdf::as_ptime(rhs);};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return rdf::as_time_duration(lhs) <= rdf::as_time_duration(rhs);};
f_le(): m_rule_session_p(NULL) {};
f_le(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-GT-----------------------------------------------------------------------------------------------------------------
struct f_gt
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return ">";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs>rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs>rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs>rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs>rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs>rhs;};
return_type operator()(date_type const lhs, date_type const rhs)const{return rdf::date_adaptor(lhs) > rdf::date_adaptor(rhs);};
return_type operator()(date_type const lhs, time_type const rhs)const{return rdf::date_adaptor(lhs) > rdf::as_ptime(rhs).date();};
return_type operator()(time_type const lhs, date_type const rhs)const{return rdf::as_ptime(lhs).date() > rdf::date_adaptor(rhs);};
return_type operator()(time_type const lhs, time_type const rhs)const{return rdf::as_ptime(lhs) > rdf::as_ptime(rhs);};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return rdf::as_time_duration(lhs) > rdf::as_time_duration(rhs);};
f_gt(): m_rule_session_p(NULL) {};
f_gt(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-GE-----------------------------------------------------------------------------------------------------------------
struct f_ge
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return ">=";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs>=rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs>=rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs>=rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs>=rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs>=rhs;};
return_type operator()(date_type const lhs, date_type const rhs)const{return rdf::date_adaptor(lhs) >= rdf::date_adaptor(rhs);};
return_type operator()(date_type const lhs, time_type const rhs)const{return rdf::date_adaptor(lhs) >= rdf::as_ptime(rhs).date();};
return_type operator()(time_type const lhs, date_type const rhs)const{return rdf::as_ptime(lhs).date() >= rdf::date_adaptor(rhs);};
return_type operator()(time_type const lhs, time_type const rhs)const{return rdf::as_ptime(lhs) >= rdf::as_ptime(rhs);};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return rdf::as_time_duration(lhs) >= rdf::as_time_duration(rhs);};
f_ge(): m_rule_session_p(NULL) {};
f_ge(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-EQ-----------------------------------------------------------------------------------------------------------------
struct f_eq
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "==";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs==rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs==rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs==rhs;};
return_type operator()(u_int_type const lhs, real_type const rhs)const{return lhs==rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs==rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs==rhs;};
return_type operator()(std::string const&lhs, std::string const&rhs)const{return lhs==rhs;};
return_type operator()(date_type const lhs, date_type const rhs)const{return lhs.d == rhs.d;};
return_type operator()(date_type const lhs, time_type const rhs)const{return rdf::date_adaptor(lhs) == rdf::as_ptime(rhs).date();};
return_type operator()(time_type const lhs, date_type const rhs)const{return rdf::as_ptime(lhs).date() == rdf::date_adaptor(rhs);};
return_type operator()(time_type const lhs, time_type const rhs)const{return lhs.d == rhs.d;};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return lhs.d == rhs.d;};
f_eq(): m_rule_session_p(NULL) {};
f_eq(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-NE-----------------------------------------------------------------------------------------------------------------
struct f_ne
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "!=";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs!=rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs!=rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs!=rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs!=rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs!=rhs;};
return_type operator()(std::string const&lhs, std::string const&rhs)const{return lhs!=rhs;};
return_type operator()(date_type const lhs, date_type const rhs)const{return lhs.d != rhs.d;};
return_type operator()(date_type const lhs, time_type const rhs)const{return rdf::date_adaptor(lhs) != rdf::as_ptime(rhs).date();};
return_type operator()(time_type const lhs, date_type const rhs)const{return rdf::as_ptime(lhs).date() != rdf::date_adaptor(rhs);};
return_type operator()(time_type const lhs, time_type const rhs)const{return lhs.d != rhs.d;};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return lhs.d != rhs.d;};
f_ne(): m_rule_session_p(NULL) {};
f_ne(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-AND----------------------------------------------------------------------------------------------------------------
struct f_and
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "and";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs and rhs;};
return_type operator()(int_type const lhs, u_int_type const rhs)const{return lhs and rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs and rhs;};
return_type operator()(int_type const lhs, date_type const rhs)const{return lhs and rhs.d;};
return_type operator()(u_int_type const lhs, int_type const rhs)const{return lhs and rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs and rhs;};
return_type operator()(u_int_type const lhs, real_type const rhs)const{return lhs and rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs and rhs;};
return_type operator()(real_type const lhs, u_int_type const rhs)const{return lhs and rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs and rhs;};
return_type operator()(date_type const lhs, date_type const rhs)const{return lhs.d and rhs.d;};
return_type operator()(date_type const lhs, time_type const rhs)const{return lhs.d and rhs.d;};
return_type operator()(date_type const lhs, duration_type const rhs)const{return lhs.d and rhs.d;};
return_type operator()(date_type const lhs, int_type const rhs)const{return lhs.d and rhs;};
return_type operator()(time_type const lhs, date_type const rhs)const{return lhs.d and rhs.d;};
return_type operator()(time_type const lhs, time_type const rhs)const{return lhs.d and rhs.d;};
return_type operator()(time_type const lhs, duration_type const rhs)const{return lhs.d and rhs.d;};
return_type operator()(duration_type const lhs, date_type const rhs)const{return lhs.d and rhs.d;};
return_type operator()(duration_type const lhs, time_type const rhs)const{return lhs.d and rhs.d;};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return lhs.d and rhs.d;};
f_and(): m_rule_session_p(NULL) {};
f_and(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-OR----------------------------------------------------------------------------------------------------------------
struct f_or
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "or";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs or rhs;};
return_type operator()(int_type const lhs, u_int_type const rhs)const{return lhs or rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs or rhs;};
return_type operator()(int_type const lhs, date_type const rhs)const{return lhs or rhs.d;};
return_type operator()(u_int_type const lhs, int_type const rhs)const{return lhs or rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs or rhs;};
return_type operator()(u_int_type const lhs, real_type const rhs)const{return lhs or rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs or rhs;};
return_type operator()(real_type const lhs, u_int_type const rhs)const{return lhs or rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs or rhs;};
return_type operator()(date_type const lhs, date_type const rhs)const{return lhs.d or rhs.d;};
return_type operator()(date_type const lhs, time_type const rhs)const{return lhs.d or rhs.d;};
return_type operator()(date_type const lhs, duration_type const rhs)const{return lhs.d or rhs.d;};
return_type operator()(date_type const lhs, int_type const rhs)const{return lhs.d or rhs;};
return_type operator()(time_type const lhs, date_type const rhs)const{return lhs.d or rhs.d;};
return_type operator()(time_type const lhs, time_type const rhs)const{return lhs.d or rhs.d;};
return_type operator()(time_type const lhs, duration_type const rhs)const{return lhs.d or rhs.d;};
return_type operator()(duration_type const lhs, date_type const rhs)const{return lhs.d or rhs.d;};
return_type operator()(duration_type const lhs, time_type const rhs)const{return lhs.d or rhs.d;};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return lhs.d or rhs.d;};
f_or(): m_rule_session_p(NULL) {};
f_or(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-BIT_AND----------------------------------------------------------------------------------------------------------------
struct f_bit_and
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "bit_and";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs & rhs;};
return_type operator()(int_type const lhs, u_int_type const rhs)const{return lhs & rhs;};
return_type operator()(u_int_type const lhs, int_type const rhs)const{return lhs & rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs & rhs;};
f_bit_and(): m_rule_session_p(NULL) {};
f_bit_and(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-BIT_OR----------------------------------------------------------------------------------------------------------------
struct f_bit_or
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "bit_or";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs | rhs;};
return_type operator()(int_type const lhs, u_int_type const rhs)const{return lhs | rhs;};
return_type operator()(u_int_type const lhs, int_type const rhs)const{return lhs | rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs | rhs;};
f_bit_or(): m_rule_session_p(NULL) {};
f_bit_or(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-XOR----------------------------------------------------------------------------------------------------------------
struct f_xor
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "xor";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs xor rhs;};
return_type operator()(int_type const lhs, u_int_type const rhs)const{return lhs xor rhs;};
return_type operator()(int_type const lhs, date_type const rhs)const{return lhs xor rhs.d;};
return_type operator()(u_int_type const lhs, int_type const rhs)const{return lhs xor rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs xor rhs;};
return_type operator()(date_type const lhs, date_type const rhs)const{return lhs.d xor rhs.d;};
return_type operator()(date_type const lhs, time_type const rhs)const{return lhs.d xor rhs.d;};
return_type operator()(date_type const lhs, duration_type const rhs)const{return lhs.d xor rhs.d;};
return_type operator()(date_type const lhs, int_type const rhs)const{return lhs.d xor rhs;};
return_type operator()(time_type const lhs, date_type const rhs)const{return lhs.d xor rhs.d;};
return_type operator()(time_type const lhs, time_type const rhs)const{return lhs.d xor rhs.d;};
return_type operator()(time_type const lhs, duration_type const rhs)const{return lhs.d xor rhs.d;};
return_type operator()(duration_type const lhs, date_type const rhs)const{return lhs.d xor rhs.d;};
return_type operator()(duration_type const lhs, time_type const rhs)const{return lhs.d xor rhs.d;};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return lhs.d xor rhs.d;};
f_xor(): m_rule_session_p(NULL) {};
f_xor(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-ADD----------------------------------------------------------------------------------------------------------------
struct f_add
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "+";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs + rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs + rhs;};
return_type operator()(int_type const lhs, date_type const rhs)const{return rdf::date_adaptor(rdf::date_adaptor(rhs) + boost::gregorian::days(lhs)).as_date_type();};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs + rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs + rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs + rhs;};
return_type operator()(std::string const&lhs, std::string const&rhs)const{return lhs + rhs;};
return_type operator()(date_type const lhs, duration_type const rhs)const{return rdf::as_time_type(boost::posix_time::ptime(rdf::date_adaptor(lhs), rdf::as_time_duration(rhs)));};
return_type operator()(date_type const lhs, int_type const rhs)const{return rdf::date_adaptor(rdf::date_adaptor(lhs) + boost::gregorian::days(rhs)).as_date_type();};
return_type operator()(time_type const lhs, duration_type const rhs)const{return rdf::as_time_type(rdf::as_ptime(lhs) + rdf::as_time_duration(rhs));};
return_type operator()(duration_type const lhs, date_type const rhs)const{return rdf::as_time_type(boost::posix_time::ptime(rdf::date_adaptor(rhs), rdf::as_time_duration(lhs)));};
return_type operator()(duration_type const lhs, time_type const rhs)const{return rdf::as_time_type(rdf::as_ptime(rhs) + rdf::as_time_duration(lhs));};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return rdf::as_duration_type(rdf::as_time_duration(lhs) + rdf::as_time_duration(rhs));};
f_add(): m_rule_session_p(NULL) {};
f_add(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-SUB----------------------------------------------------------------------------------------------------------------
struct f_sub
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "-";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs - rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs - rhs;};
return_type operator()(int_type const lhs, date_type const rhs)const{return rdf::date_adaptor(rdf::date_adaptor(rhs) - boost::gregorian::days(lhs)).as_date_type();};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs - rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs - rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs - rhs;};
return_type operator()(date_type const lhs, duration_type const rhs)const{return rdf::as_time_type(boost::posix_time::ptime(rdf::date_adaptor(lhs), boost::posix_time::time_duration(0, 0, 0, 0)) - rdf::as_time_duration(rhs));};
return_type operator()(date_type const lhs, int_type const rhs)const{return rdf::date_adaptor(rdf::date_adaptor(lhs) - boost::gregorian::days(rhs)).as_date_type();};
return_type operator()(time_type const lhs, duration_type const rhs)const{return rdf::as_time_type(rdf::as_ptime(lhs) - rdf::as_time_duration(rhs));};
return_type operator()(duration_type const lhs, duration_type const rhs)const{return rdf::as_duration_type(rdf::as_time_duration(lhs) - rdf::as_time_duration(rhs));};
f_sub(): m_rule_session_p(NULL) {};
f_sub(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-MUL----------------------------------------------------------------------------------------------------------------
struct f_mul
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "*";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs * rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs * rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs * rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs * rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs * rhs;};
f_mul(): m_rule_session_p(NULL) {};
f_mul(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-DIV----------------------------------------------------------------------------------------------------------------
struct f_div
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "/";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs / rhs;};
return_type operator()(int_type const lhs, real_type const rhs)const{return lhs / rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs / rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return lhs / rhs;};
return_type operator()(real_type const lhs, real_type const rhs)const{return lhs / rhs;};
f_div(): m_rule_session_p(NULL) {};
f_div(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-POW----------------------------------------------------------------------------------------------------------------
struct f_pow
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "pow";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return boost::numeric_cast<real_type>(pow(lhs, rhs));};
return_type operator()(int_type const lhs, u_int_type const rhs)const{return boost::numeric_cast<real_type>(pow(lhs, rhs));};
return_type operator()(int_type const lhs, real_type const rhs)const{return boost::numeric_cast<real_type>(pow(lhs, rhs));};
return_type operator()(u_int_type const lhs, int_type const rhs)const{return boost::numeric_cast<real_type>(pow(lhs, rhs));};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return boost::numeric_cast<real_type>(pow(lhs, rhs));};
return_type operator()(u_int_type const lhs, real_type const rhs)const{return boost::numeric_cast<real_type>(pow(lhs, rhs));};
return_type operator()(real_type const lhs, int_type const rhs)const{return boost::numeric_cast<real_type>(pow(lhs, rhs));};
return_type operator()(real_type const lhs, u_int_type const rhs)const{return boost::numeric_cast<real_type>(pow(lhs, rhs));};
return_type operator()(real_type const lhs, real_type const rhs)const{return boost::numeric_cast<real_type>(pow(lhs, rhs));};
f_pow(): m_rule_session_p(NULL) {};
f_pow(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-MOD----------------------------------------------------------------------------------------------------------------
struct f_mod
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "%";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(int_type const lhs, int_type const rhs)const{return lhs % rhs;};
return_type operator()(int_type const lhs, u_int_type const rhs)const{return lhs % rhs;};
return_type operator()(u_int_type const lhs, int_type const rhs)const{return lhs % rhs;};
return_type operator()(u_int_type const lhs, u_int_type const rhs)const{return lhs % rhs;};
return_type operator()(real_type const lhs, int_type const rhs)const{return boost::numeric_cast<real_type>(fmod(lhs, rhs));};
return_type operator()(real_type const lhs, u_int_type const rhs)const{return boost::numeric_cast<real_type>(fmod(lhs, rhs));};
return_type operator()(real_type const lhs, real_type const rhs)const{return boost::numeric_cast<real_type>(fmod(lhs, rhs));};
f_mod(): m_rule_session_p(NULL) {};
f_mod(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-STR-EQ-CASE----------------------------------------------------------------------------------------------------------------
struct f_str_eq_case
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "eq_case";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(std::string const&lhs, std::string const&rhs)const{return rhs==lhs;};
f_str_eq_case(): m_rule_session_p(NULL) {};
f_str_eq_case(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-STR-EQ-NO_CASE----------------------------------------------------------------------------------------------------------------
inline bool no_case_compare(char l, char r){return toupper(l) == toupper(r);};
struct f_str_eq_no_case
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "eq_no_case";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(std::string const&lhs, std::string const&rhs)const
{
if(lhs.length() != rhs.length()) return false;
return equal(lhs.begin(), lhs.end(), rhs.begin(), no_case_compare);
};
f_str_eq_no_case(): m_rule_session_p(NULL) {};
f_str_eq_no_case(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-STR-STARTS_WITH----------------------------------------------------------------------------------------------------------------
struct f_str_starts_with
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "starts_with";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(std::string const&lhs, std::string const&rhs)const
{
std::size_t ll = lhs.length();
std::size_t lr = rhs.length();
if(ll < lr) return 0;
return lhs.substr(0, lr) == rhs;
};
f_str_starts_with(): m_rule_session_p(NULL) {};
f_str_starts_with(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-STR-ENDS_WITH----------------------------------------------------------------------------------------------------------------
struct f_str_ends_with
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "ends_with";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(std::string const&lhs, std::string const&rhs)const
{
std::size_t ll = lhs.length();
std::size_t lr = rhs.length();
if(ll < lr) return 0;
return lhs.substr(ll-lr, ll) == rhs;
};
f_str_ends_with(): m_rule_session_p(NULL) {};
f_str_ends_with(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-STR-EQ-CONTAINS----------------------------------------------------------------------------------------------------------------
struct f_str_contains
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "contains";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(std::string const&lhs, std::string const&rhs)const{return lhs.find(rhs) != std::string::npos;};
f_str_contains(): m_rule_session_p(NULL) {};
f_str_contains(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-STR_TO_UPPER-----------------------------------------------------------------------------------------------------------------
struct u_str_to_upper
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "to_upper";};
template<class T>return_type const operator()(T const&)const{return no_value();};
return_type operator()(std::string const&lhs)const
{
std::string str(lhs);
std::string::iterator itor = str.begin();
std::string::iterator end = str.end();
for(; itor!=end; ++itor) *itor = toupper(*itor);
return str;
};
u_str_to_upper(): m_rule_session_p(NULL) {};
u_str_to_upper(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-STR_TO_LOWER-----------------------------------------------------------------------------------------------------------------
struct u_str_to_lower
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "to_lower";};
template<class T>return_type const operator()(T const&)const{return no_value();};
return_type operator()(std::string const&lhs)const
{
std::string str(lhs);
std::string::iterator itor = str.begin();
std::string::iterator end = str.end();
for(; itor!=end; ++itor) *itor = tolower(*itor);
return str;
};
u_str_to_lower(): m_rule_session_p(NULL) {};
u_str_to_lower(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-STR_TO_LOWER-----------------------------------------------------------------------------------------------------------------
struct u_str_len
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "length_of";};
template<class T>return_type const operator()(T const&)const{return no_value();};
return_type operator()(std::string const&lhs)const{return int(lhs.length());};
u_str_len(): m_rule_session_p(NULL) {};
u_str_len(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-IS_TYPE----------------------------------------------------------------------------------------------------------------
//enum literal_type {unset, integer, unsigned_integer, real, string};
struct f_is_type
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "is_type";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(no_value const lhs, int_type const rhs)const{return int(rhs == rdf::unset);};
return_type operator()(int_type const lhs, int_type const rhs)const{return int(rhs == rdf::integer);};
return_type operator()(u_int_type const lhs, int_type const rhs)const{return int(rhs == rdf::unsigned_integer);};
return_type operator()(real_type const lhs, int_type const rhs)const{return int(rhs == rdf::real);};
return_type operator()(date_type const lhs, int_type const rhs)const{return int(rhs == rdf::date);};
return_type operator()(time_type const lhs, int_type const rhs)const{return int(rhs == rdf::time);};
return_type operator()(duration_type const lhs, int_type const rhs)const{return int(rhs == rdf::duration);};
return_type operator()(std::string const&lhs, int_type const rhs)const{return int(rhs == rdf::string);};
f_is_type(): m_rule_session_p(NULL) {};
f_is_type(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-GET_TYPE-----------------------------------------------------------------------------------------------------------------
struct u_get_type
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "get_type";};
return_type operator()(no_value const lhs)const{return int(rdf::unset);};
return_type operator()(int_type const lhs)const{return int(rdf::integer);};
return_type operator()(u_int_type const lhs)const{return int(rdf::unsigned_integer);};
return_type operator()(real_type const lhs)const{return int(rdf::real);};
return_type operator()(date_type const lhs)const{return int(rdf::date);};
return_type operator()(time_type const lhs)const{return int(rdf::time);};
return_type operator()(duration_type const lhs)const{return int(rdf::duration);};
return_type operator()(std::string const&lhs)const{return int(rdf::string);};
u_get_type(): m_rule_session_p(NULL) {};
u_get_type(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-GET_AGE-----------------------------------------------------------------------------------------------------------------
struct u_get_age
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "get_age";};
template<class T>return_type const operator()(T const& lhs)const{return no_value();};
return_type operator()(date_type const lhs)const
{
boost::gregorian::date today = boost::gregorian::day_clock::local_day();
rdf::date_adaptor birthday(lhs);
int age = today.year() - birthday.year();
if(today.day_of_year() < birthday.day_of_year()) age -= 1;
return age;
};
u_get_age(): m_rule_session_p(NULL) {};
u_get_age(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-GET_AGE_AS_OF---------------------------------------------------------------------------------------------------------------
struct f_get_age_as_of
{
typedef literal_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "get_age_as_of";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type operator()(date_type const lhs, date_type const rhs)const
{
rdf::date_adaptor birthday(lhs);
rdf::date_adaptor as_of(rhs);
int age = as_of.year() - birthday.year();
if(as_of.day_of_year() < birthday.day_of_year()) age -= 1;
return age;
};
f_get_age_as_of(): m_rule_session_p(NULL) {};
f_get_age_as_of(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-IS_BNODE-----------------------------------------------------------------------------------------------------------------
// return true if resource is a bnode
struct u_is_bnode
{
typedef resource_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "is_bnode";};
return_type const operator()(rdf::index_type const lhs)const{return lhs ? lhs->is_bnode():false;};
u_is_bnode(): m_rule_session_p(NULL) {};
u_is_bnode(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-IS_LITERAL-----------------------------------------------------------------------------------------------------------------
// return true if resource is a literal
struct u_is_literal
{
typedef resource_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "is_literal";};
return_type const operator()(rdf::index_type const lhs)const{return lhs ? lhs->is_literal():false;};
u_is_literal(): m_rule_session_p(NULL) {};
u_is_literal(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-IS_RESOURCE-----------------------------------------------------------------------------------------------------------------
// return true if resource is a resource (not bnode or literal)
struct u_is_resource
{
typedef resource_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "is_resource";};
return_type const operator()(rdf::index_type const lhs)const{return lhs ? lhs->is_resource():false;};
u_is_resource(): m_rule_session_p(NULL) {};
u_is_resource(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-CREATE_BNODE-----------------------------------------------------------------------------------------------------------------
// return a new instance of type bnode
struct u_create_bnode
{
typedef literal_operator_tag operant_type;
typedef rdf::index_type const return_type;
inline char const* name()const{return "create_bnode";};
template<class T>
return_type const operator()(T const&)const{return m_rule_session_p->get_rdf_session().create_bnode_as_index();};
u_create_bnode(): m_rule_session_p(NULL) {};
u_create_bnode(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-CREATE_RESOURCE-----------------------------------------------------------------------------------------------------------------
// return a new instance of type resource
struct u_create_resource
{
typedef literal_operator_tag operant_type;
typedef expression_data const return_type;
inline char const* name()const{return "create_resource";};
template<class T>
return_type const operator()(T const&)const{return no_value();};
return_type const operator()(std::string const& lhs)const{return m_rule_session_p->get_rdf_session().create_resource_as_index(lhs);};
u_create_resource(): m_rule_session_p(NULL) {};
u_create_resource(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-CREATE_LITERAL-----------------------------------------------------------------------------------------------------------------
// return a new instance of type resource
struct u_create_literal
{
typedef literal_operator_tag operant_type;
typedef rdf::index_type const return_type;
inline char const* name()const{return "create_literal";};
template<class T>
return_type const operator()(T const& lhs)const{return m_rule_session_p->get_rdf_session().create_literal_as_index(boost::lexical_cast<std::string>(lhs), lhs);};
u_create_literal(): m_rule_session_p(NULL) {};
u_create_literal(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-SAME_AS-----------------------------------------------------------------------------------------------------------------
// return true if resources (lhs and rhs) are the same
struct f_same_as
{
typedef resource_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "same_as";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type const operator()(rdf::index_type const lhs, rdf::index_type const rhs)const{return lhs == rhs;};
f_same_as(): m_rule_session_p(NULL) {};
f_same_as(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-DIFFERENT_FROM-----------------------------------------------------------------------------------------------------------------
// return true if resources (lhs and rhs) have different index_type
struct f_different_from
{
typedef resource_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "different_from";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type const operator()(rdf::index_type const lhs, rdf::index_type const rhs)const{return lhs != rhs;};
f_different_from(): m_rule_session_p(NULL) {};
f_different_from(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-EXIST-----------------------------------------------------------------------------------------------------------------
// return true if resources lhs has at least one relation with predicate rhs.
struct f_exist
{
typedef resource_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "exist";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type const operator()(rdf::index_type const lhs, rdf::index_type const rhs)const
{
return m_rule_session_p->get_rdf_session().contains(lhs, rhs, rdf::all_objects());
};
f_exist(): m_rule_session_p(NULL) {};
f_exist(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-EXIST_NOT-----------------------------------------------------------------------------------------------------------------
// return true if resources lhs has at least one relation with predicate rhs.
struct f_exist_not
{
typedef resource_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "exist_not";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type const operator()(rdf::index_type const lhs, rdf::index_type const rhs)const
{
return !m_rule_session_p->get_rdf_session().contains(lhs, rhs, rdf::all_objects());
};
f_exist_not(): m_rule_session_p(NULL) {};
f_exist_not(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-test_max_cadinality-----------------------------------------------------------------------------------------------------------------
// return true if resources lhs fails the max_cardinality test specified by rhs.
struct f_test_max_cardinality
{
typedef resource_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "test_max_cardinality";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type const operator()(rdf::index_type const lhs, rdf::index_type const rhs)const
{
return m_rule_session_p->test_max_cardinality(lhs, rhs);
};
f_test_max_cardinality(): m_rule_session_p(NULL) {};
f_test_max_cardinality(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-test_min_cardinality-----------------------------------------------------------------------------------------------------------------
// return true if resources lhs fails the min_cardinality test specified by rhs.
struct f_test_min_cardinality
{
typedef resource_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "test_min_cardinality";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type const operator()(rdf::index_type const lhs, rdf::index_type const rhs)const
{
return m_rule_session_p->test_min_cardinality(lhs, rhs);
};
f_test_min_cardinality(): m_rule_session_p(NULL) {};
f_test_min_cardinality(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --operator-test_cardinality-----------------------------------------------------------------------------------------------------------------
// return true if resources lhs fails the cardinality test specified by rhs.
struct f_test_cardinality
{
typedef resource_operator_tag operant_type;
typedef expression_data return_type;
inline char const* name()const{return "test_cardinality";};
template<class T, class U>return_type const operator()(T const&, U const&)const{return no_value();};
return_type const operator()(rdf::index_type const lhs, rdf::index_type const rhs)const
{
return m_rule_session_p->test_cardinality(lhs, rhs);
};
f_test_cardinality(): m_rule_session_p(NULL) {};
f_test_cardinality(rule_session * rule_session_p): m_rule_session_p(rule_session_p) {};
private:
rule_session * m_rule_session_p;
};
// --<<<--VISITORS----------------------------------------------------------------------------------------------------------------
}; /* internal namespace */
}; /* rule namespace */
#endif /*RULE_OPERATORS_H_*/