use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class ModelImplementationProvider method getKiePMMLModelWithSourcesCompiled.
/**
* Method provided only to have <b>drools</b> models working when invoked by a <code>MiningModel</code>
* Default implementation provided for <b>not-drools</b> models.
* @param compilationDTO
* @return
* @throws KiePMMLInternalException
*/
default KiePMMLModelWithSources getKiePMMLModelWithSourcesCompiled(final CompilationDTO<T> compilationDTO) {
KiePMMLModelWithSources toReturn = getKiePMMLModelWithSources(compilationDTO);
final Map<String, String> sourcesMap = ((HasSourcesMap) toReturn).getSourcesMap();
try {
compilationDTO.compileAndLoadClass(sourcesMap);
} catch (Exception e) {
throw new KiePMMLException(e);
}
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLMiningFieldFactory method getIntervalsExpressions.
private static NodeList<Expression> getIntervalsExpressions(final DataField dataField) {
final NodeList<Expression> toReturn = new NodeList<>();
if (dataField.hasIntervals()) {
dataField.getIntervals().forEach(interval -> {
BlockStmt intervalStmt = getIntervalVariableDeclaration("name", interval);
Expression toAdd = intervalStmt.getStatement(0).asExpressionStmt().getExpression().asVariableDeclarationExpr().getVariable(0).getInitializer().orElseThrow(() -> new KiePMMLInternalException(String.format("Failed to create initializer " + "for " + "Interval %s", interval)));
toReturn.add(toAdd);
});
}
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method init.
/**
* Initialize the given <code>ClassOrInterfaceDeclaration</code> with all the <b>common</b> code needed to
* generate a <code>KiePMMLModel</code>
* @param compilationDTO
* @param modelTemplate
*/
public static void init(final CompilationDTO<? extends Model> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
final String name = compilationDTO.getModelName();
final String generatedClassName = compilationDTO.getSimpleClassName();
final List<MiningField> miningFields = compilationDTO.getKieMiningFields();
final List<OutputField> outputFields = compilationDTO.getKieOutputFields();
final List<TargetField> targetFields = compilationDTO.getKieTargetFields();
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();
}
final PMML_MODEL pmmlModelEnum = compilationDTO.getPMML_MODEL();
final NameExpr pmmlMODELExpression = new NameExpr(pmmlModelEnum.getClass().getName() + "." + pmmlModelEnum.name());
String targetFieldName = compilationDTO.getTargetFieldName();
final Expression targetFieldExpression;
if (targetFieldName != null) {
targetFieldExpression = new StringLiteralExpr(targetFieldName);
} else {
targetFieldExpression = new NullLiteralExpr();
}
setKiePMMLModelConstructor(generatedClassName, constructorDeclaration, name, miningFields, outputFields, targetFields);
addTransformationsInClassOrInterfaceDeclaration(modelTemplate, compilationDTO.getTransformationDictionary(), compilationDTO.getLocalTransformations());
final BlockStmt body = constructorDeclaration.getBody();
CommonCodegenUtils.setAssignExpressionValue(body, "pmmlMODEL", pmmlMODELExpression);
CommonCodegenUtils.setAssignExpressionValue(body, "miningFunction", miningFunctionExpression);
CommonCodegenUtils.setAssignExpressionValue(body, "targetField", targetFieldExpression);
addGetCreatedKiePMMLMiningFieldsMethod(modelTemplate, compilationDTO.getMiningSchema().getMiningFields(), compilationDTO.getFields());
MethodCallExpr getCreatedKiePMMLMiningFieldsExpr = new MethodCallExpr();
getCreatedKiePMMLMiningFieldsExpr.setScope(new ThisExpr());
getCreatedKiePMMLMiningFieldsExpr.setName(GET_CREATED_KIEPMMLMININGFIELDS);
CommonCodegenUtils.setAssignExpressionValue(body, "kiePMMLMiningFields", getCreatedKiePMMLMiningFieldsExpr);
if (compilationDTO.getOutput() != null) {
addGetCreatedKiePMMLOutputFieldsMethod(modelTemplate, compilationDTO.getOutput().getOutputFields());
MethodCallExpr getCreatedKiePMMLOutputFieldsExpr = new MethodCallExpr();
getCreatedKiePMMLOutputFieldsExpr.setScope(new ThisExpr());
getCreatedKiePMMLOutputFieldsExpr.setName(GET_CREATED_KIEPMMLOUTPUTFIELDS);
CommonCodegenUtils.setAssignExpressionValue(body, "kiePMMLOutputFields", getCreatedKiePMMLOutputFieldsExpr);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class CommonCodegenUtils method addMethod.
/**
* Add a <code>MethodDeclaration</code> to the class
* @param methodTemplate
* @param tableTemplate
* @param methodName
* @return
*/
public static MethodDeclaration addMethod(final MethodDeclaration methodTemplate, final ClassOrInterfaceDeclaration tableTemplate, final String methodName) {
final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName())));
final MethodDeclaration toReturn = tableTemplate.addMethod(methodName).setBody(body);
toReturn.setModifiers(methodTemplate.getModifiers());
methodTemplate.getParameters().forEach(toReturn::addParameter);
toReturn.setType(methodTemplate.getType());
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class CommonCodegenUtils method populateListInListGetter.
/**
* Method to be used to populate a <code>List</code> inside a getter method meant to return only that <code>List</code>
* @param toAdd
* @param methodDeclaration
* @param listName
*/
public static void populateListInListGetter(final List<? extends Expression> toAdd, final MethodDeclaration methodDeclaration, final String listName) {
final BlockStmt body = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_IN_METHOD, methodDeclaration)));
Optional<ReturnStmt> oldReturn = body.getStatements().parallelStream().filter(statement -> statement instanceof ReturnStmt).map(ReturnStmt.class::cast).findFirst();
oldReturn.ifPresent(Node::remove);
toAdd.forEach(expression -> {
NodeList<Expression> arguments = NodeList.nodeList(expression);
MethodCallExpr methodCallExpr = new MethodCallExpr();
methodCallExpr.setScope(new NameExpr(listName));
methodCallExpr.setName("add");
methodCallExpr.setArguments(arguments);
ExpressionStmt expressionStmt = new ExpressionStmt();
expressionStmt.setExpression(methodCallExpr);
body.addStatement(expressionStmt);
});
body.addStatement(getReturnStmt(listName));
}
Aggregations