Search in sources :

Example 1 with HasKnowledgeBuilderMock

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);
}
Also used : Serializable(java.io.Serializable) HasKnowledgeBuilderMock(org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock) KiePMMLMiningModel(org.kie.pmml.models.mining.model.KiePMMLMiningModel) MiningModel(org.dmg.pmml.mining.MiningModel) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KiePMMLMiningModel(org.kie.pmml.models.mining.model.KiePMMLMiningModel) PMML(org.dmg.pmml.PMML)

Example 2 with HasKnowledgeBuilderMock

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();
        }
    });
}
Also used : HasKnowledgeBuilderMock(org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) MiningModel(org.dmg.pmml.mining.MiningModel) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) MiningModelCompilationDTO(org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO) Test(org.junit.Test)

Example 3 with HasKnowledgeBuilderMock

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);
}
Also used : HasKnowledgeBuilderMock(org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock) MiningModel(org.dmg.pmml.mining.MiningModel) HashMap(java.util.HashMap) SegmentCompilationDTO(org.kie.pmml.models.mining.compiler.dto.SegmentCompilationDTO) MiningModelCompilationDTO(org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO) Segment(org.dmg.pmml.mining.Segment) Test(org.junit.Test)

Example 4 with HasKnowledgeBuilderMock

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());
    }
}
Also used : KiePMMLMiningModelWithSources(org.kie.pmml.models.mining.model.KiePMMLMiningModelWithSources) HasKnowledgeBuilderMock(org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock) KiePMMLMiningModel(org.kie.pmml.models.mining.model.KiePMMLMiningModel) MiningModel(org.dmg.pmml.mining.MiningModel) HashMap(java.util.HashMap) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PMML(org.dmg.pmml.PMML) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap)

Example 5 with HasKnowledgeBuilderMock

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());
}
Also used : HasKnowledgeBuilderMock(org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock) KiePMMLMiningModel(org.kie.pmml.models.mining.model.KiePMMLMiningModel) MiningModel(org.dmg.pmml.mining.MiningModel) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) ArrayList(java.util.ArrayList) MiningModelCompilationDTO(org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO) Test(org.junit.Test)

Aggregations

MiningModel (org.dmg.pmml.mining.MiningModel)11 HasKnowledgeBuilderMock (org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock)11 Test (org.junit.Test)9 MiningModelCompilationDTO (org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO)9 ArrayList (java.util.ArrayList)7 KiePMMLModel (org.kie.pmml.commons.model.KiePMMLModel)7 KiePMMLMiningModel (org.kie.pmml.models.mining.model.KiePMMLMiningModel)5 Segment (org.dmg.pmml.mining.Segment)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 JAXBException (javax.xml.bind.JAXBException)3 SegmentCompilationDTO (org.kie.pmml.models.mining.compiler.dto.SegmentCompilationDTO)3 SAXException (org.xml.sax.SAXException)3 PMML (org.dmg.pmml.PMML)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 Expression (com.github.javaparser.ast.expr.Expression)1