Search in sources :

Example 1 with MiningModelCompilationDTO

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));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) KiePMMLMiningModel(org.kie.pmml.models.mining.model.KiePMMLMiningModel) MiningModel(org.dmg.pmml.mining.MiningModel) Expression(com.github.javaparser.ast.expr.Expression) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) MiningModelCompilationDTO(org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO) PMML_MODEL(org.kie.pmml.api.enums.PMML_MODEL) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) Test(org.junit.Test)

Example 2 with MiningModelCompilationDTO

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();
        }
    });
}
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 MiningModelCompilationDTO

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);
}
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 MiningModelCompilationDTO

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());
}
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)

Example 5 with MiningModelCompilationDTO

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();
        }
    });
}
Also used : StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) BeforeClass(org.junit.BeforeClass) JavaParserUtils.getFromFileName(org.kie.pmml.compiler.commons.utils.JavaParserUtils.getFromFileName) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KiePMMLMiningModel(org.kie.pmml.models.mining.model.KiePMMLMiningModel) CodegenTestUtils.commonEvaluateConstructor(org.kie.pmml.compiler.commons.testutils.CodegenTestUtils.commonEvaluateConstructor) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) PMML_MODEL(org.kie.pmml.api.enums.PMML_MODEL) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) Assert.fail(org.junit.Assert.fail) CompilationUnit(com.github.javaparser.ast.CompilationUnit) KiePMMLModelUtils.getSanitizedPackageName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedPackageName) MiningModel(org.dmg.pmml.mining.MiningModel) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) HasKnowledgeBuilderMock(org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock) PACKAGE_NAME(org.kie.pmml.commons.Constants.PACKAGE_NAME) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) NameExpr(com.github.javaparser.ast.expr.NameExpr) MiningModelCompilationDTO(org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) List(java.util.List) CommonCompilationDTO(org.kie.pmml.compiler.api.dto.CommonCompilationDTO) SAXException(org.xml.sax.SAXException) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) PACKAGE_CLASS_TEMPLATE(org.kie.pmml.commons.Constants.PACKAGE_CLASS_TEMPLATE) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLModelUtils.getSanitizedClassName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedClassName) 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) KiePMMLMiningModel(org.kie.pmml.models.mining.model.KiePMMLMiningModel) 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)

Aggregations

MiningModel (org.dmg.pmml.mining.MiningModel)10 Test (org.junit.Test)10 MiningModelCompilationDTO (org.kie.pmml.models.mining.compiler.dto.MiningModelCompilationDTO)10 HasKnowledgeBuilderMock (org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock)9 ArrayList (java.util.ArrayList)7 KiePMMLModel (org.kie.pmml.commons.model.KiePMMLModel)7 Segment (org.dmg.pmml.mining.Segment)4 KiePMMLMiningModel (org.kie.pmml.models.mining.model.KiePMMLMiningModel)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 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)2 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)2 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)2 Expression (com.github.javaparser.ast.expr.Expression)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)2 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)2