use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class JavaParserUtils method getKiePMMLModelCompilationUnit.
/**
* @param className
* @param packageName
* @param javaTemplate the name of the <b>file</b> to be used as template source
* @param modelClassName the name of the class used in the provided template
* @return
*/
public static CompilationUnit getKiePMMLModelCompilationUnit(final String className, final String packageName, final String javaTemplate, final String modelClassName) {
logger.trace("getKiePMMLModelCompilationUnit {} {}", className, packageName);
CompilationUnit templateCU = getFromFileName(javaTemplate);
CompilationUnit toReturn = templateCU.clone();
if (packageName != null && !packageName.isEmpty()) {
toReturn.setPackageDeclaration(packageName);
}
ClassOrInterfaceDeclaration modelTemplate = toReturn.getClassByName(modelClassName).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + modelClassName));
modelTemplate.setName(className);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class JavaParserUtils method getFullClassName.
/**
* Return the fully qualified name of the generated class.
* It throws <code>KiePMMLException</code> if the package name is missing
* @param cu
* @return
*/
public static String getFullClassName(final CompilationUnit cu) {
String packageName = cu.getPackageDeclaration().orElseThrow(() -> new KiePMMLException("Missing package declaration for " + cu.toString())).getName().asString();
String className = cu.getType(0).getName().asString();
return packageName + "." + className;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLCompoundPredicateFactory method getCompoundPredicateVariableDeclaration.
static BlockStmt getCompoundPredicateVariableDeclaration(final String variableName, final CompoundPredicate compoundPredicate, final List<Field<?>> fields) {
final MethodDeclaration methodDeclaration = COMPOUND_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLCOMPOUNDPREDICATE).get(0).clone();
final BlockStmt compoundPredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(compoundPredicateBody, COMPOUND_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, COMPOUND_PREDICATE, compoundPredicateBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
for (Predicate predicate : compoundPredicate.getPredicates()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getKiePMMLPredicate(nestedVariableName, predicate, fields);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
final BOOLEAN_OPERATOR booleanOperator = BOOLEAN_OPERATOR.byName(compoundPredicate.getBooleanOperator().value());
final NameExpr booleanOperatorExpr = new NameExpr(BOOLEAN_OPERATOR.class.getName() + "." + booleanOperator.name());
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COMPOUND_PREDICATE, compoundPredicateBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
builder.setArgument(1, booleanOperatorExpr);
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
compoundPredicateBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLOutputFieldFactory method getOutputFieldVariableDeclaration.
static BlockStmt getOutputFieldVariableDeclaration(final String variableName, final OutputField outputField) {
final MethodDeclaration methodDeclaration = OUTPUTFIELD_TEMPLATE.getMethodsByName(GETKIEPMMLOUTPUTFIELD).get(0).clone();
final BlockStmt outputFieldBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(outputFieldBody, OUTPUTFIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, OUTPUTFIELD, outputFieldBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
final Expression expressionExpr;
if (outputField.getExpression() != null) {
String nestedVariableName = String.format("%s_Expression", variableName);
BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, outputField.getExpression());
toAdd.getStatements().forEach(toReturn::addStatement);
expressionExpr = new NameExpr(nestedVariableName);
} else {
expressionExpr = new NullLiteralExpr();
}
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, OUTPUTFIELD, toReturn))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final StringLiteralExpr nameExpr = new StringLiteralExpr(outputField.getName().getValue());
final RESULT_FEATURE resultFeature = RESULT_FEATURE.byName(outputField.getResultFeature().value());
final NameExpr resultFeatureExpr = new NameExpr(RESULT_FEATURE.class.getName() + "." + resultFeature.name());
final Expression targetFieldExpr = outputField.getTargetField() != null ? getExpressionForObject(outputField.getTargetField().getValue()) : new NullLiteralExpr();
final Expression valueExpr = outputField.getValue() != null ? getExpressionForObject(outputField.getValue()) : new NullLiteralExpr();
final Expression dataTypeExpression = getExpressionForDataType(outputField.getDataType());
final Expression rankExpr = outputField.getRank() != null ? getExpressionForObject(outputField.getRank()) : new NullLiteralExpr();
builder.setArgument(0, nameExpr);
getChainedMethodCallExprFrom("withResultFeature", initializer).setArgument(0, resultFeatureExpr);
getChainedMethodCallExprFrom("withTargetField", initializer).setArgument(0, targetFieldExpr);
getChainedMethodCallExprFrom("withValue", initializer).setArgument(0, valueExpr);
getChainedMethodCallExprFrom("withDataType", initializer).setArgument(0, dataTypeExpression);
getChainedMethodCallExprFrom("withRank", initializer).setArgument(0, rankExpr);
getChainedMethodCallExprFrom("withKiePMMLExpression", initializer).setArgument(0, expressionExpr);
outputFieldBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLSimplePredicateFactory method getSimplePredicateVariableDeclaration.
static BlockStmt getSimplePredicateVariableDeclaration(final String variableName, final SimplePredicate simplePredicate, final List<Field<?>> fields) {
final MethodDeclaration methodDeclaration = SIMPLE_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLSIMPLEPREDICATE).get(0).clone();
final BlockStmt simplePredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(simplePredicateBody, SIMPLE_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, SIMPLE_PREDICATE, simplePredicateBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
final OPERATOR operator = OPERATOR.byName(simplePredicate.getOperator().value());
final NameExpr operatorExpr = new NameExpr(OPERATOR.class.getName() + "." + operator.name());
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, SIMPLE_PREDICATE, simplePredicateBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
builder.setArgument(0, new StringLiteralExpr(simplePredicate.getField().getValue()));
builder.setArgument(2, operatorExpr);
DataType dataType = getDataType(fields, simplePredicate.getField().getValue());
Object actualValue = DATA_TYPE.byName(dataType.value()).getActualValue(simplePredicate.getValue());
getChainedMethodCallExprFrom("withValue", initializer).setArgument(0, getExpressionForObject(actualValue));
simplePredicateBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
Aggregations