Search in sources :

Example 6 with MINING_FUNCTION

use of org.kie.pmml.api.enums.MINING_FUNCTION in project drools by kiegroup.

the class KiePMMLTreeModelFactoryTest method setConstructor.

@Test
public void setConstructor() {
    final String targetField = "whatIdo";
    final ClassOrInterfaceDeclaration modelTemplate = classOrInterfaceDeclaration.clone();
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    final CommonCompilationDTO<TreeModel> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, treeModel, new HasKnowledgeBuilderMock(knowledgeBuilder));
    final DroolsCompilationDTO<TreeModel> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(compilationDTO, new HashMap<>());
    KiePMMLTreeModelFactory.setConstructor(droolsCompilationDTO, modelTemplate);
    Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>();
    superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", treeModel.getModelName())));
    superInvocationExpressionsMap.put(2, new NameExpr(String.format("\"%s\"", treeModel.getAlgorithmName())));
    MINING_FUNCTION miningFunction = MINING_FUNCTION.byName(treeModel.getMiningFunction().value());
    PMML_MODEL pmmlModel = PMML_MODEL.byName(treeModel.getClass().getSimpleName());
    Map<String, Expression> assignExpressionMap = new HashMap<>();
    assignExpressionMap.put("targetField", new StringLiteralExpr(targetField));
    assignExpressionMap.put("miningFunction", new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name()));
    assignExpressionMap.put("pmmlMODEL", new NameExpr(pmmlModel.getClass().getName() + "." + pmmlModel.name()));
    ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().get();
    assertTrue(commonEvaluateConstructor(constructorDeclaration, getSanitizedClassName(treeModel.getModelName()), superInvocationExpressionsMap, assignExpressionMap));
}
Also used : HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) 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) KiePMMLTreeModel(org.kie.pmml.models.drools.tree.model.KiePMMLTreeModel) TreeModel(org.dmg.pmml.tree.TreeModel) Expression(com.github.javaparser.ast.expr.Expression) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) PMML_MODEL(org.kie.pmml.api.enums.PMML_MODEL) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) Test(org.junit.Test)

Example 7 with MINING_FUNCTION

use of org.kie.pmml.api.enums.MINING_FUNCTION in project drools by kiegroup.

the class KiePMMLTreeModelFactoryTest method setConstructor.

@Test
public void setConstructor() {
    String className = getSanitizedClassName(treeModel1.getModelName());
    CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, PACKAGE_NAME, KIE_PMML_TREE_MODEL_TEMPLATE_JAVA, KIE_PMML_TREE_MODEL_TEMPLATE);
    ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
    String targetField = "whatIdo";
    String fullNodeClassName = "full.Node.ClassName";
    CommonCompilationDTO<TreeModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml1, treeModel1, new HasClassLoaderMock());
    KiePMMLTreeModelFactory.setConstructor(TreeCompilationDTO.fromCompilationDTO(source), modelTemplate, fullNodeClassName);
    ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName()))).clone();
    BlockStmt body = constructorDeclaration.getBody();
    // targetField
    Optional<AssignExpr> optRetrieved = CommonCodegenUtils.getAssignExpression(body, "targetField");
    assertTrue(optRetrieved.isPresent());
    AssignExpr retrieved = optRetrieved.get();
    Expression initializer = retrieved.getValue();
    assertTrue(initializer instanceof StringLiteralExpr);
    String expected = String.format("\"%s\"", targetField);
    assertEquals(expected, initializer.toString());
    // miningFunction
    optRetrieved = CommonCodegenUtils.getAssignExpression(body, "miningFunction");
    assertTrue(optRetrieved.isPresent());
    retrieved = optRetrieved.get();
    initializer = retrieved.getValue();
    assertTrue(initializer instanceof NameExpr);
    MINING_FUNCTION miningFunction = MINING_FUNCTION.byName(treeModel1.getMiningFunction().value());
    expected = miningFunction.getClass().getName() + "." + miningFunction.name();
    assertEquals(expected, initializer.toString());
    // pmmlMODEL
    optRetrieved = CommonCodegenUtils.getAssignExpression(body, "pmmlMODEL");
    assertTrue(optRetrieved.isPresent());
    retrieved = optRetrieved.get();
    initializer = retrieved.getValue();
    assertTrue(initializer instanceof NameExpr);
    expected = PMML_MODEL.TREE_MODEL.getClass().getName() + "." + PMML_MODEL.TREE_MODEL.name();
    assertEquals(expected, initializer.toString());
    // nodeFunction
    optRetrieved = CommonCodegenUtils.getAssignExpression(body, "nodeFunction");
    assertTrue(optRetrieved.isPresent());
    retrieved = optRetrieved.get();
    initializer = retrieved.getValue();
    assertTrue(initializer instanceof MethodReferenceExpr);
    expected = fullNodeClassName;
    assertEquals(expected, ((MethodReferenceExpr) initializer).getScope().toString());
    expected = "evaluateNode";
    assertEquals(expected, ((MethodReferenceExpr) initializer).getIdentifier());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) KiePMMLTreeModel(org.kie.pmml.models.tree.model.KiePMMLTreeModel) TreeModel(org.dmg.pmml.tree.TreeModel) Expression(com.github.javaparser.ast.expr.Expression) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) Test(org.junit.Test)

Example 8 with MINING_FUNCTION

use of org.kie.pmml.api.enums.MINING_FUNCTION in project drools by kiegroup.

the class KiePMMLScorecardModelFactoryTest method setConstructor.

@Test
public void setConstructor() {
    final String targetField = "overallScore";
    final ClassOrInterfaceDeclaration modelTemplate = classOrInterfaceDeclaration.clone();
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    final CommonCompilationDTO<Scorecard> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, scorecardModel, new HasKnowledgeBuilderMock(knowledgeBuilder));
    final DroolsCompilationDTO<Scorecard> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(compilationDTO, new HashMap<>());
    KiePMMLScorecardModelFactory.setConstructor(droolsCompilationDTO, modelTemplate);
    Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>();
    superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", scorecardModel.getModelName())));
    MINING_FUNCTION miningFunction = MINING_FUNCTION.byName(scorecardModel.getMiningFunction().value());
    PMML_MODEL pmmlModel = PMML_MODEL.byName(scorecardModel.getClass().getSimpleName());
    Map<String, Expression> assignExpressionMap = new HashMap<>();
    assignExpressionMap.put("targetField", new StringLiteralExpr(targetField));
    assignExpressionMap.put("miningFunction", new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name()));
    assignExpressionMap.put("pmmlMODEL", new NameExpr(pmmlModel.getClass().getName() + "." + pmmlModel.name()));
    ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().get();
    assertTrue(commonEvaluateConstructor(constructorDeclaration, getSanitizedClassName(scorecardModel.getModelName()), superInvocationExpressionsMap, assignExpressionMap));
}
Also used : HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) 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) Expression(com.github.javaparser.ast.expr.Expression) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Scorecard(org.dmg.pmml.scorecard.Scorecard) PMML_MODEL(org.kie.pmml.api.enums.PMML_MODEL) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) Test(org.junit.Test)

Example 9 with MINING_FUNCTION

use of org.kie.pmml.api.enums.MINING_FUNCTION in project drools by kiegroup.

the class KiePMMLDroolsModelFactoryUtilsTest method setConstructor.

@Test
public void setConstructor() {
    Model model = new TreeModel();
    PMML_MODEL pmmlModel = PMML_MODEL.byName(model.getClass().getSimpleName());
    ConstructorDeclaration constructorDeclaration = MODEL_TEMPLATE.getDefaultConstructor().get();
    SimpleName tableName = new SimpleName("TABLE_NAME");
    String targetField = "TARGET_FIELD";
    MINING_FUNCTION miningFunction = MINING_FUNCTION.CLASSIFICATION;
    String kModulePackageName = getSanitizedPackageName("kModulePackageName");
    KiePMMLDroolsModelFactoryUtils.setConstructor(model, constructorDeclaration, tableName, targetField, miningFunction, kModulePackageName);
    Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>();
    Map<String, Expression> assignExpressionMap = new HashMap<>();
    assignExpressionMap.put("targetField", new StringLiteralExpr(targetField));
    assignExpressionMap.put("miningFunction", new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name()));
    assignExpressionMap.put("pmmlMODEL", new NameExpr(pmmlModel.getClass().getName() + "." + pmmlModel.name()));
    assignExpressionMap.put("kModulePackageName", new StringLiteralExpr(kModulePackageName));
    assertTrue(commonEvaluateConstructor(constructorDeclaration, tableName.asString(), superInvocationExpressionsMap, assignExpressionMap));
}
Also used : HashMap(java.util.HashMap) SimpleName(com.github.javaparser.ast.expr.SimpleName) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) TreeModel(org.dmg.pmml.tree.TreeModel) Expression(com.github.javaparser.ast.expr.Expression) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Model(org.dmg.pmml.Model) TreeModel(org.dmg.pmml.tree.TreeModel) PMML_MODEL(org.kie.pmml.api.enums.PMML_MODEL) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) Test(org.junit.Test)

Example 10 with MINING_FUNCTION

use of org.kie.pmml.api.enums.MINING_FUNCTION 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

MINING_FUNCTION (org.kie.pmml.api.enums.MINING_FUNCTION)10 Expression (com.github.javaparser.ast.expr.Expression)9 NameExpr (com.github.javaparser.ast.expr.NameExpr)9 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)9 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)8 Test (org.junit.Test)7 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)6 HashMap (java.util.HashMap)6 PMML_MODEL (org.kie.pmml.api.enums.PMML_MODEL)6 TreeModel (org.dmg.pmml.tree.TreeModel)4 HasClassLoaderMock (org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock)4 CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)3 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)3 KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)3 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 CommonCodegenUtils.addListPopulationByMethodCallExpr (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.addListPopulationByMethodCallExpr)2