use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class TestingModelImplementationProvider method getKiePMMLTestModelSourcesMap.
private Map<String, String> getKiePMMLTestModelSourcesMap(final CompilationDTO<TestModel> compilationDTO) {
String className = compilationDTO.getSimpleClassName();
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, compilationDTO.getPackageName(), KIE_PMML_TEST_MODEL_TEMPLATE_JAVA, KIE_PMML_TEST_MODEL_TEMPLATE);
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
String modelName = compilationDTO.getModelName();
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
setConstructor(className, constructorDeclaration, modelName);
Map<String, String> toReturn = new HashMap<>();
toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLNodeFactory method populateEvaluateNode.
/**
* Populate <b>nodeFunctions, score, predicateFunction</b> <code>VariableDeclarator</code>s initializers of the given <code>BlockStmt</code>
*
* @param toPopulate
* @param nodeNamesDTO
* @param fields
* @param isRoot
*/
static void populateEvaluateNode(final JavaParserDTO toPopulate, final NodeNamesDTO nodeNamesDTO, final List<Field<?>> fields, final boolean isRoot) {
String nodeClassName = nodeNamesDTO.nodeClassName;
final BlockStmt evaluateNodeBody = isRoot ? toPopulate.evaluateRootNodeBody : toPopulate.getEvaluateNestedNodeMethodDeclaration(nodeClassName).getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, EVALUATE_NODE + nodeClassName)));
// set 'predicate'
populateEvaluateNodeWithPredicate(evaluateNodeBody, nodeNamesDTO.node.getPredicate(), fields);
// set 'nodeFunctions'
final List<String> nestedNodesFullClasses = nodeNamesDTO.getNestedNodesFullClassNames(toPopulate.packageName);
populateEvaluateNodeWithNodeFunctions(evaluateNodeBody, nestedNodesFullClasses);
// set 'score'
populateEvaluateNodeWithScore(evaluateNodeBody, nodeNamesDTO.node.getScore());
// set 'scoreDistributions'
if (nodeNamesDTO.node.hasScoreDistributions()) {
populateEvaluateNodeWithScoreDistributions(evaluateNodeBody, nodeNamesDTO.node.getScoreDistributions());
}
// set 'missingValuePenalty'
if (nodeNamesDTO.missingValuePenalty != null) {
populateEvaluateNodeWithMissingValuePenalty(evaluateNodeBody, nodeNamesDTO.missingValuePenalty);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLNodeFactory method mergeNode.
/**
* Adjust the <b>evaluateNode(?)</b> references to the ones declared in the given
* <code>JavaParserDTO.nodeTemplate</code>
*
* @param toPopulate
* @param nestedNodeNamesDTO
*/
static void mergeNode(final JavaParserDTO toPopulate, final NodeNamesDTO nestedNodeNamesDTO) {
final MethodCallExpr evaluateNodeInitializer;
// node
if (Objects.equals(toPopulate.nodeClassName, nestedNodeNamesDTO.parentNodeClassName)) {
evaluateNodeInitializer = toPopulate.evaluateRootNodeReferencesDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NODE_FUNCTIONS, toPopulate.evaluateRootNodeReferencesDeclarator))).asMethodCallExpr();
} else {
String expected = EVALUATE_NODE + nestedNodeNamesDTO.parentNodeClassName;
final MethodDeclaration evaluateNestedNodeMethod = toPopulate.nodeTemplate.getMethodsByName(expected).get(0);
final BlockStmt evaluateNestedNodeBody = evaluateNestedNodeMethod.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_IN_METHOD, expected)));
final VariableDeclarator nestedNodeVariableDeclarator = evaluateNestedNodeBody.findAll(VariableDeclarator.class).stream().filter(variableDeclarator -> variableDeclarator.getName().asString().equals(NODE_FUNCTIONS)).findFirst().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, expected, evaluateNestedNodeBody)));
evaluateNodeInitializer = nestedNodeVariableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NODE_FUNCTIONS, nestedNodeVariableDeclarator))).asMethodCallExpr();
}
mergeNodeReferences(toPopulate, nestedNodeNamesDTO, evaluateNodeInitializer);
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLTreeModelFactory method setConstructor.
static void setConstructor(final TreeCompilationDTO compilationDTO, final ClassOrInterfaceDeclaration modelTemplate, final String fullNodeClassName) {
KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
final BlockStmt body = constructorDeclaration.getBody();
// set predicate function
MethodReferenceExpr nodeReference = new MethodReferenceExpr();
nodeReference.setScope(new NameExpr(fullNodeClassName));
nodeReference.setIdentifier("evaluateNode");
CommonCodegenUtils.setAssignExpressionValue(body, "nodeFunction", nodeReference);
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLTreeModelFactoryTest method setConstructor.
@Test
public void setConstructor() {
String className = getSanitizedClassName(treeModel1.getModelName());
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, PACKAGE_NAME, KIE_PMML_TREE_MODEL_TEMPLATE_JAVA, KIE_PMML_TREE_MODEL_TEMPLATE);
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
String targetField = "whatIdo";
String fullNodeClassName = "full.Node.ClassName";
CommonCompilationDTO<TreeModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml1, treeModel1, new HasClassLoaderMock());
KiePMMLTreeModelFactory.setConstructor(TreeCompilationDTO.fromCompilationDTO(source), modelTemplate, fullNodeClassName);
ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName()))).clone();
BlockStmt body = constructorDeclaration.getBody();
// targetField
Optional<AssignExpr> optRetrieved = CommonCodegenUtils.getAssignExpression(body, "targetField");
assertTrue(optRetrieved.isPresent());
AssignExpr retrieved = optRetrieved.get();
Expression initializer = retrieved.getValue();
assertTrue(initializer instanceof StringLiteralExpr);
String expected = String.format("\"%s\"", targetField);
assertEquals(expected, initializer.toString());
// miningFunction
optRetrieved = CommonCodegenUtils.getAssignExpression(body, "miningFunction");
assertTrue(optRetrieved.isPresent());
retrieved = optRetrieved.get();
initializer = retrieved.getValue();
assertTrue(initializer instanceof NameExpr);
MINING_FUNCTION miningFunction = MINING_FUNCTION.byName(treeModel1.getMiningFunction().value());
expected = miningFunction.getClass().getName() + "." + miningFunction.name();
assertEquals(expected, initializer.toString());
// pmmlMODEL
optRetrieved = CommonCodegenUtils.getAssignExpression(body, "pmmlMODEL");
assertTrue(optRetrieved.isPresent());
retrieved = optRetrieved.get();
initializer = retrieved.getValue();
assertTrue(initializer instanceof NameExpr);
expected = PMML_MODEL.TREE_MODEL.getClass().getName() + "." + PMML_MODEL.TREE_MODEL.name();
assertEquals(expected, initializer.toString());
// nodeFunction
optRetrieved = CommonCodegenUtils.getAssignExpression(body, "nodeFunction");
assertTrue(optRetrieved.isPresent());
retrieved = optRetrieved.get();
initializer = retrieved.getValue();
assertTrue(initializer instanceof MethodReferenceExpr);
expected = fullNodeClassName;
assertEquals(expected, ((MethodReferenceExpr) initializer).getScope().toString());
expected = "evaluateNode";
assertEquals(expected, ((MethodReferenceExpr) initializer).getIdentifier());
}
Aggregations