use of org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO in project drools by kiegroup.
the class KiePMMLMiningModelFactoryTest method setConstructor.
@Test
public void setConstructor() {
PMML_MODEL pmmlModel = PMML_MODEL.byName(MINING_MODEL.getClass().getSimpleName());
final ClassOrInterfaceDeclaration modelTemplate = MODEL_TEMPLATE.clone();
MINING_FUNCTION miningFunction = MINING_FUNCTION.byName(MINING_MODEL.getMiningFunction().value());
final CommonCompilationDTO<MiningModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, MINING_MODEL, new HasClassLoaderMock());
final MiningModelCompilationDTO compilationDTO = MiningModelCompilationDTO.fromCompilationDTO(source);
KiePMMLMiningModelFactory.setConstructor(compilationDTO, modelTemplate);
Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>();
superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", MINING_MODEL.getModelName())));
Map<String, Expression> assignExpressionMap = new HashMap<>();
assignExpressionMap.put("targetField", new StringLiteralExpr(targetFieldName));
assignExpressionMap.put("miningFunction", new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name()));
assignExpressionMap.put("pmmlMODEL", new NameExpr(pmmlModel.getClass().getName() + "." + pmmlModel.name()));
ClassOrInterfaceType kiePMMLSegmentationClass = parseClassOrInterfaceType(compilationDTO.getSegmentationCanonicalClassName());
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(kiePMMLSegmentationClass);
assignExpressionMap.put("segmentation", objectCreationExpr);
ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().get();
assertTrue(commonEvaluateConstructor(constructorDeclaration, getSanitizedClassName(MINING_MODEL.getModelName()), superInvocationExpressionsMap, assignExpressionMap));
}
use of org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO in project drools by kiegroup.
the class KiePMMLSegmentationFactoryTest method getSegmentationSourcesMapCompiled.
@Test
public void getSegmentationSourcesMapCompiled() {
final HasKnowledgeBuilderMock hasKnowledgeBuilderMock = new HasKnowledgeBuilderMock(KNOWLEDGE_BUILDER);
final CommonCompilationDTO<MiningModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, MINING_MODEL, hasKnowledgeBuilderMock);
final MiningModelCompilationDTO compilationDTO = MiningModelCompilationDTO.fromCompilationDTO(source);
final List<KiePMMLModel> nestedModels = new ArrayList<>();
final List<String> expectedGeneratedClasses = MINING_MODEL.getSegmentation().getSegments().stream().map(this::getExpectedNestedModelClass).collect(Collectors.toList());
expectedGeneratedClasses.forEach(expectedGeneratedClass -> {
try {
hasKnowledgeBuilderMock.getClassLoader().loadClass(expectedGeneratedClass);
fail("Expecting class not found: " + expectedGeneratedClass);
} catch (Exception e) {
assertTrue(e instanceof ClassNotFoundException);
}
});
final Map<String, String> retrieved = KiePMMLSegmentationFactory.getSegmentationSourcesMapCompiled(compilationDTO, nestedModels);
assertNotNull(retrieved);
int expectedNestedModels = MINING_MODEL.getSegmentation().getSegments().size();
assertEquals(expectedNestedModels, nestedModels.size());
expectedGeneratedClasses.forEach(expectedGeneratedClass -> {
try {
hasKnowledgeBuilderMock.getClassLoader().loadClass(expectedGeneratedClass);
} catch (Exception e) {
fail("Expecting class to be loaded, but got: " + e.getClass().getName() + " -> " + e.getMessage());
e.printStackTrace();
}
});
}
use of org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO in project drools by kiegroup.
the class KiePMMLSegmentFactoryTest method getSegmentSourcesMapHasSourcesWithKiePMMLModelClass.
@Test
public void getSegmentSourcesMapHasSourcesWithKiePMMLModelClass() {
final Segment segment = MINING_MODEL.getSegmentation().getSegments().get(0);
final String regressionModelName = "CategoricalVariablesRegression";
final String kiePMMLModelClass = PACKAGE_NAME + "." + regressionModelName;
final Map<String, String> sourcesMap = new HashMap<>();
sourcesMap.put(kiePMMLModelClass, String.format("public class %s {}", regressionModelName));
final CommonCompilationDTO<MiningModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, MINING_MODEL, new HasKnowledgeBuilderMock(KNOWLEDGE_BUILDER));
final MiningModelCompilationDTO compilationDTO = MiningModelCompilationDTO.fromCompilationDTO(source);
final SegmentCompilationDTO segmentCompilationDTO = SegmentCompilationDTO.fromGeneratedPackageNameAndFields(compilationDTO, segment, compilationDTO.getFields());
final Map<String, String> retrieved = KiePMMLSegmentFactory.getSegmentSourcesMap(segmentCompilationDTO, true);
commonEvaluateMap(retrieved, segment);
}
use of org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO in project drools by kiegroup.
the class KiePMMLMiningModelFactoryTest method getKiePMMLMiningModelSourcesMap.
@Test
public void getKiePMMLMiningModelSourcesMap() {
final List<KiePMMLModel> nestedModels = new ArrayList<>();
final CommonCompilationDTO<MiningModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, MINING_MODEL, new HasKnowledgeBuilderMock(KNOWLEDGE_BUILDER));
final MiningModelCompilationDTO compilationDTO = MiningModelCompilationDTO.fromCompilationDTO(source);
final Map<String, String> retrieved = KiePMMLMiningModelFactory.getKiePMMLMiningModelSourcesMap(compilationDTO, nestedModels);
assertNotNull(retrieved);
int expectedNestedModels = MINING_MODEL.getSegmentation().getSegments().size();
assertEquals(expectedNestedModels, nestedModels.size());
}
use of org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO in project drools by kiegroup.
the class KiePMMLMiningModelFactoryTest method getKiePMMLMiningModelSourcesMapCompiled.
@Test
public void getKiePMMLMiningModelSourcesMapCompiled() {
final List<KiePMMLModel> nestedModels = new ArrayList<>();
final HasKnowledgeBuilderMock hasKnowledgeBuilderMock = new HasKnowledgeBuilderMock(KNOWLEDGE_BUILDER);
final CommonCompilationDTO<MiningModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, MINING_MODEL, hasKnowledgeBuilderMock);
final MiningModelCompilationDTO compilationDTO = MiningModelCompilationDTO.fromCompilationDTO(source);
final List<String> expectedGeneratedClasses = MINING_MODEL.getSegmentation().getSegments().stream().map(segment -> {
String modelName = segment.getModel().getModelName();
String sanitizedPackageName = getSanitizedPackageName(compilationDTO.getSegmentationPackageName() + "." + segment.getId());
String sanitizedClassName = getSanitizedClassName(modelName);
return String.format(PACKAGE_CLASS_TEMPLATE, sanitizedPackageName, sanitizedClassName);
}).collect(Collectors.toList());
expectedGeneratedClasses.forEach(expectedGeneratedClass -> {
try {
hasKnowledgeBuilderMock.getClassLoader().loadClass(expectedGeneratedClass);
fail("Expecting class not found: " + expectedGeneratedClass);
} catch (Exception e) {
assertTrue(e instanceof ClassNotFoundException);
}
});
final Map<String, String> retrieved = KiePMMLMiningModelFactory.getKiePMMLMiningModelSourcesMapCompiled(compilationDTO, nestedModels);
assertNotNull(retrieved);
int expectedNestedModels = MINING_MODEL.getSegmentation().getSegments().size();
assertEquals(expectedNestedModels, nestedModels.size());
expectedGeneratedClasses.forEach(expectedGeneratedClass -> {
try {
hasKnowledgeBuilderMock.getClassLoader().loadClass(expectedGeneratedClass);
} catch (Exception e) {
fail("Expecting class to be loaded, but got: " + e.getClass().getName() + " -> " + e.getMessage());
e.printStackTrace();
}
});
}
Aggregations