Search in sources :

Example 26 with KiePMMLOriginalTypeGeneratedType

use of org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType in project drools by kiegroup.

the class KiePMMLScorecardModelFactoryTest method getKiePMMLScorecardModelSourcesMap.

@Test
public void getKiePMMLScorecardModelSourcesMap() {
    final DataDictionary dataDictionary = pmml.getDataDictionary();
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = getFieldTypeMap(dataDictionary, pmml.getTransformationDictionary(), scorecardModel.getLocalTransformations());
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    final CommonCompilationDTO<Scorecard> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, scorecardModel, new HasKnowledgeBuilderMock(knowledgeBuilder));
    final DroolsCompilationDTO<Scorecard> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(compilationDTO, fieldTypeMap);
    Map<String, String> retrieved = KiePMMLScorecardModelFactory.getKiePMMLScorecardModelSourcesMap(droolsCompilationDTO);
    assertNotNull(retrieved);
    assertEquals(1, retrieved.size());
}
Also used : HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) Scorecard(org.dmg.pmml.scorecard.Scorecard) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 27 with KiePMMLOriginalTypeGeneratedType

use of org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType in project drools by kiegroup.

the class KiePMMLScorecardModelFactoryTest method getKiePMMLScorecardModel.

@Test
public void getKiePMMLScorecardModel() throws Exception {
    final DataDictionary dataDictionary = pmml.getDataDictionary();
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = getFieldTypeMap(dataDictionary, pmml.getTransformationDictionary(), scorecardModel.getLocalTransformations());
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    final CommonCompilationDTO<Scorecard> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, scorecardModel, new HasKnowledgeBuilderMock(knowledgeBuilder));
    final DroolsCompilationDTO<Scorecard> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(compilationDTO, fieldTypeMap);
    KiePMMLScorecardModel retrieved = KiePMMLScorecardModelFactory.getKiePMMLScorecardModel(droolsCompilationDTO);
    assertNotNull(retrieved);
    assertEquals(scorecardModel.getModelName(), retrieved.getName());
    assertEquals(TARGET_FIELD, retrieved.getTargetField());
}
Also used : KiePMMLScorecardModel(org.kie.pmml.models.drools.scorecard.model.KiePMMLScorecardModel) HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) Scorecard(org.dmg.pmml.scorecard.Scorecard) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 28 with KiePMMLOriginalTypeGeneratedType

use of org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType in project drools by kiegroup.

the class KiePMMLASTFactoryUtilsTest method getConstraintEntryFromSimplePredicates.

@Test
public void getConstraintEntryFromSimplePredicates() {
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    String fieldName = "FIELD_NAME";
    List<SimplePredicate> simplePredicates = IntStream.range(0, 2).mapToObj(index -> {
        fieldTypeMap.put(fieldName, new KiePMMLOriginalTypeGeneratedType(DataType.STRING.value(), getSanitizedClassName(fieldName.toUpperCase())));
        return PMMLModelTestUtils.getSimplePredicate(fieldName, "VALUE-" + index, SimplePredicate.Operator.LESS_THAN);
    }).collect(Collectors.toList());
    final KiePMMLFieldOperatorValue retrieved = KiePMMLASTFactoryUtils.getConstraintEntryFromSimplePredicates(fieldName, BOOLEAN_OPERATOR.OR, simplePredicates, fieldTypeMap);
    assertEquals(fieldName, retrieved.getName());
    assertNotNull(retrieved.getConstraintsAsString());
    String expected = "value < \"VALUE-0\" || value < \"VALUE-1\"";
    assertEquals(expected, retrieved.getConstraintsAsString());
}
Also used : IntStream(java.util.stream.IntStream) Predicate(org.dmg.pmml.Predicate) BeforeClass(org.junit.BeforeClass) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PMMLModelTestUtils(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils) KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) Map(java.util.Map) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Assert.assertNotNull(org.junit.Assert.assertNotNull) DataType(org.dmg.pmml.DataType) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) OPERATOR(org.kie.pmml.api.enums.OPERATOR) Consumer(java.util.function.Consumer) List(java.util.List) SimplePredicate(org.dmg.pmml.SimplePredicate) Assert.assertFalse(org.junit.Assert.assertFalse) PMMLModelTestUtils.getRandomSimplePredicateOperator(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimplePredicateOperator) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLModelUtils.getSanitizedClassName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedClassName) PMMLModelTestUtils.getRandomObject(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomObject) HashMap(java.util.HashMap) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 29 with KiePMMLOriginalTypeGeneratedType

use of org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType in project drools by kiegroup.

the class KiePMMLDroolsModelFactoryUtilsTest method addFieldTypeMapPopulation.

@Test
public void addFieldTypeMapPopulation() {
    BlockStmt blockStmt = new BlockStmt();
    Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    IntStream.range(0, 3).forEach(index -> {
        String key = "KEY-" + index;
        KiePMMLOriginalTypeGeneratedType value = new KiePMMLOriginalTypeGeneratedType("ORIGINALTYPE-" + index, "GENERATEDTYPE-" + index);
        fieldTypeMap.put(key, value);
    });
    KiePMMLDroolsModelFactoryUtils.addFieldTypeMapPopulation(blockStmt, fieldTypeMap);
    commonEvaluateFieldTypeMap(blockStmt, fieldTypeMap, fieldTypeMap.size());
}
Also used : HashMap(java.util.HashMap) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 30 with KiePMMLOriginalTypeGeneratedType

use of org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType in project drools by kiegroup.

the class KiePMMLDroolsModelFactoryUtilsTest method getKiePMMLModelCompilationUnit.

@Test
public void getKiePMMLModelCompilationUnit() {
    DataDictionary dataDictionary = new DataDictionary();
    String targetFieldString = "target.field";
    FieldName targetFieldName = FieldName.create(targetFieldString);
    dataDictionary.addDataFields(new DataField(targetFieldName, OpType.CONTINUOUS, DataType.DOUBLE));
    String modelName = "ModelName";
    TreeModel model = new TreeModel();
    model.setModelName(modelName);
    model.setMiningFunction(MiningFunction.CLASSIFICATION);
    MiningField targetMiningField = new MiningField(targetFieldName);
    targetMiningField.setUsageType(MiningField.UsageType.TARGET);
    MiningSchema miningSchema = new MiningSchema();
    miningSchema.addMiningFields(targetMiningField);
    model.setMiningSchema(miningSchema);
    Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    fieldTypeMap.put(targetFieldString, new KiePMMLOriginalTypeGeneratedType(targetFieldString, getSanitizedClassName(targetFieldString)));
    String packageName = "net.test";
    PMML pmml = new PMML();
    pmml.setDataDictionary(dataDictionary);
    pmml.addModels(model);
    final CommonCompilationDTO<TreeModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(packageName, pmml, model, new HasClassLoaderMock());
    final DroolsCompilationDTO<TreeModel> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(source, fieldTypeMap);
    CompilationUnit retrieved = KiePMMLDroolsModelFactoryUtils.getKiePMMLModelCompilationUnit(droolsCompilationDTO, TEMPLATE_SOURCE, TEMPLATE_CLASS_NAME);
    assertEquals(droolsCompilationDTO.getPackageName(), retrieved.getPackageDeclaration().get().getNameAsString());
    ConstructorDeclaration constructorDeclaration = retrieved.getClassByName(modelName).get().getDefaultConstructor().get();
    MINING_FUNCTION miningFunction = MINING_FUNCTION.CLASSIFICATION;
    PMML_MODEL pmmlModel = PMML_MODEL.byName(model.getClass().getSimpleName());
    Map<String, Expression> assignExpressionMap = new HashMap<>();
    assignExpressionMap.put("targetField", new StringLiteralExpr(targetFieldString));
    assignExpressionMap.put("miningFunction", new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name()));
    assignExpressionMap.put("pmmlMODEL", new NameExpr(pmmlModel.getClass().getName() + "." + pmmlModel.name()));
    String expectedKModulePackageName = getSanitizedPackageName(packageName + "." + modelName);
    assignExpressionMap.put("kModulePackageName", new StringLiteralExpr(expectedKModulePackageName));
    assertTrue(commonEvaluateAssignExpr(constructorDeclaration.getBody(), assignExpressionMap));
    // The last "1" is for
    int expectedMethodCallExprs = assignExpressionMap.size() + fieldTypeMap.size() + 1;
    // the super invocation
    commonEvaluateFieldTypeMap(constructorDeclaration.getBody(), fieldTypeMap, expectedMethodCallExprs);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MiningField(org.dmg.pmml.MiningField) HashMap(java.util.HashMap) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) DataDictionary(org.dmg.pmml.DataDictionary) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) TreeModel(org.dmg.pmml.tree.TreeModel) DataField(org.dmg.pmml.DataField) MiningSchema(org.dmg.pmml.MiningSchema) Expression(com.github.javaparser.ast.expr.Expression) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) PMML(org.dmg.pmml.PMML) FieldName(org.dmg.pmml.FieldName) PMML_MODEL(org.kie.pmml.api.enums.PMML_MODEL) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) Test(org.junit.Test)

Aggregations

KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)45 Test (org.junit.Test)32 HashMap (java.util.HashMap)29 ArrayList (java.util.ArrayList)16 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)14 KiePMMLASTTestUtils.getPredicateASTFactoryData (org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData)12 KiePMMLDroolsAST (org.kie.pmml.models.drools.ast.KiePMMLDroolsAST)11 SimplePredicate (org.dmg.pmml.SimplePredicate)9 KiePMMLDroolsType (org.kie.pmml.models.drools.ast.KiePMMLDroolsType)9 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)8 Field (org.dmg.pmml.Field)7 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)7 List (java.util.List)6 CompoundPredicate (org.dmg.pmml.CompoundPredicate)6 DataDictionary (org.dmg.pmml.DataDictionary)6 Map (java.util.Map)5 DataField (org.dmg.pmml.DataField)5 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)5 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)5 KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)5