ExpressionUtil { static public FieldValue evaluateApply(Apply apply, EvaluationContext context){ String mapMissingTo = apply.getMapMissingTo(); List<Expression> expressions = apply.getExpressions(); List<FieldValue> values = new ArrayList<>(expressions.size()); Iterator<Expression> arguments = expressions.iterator(); String function = apply.getFunction(); if(function == null){ throw new MissingAttributeException(apply, PMMLAttributes.APPLY_FUNCTION); } condition: if((PMMLFunctions.IF).equals(function)){ if(arguments.hasNext()){ FieldValue flag = evaluate(arguments.next(), context); if(flag == null && mapMissingTo != null){ return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, mapMissingTo); } values.add(flag); if(flag == null){ if(arguments.hasNext()){ arguments.next(); values.add(FieldValues.MISSING_VALUE); if(arguments.hasNext()){ arguments.next(); values.add(FieldValues.MISSING_VALUE); } } break condition; } if(flag.asBoolean()){ if(arguments.hasNext()){ FieldValue trueValue = evaluate(arguments.next(), context); if(FieldValueUtil.isMissing(trueValue) && mapMissingTo != null){ return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, mapMissingTo); } values.add(trueValue); if(arguments.hasNext()){ arguments.next(); values.add(FieldValues.MISSING_VALUE); } } } else { if(arguments.hasNext()){ arguments.next(); values.add(FieldValues.MISSING_VALUE); if(arguments.hasNext()){ FieldValue falseValue = evaluate(arguments.next(), context); if(FieldValueUtil.isMissing(falseValue) && mapMissingTo != null){ return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, mapMissingTo); } values.add(falseValue); } } } } } while(arguments.hasNext()){ FieldValue value = evaluate(arguments.next(), context); if(FieldValueUtil.isMissing(value) && mapMissingTo != null){ return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, mapMissingTo); } values.add(value); } String defaultValue = apply.getDefaultValue(); FieldValue result; SymbolTable<String> symbolTable = EvaluationContext.FUNCTION_GUARD_PROVIDER.get(); if(symbolTable != null){ symbolTable.lock(function); } try { result = evaluateFunction(function, values, context); } catch(InvalidResultException ire){ InvalidValueTreatmentMethod invalidValueTreatmentMethod = apply.getInvalidValueTreatment(); switch(invalidValueTreatmentMethod){ case RETURN_INVALID: throw new InvalidResultException("Function application yielded an invalid result", apply) .initCause(ire); case AS_IS: throw ire; case AS_MISSING: return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, defaultValue); default: throw new UnsupportedAttributeException(apply, invalidValueTreatmentMethod); } } finally { if(symbolTable != null){ symbolTable.release(function); } } if(result == null && defaultValue != null){ return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, defaultValue); } return result; } 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 evaluateApply(){ FieldName name = FieldName.create("x"); Apply apply = new Apply(PMMLFunctions.DIVIDE) .addExpressions(new FieldRef(name), new Constant("0")); assertEquals(null, evaluate(apply, name, null)); apply.setDefaultValue("-1"); assertEquals("-1", evaluate(apply, name, null)); apply.setMapMissingTo("missing"); assertEquals("missing", evaluate(apply, name, null)); apply.setInvalidValueTreatment(InvalidValueTreatmentMethod.RETURN_INVALID); try { evaluate(apply, name, 1); fail(); } catch(InvalidResultException ire){ } apply.setInvalidValueTreatment(InvalidValueTreatmentMethod.AS_IS); try { evaluate(apply, name, 1); fail(); } catch(InvalidResultException ire){ } apply.setInvalidValueTreatment(InvalidValueTreatmentMethod.AS_MISSING); assertEquals("-1", evaluate(apply, name, 1)); }