use of org.jpmml.rexp.FunctionExpression in project jpmml-r by jpmml.
the class ExpressionTranslatorTest method translateFunctionExpression.
@Test
public void translateFunctionExpression() {
String string = "parent(first = child(A, log(A)), child(1 + B, right = 0), \"third\" = child(left = 0, c(A, B, C)))";
FunctionExpression functionExpression = (FunctionExpression) ExpressionTranslator.translateExpression(string);
checkFunctionExpression(functionExpression, "parent", "first", null, "third");
FunctionExpression.Argument first = functionExpression.getArgument("first");
FunctionExpression.Argument second;
try {
second = functionExpression.getArgument("second");
fail();
} catch (IllegalArgumentException iae) {
second = functionExpression.getArgument(1);
}
FunctionExpression.Argument third = functionExpression.getArgument("third");
assertEquals("first = child(A, log(A))", first.format());
assertEquals("child(A, log(A))", first.formatExpression());
List<Expression> expressions = checkFunctionExpression((FunctionExpression) first.getExpression(), "child", null, null);
assertTrue(ReflectionUtil.equals(new FieldRef("A"), expressions.get(0)));
assertTrue(ReflectionUtil.equals(PMMLUtil.createApply(PMMLFunctions.LN, new FieldRef("A")), expressions.get(1)));
assertEquals("child(1 + B, right = 0)", second.format());
assertEquals("child(1 + B, right = 0)", second.formatExpression());
expressions = checkFunctionExpression((FunctionExpression) second.getExpression(), "child", null, "right");
assertTrue(ReflectionUtil.equals(PMMLUtil.createApply(PMMLFunctions.ADD, PMMLUtil.createConstant("1", DataType.INTEGER), new FieldRef("B")), expressions.get(0)));
assertTrue(ReflectionUtil.equals(PMMLUtil.createConstant("0", DataType.INTEGER), expressions.get(1)));
assertEquals("\"third\" = child(left = 0, c(A, B, C))", third.format());
assertEquals("child(left = 0, c(A, B, C))", third.formatExpression());
expressions = checkFunctionExpression((FunctionExpression) third.getExpression(), "child", "left", null);
assertTrue(ReflectionUtil.equals(PMMLUtil.createConstant("0", DataType.INTEGER), expressions.get(0)));
checkFunctionExpression((FunctionExpression) expressions.get(1), "c", null, null, null);
}
use of org.jpmml.rexp.FunctionExpression in project jpmml-r by jpmml.
the class ExpressionTranslatorTest method checkFunctionExpression.
private static List<Expression> checkFunctionExpression(FunctionExpression functionExpression, String function, String... tags) {
assertEquals(function, functionExpression.getFunction());
List<FunctionExpression.Argument> arguments = functionExpression.getArguments();
assertEquals(tags.length, arguments.size());
List<Expression> expressions = new ArrayList<>();
for (int i = 0; i < arguments.size(); i++) {
FunctionExpression.Argument argument = arguments.get(i);
String tag = argument.getTag();
Expression expression = argument.getExpression();
assertEquals(tag, tags[i]);
expressions.add(expression);
}
return expressions;
}
use of org.jpmml.rexp.FunctionExpression in project jpmml-r by jpmml.
the class FunctionExpressionTest method checkId.
@Test
public void checkId() {
FunctionExpression expression = new FunctionExpression(null, "c", Collections.emptyList());
assertTrue(expression.hasId("base", "c"));
assertFalse(expression.hasId("base", "if"));
expression = new FunctionExpression("base", "c", Collections.emptyList());
assertTrue(expression.hasId("base", "c"));
assertFalse(expression.hasId("base", "if"));
}
Aggregations