@Test public void evaluateSimplePredicate(){ FieldName age = FieldName.create("age"); SimplePredicate simplePredicate = new SimplePredicate(age, SimplePredicate.Operator.IS_MISSING, null); assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30)); assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, null)); simplePredicate.setOperator(SimplePredicate.Operator.IS_NOT_MISSING); assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30)); assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, null)); simplePredicate.setValue("30"); simplePredicate.setOperator(SimplePredicate.Operator.EQUAL); assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30)); assertEquals(null, evaluate(simplePredicate, age, null)); simplePredicate.setOperator(SimplePredicate.Operator.NOT_EQUAL); assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30)); assertEquals(null, evaluate(simplePredicate, age, null)); simplePredicate.setOperator(SimplePredicate.Operator.LESS_THAN); assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30)); simplePredicate.setOperator(SimplePredicate.Operator.LESS_OR_EQUAL); assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30)); simplePredicate.setOperator(SimplePredicate.Operator.GREATER_OR_EQUAL); assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30)); simplePredicate.setOperator(SimplePredicate.Operator.GREATER_THAN); assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30)); }
static public Boolean evaluateSimplePredicate(SimplePredicate simplePredicate, EvaluationContext context){ FieldName name = simplePredicate.getField(); if(name == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_FIELD); } SimplePredicate.Operator operator = simplePredicate.getOperator(); if(operator == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_OPERATOR); } switch(operator){ case IS_MISSING: case IS_NOT_MISSING: if(simplePredicate.hasValue()){ throw new MisplacedAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE, simplePredicate.getValue()); } break; default: if(!simplePredicate.hasValue()){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE); } break; } FieldValue value = context.evaluate(name); switch(operator){ case IS_MISSING: return Boolean.valueOf(FieldValueUtil.isMissing(value)); case IS_NOT_MISSING: return Boolean.valueOf(!FieldValueUtil.isMissing(value)); default: break; } if(FieldValueUtil.isMissing(value)){ return null; } switch(operator){ case EQUAL: return value.equals(simplePredicate); case NOT_EQUAL: return !value.equals(simplePredicate); default: break; } int order = value.compareTo(simplePredicate); switch(operator){ case LESS_THAN: return Boolean.valueOf(order < 0); case LESS_OR_EQUAL: return Boolean.valueOf(order <= 0); case GREATER_OR_EQUAL: return Boolean.valueOf(order >= 0); case GREATER_THAN: return Boolean.valueOf(order > 0); default: throw new UnsupportedAttributeException(simplePredicate, operator); } }
PredicateUtil { static public Boolean evaluateSimplePredicate(SimplePredicate simplePredicate, EvaluationContext context){ FieldName name = simplePredicate.getField(); if(name == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_FIELD); } SimplePredicate.Operator operator = simplePredicate.getOperator(); if(operator == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_OPERATOR); } switch(operator){ case IS_MISSING: case IS_NOT_MISSING: if(simplePredicate.hasValue()){ throw new MisplacedAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE, simplePredicate.getValue()); } break; default: if(!simplePredicate.hasValue()){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE); } break; } FieldValue value = context.evaluate(name); switch(operator){ case IS_MISSING: return Boolean.valueOf(FieldValueUtil.isMissing(value)); case IS_NOT_MISSING: return Boolean.valueOf(!FieldValueUtil.isMissing(value)); default: break; } if(FieldValueUtil.isMissing(value)){ return null; } switch(operator){ case EQUAL: return value.equals(simplePredicate); case NOT_EQUAL: return !value.equals(simplePredicate); default: break; } int order = value.compareTo(simplePredicate); switch(operator){ case LESS_THAN: return Boolean.valueOf(order < 0); case LESS_OR_EQUAL: return Boolean.valueOf(order <= 0); case GREATER_OR_EQUAL: return Boolean.valueOf(order >= 0); case GREATER_THAN: return Boolean.valueOf(order > 0); default: throw new UnsupportedAttributeException(simplePredicate, operator); } } }
PredicateUtil { static public Boolean evaluateSimplePredicate(SimplePredicate simplePredicate, EvaluationContext context){ FieldName name = simplePredicate.getField(); if(name == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_FIELD); } SimplePredicate.Operator operator = simplePredicate.getOperator(); if(operator == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_OPERATOR); } switch(operator){ case IS_MISSING: case IS_NOT_MISSING: if(simplePredicate.hasValue()){ throw new MisplacedAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE, simplePredicate.getValue()); } break; default: if(!simplePredicate.hasValue()){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE); } break; } FieldValue value = context.evaluate(name); switch(operator){ case IS_MISSING: return Boolean.valueOf(FieldValueUtil.isMissing(value)); case IS_NOT_MISSING: return Boolean.valueOf(!FieldValueUtil.isMissing(value)); default: break; } if(FieldValueUtil.isMissing(value)){ return null; } switch(operator){ case EQUAL: return value.equals(simplePredicate); case NOT_EQUAL: return !value.equals(simplePredicate); default: break; } int order = value.compareTo(simplePredicate); switch(operator){ case LESS_THAN: return Boolean.valueOf(order < 0); case LESS_OR_EQUAL: return Boolean.valueOf(order <= 0); case GREATER_OR_EQUAL: return Boolean.valueOf(order >= 0); case GREATER_THAN: return Boolean.valueOf(order > 0); default: throw new UnsupportedAttributeException(simplePredicate, operator); } } private  PredicateUtil(); }
PredicateUtil { static public Boolean evaluateSimplePredicate(SimplePredicate simplePredicate, EvaluationContext context){ FieldName name = simplePredicate.getField(); if(name == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_FIELD); } SimplePredicate.Operator operator = simplePredicate.getOperator(); if(operator == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_OPERATOR); } switch(operator){ case IS_MISSING: case IS_NOT_MISSING: if(simplePredicate.hasValue()){ throw new MisplacedAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE, simplePredicate.getValue()); } break; default: if(!simplePredicate.hasValue()){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE); } break; } FieldValue value = context.evaluate(name); switch(operator){ case IS_MISSING: return Boolean.valueOf(FieldValueUtil.isMissing(value)); case IS_NOT_MISSING: return Boolean.valueOf(!FieldValueUtil.isMissing(value)); default: break; } if(FieldValueUtil.isMissing(value)){ return null; } switch(operator){ case EQUAL: return value.equals(simplePredicate); case NOT_EQUAL: return !value.equals(simplePredicate); default: break; } int order = value.compareTo(simplePredicate); switch(operator){ case LESS_THAN: return Boolean.valueOf(order < 0); case LESS_OR_EQUAL: return Boolean.valueOf(order <= 0); case GREATER_OR_EQUAL: return Boolean.valueOf(order >= 0); case GREATER_THAN: return Boolean.valueOf(order > 0); default: throw new UnsupportedAttributeException(simplePredicate, operator); } } private  PredicateUtil(); static Predicate ensurePredicate(E hasPredicate); static Boolean evaluatePredicateContainer(E hasPredicate, EvaluationContext context); static Boolean evaluate(Predicate predicate, EvaluationContext context); static Boolean evaluateSimplePredicate(SimplePredicate simplePredicate, EvaluationContext context); static Boolean evaluateSimpleSetPredicate(SimpleSetPredicate simpleSetPredicate, EvaluationContext context); static Boolean evaluateCompoundPredicate(CompoundPredicate compoundPredicate, EvaluationContext context); static CompoundPredicateResult evaluateCompoundPredicateInternal(CompoundPredicate compoundPredicate, EvaluationContext context); static Boolean evaluateTrue(True truePredicate); static Boolean evaluateFalse(False falsePredicate); static Boolean evaluateJavaPredicate(JavaPredicate javaPredicate, EvaluationContext context); static Boolean binaryAnd(Boolean left, Boolean right); static Boolean binaryOr(Boolean left, Boolean right); static Boolean binaryXor(Boolean left, Boolean right); }
PredicateUtil { static public Boolean evaluateSimplePredicate(SimplePredicate simplePredicate, EvaluationContext context){ FieldName name = simplePredicate.getField(); if(name == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_FIELD); } SimplePredicate.Operator operator = simplePredicate.getOperator(); if(operator == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_OPERATOR); } switch(operator){ case IS_MISSING: case IS_NOT_MISSING: if(simplePredicate.hasValue()){ throw new MisplacedAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE, simplePredicate.getValue()); } break; default: if(!simplePredicate.hasValue()){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE); } break; } FieldValue value = context.evaluate(name); switch(operator){ case IS_MISSING: return Boolean.valueOf(FieldValueUtil.isMissing(value)); case IS_NOT_MISSING: return Boolean.valueOf(!FieldValueUtil.isMissing(value)); default: break; } if(FieldValueUtil.isMissing(value)){ return null; } switch(operator){ case EQUAL: return value.equals(simplePredicate); case NOT_EQUAL: return !value.equals(simplePredicate); default: break; } int order = value.compareTo(simplePredicate); switch(operator){ case LESS_THAN: return Boolean.valueOf(order < 0); case LESS_OR_EQUAL: return Boolean.valueOf(order <= 0); case GREATER_OR_EQUAL: return Boolean.valueOf(order >= 0); case GREATER_THAN: return Boolean.valueOf(order > 0); default: throw new UnsupportedAttributeException(simplePredicate, operator); } } private  PredicateUtil(); static Predicate ensurePredicate(E hasPredicate); static Boolean evaluatePredicateContainer(E hasPredicate, EvaluationContext context); static Boolean evaluate(Predicate predicate, EvaluationContext context); static Boolean evaluateSimplePredicate(SimplePredicate simplePredicate, EvaluationContext context); static Boolean evaluateSimpleSetPredicate(SimpleSetPredicate simpleSetPredicate, EvaluationContext context); static Boolean evaluateCompoundPredicate(CompoundPredicate compoundPredicate, EvaluationContext context); static CompoundPredicateResult evaluateCompoundPredicateInternal(CompoundPredicate compoundPredicate, EvaluationContext context); static Boolean evaluateTrue(True truePredicate); static Boolean evaluateFalse(False falsePredicate); static Boolean evaluateJavaPredicate(JavaPredicate javaPredicate, EvaluationContext context); static Boolean binaryAnd(Boolean left, Boolean right); static Boolean binaryOr(Boolean left, Boolean right); static Boolean binaryXor(Boolean left, Boolean right);  }