ExpressionUtil { static public FieldValue evaluate(DerivedField derivedField, EvaluationContext context){ FieldName name = derivedField.getName(); if(name == null){ throw new MissingAttributeException(derivedField, PMMLAttributes.DERIVEDFIELD_NAME); } SymbolTable<FieldName> symbolTable = EvaluationContext.DERIVEDFIELD_GUARD_PROVIDER.get(); if(symbolTable != null){ symbolTable.lock(name); } try { return evaluateTypedExpressionContainer(derivedField, context); } finally { if(symbolTable != null){ symbolTable.release(name); } } } private  ExpressionUtil(); static FieldName ensureField(E hasField); static Expression ensureExpression(E hasExpression); static FieldValue evaluateExpressionContainer(E hasExpression, EvaluationContext context); static FieldValue evaluateTypedExpressionContainer(E hasTypedExpression, EvaluationContext context); static FieldValue evaluate(DerivedField derivedField, EvaluationContext context); static FieldValue evaluate(DefineFunction defineFunction, List<FieldValue> values, EvaluationContext context); static FieldValue evaluate(Expression expression, EvaluationContext context); static FieldValue evaluateConstant(Constant constant); static FieldValue evaluateFieldRef(FieldRef fieldRef, EvaluationContext context); static FieldValue evaluateNormContinuous(NormContinuous normContinuous, EvaluationContext context); static FieldValue evaluateNormDiscrete(NormDiscrete normDiscrete, EvaluationContext context); static FieldValue evaluateDiscretize(Discretize discretize, EvaluationContext context); static FieldValue evaluateMapValues(MapValues mapValues, EvaluationContext context); static FieldValue evaluateTextIndex(TextIndex textIndex, EvaluationContext context); static FieldValue evaluateApply(Apply apply, EvaluationContext context); @SuppressWarnings ( value = {"unchecked"} ) static FieldValue evaluateAggregate(Aggregate aggregate, EvaluationContext context); static FieldValue evaluateJavaExpression(JavaExpression javaExpression, EvaluationContext context); static boolean isEmptyContent(Object value);  }
@Test public void evaluateConstantNaN(){ Constant constant = new Constant("NaN"); assertEquals(Double.NaN, evaluate(constant)); constant.setDataType(DataType.FLOAT); assertEquals(Float.NaN, evaluate(constant)); }
@Test public void evaluateTextIndexNormalization(){ FieldName name = FieldName.create("x"); TextIndexNormalization stepOne = new TextIndexNormalization(); List<List<String>> cells = Arrays.asList( Arrays.asList("interfaces?", "interface", "true"), Arrays.asList("is|are|seem(ed|s?)|were", "be", "true"), Arrays.asList("user friendl(y|iness)", "user_friendly", "true") ); stepOne.setInlineTable(createInlineTable(cells, stepOne)); TextIndexNormalization stepTwo = new TextIndexNormalization() .setInField("re") .setOutField("feature"); cells = Arrays.asList( Arrays.asList("interface be (user_friendly|well designed|excellent)", "ui_good", "true") ); stepTwo.setInlineTable(createInlineTable(cells, stepTwo)); TextIndex textIndex = new TextIndex(name, new Constant("ui_good")) .setLocalTermWeights(TextIndex.LocalTermWeights.BINARY) .setCaseSensitive(false) .addTextIndexNormalizations(stepOne, stepTwo); assertEquals(1, evaluate(textIndex, name, "Testing the app for a few days convinced me the interfaces are excellent!")); }
@Test public void evaluateApplyCondition(){ FieldName name = FieldName.create("x"); Apply condition = new Apply(PMMLFunctions.ISNOTMISSING) .addExpressions(new FieldRef(name)); Apply apply = new Apply(PMMLFunctions.IF) .addExpressions(condition); try { evaluate(apply, name, null); fail(); } catch(FunctionException fe){ } Expression thenPart = new Apply(PMMLFunctions.ABS) .addExpressions(new FieldRef(name)); apply.addExpressions(thenPart); assertEquals(1, evaluate(apply, name, 1)); assertEquals(1, evaluate(apply, name, -1)); assertEquals(null, evaluate(apply, name, null)); Expression elsePart = new Constant("-1") .setDataType(DataType.DOUBLE); apply.addExpressions(elsePart); assertEquals(-1d, evaluate(apply, name, null)); apply.addExpressions(new FieldRef(name)); try { evaluate(apply, name, null); fail(); } catch(FunctionException fe){ } }
@Test public void evaluateApplyJavaFunction(){ FieldName name = FieldName.create("x"); FieldRef fieldRef = new FieldRef(name); Apply apply = new Apply(EchoFunction.class.getName()) .addExpressions(fieldRef); try { evaluate(apply); fail(); } catch(EvaluationException ee){ assertEquals(fieldRef, ee.getContext()); } assertEquals("Hello World!", evaluate(apply, name, "Hello World!")); }
@Test public void evaluateAggregateArithmetic(){ FieldName name = FieldName.create("x"); List<Integer> values = Arrays.asList(1, 2, 3); Aggregate aggregate = new Aggregate(name, Aggregate.Function.COUNT); assertEquals(3, evaluate(aggregate, name, values)); aggregate.setFunction(Aggregate.Function.SUM); assertEquals(6, evaluate(aggregate, name, values)); aggregate.setFunction(Aggregate.Function.AVERAGE); assertEquals(2d, evaluate(aggregate, name, values)); }