Search in sources :

Example 16 with FieldRef

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

the class ExpressionTranslatorTest method translateArithmeticExpression.

@Test
public void translateArithmeticExpression() {
    String string = "-((x1 - 1) / (x2 + 1))";
    Apply expected = PMMLUtil.createApply(PMMLFunctions.MULTIPLY, PMMLUtil.createConstant(-1), PMMLUtil.createApply(PMMLFunctions.DIVIDE, PMMLUtil.createApply(PMMLFunctions.SUBTRACT, new FieldRef(FieldName.create("x1")), PMMLUtil.createConstant(1, DataType.DOUBLE)), PMMLUtil.createApply(PMMLFunctions.ADD, new FieldRef(FieldName.create("x2")), PMMLUtil.createConstant(1, DataType.DOUBLE))));
    checkExpression(expected, string);
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 17 with FieldRef

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

the class ExpressionCompactorTest method compactLogicalExpression.

@Test
public void compactLogicalExpression() {
    FieldRef fieldRef = new FieldRef(FieldName.create("x"));
    Apply first = createApply("equal", fieldRef, createConstant("1"));
    Apply leftLeftChild = createApply("equal", fieldRef, createConstant("2/L/L"));
    Apply leftRightChild = createApply("equal", fieldRef, createConstant("2/L/R"));
    Apply leftChild = createApply("or", leftLeftChild, leftRightChild);
    Apply rightChild = createApply("equal", fieldRef, createConstant("2/R"));
    Apply second = createApply("or", leftChild, rightChild);
    Apply third = createApply("equal", fieldRef, createConstant("3"));
    Apply apply = compact(createApply("or", first, second, third));
    assertEquals(Arrays.asList(first, leftLeftChild, leftRightChild, rightChild, third), apply.getExpressions());
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 18 with FieldRef

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

the class PreProcessEncoder method encodeExpression.

private Expression encodeExpression(Feature feature) {
    FieldName name = feature.getName();
    Expression expression = feature.ref();
    List<Double> ranges = this.ranges.get(name);
    if (ranges != null) {
        Double min = ranges.get(0);
        Double max = ranges.get(1);
        expression = PMMLUtil.createApply("/", PMMLUtil.createApply("-", expression, PMMLUtil.createConstant(min)), PMMLUtil.createConstant(max - min));
    }
    Double mean = this.mean.get(name);
    if (mean != null) {
        expression = PMMLUtil.createApply("-", expression, PMMLUtil.createConstant(mean));
    }
    Double std = this.std.get(name);
    if (std != null) {
        expression = PMMLUtil.createApply("/", expression, PMMLUtil.createConstant(std));
    }
    Double median = this.median.get(name);
    if (median != null) {
        expression = PMMLUtil.createApply("if", PMMLUtil.createApply("isNotMissing", new FieldRef(name)), expression, PMMLUtil.createConstant(median));
    }
    if (expression instanceof FieldRef) {
        return null;
    }
    return expression;
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) FieldName(org.dmg.pmml.FieldName)

Example 19 with FieldRef

use of org.dmg.pmml.FieldRef in project drools by kiegroup.

the class KiePMMLApplyFactoryTest method getApplyVariableDeclarationWithApply.

@Test
public void getApplyVariableDeclarationWithApply() throws IOException {
    String variableName = "variableName";
    Apply nestedApply = new Apply();
    nestedApply.setFunction("nested_function");
    String mapMissingTo = "mapMissingTo";
    nestedApply.setMapMissingTo(mapMissingTo);
    String defaultValue = "defaultValue";
    nestedApply.setDefaultValue(defaultValue);
    InvalidValueTreatmentMethod nestedInvalidValueTreatmentMethod = InvalidValueTreatmentMethod.AS_MISSING;
    nestedApply.setInvalidValueTreatment(nestedInvalidValueTreatmentMethod);
    FieldRef fieldRef1 = new FieldRef();
    fieldRef1.setField(FieldName.create(PARAM_1));
    FieldRef fieldRef2 = new FieldRef();
    fieldRef2.setField(FieldName.create(PARAM_2));
    nestedApply.addExpressions(fieldRef1, fieldRef2);
    Apply apply = new Apply();
    apply.setFunction(function);
    InvalidValueTreatmentMethod invalidValueTreatmentMethod = InvalidValueTreatmentMethod.AS_MISSING;
    apply.setInvalidValueTreatment(invalidValueTreatmentMethod);
    apply.addExpressions(nestedApply);
    BlockStmt retrieved = KiePMMLApplyFactory.getApplyVariableDeclaration(variableName, apply);
    String text = getFileContent(TEST_03_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, PARAM_1, PARAM_2, defaultValue, mapMissingTo, nestedInvalidValueTreatmentMethod.value(), variableName, invalidValueTreatmentMethod.value()));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(KiePMMLFieldRef.class, KiePMMLApply.class, Collections.class, Arrays.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : FieldRef(org.dmg.pmml.FieldRef) KiePMMLFieldRef(org.kie.pmml.commons.model.expressions.KiePMMLFieldRef) KiePMMLApply(org.kie.pmml.commons.model.expressions.KiePMMLApply) Apply(org.dmg.pmml.Apply) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) InvalidValueTreatmentMethod(org.dmg.pmml.InvalidValueTreatmentMethod) Test(org.junit.Test)

Example 20 with FieldRef

use of org.dmg.pmml.FieldRef in project drools by kiegroup.

the class KiePMMLDerivedFieldFactoryTest method getDerivedFieldVariableDeclarationWithFieldRef.

@Test
public void getDerivedFieldVariableDeclarationWithFieldRef() throws IOException {
    final String variableName = "variableName";
    FieldRef fieldRef = new FieldRef();
    fieldRef.setField(FieldName.create("FIELD_REF"));
    DerivedField derivedField = new DerivedField();
    derivedField.setName(FieldName.create(PARAM_1));
    derivedField.setDataType(DataType.DOUBLE);
    derivedField.setOpType(OpType.CONTINUOUS);
    derivedField.setExpression(fieldRef);
    String dataType = getDATA_TYPEString(derivedField.getDataType());
    String opType = getOP_TYPEString(derivedField.getOpType());
    BlockStmt retrieved = KiePMMLDerivedFieldFactory.getDerivedFieldVariableDeclaration(variableName, derivedField);
    String text = getFileContent(TEST_02_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, fieldRef.getField().getValue(), variableName, derivedField.getName().getValue(), dataType, opType));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(KiePMMLFieldRef.class, KiePMMLDerivedField.class, Collections.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : FieldRef(org.dmg.pmml.FieldRef) KiePMMLFieldRef(org.kie.pmml.commons.model.expressions.KiePMMLFieldRef) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) CommonTestingUtils.getOP_TYPEString(org.kie.pmml.compiler.api.CommonTestingUtils.getOP_TYPEString) CommonTestingUtils.getDATA_TYPEString(org.kie.pmml.compiler.api.CommonTestingUtils.getDATA_TYPEString) DerivedField(org.dmg.pmml.DerivedField) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) 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