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