use of org.dmg.pmml.Expression in project jpmml-r by jpmml.
the class FormulaUtil method createFormula.
public static Formula createFormula(RExp terms, FormulaContext context, RExpEncoder encoder) {
Formula formula = new Formula(encoder);
RIntegerVector factors = (RIntegerVector) terms.getAttributeValue("factors");
RStringVector dataClasses = (RStringVector) terms.getAttributeValue("dataClasses");
RStringVector variableRows = factors.dimnames(0);
RStringVector termColumns = factors.dimnames(1);
VariableMap expressionFields = new VariableMap();
for (int i = 0; i < variableRows.size(); i++) {
String variable = variableRows.getDequotedValue(i);
FieldName name = FieldName.create(variable);
OpType opType = OpType.CONTINUOUS;
DataType dataType = RExpUtil.getDataType(dataClasses.getValue(variable));
List<String> categories = context.getCategories(variable);
if (categories != null && categories.size() > 0) {
opType = OpType.CATEGORICAL;
}
Expression expression = null;
FieldName shortName = name;
expression: if (variable.indexOf('(') > -1 && variable.indexOf(')') > -1) {
FunctionExpression functionExpression;
try {
functionExpression = (FunctionExpression) ExpressionTranslator.translateExpression(variable);
} catch (Exception e) {
break expression;
}
if (functionExpression.hasId("base", "cut")) {
expression = encodeCutExpression(functionExpression, categories, expressionFields, encoder);
} else if (functionExpression.hasId("base", "I")) {
expression = encodeIdentityExpression(functionExpression, expressionFields, encoder);
} else if (functionExpression.hasId("base", "ifelse")) {
expression = encodeIfElseExpression(functionExpression, expressionFields, encoder);
} else if (functionExpression.hasId("plyr", "mapvalues")) {
expression = encodeMapValuesExpression(functionExpression, categories, expressionFields, encoder);
} else if (functionExpression.hasId("plyr", "revalue")) {
expression = encodeReValueExpression(functionExpression, categories, expressionFields, encoder);
} else {
break expression;
}
FunctionExpression.Argument xArgument = functionExpression.getArgument("x", 0);
String value = (xArgument.formatExpression()).trim();
shortName = FieldName.create(functionExpression.hasId("base", "I") ? value : (functionExpression.getFunction() + "(" + value + ")"));
}
if (expression != null) {
DerivedField derivedField = encoder.createDerivedField(name, opType, dataType, expression).addExtensions(createExtension(variable));
if (categories != null && categories.size() > 0) {
formula.addField(derivedField, categories);
} else {
formula.addField(derivedField);
}
if (!(name).equals(shortName)) {
encoder.renameField(name, shortName);
}
} else {
if ((DataType.BOOLEAN).equals(dataType)) {
categories = Arrays.asList("false", "true");
}
if (categories != null && categories.size() > 0) {
DataField dataField = encoder.createDataField(name, OpType.CATEGORICAL, dataType, categories);
List<String> categoryNames;
List<String> categoryValues;
switch(dataType) {
case BOOLEAN:
categoryNames = Arrays.asList("FALSE", "TRUE");
categoryValues = Arrays.asList("false", "true");
break;
default:
categoryNames = categories;
categoryValues = categories;
break;
}
formula.addField(dataField, categoryNames, categoryValues);
} else {
DataField dataField = encoder.createDataField(name, OpType.CONTINUOUS, dataType);
formula.addField(dataField);
}
}
}
Collection<Map.Entry<FieldName, List<String>>> entries = expressionFields.entrySet();
for (Map.Entry<FieldName, List<String>> entry : entries) {
FieldName name = entry.getKey();
List<String> categories = entry.getValue();
DataField dataField = encoder.getDataField(name);
if (dataField == null) {
OpType opType = OpType.CONTINUOUS;
DataType dataType = DataType.DOUBLE;
if (categories != null && categories.size() > 0) {
opType = OpType.CATEGORICAL;
}
RGenericVector data = context.getData();
if (data != null && data.hasValue(name.getValue())) {
RVector<?> column = (RVector<?>) data.getValue(name.getValue());
dataType = column.getDataType();
}
dataField = encoder.createDataField(name, opType, dataType, categories);
}
}
return formula;
}
use of org.dmg.pmml.Expression in project jpmml-r by jpmml.
the class ExpressionCompactor method negateExpression.
private static void negateExpression(Apply apply) {
List<Expression> expressions = apply.getExpressions();
if (expressions.size() != 1) {
throw new IllegalArgumentException();
}
ListIterator<Expression> expressionIt = expressions.listIterator();
Expression expression = expressionIt.next();
if (expression instanceof Apply) {
Apply nestedApply = (Apply) expression;
String negatedFunction = negate(nestedApply.getFunction());
if (negatedFunction != null) {
apply.setFunction(negatedFunction);
expressionIt.remove();
List<Expression> nestedExpressions = nestedApply.getExpressions();
for (Expression nestedExpression : nestedExpressions) {
expressionIt.add(nestedExpression);
}
}
}
}
use of org.dmg.pmml.Expression in project jpmml-r by jpmml.
the class ExpressionCompactor method inlineLogicalExpressions.
private static void inlineLogicalExpressions(Apply apply) {
String function = apply.getFunction();
List<Expression> expressions = apply.getExpressions();
for (ListIterator<Expression> expressionIt = expressions.listIterator(); expressionIt.hasNext(); ) {
Expression expression = expressionIt.next();
if (expression instanceof Apply) {
Apply nestedApply = (Apply) expression;
if ((function).equals(nestedApply.getFunction())) {
expressionIt.remove();
// Deep inline
inlineLogicalExpressions(nestedApply);
List<Expression> nestedExpressions = nestedApply.getExpressions();
for (Expression nestedExpression : nestedExpressions) {
expressionIt.add(nestedExpression);
}
}
}
}
}
use of org.dmg.pmml.Expression 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.getExpression();
if (expression instanceof Apply) {
Apply apply = (Apply) expression;
if (checkApply(apply, "pow", FieldRef.class, Constant.class)) {
List<Expression> expressions = apply.getExpressions();
FieldRef fieldRef = (FieldRef) expressions.get(0);
Constant constant = (Constant) expressions.get(1);
try {
int power = Integer.parseInt(constant.getValue());
feature = new PowerFeature(encoder, fieldRef.getField(), DataType.DOUBLE, power);
} catch (NumberFormatException nfe) {
// Ignored
}
}
}
}
putFeature(field.getName(), feature);
this.fields.add(field);
}
use of org.dmg.pmml.Expression in project jpmml-r by jpmml.
the class Formula method checkApply.
private static boolean checkApply(Apply apply, String function, Class<? extends Expression>... expressionClazzes) {
if ((function).equals(apply.getFunction())) {
List<Expression> expressions = apply.getExpressions();
if (expressionClazzes.length == expressions.size()) {
for (int i = 0; i < expressionClazzes.length; i++) {
Class<? extends Expression> expressionClazz = expressionClazzes[i];
Expression expression = expressions.get(i);
if (!(expressionClazz).isInstance(expression)) {
return false;
}
}
return true;
}
}
return false;
}
Aggregations