DefineFunctionUtils { static MethodDeclaration getExpressionMethodDeclaration(final String methodName, final Expression expression, final DataType dataType, final List<ParameterField> parameterFields) { final ClassOrInterfaceType returnedType = parseClassOrInterfaceType(getBoxedClassName(dataType)); final LinkedHashMap<String, ClassOrInterfaceType> parameterNameTypeMap = new LinkedHashMap<>(DEFAULT_PARAMETERTYPE_MAP); parameterNameTypeMap.putAll(getNameClassOrInterfaceTypeMap(parameterFields)); if (expression instanceof Aggregate) { return getAggregatedMethodDeclaration(methodName, (Aggregate) expression, returnedType, parameterNameTypeMap); } else if (expression instanceof Apply) { return getApplyMethodDeclaration(methodName, (Apply) expression, returnedType, parameterNameTypeMap); } else if (expression instanceof Constant) { return getConstantMethodDeclaration(methodName, (Constant) expression, returnedType, parameterNameTypeMap); } else if (expression instanceof Discretize) { return getDiscretizeMethodDeclaration(methodName, (Discretize) expression, returnedType, parameterNameTypeMap); } else if (expression instanceof FieldRef) { return getFieldRefMethodDeclaration(methodName, (FieldRef) expression, returnedType, parameterNameTypeMap); } else if (expression instanceof Lag) { return getLagMethodDeclaration(methodName, (Lag) expression, returnedType, parameterNameTypeMap); } else if (expression instanceof MapValues) { return getMapValuesMethodDeclaration(methodName, (MapValues) expression, returnedType, parameterNameTypeMap); } else if (expression instanceof NormContinuous) { return getNormContinuousMethodDeclaration(methodName, (NormContinuous) expression, returnedType, parameterNameTypeMap); } else if (expression instanceof NormDiscrete) { return getNormDiscreteMethodDeclaration(methodName, (NormDiscrete) expression, returnedType, parameterNameTypeMap); } else if (expression instanceof TextIndex) { return getTextIndexMethodDeclaration(methodName, (TextIndex) expression, returnedType, parameterNameTypeMap); } else { throw new IllegalArgumentException(String.format("Expression %s not managed", expression.getClass())); } } private  DefineFunctionUtils();   }
@Test(expected = IllegalArgumentException.class) public void getExpressionMethodDeclarationUnknownExpression() { Expression expression = new Expression() { @Override public VisitorAction accept(Visitor visitor) { return null; } }; DefineFunctionUtils.getExpressionMethodDeclaration("", expression, DataType.STRING, Collections.emptyList()); }
@Test public void getExpressionMethodDeclarationUnsupportedExpression() { for (Supplier<Expression> supplier : unsupportedExpressionSupplier) { Expression expression = supplier.get(); try { DefineFunctionUtils.getExpressionMethodDeclaration("", expression, DataType.STRING, Collections.emptyList()); fail(String.format("Expecting KiePMMLException for %s", expression.getClass())); } catch (Exception e) { assertEquals(KiePMMLException.class, e.getClass()); } } }
@Test public void getExpressionMethodDeclarationSupportedExpression() { for (Supplier<Expression> supplier : supportedExpressionSupplier) { Expression expression = supplier.get(); try { DefineFunctionUtils.getExpressionMethodDeclaration("METHOD_NAME", expression, DataType.STRING, Collections.emptyList()); } catch (Exception e) { fail(String.format("Unexpected %s for %s", e, expression.getClass())); } } }