Search in sources :

Example 1 with FunctionExpression

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);
}
Also used : FunctionExpression(org.jpmml.rexp.FunctionExpression) FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) FunctionExpression(org.jpmml.rexp.FunctionExpression) Test(org.junit.Test)

Example 2 with FunctionExpression

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;
}
Also used : FunctionExpression(org.jpmml.rexp.FunctionExpression) Expression(org.dmg.pmml.Expression) FunctionExpression(org.jpmml.rexp.FunctionExpression) ArrayList(java.util.ArrayList)

Example 3 with FunctionExpression

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"));
}
Also used : FunctionExpression(org.jpmml.rexp.FunctionExpression) Test(org.junit.Test)

Aggregations

FunctionExpression (org.jpmml.rexp.FunctionExpression)3 Expression (org.dmg.pmml.Expression)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 FieldRef (org.dmg.pmml.FieldRef)1