use of org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock in project drools by kiegroup.
the class MiningModelImplementationProviderTest method commonGetKiePMMLModel.
private void commonGetKiePMMLModel(String source) throws Exception {
final PMML pmml = getPMML(source);
final 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 KiePMMLMiningModel retrieved = PROVIDER.getKiePMMLModel(compilationDTO);
assertNotNull(retrieved);
assertTrue(retrieved instanceof Serializable);
commonVerifyIsDeepCloneable(retrieved);
}
use of org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock 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.HasKnowledgeBuilderMock 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.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());
}
Aggregations