use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLLocalTransformationsFactory method getKiePMMLLocalTransformationsVariableDeclaration.
/**
* @param localTransformations
* @return
*/
static BlockStmt getKiePMMLLocalTransformationsVariableDeclaration(final LocalTransformations localTransformations) {
final MethodDeclaration methodDeclaration = LOCAL_TRANSFORMATIONS_TEMPLATE.getMethodsByName(GETKIEPMMLLOCALTRANSFORMATIONS).get(0).clone();
final BlockStmt transformationDictionaryBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(transformationDictionaryBody, LOCAL_TRANSFORMATIONS).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, LOCAL_TRANSFORMATIONS, transformationDictionaryBody)));
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, LOCAL_TRANSFORMATIONS, methodDeclaration))).asMethodCallExpr();
final BlockStmt toReturn = new BlockStmt();
if (localTransformations.hasDerivedFields()) {
NodeList<Expression> derivedFields = addDerivedField(toReturn, localTransformations.getDerivedFields());
getChainedMethodCallExprFrom("withDerivedFields", initializer).setArguments(derivedFields);
}
transformationDictionaryBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLNormDiscreteFactory method getNormDiscreteVariableDeclaration.
static BlockStmt getNormDiscreteVariableDeclaration(final String variableName, final NormDiscrete normDiscrete) {
final MethodDeclaration methodDeclaration = NORMDISCRETE_TEMPLATE.getMethodsByName(GETKIEPMMLNORMDISCRETE).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, NORM_DISCRETE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, NORM_DISCRETE, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NORM_DISCRETE, toReturn))).asObjectCreationExpr();
final StringLiteralExpr nameExpr = new StringLiteralExpr(normDiscrete.getField().getValue());
final Expression mapMissingToExpr = getExpressionForObject(normDiscrete.getMapMissingTo());
objectCreationExpr.getArguments().set(0, nameExpr);
objectCreationExpr.getArguments().set(2, new StringLiteralExpr((String) normDiscrete.getValue()));
objectCreationExpr.getArguments().set(3, mapMissingToExpr);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLFactoryFactory method getFactorySourceCode.
public static Map<String, String> getFactorySourceCode(String factoryClassName, String packageName, Map<String, Boolean> generatedClassesModelTypeMap) {
logger.trace("getFactorySourceCode {} {} {}", factoryClassName, packageName, generatedClassesModelTypeMap);
String fullClassName = packageName + "." + factoryClassName;
Map<String, String> toReturn = new HashMap<>();
CompilationUnit templateCU = getFromFileName(KIE_PMML_MODEL_FACTORY_TEMPLATE_JAVA);
CompilationUnit cloneCU = templateCU.clone();
cloneCU.setPackageDeclaration(packageName);
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(KIE_PMML_MODEL_FACTORY_TEMPLATE).orElseThrow(() -> new RuntimeException(MAIN_CLASS_NOT_FOUND));
modelTemplate.setName(factoryClassName);
final FieldDeclaration fieldByName = modelTemplate.getFieldByName(KIE_PMML_MODELS_FIELD).orElseThrow(() -> new KiePMMLException(String.format("Failed to find FieldDeclaration %s in template" + " %s", KIE_PMML_MODELS_FIELD, KIE_PMML_MODEL_FACTORY_TEMPLATE_JAVA)));
populateKiePmmlFields(fieldByName, generatedClassesModelTypeMap);
toReturn.put(fullClassName, cloneCU.toString());
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLNodeFactory method mergeNodeReferences.
/**
* Adjust the <b>evaluateNode(?)</b> references to the ones declared in the given
* <code>JavaParserDTO.nodeTemplate</code>
*
* @param toPopulate
* @param nestedNodeNamesDTO
* @param evaluateNodeInitializer
*/
static void mergeNodeReferences(final JavaParserDTO toPopulate, final NodeNamesDTO nestedNodeNamesDTO, final MethodCallExpr evaluateNodeInitializer) {
final NodeList<Expression> evaluateNodeReferences = evaluateNodeInitializer.getArguments();
final String expectedReference = String.format(PACKAGE_CLASS_TEMPLATE, toPopulate.packageName, nestedNodeNamesDTO.nodeClassName);
Optional<MethodReferenceExpr> found = Optional.empty();
for (Expression expression : evaluateNodeReferences) {
if (expectedReference.equals(expression.asMethodReferenceExpr().getScope().toString())) {
found = Optional.of(expression.asMethodReferenceExpr());
break;
}
}
final MethodReferenceExpr evaluateNodeReference = found.orElseThrow(() -> new KiePMMLException(String.format(MISSING_METHOD_REFERENCE_TEMPLATE, expectedReference, evaluateNodeInitializer)));
String identifier = EVALUATE_NODE + nestedNodeNamesDTO.nodeClassName;
evaluateNodeReference.setScope(new NameExpr(toPopulate.nodeClassName));
evaluateNodeReference.setIdentifier(identifier);
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLNodeFactory method getKiePMMLNode.
public static KiePMMLNode getKiePMMLNode(final Node node, final List<Field<?>> fields, final String packageName, final Double missingValuePenalty, final HasClassLoader hasClassLoader) {
logger.trace("getKiePMMLTreeNode {} {}", packageName, node);
final KiePMMLNodeFactory.NodeNamesDTO nodeNamesDTO = new KiePMMLNodeFactory.NodeNamesDTO(node, createNodeClassName(), null, missingValuePenalty);
final Map<String, String> sourcesMap = getKiePMMLNodeSourcesMap(nodeNamesDTO, fields, packageName);
String fullClassName = packageName + "." + nodeNamesDTO.nodeClassName;
try {
Class<?> kiePMMLNodeClass = hasClassLoader.compileAndLoadClass(sourcesMap, fullClassName);
return (KiePMMLNode) kiePMMLNodeClass.newInstance();
} catch (Exception e) {
throw new KiePMMLException(e);
}
}
Aggregations