use of org.kie.pmml.compiler.api.dto.CompilationDTO in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method populateGetCreatedOutputFieldsMethod.
@Test
public void populateGetCreatedOutputFieldsMethod() throws IOException {
final CompilationDTO compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmmlModel, model, new HasClassLoaderMock());
KiePMMLModelFactoryUtils.populateGetCreatedOutputFieldsMethod(classOrInterfaceDeclaration, compilationDTO.getKieOutputFields());
final MethodDeclaration retrieved = classOrInterfaceDeclaration.getMethodsByName(GET_CREATED_OUTPUTFIELDS).get(0);
String text = getFileContent(TEST_13_SOURCE);
MethodDeclaration expected = JavaParserUtils.parseMethod(text);
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
}
use of org.kie.pmml.compiler.api.dto.CompilationDTO in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method addGetCreatedKiePMMLMiningFieldsMethod.
@Test
public void addGetCreatedKiePMMLMiningFieldsMethod() throws IOException {
final CompilationDTO compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmmlModel, model, new HasClassLoaderMock());
ClassOrInterfaceDeclaration modelTemplate = new ClassOrInterfaceDeclaration();
KiePMMLModelFactoryUtils.addGetCreatedKiePMMLMiningFieldsMethod(modelTemplate, compilationDTO.getMiningSchema().getMiningFields(), compilationDTO.getFields());
final MethodDeclaration retrieved = modelTemplate.getMethodsByName(GET_CREATED_KIEPMMLMININGFIELDS).get(0);
String text = getFileContent(TEST_12_SOURCE);
BlockStmt expected = JavaParserUtils.parseBlock(text);
assertTrue(JavaParserUtils.equalsNode(expected, retrieved.getBody().get()));
}
use of org.kie.pmml.compiler.api.dto.CompilationDTO in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method commonPopulateGetCreatedKiePMMLOutputFieldsMethod.
@Test
public void commonPopulateGetCreatedKiePMMLOutputFieldsMethod() throws IOException {
final CompilationDTO compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmmlModel, model, new HasClassLoaderMock());
final MethodDeclaration methodDeclaration = new MethodDeclaration();
KiePMMLModelFactoryUtils.commonPopulateGetCreatedKiePMMLOutputFieldsMethod(methodDeclaration, compilationDTO.getOutput().getOutputFields());
String text = getFileContent(TEST_05_SOURCE);
MethodDeclaration expected = JavaParserUtils.parseMethod(text);
assertTrue(JavaParserUtils.equalsNode(expected, methodDeclaration));
}
use of org.kie.pmml.compiler.api.dto.CompilationDTO 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.compiler.api.dto.CompilationDTO in project drools by kiegroup.
the class KiePMMLRegressionModelFactory method getKiePMMLRegressionModelSourcesMap.
// Source code generation
public static Map<String, String> getKiePMMLRegressionModelSourcesMap(final RegressionCompilationDTO compilationDTO) throws IOException {
logger.trace("getKiePMMLRegressionModelSourcesMap {} {} {}", compilationDTO.getFields(), compilationDTO.getModel(), compilationDTO.getPackageName());
String className = compilationDTO.getSimpleClassName();
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, compilationDTO.getPackageName(), KIE_PMML_REGRESSION_MODEL_TEMPLATE_JAVA, KIE_PMML_REGRESSION_MODEL_TEMPLATE);
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
Map<String, KiePMMLTableSourceCategory> tablesSourceMap = getRegressionTablesMap(compilationDTO);
String nestedTable = tablesSourceMap.size() == 1 ? tablesSourceMap.keySet().iterator().next() : tablesSourceMap.keySet().stream().filter(tableName -> tableName.startsWith(compilationDTO.getPackageName() + ".KiePMMLClassificationTable")).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find expected " + "KiePMMLClassificationTable"));
setStaticGetter(compilationDTO, modelTemplate, nestedTable);
Map<String, String> toReturn = tablesSourceMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getSource()));
toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
return toReturn;
}
Aggregations