PredicateUtil { static public Boolean evaluate(Predicate predicate, EvaluationContext context){ try { return evaluatePredicate(predicate, context); } catch(PMMLException pe){ throw pe.ensureContext(predicate); } } 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);  }
@Test public void evaluateBooleanSimplePredicate(){ FieldName flag = FieldName.create("flag"); SimplePredicate simplePredicate = new SimplePredicate(flag, SimplePredicate.Operator.EQUAL, "true"); assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, true)); assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, false)); simplePredicate.setOperator(SimplePredicate.Operator.NOT_EQUAL); assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, true)); assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, false)); simplePredicate.setValue("0.5"); simplePredicate.setOperator(SimplePredicate.Operator.LESS_OR_EQUAL); assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, true)); assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, false)); simplePredicate.setOperator(SimplePredicate.Operator.GREATER_THAN); assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, true)); assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, false)); }
@Test public void evaluateSurrogateCompoundPredicate(){ FieldName temperature = FieldName.create("temperature"); FieldName humidity = FieldName.create("humidity"); CompoundPredicate temperaturePredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, null) .addPredicates( new SimplePredicate(temperature, SimplePredicate.Operator.LESS_THAN, "90"), new SimplePredicate(temperature, SimplePredicate.Operator.GREATER_THAN, "50") ); SimplePredicate humidityPredicate = new SimplePredicate(humidity, SimplePredicate.Operator.GREATER_OR_EQUAL, "80"); CompoundPredicate compoundPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.SURROGATE, null) .addPredicates(temperaturePredicate, humidityPredicate); assertEquals(Boolean.TRUE, evaluate(compoundPredicate, temperature, 70, humidity, null)); assertEquals(Boolean.FALSE, evaluate(compoundPredicate, temperature, 40, humidity, null)); assertEquals(Boolean.FALSE, evaluate(compoundPredicate, temperature, 100, humidity, null)); assertEquals(Boolean.TRUE, evaluate(compoundPredicate, temperature, null, humidity, 90)); assertEquals(Boolean.FALSE, evaluate(compoundPredicate, temperature, null, humidity, 70)); assertEquals(null, evaluate(compoundPredicate, temperature, null, humidity, null)); }
@Test public void evaluateBooleanCompoundPredicate(){ CompoundPredicate compoundPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, null) .addPredicates(True.INSTANCE, False.INSTANCE); assertEquals(Boolean.FALSE, evaluate(compoundPredicate)); compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.OR); assertEquals(Boolean.TRUE, evaluate(compoundPredicate)); compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.XOR); assertEquals(Boolean.TRUE, evaluate(compoundPredicate)); }