Search in sources :

Example 11 with FieldRef

use of org.dmg.pmml.FieldRef in project jpmml-r by jpmml.

the class ExpressionTranslatorTest method translateLogicalExpressionChain.

@Test
public void translateLogicalExpressionChain() {
    String string = "(x == 0) | ((x == 1) | (x == 2)) | x == 3";
    Apply left = PMMLUtil.createApply(PMMLFunctions.EQUAL, new FieldRef("x"), PMMLUtil.createConstant("0", DataType.INTEGER));
    Apply middleLeft = PMMLUtil.createApply(PMMLFunctions.EQUAL, new FieldRef("x"), PMMLUtil.createConstant("1", DataType.INTEGER));
    Apply middleRight = PMMLUtil.createApply(PMMLFunctions.EQUAL, new FieldRef("x"), PMMLUtil.createConstant("2", DataType.INTEGER));
    Apply right = PMMLUtil.createApply(PMMLFunctions.EQUAL, new FieldRef("x"), PMMLUtil.createConstant("3", DataType.INTEGER));
    Expression expected = PMMLUtil.createApply(PMMLFunctions.OR, PMMLUtil.createApply(PMMLFunctions.OR, left, PMMLUtil.createApply(PMMLFunctions.OR, middleLeft, middleRight)), right);
    Expression actual = ExpressionTranslator.translateExpression(string, false);
    assertTrue(ReflectionUtil.equals(expected, actual));
    expected = PMMLUtil.createApply(PMMLFunctions.OR, left, middleLeft, middleRight, right);
    actual = ExpressionTranslator.translateExpression(string, true);
    assertTrue(ReflectionUtil.equals(expected, actual));
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) FunctionExpression(org.jpmml.rexp.FunctionExpression) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 12 with FieldRef

use of org.dmg.pmml.FieldRef 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 13 with FieldRef

use of org.dmg.pmml.FieldRef in project jpmml-r by jpmml.

the class PreProcessEncoder method encodeExpression.

private Expression encodeExpression(String name, Expression expression) {
    List<Double> ranges = this.ranges.get(name);
    if (ranges != null) {
        Double min = ranges.get(0);
        Double max = ranges.get(1);
        if (!ValueUtil.isZero(min)) {
            expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(min));
        }
        if (!ValueUtil.isOne(max - min)) {
            expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(max - min));
        }
    }
    Double mean = this.mean.get(name);
    if (mean != null && !ValueUtil.isZero(mean)) {
        expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(mean));
    }
    Double std = this.std.get(name);
    if (std != null && !ValueUtil.isOne(std)) {
        expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(std));
    }
    Double median = this.median.get(name);
    if (median != null) {
        expression = PMMLUtil.createApply(PMMLFunctions.IF, PMMLUtil.createApply(PMMLFunctions.ISNOTMISSING, new FieldRef(name)), expression, PMMLUtil.createConstant(median));
    }
    return expression;
}
Also used : FieldRef(org.dmg.pmml.FieldRef)

Example 14 with FieldRef

use of org.dmg.pmml.FieldRef in project jpmml-sparkml by jpmml.

the class ExpressionTranslatorTest method translateCaseWhenExpression.

@Test
public void translateCaseWhenExpression() {
    String string = "CASE WHEN x1 < 0 THEN x1 WHEN x2 > 0 THEN x2 ELSE 0 END";
    FieldRef first = new FieldRef(FieldName.create("x1"));
    FieldRef second = new FieldRef(FieldName.create("x2"));
    Constant zero = PMMLUtil.createConstant(0, DataType.DOUBLE);
    Apply expected = PMMLUtil.createApply(PMMLFunctions.IF, PMMLUtil.createApply(PMMLFunctions.LESSTHAN, first, zero), first, PMMLUtil.createApply(PMMLFunctions.IF, PMMLUtil.createApply(PMMLFunctions.GREATERTHAN, second, zero), second, zero));
    checkExpression(expected, string);
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Constant(org.dmg.pmml.Constant) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 15 with FieldRef

use of org.dmg.pmml.FieldRef in project jpmml-sparkml by jpmml.

the class ExpressionTranslatorTest method translateIfExpression.

@Test
public void translateIfExpression() {
    String string = "if(status in (-1, 1), x1 != 0, x2 != 0)";
    Apply expected = PMMLUtil.createApply(PMMLFunctions.IF, PMMLUtil.createApply(PMMLFunctions.ISIN, new FieldRef(FieldName.create("status")), PMMLUtil.createConstant(-1), PMMLUtil.createConstant(1)), PMMLUtil.createApply(PMMLFunctions.NOTEQUAL, new FieldRef(FieldName.create("x1")), PMMLUtil.createConstant(0, DataType.DOUBLE)), PMMLUtil.createApply(PMMLFunctions.NOTEQUAL, new FieldRef(FieldName.create("x2")), PMMLUtil.createConstant(0, DataType.DOUBLE)));
    checkExpression(expected, string);
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Aggregations

FieldRef (org.dmg.pmml.FieldRef)40 Test (org.junit.Test)23 Apply (org.dmg.pmml.Apply)17 Expression (org.dmg.pmml.Expression)11 KiePMMLFieldRef (org.kie.pmml.commons.model.expressions.KiePMMLFieldRef)10 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)8 Statement (com.github.javaparser.ast.stmt.Statement)8 Constant (org.dmg.pmml.Constant)7 DerivedField (org.dmg.pmml.DerivedField)7 FunctionExpression (org.jpmml.rexp.FunctionExpression)7 ArrayList (java.util.ArrayList)6 FieldName (org.dmg.pmml.FieldName)6 KiePMMLApply (org.kie.pmml.commons.model.expressions.KiePMMLApply)6 DefineFunction (org.dmg.pmml.DefineFunction)5 ParameterField (org.dmg.pmml.ParameterField)5 KiePMMLConstant (org.kie.pmml.commons.model.expressions.KiePMMLConstant)4 List (java.util.List)3 DataType (org.dmg.pmml.DataType)3 OpType (org.dmg.pmml.OpType)3 Transformation (org.jpmml.converter.Transformation)3