use of org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock in project drools by kiegroup.
the class KiePMMLSegmentFactoryTest method getSegmentsSourcesMap.
@Test
public void getSegmentsSourcesMap() {
final List<Segment> segments = MINING_MODEL.getSegmentation().getSegments();
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 = KiePMMLSegmentFactory.getSegmentsSourcesMap(compilationDTO, nestedModels);
assertNotNull(retrieved);
commonEvaluateNestedModels(nestedModels);
for (Segment segment : segments) {
commonEvaluateMap(retrieved, segment);
}
}
use of org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock in project drools by kiegroup.
the class MiningModelImplementationProviderTest method commonGetKiePMMLModelWithSources.
private void commonGetKiePMMLModelWithSources(String source) throws Exception {
final PMML pmml = getPMML(source);
KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
final MiningModel miningmodel = (MiningModel) pmml.getModels().get(0);
final CommonCompilationDTO<MiningModel> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, miningmodel, new HasKnowledgeBuilderMock(knowledgeBuilder));
final KiePMMLMiningModelWithSources retrieved = (KiePMMLMiningModelWithSources) PROVIDER.getKiePMMLModelWithSources(compilationDTO);
assertNotNull(retrieved);
commonVerifyIsDeepCloneable(retrieved);
assertNotNull(retrieved.getNestedModels());
assertFalse(retrieved.getNestedModels().isEmpty());
final Map<String, String> sourcesMap = new HashMap<>(retrieved.getSourcesMap());
assertFalse(sourcesMap.isEmpty());
try {
KieMemoryCompiler.compile(sourcesMap, Thread.currentThread().getContextClassLoader());
fail("Expecting compilation error without nested models sources");
} catch (Exception e) {
// Expected
}
retrieved.getNestedModels().forEach(nestedModel -> sourcesMap.putAll(((HasSourcesMap) nestedModel).getSourcesMap()));
try {
KieMemoryCompiler.compile(sourcesMap, Thread.currentThread().getContextClassLoader());
} catch (Exception e) {
fail(e.getMessage());
}
}
use of org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock 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.HasKnowledgeBuilderMock 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();
}
});
}
use of org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock in project drools by kiegroup.
the class KiePMMLMiningModelFactoryTest method getKiePMMLMiningModel.
@Test
public void getKiePMMLMiningModel() {
final CommonCompilationDTO<MiningModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, MINING_MODEL, new HasKnowledgeBuilderMock(KNOWLEDGE_BUILDER));
final MiningModelCompilationDTO compilationDTO = MiningModelCompilationDTO.fromCompilationDTO(source);
final KiePMMLMiningModel retrieved = KiePMMLMiningModelFactory.getKiePMMLMiningModel(compilationDTO);
assertNotNull(retrieved);
assertEquals(MINING_MODEL.getAlgorithmName(), retrieved.getAlgorithmName());
assertEquals(MINING_MODEL.isScorable(), retrieved.isScorable());
assertEquals(targetFieldName, retrieved.getTargetField());
}
Aggregations