use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLClusteringModelFactory method setConstructor.
static void setConstructor(final CompilationDTO<ClusteringModel> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
final BlockStmt body = constructorDeclaration.getBody();
ClusteringModel clusteringModel = compilationDTO.getModel();
body.addStatement(assignExprFrom("modelClass", modelClassFrom(clusteringModel.getModelClass())));
clusteringModel.getClusters().stream().map(KiePMMLClusteringModelFactory::clusterCreationExprFrom).map(expr -> methodCallExprFrom("clusters", "add", expr)).forEach(body::addStatement);
clusteringModel.getClusteringFields().stream().map(KiePMMLClusteringModelFactory::clusteringFieldCreationExprFrom).map(expr -> methodCallExprFrom("clusteringFields", "add", expr)).forEach(body::addStatement);
body.addStatement(assignExprFrom("comparisonMeasure", comparisonMeasureCreationExprFrom(clusteringModel.getComparisonMeasure())));
if (clusteringModel.getMissingValueWeights() != null) {
body.addStatement(assignExprFrom("missingValueWeights", missingValueWeightsCreationExprFrom(clusteringModel.getMissingValueWeights())));
}
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLMiningModelFactory method setConstructor.
static void setConstructor(final MiningModelCompilationDTO compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
final BlockStmt body = constructorDeclaration.getBody();
ClassOrInterfaceType kiePMMLSegmentationClass = parseClassOrInterfaceType(compilationDTO.getSegmentationCanonicalClassName());
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(kiePMMLSegmentationClass);
CommonCodegenUtils.setAssignExpressionValue(body, "segmentation", objectCreationExpr);
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLSegmentationFactory method getSegmentationSourcesMapCommon.
static Map<String, String> getSegmentationSourcesMapCommon(final MiningModelCompilationDTO compilationDTO, final Map<String, String> toReturn) {
logger.debug("getSegmentationSourcesMapCommon {}", compilationDTO.getSegmentation());
String className = compilationDTO.getSegmentationClassName();
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, compilationDTO.getSegmentationPackageName(), KIE_PMML_SEGMENTATION_TEMPLATE_JAVA, KIE_PMML_SEGMENTATION_TEMPLATE);
ClassOrInterfaceDeclaration segmentationTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
final ConstructorDeclaration constructorDeclaration = segmentationTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, compilationDTO.getSegmentationName())));
// Avoid stream/map to preserve insertion order and then execution order
final List<String> segmentsClasses = new ArrayList<>();
if (compilationDTO.getSegmentation().getSegments() != null) {
for (Segment segment : compilationDTO.getSegmentation().getSegments()) {
segmentsClasses.add(getSanitizedPackageName(compilationDTO.getSegmentationPackageName() + "." + segment.getId()) + "." + getSanitizedClassName(segment.getId()));
}
}
if (!toReturn.keySet().containsAll(segmentsClasses)) {
String missingClasses = String.join(", ", segmentsClasses);
throw new KiePMMLException("Expected generated class " + missingClasses + " not found");
}
setConstructor(className, compilationDTO.getSegmentationName(), constructorDeclaration, MULTIPLE_MODEL_METHOD.byName(compilationDTO.getSegmentation().getMultipleModelMethod().value()), segmentsClasses);
toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLSegmentFactory method getSegmentSourcesMap.
static Map<String, String> getSegmentSourcesMap(final SegmentCompilationDTO segmentCompilationDTO, final boolean isInterpreted) {
logger.debug(GET_SEGMENT, segmentCompilationDTO.getSegment());
String kiePMMLModelClass = segmentCompilationDTO.getPackageCanonicalClassName();
final String className = getSanitizedClassName(segmentCompilationDTO.getId());
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, segmentCompilationDTO.getPackageName(), KIE_PMML_SEGMENT_TEMPLATE_JAVA, KIE_PMML_SEGMENT_TEMPLATE);
ClassOrInterfaceDeclaration segmentTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
final ConstructorDeclaration constructorDeclaration = segmentTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, segmentTemplate.getName())));
final Map<String, String> toReturn = new HashMap<>();
setConstructor(segmentCompilationDTO.getId(), className, constructorDeclaration, kiePMMLModelClass, isInterpreted, segmentCompilationDTO.getWeight().doubleValue());
populateGetPredicateMethod(segmentCompilationDTO.getPredicate(), segmentCompilationDTO.getFields(), segmentTemplate);
toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method addTransformationsInClassOrInterfaceDeclaration.
/**
* Add <b>common</b> and <b>local</b> transformations management inside the given
* <code>ClassOrInterfaceDeclaration</code>
* @param toPopulate
* @param transformationDictionary
* @param localTransformations
*/
public static void addTransformationsInClassOrInterfaceDeclaration(final ClassOrInterfaceDeclaration toPopulate, final TransformationDictionary transformationDictionary, final LocalTransformations localTransformations) {
String createTransformationDictionary = null;
if (transformationDictionary != null) {
BlockStmt createTransformationDictionaryBody = KiePMMLTransformationDictionaryFactory.getKiePMMLTransformationDictionaryVariableDeclaration(transformationDictionary);
createTransformationDictionaryBody.addStatement(getReturnStmt(TRANSFORMATION_DICTIONARY));
createTransformationDictionary = "createTransformationDictionary";
MethodDeclaration createTransformationDictionaryMethod = toPopulate.addMethod(createTransformationDictionary, Modifier.Keyword.PRIVATE);
createTransformationDictionaryMethod.setType(KiePMMLTransformationDictionary.class.getName());
createTransformationDictionaryMethod.setBody(createTransformationDictionaryBody);
}
String createLocalTransformations = null;
if (localTransformations != null) {
BlockStmt createLocalTransformationsBody = KiePMMLLocalTransformationsFactory.getKiePMMLLocalTransformationsVariableDeclaration(localTransformations);
createLocalTransformationsBody.addStatement(getReturnStmt(LOCAL_TRANSFORMATIONS));
createLocalTransformations = "createLocalTransformations";
MethodDeclaration createLocalTransformationsMethod = toPopulate.addMethod(createLocalTransformations, Modifier.Keyword.PRIVATE);
createLocalTransformationsMethod.setType(KiePMMLLocalTransformations.class.getName());
createLocalTransformationsMethod.setBody(createLocalTransformationsBody);
}
final ConstructorDeclaration constructorDeclaration = toPopulate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, toPopulate.getName())));
populateTransformationsInConstructor(constructorDeclaration, createTransformationDictionary, createLocalTransformations);
}
Aggregations