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));
}
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);
}
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;
}
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);
}
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);
}
Aggregations