use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method initStaticGetter.
/**
* Populate the given <code>ClassOrInterfaceDeclaration</code>' <b>staticGetter</b> with the <b>common</b>
* parameters needed to
* instantiate a <code>KiePMMLModel</code>
* @param compilationDTO
* @param modelTemplate
* @return
*/
public static void initStaticGetter(final CompilationDTO<? extends Model> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
final MethodDeclaration staticGetterMethod = modelTemplate.getMethodsByName(GET_MODEL).get(0);
final BlockStmt staticGetterBody = staticGetterMethod.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, staticGetterMethod)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(staticGetterBody, TO_RETURN).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TO_RETURN, staticGetterBody)));
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TO_RETURN, staticGetterBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final String name = compilationDTO.getModelName();
final Expression miningFunctionExpression;
if (compilationDTO.getMINING_FUNCTION() != null) {
MINING_FUNCTION miningFunction = compilationDTO.getMINING_FUNCTION();
miningFunctionExpression = new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name());
} else {
miningFunctionExpression = new NullLiteralExpr();
}
builder.setArgument(0, new StringLiteralExpr(name));
builder.setArgument(1, miningFunctionExpression);
String targetFieldName = compilationDTO.getTargetFieldName();
final Expression targetFieldExpression;
if (targetFieldName != null) {
targetFieldExpression = new StringLiteralExpr(targetFieldName);
} else {
targetFieldExpression = new NullLiteralExpr();
}
getChainedMethodCallExprFrom("withTargetField", initializer).setArgument(0, targetFieldExpression);
//
populateGetCreatedMiningFieldsMethod(modelTemplate, compilationDTO.getKieMiningFields());
populateGetCreatedOutputFieldsMethod(modelTemplate, compilationDTO.getKieOutputFields());
populateGetCreatedKiePMMLMiningFieldsMethod(modelTemplate, compilationDTO.getMiningSchema().getMiningFields(), compilationDTO.getFields());
if (compilationDTO.getOutput() != null) {
populateGetCreatedKiePMMLOutputFieldsMethod(modelTemplate, compilationDTO.getOutput().getOutputFields());
}
if (compilationDTO.getKieTargetFields() != null) {
populateGetCreatedKiePMMLTargetsMethod(modelTemplate, compilationDTO.getKieTargetFields());
}
populateGetCreatedTransformationDictionaryMethod(modelTemplate, compilationDTO.getTransformationDictionary());
populateGetCreatedLocalTransformationsMethod(modelTemplate, compilationDTO.getLocalTransformations());
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLDerivedFieldFactory method getDerivedFieldVariableDeclaration.
static BlockStmt getDerivedFieldVariableDeclaration(final String variableName, final DerivedField derivedField) {
final MethodDeclaration methodDeclaration = DERIVED_FIELD_TEMPLATE.getMethodsByName(GETKIEPMMLDERIVEDFIELD).get(0).clone();
final BlockStmt derivedFieldBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(derivedFieldBody, DERIVED_FIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, DERIVED_FIELD, derivedFieldBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, 0);
BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, derivedField.getExpression());
toAdd.getStatements().forEach(toReturn::addStatement);
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, DERIVED_FIELD, derivedFieldBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final Expression dataTypeExpression = getExpressionForDataType(derivedField.getDataType());
final Expression opTypeExpression = getExpressionForOpType(derivedField.getOpType());
builder.setArgument(0, new StringLiteralExpr(derivedField.getName().getValue()));
builder.setArgument(2, dataTypeExpression);
builder.setArgument(3, opTypeExpression);
builder.setArgument(4, new NameExpr(nestedVariableName));
getChainedMethodCallExprFrom("withDisplayName", initializer).setArgument(0, getExpressionForObject(derivedField.getDisplayName()));
derivedFieldBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLFieldRefFactory method getFieldRefVariableDeclaration.
static BlockStmt getFieldRefVariableDeclaration(final String variableName, final FieldRef fieldRef) {
final MethodDeclaration methodDeclaration = FIELDREF_TEMPLATE.getMethodsByName(GETKIEPMMLFIELDREF).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, FIELD_REF).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, FIELD_REF, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, FIELD_REF, toReturn))).asObjectCreationExpr();
final StringLiteralExpr nameExpr = new StringLiteralExpr(fieldRef.getField().getValue());
final Expression mapMissingToExpr = getExpressionForObject(fieldRef.getMapMissingTo());
objectCreationExpr.getArguments().set(0, nameExpr);
objectCreationExpr.getArguments().set(2, mapMissingToExpr);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLIntervalFactory method getIntervalVariableDeclaration.
static BlockStmt getIntervalVariableDeclaration(final String variableName, final Interval interval) {
final MethodDeclaration methodDeclaration = INTERVAL_TEMPLATE.getMethodsByName(GETKIEPMMLINTERVAL).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, INTERVAL).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, INTERVAL, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, INTERVAL, toReturn))).asObjectCreationExpr();
final Expression leftMarginExpr = getExpressionForObject(interval.getLeftMargin());
final Expression rightMarginExpr = getExpressionForObject(interval.getRightMargin());
final CLOSURE closure = CLOSURE.byName(interval.getClosure().value());
final NameExpr closureExpr = new NameExpr(CLOSURE.class.getName() + "." + closure.name());
objectCreationExpr.getArguments().set(0, leftMarginExpr);
objectCreationExpr.getArguments().set(1, rightMarginExpr);
objectCreationExpr.getArguments().set(2, closureExpr);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLMapValuesFactory method getMapValuesVariableDeclaration.
static BlockStmt getMapValuesVariableDeclaration(final String variableName, final MapValues mapValues) {
if (mapValues.getInlineTable() == null && mapValues.getTableLocator() != null) {
throw new UnsupportedOperationException("TableLocator not supported, yet");
}
final MethodDeclaration methodDeclaration = MAPVALUES_TEMPLATE.getMethodsByName(GETKIEPMMLMAPVALUES).get(0).clone();
final BlockStmt mapValuesBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(mapValuesBody, MAPVALUES).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, MAPVALUES, mapValuesBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
if (mapValues.hasFieldColumnPairs()) {
for (FieldColumnPair fieldColumnPair : mapValues.getFieldColumnPairs()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getFieldColumnPairVariableDeclaration(nestedVariableName, fieldColumnPair);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
}
String inlineTableVariableName = String.format("%s_InlineTable", variableName);
BlockStmt toAdd = getInlineTableVariableDeclaration(inlineTableVariableName, mapValues.getInlineTable());
toAdd.getStatements().forEach(toReturn::addStatement);
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, MAPVALUES, toReturn))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
final StringLiteralExpr outputColumnExpr = new StringLiteralExpr(mapValues.getOutputColumn());
builder.setArgument(0, nameExpr);
builder.setArgument(2, outputColumnExpr);
final Expression dataTypeExpression = getExpressionForDataType(mapValues.getDataType());
getChainedMethodCallExprFrom("withDefaultValue", initializer).setArgument(0, getExpressionForObject(mapValues.getDefaultValue()));
getChainedMethodCallExprFrom("withMapMissingTo", initializer).setArgument(0, getExpressionForObject(mapValues.getMapMissingTo()));
getChainedMethodCallExprFrom("withDataType", initializer).setArgument(0, dataTypeExpression);
getChainedMethodCallExprFrom("withKiePMMLInlineTable", initializer).setArgument(0, new NameExpr(inlineTableVariableName));
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
mapValuesBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
Aggregations