Search in sources :

Example 31 with FieldRef

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

the class ExpressionTranslatorTest method translateArithmeticExpressionChain.

@Test
public void translateArithmeticExpressionChain() {
    String string = "A + B - X + C";
    Expression expected = PMMLUtil.createApply(PMMLFunctions.ADD, PMMLUtil.createApply(PMMLFunctions.SUBTRACT, PMMLUtil.createApply(PMMLFunctions.ADD, new FieldRef("A"), new FieldRef("B")), new FieldRef("X")), new FieldRef("C"));
    Expression actual = ExpressionTranslator.translateExpression(string);
    assertTrue(ReflectionUtil.equals(expected, actual));
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) FunctionExpression(org.jpmml.rexp.FunctionExpression) Test(org.junit.Test)

Example 32 with FieldRef

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

the class Formula method addField.

public void addField(Field<?> field) {
    RExpEncoder encoder = getEncoder();
    Feature feature = new ContinuousFeature(encoder, field);
    if (field instanceof DerivedField) {
        DerivedField derivedField = (DerivedField) field;
        Expression expression = derivedField.requireExpression();
        if (expression instanceof Apply) {
            Apply apply = (Apply) expression;
            if (checkApply(apply, PMMLFunctions.POW, FieldRef.class, Constant.class)) {
                List<Expression> expressions = apply.getExpressions();
                FieldRef fieldRef = (FieldRef) expressions.get(0);
                Constant constant = (Constant) expressions.get(1);
                try {
                    String string = ValueUtil.asString(constant.getValue());
                    int power = Integer.parseInt(string);
                    feature = new PowerFeature(encoder, fieldRef.requireField(), DataType.DOUBLE, power);
                } catch (NumberFormatException nfe) {
                // Ignored
                }
            }
        }
    }
    putFeature(field.requireName(), feature);
    this.fields.add(field);
}
Also used : PowerFeature(org.jpmml.converter.PowerFeature) FieldRef(org.dmg.pmml.FieldRef) Apply(org.dmg.pmml.Apply) Constant(org.dmg.pmml.Constant) Feature(org.jpmml.converter.Feature) PowerFeature(org.jpmml.converter.PowerFeature) BinaryFeature(org.jpmml.converter.BinaryFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) BooleanFeature(org.jpmml.converter.BooleanFeature) InteractionFeature(org.jpmml.converter.InteractionFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Expression(org.dmg.pmml.Expression) DerivedField(org.dmg.pmml.DerivedField)

Example 33 with FieldRef

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

the class AliasExpressionTest method unwrap.

@Test
public void unwrap() {
    FieldRef fieldRef = new FieldRef(FieldName.create("x"));
    Expression expression = new AliasExpression("parent", new AliasExpression("child", fieldRef));
    checkExpression(fieldRef, expression);
    expression = new AliasExpression("parent", PMMLUtil.createApply(PMMLFunctions.ADD, new AliasExpression("left child", fieldRef), new AliasExpression("right child", fieldRef)));
    checkExpression(PMMLUtil.createApply(PMMLFunctions.ADD, fieldRef, fieldRef), expression);
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) Test(org.junit.Test)

Example 34 with FieldRef

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

the class ExpressionTranslatorTest method translateLogicalExpression.

@Test
public void translateLogicalExpression() {
    String string = "isnull(x1) and not(isnotnull(x2))";
    FieldRef first = new FieldRef(FieldName.create("x1"));
    FieldRef second = new FieldRef(FieldName.create("x2"));
    Apply expected = PMMLUtil.createApply(PMMLFunctions.AND, PMMLUtil.createApply(PMMLFunctions.ISMISSING, first), // "not(isnotnull(..)) -> "isnull(..)"
    PMMLUtil.createApply(PMMLFunctions.ISMISSING, second));
    checkExpression(expected, string);
    string = "(x1 <= 0) or (x2 >= 0)";
    expected = PMMLUtil.createApply(PMMLFunctions.OR, PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL, first, PMMLUtil.createConstant(0, DataType.DOUBLE)), PMMLUtil.createApply(PMMLFunctions.GREATEROREQUAL, second, PMMLUtil.createConstant(0, DataType.DOUBLE)));
    checkExpression(expected, string);
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 35 with FieldRef

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

the class ClusteringModelConverter method registerOutputFields.

@Override
public List<OutputField> registerOutputFields(Label label, org.dmg.pmml.Model pmmlModel, SparkMLEncoder encoder) {
    T model = getTransformer();
    List<Integer> clusters = LabelUtil.createTargetCategories(getNumberOfClusters());
    String predictionCol = model.getPredictionCol();
    OutputField pmmlPredictedOutputField = ModelUtil.createPredictedField(FieldNameUtil.create("pmml", predictionCol), OpType.CATEGORICAL, DataType.STRING).setFinalResult(false);
    DerivedOutputField pmmlPredictedField = encoder.createDerivedField(pmmlModel, pmmlPredictedOutputField, true);
    OutputField predictedOutputField = new OutputField(FieldName.create(predictionCol), OpType.CATEGORICAL, DataType.INTEGER).setResultFeature(ResultFeature.TRANSFORMED_VALUE).setExpression(new FieldRef(pmmlPredictedField.getName()));
    DerivedOutputField predictedField = encoder.createDerivedField(pmmlModel, predictedOutputField, true);
    encoder.putOnlyFeature(predictionCol, new IndexFeature(encoder, predictedField, clusters));
    return Collections.emptyList();
}
Also used : IndexFeature(org.jpmml.converter.IndexFeature) FieldRef(org.dmg.pmml.FieldRef) DerivedOutputField(org.jpmml.converter.DerivedOutputField) OutputField(org.dmg.pmml.OutputField) DerivedOutputField(org.jpmml.converter.DerivedOutputField)

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