@Test public void evaluateAggregate(){ FieldName name = FieldName.create("x"); TypeInfo typeInfo = new SimpleTypeInfo(DataType.DATE, OpType.ORDINAL); List<?> values = Arrays.asList(TypeUtil.parse(DataType.DATE, "2013-01-01"), TypeUtil.parse(DataType.DATE, "2013-02-01"), TypeUtil.parse(DataType.DATE, "2013-03-01")); Map<FieldName, FieldValue> arguments = Collections.singletonMap(name, FieldValue.create(typeInfo, values)); Aggregate aggregate = new Aggregate(name, Aggregate.Function.COUNT); assertEquals(3, evaluate(aggregate, arguments)); aggregate.setFunction(Aggregate.Function.MIN); assertEquals(values.get(0), evaluate(aggregate, arguments)); aggregate.setFunction(Aggregate.Function.MAX); assertEquals(values.get(2), evaluate(aggregate, arguments)); typeInfo = new SimpleTypeInfo(DataType.DATE, OpType.ORDINAL, Lists.reverse(values)); arguments = Collections.singletonMap(name, FieldValue.create(typeInfo, values)); aggregate.setFunction(Aggregate.Function.MIN); assertEquals(values.get(2), evaluate(aggregate, arguments)); aggregate.setFunction(Aggregate.Function.MAX); assertEquals(values.get(0), evaluate(aggregate, arguments)); }
@SuppressWarnings ( value = {"unchecked"} ) static public FieldValue evaluateAggregate(Aggregate aggregate, EvaluationContext context){ FieldValue value = context.evaluate(ensureField(aggregate)); if(FieldValueUtil.isMissing(value)){ return FieldValues.MISSING_VALUE; } Collection<?> values = value.asCollection(); FieldName groupName = aggregate.getGroupField(); if(groupName != null){ FieldValue groupValue = context.evaluate(groupName); TypeUtil.getDataType(FieldValueUtil.getValue(groupValue)); } values = values.stream() .filter(Objects::nonNull) .map(object -> FieldValueUtil.create(value, object)) .collect(Collectors.toList()); Aggregate.Function function = aggregate.getFunction(); if(function == null){ throw new MissingAttributeException(aggregate, PMMLAttributes.AGGREGATE_FUNCTION); } switch(function){ case COUNT: return FieldValueUtil.create(TypeInfos.CONTINUOUS_INTEGER, values.size()); case SUM: return Functions.SUM.evaluate((List<FieldValue>)values); case AVERAGE: return Functions.AVG.evaluate((List<FieldValue>)values); case MIN: return Collections.min((List<ScalarValue>)values); case MAX: return Collections.max((List<ScalarValue>)values); default: throw new UnsupportedAttributeException(aggregate, function); } }
ExpressionUtil { @SuppressWarnings ( value = {"unchecked"} ) static public FieldValue evaluateAggregate(Aggregate aggregate, EvaluationContext context){ FieldValue value = context.evaluate(ensureField(aggregate)); if(FieldValueUtil.isMissing(value)){ return FieldValues.MISSING_VALUE; } Collection<?> values = value.asCollection(); FieldName groupName = aggregate.getGroupField(); if(groupName != null){ FieldValue groupValue = context.evaluate(groupName); TypeUtil.getDataType(FieldValueUtil.getValue(groupValue)); } values = values.stream() .filter(Objects::nonNull) .map(object -> FieldValueUtil.create(value, object)) .collect(Collectors.toList()); Aggregate.Function function = aggregate.getFunction(); if(function == null){ throw new MissingAttributeException(aggregate, PMMLAttributes.AGGREGATE_FUNCTION); } switch(function){ case COUNT: return FieldValueUtil.create(TypeInfos.CONTINUOUS_INTEGER, values.size()); case SUM: return Functions.SUM.evaluate((List<FieldValue>)values); case AVERAGE: return Functions.AVG.evaluate((List<FieldValue>)values); case MIN: return Collections.min((List<ScalarValue>)values); case MAX: return Collections.max((List<ScalarValue>)values); default: throw new UnsupportedAttributeException(aggregate, function); } } }
ExpressionUtil { @SuppressWarnings ( value = {"unchecked"} ) static public FieldValue evaluateAggregate(Aggregate aggregate, EvaluationContext context){ FieldValue value = context.evaluate(ensureField(aggregate)); if(FieldValueUtil.isMissing(value)){ return FieldValues.MISSING_VALUE; } Collection<?> values = value.asCollection(); FieldName groupName = aggregate.getGroupField(); if(groupName != null){ FieldValue groupValue = context.evaluate(groupName); TypeUtil.getDataType(FieldValueUtil.getValue(groupValue)); } values = values.stream() .filter(Objects::nonNull) .map(object -> FieldValueUtil.create(value, object)) .collect(Collectors.toList()); Aggregate.Function function = aggregate.getFunction(); if(function == null){ throw new MissingAttributeException(aggregate, PMMLAttributes.AGGREGATE_FUNCTION); } switch(function){ case COUNT: return FieldValueUtil.create(TypeInfos.CONTINUOUS_INTEGER, values.size()); case SUM: return Functions.SUM.evaluate((List<FieldValue>)values); case AVERAGE: return Functions.AVG.evaluate((List<FieldValue>)values); case MIN: return Collections.min((List<ScalarValue>)values); case MAX: return Collections.max((List<ScalarValue>)values); default: throw new UnsupportedAttributeException(aggregate, function); } } private  ExpressionUtil(); }
ExpressionUtil { @SuppressWarnings ( value = {"unchecked"} ) static public FieldValue evaluateAggregate(Aggregate aggregate, EvaluationContext context){ FieldValue value = context.evaluate(ensureField(aggregate)); if(FieldValueUtil.isMissing(value)){ return FieldValues.MISSING_VALUE; } Collection<?> values = value.asCollection(); FieldName groupName = aggregate.getGroupField(); if(groupName != null){ FieldValue groupValue = context.evaluate(groupName); TypeUtil.getDataType(FieldValueUtil.getValue(groupValue)); } values = values.stream() .filter(Objects::nonNull) .map(object -> FieldValueUtil.create(value, object)) .collect(Collectors.toList()); Aggregate.Function function = aggregate.getFunction(); if(function == null){ throw new MissingAttributeException(aggregate, PMMLAttributes.AGGREGATE_FUNCTION); } switch(function){ case COUNT: return FieldValueUtil.create(TypeInfos.CONTINUOUS_INTEGER, values.size()); case SUM: return Functions.SUM.evaluate((List<FieldValue>)values); case AVERAGE: return Functions.AVG.evaluate((List<FieldValue>)values); case MIN: return Collections.min((List<ScalarValue>)values); case MAX: return Collections.max((List<ScalarValue>)values); default: throw new UnsupportedAttributeException(aggregate, function); } } 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); }
ExpressionUtil { @SuppressWarnings ( value = {"unchecked"} ) static public FieldValue evaluateAggregate(Aggregate aggregate, EvaluationContext context){ FieldValue value = context.evaluate(ensureField(aggregate)); if(FieldValueUtil.isMissing(value)){ return FieldValues.MISSING_VALUE; } Collection<?> values = value.asCollection(); FieldName groupName = aggregate.getGroupField(); if(groupName != null){ FieldValue groupValue = context.evaluate(groupName); TypeUtil.getDataType(FieldValueUtil.getValue(groupValue)); } values = values.stream() .filter(Objects::nonNull) .map(object -> FieldValueUtil.create(value, object)) .collect(Collectors.toList()); Aggregate.Function function = aggregate.getFunction(); if(function == null){ throw new MissingAttributeException(aggregate, PMMLAttributes.AGGREGATE_FUNCTION); } switch(function){ case COUNT: return FieldValueUtil.create(TypeInfos.CONTINUOUS_INTEGER, values.size()); case SUM: return Functions.SUM.evaluate((List<FieldValue>)values); case AVERAGE: return Functions.AVG.evaluate((List<FieldValue>)values); case MIN: return Collections.min((List<ScalarValue>)values); case MAX: return Collections.max((List<ScalarValue>)values); default: throw new UnsupportedAttributeException(aggregate, function); } } 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);  }