use of org.kie.pmml.api.enums.PMML_MODEL 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));
}
use of org.kie.pmml.api.enums.PMML_MODEL 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);
}
use of org.kie.pmml.api.enums.PMML_MODEL in project drools by kiegroup.
the class KiePMMLDroolsModelFactoryUtils method setConstructor.
/**
* Define the <b>targetField</b>, the <b>miningFunction</b> and the <b>pmmlMODEL</b> inside the constructor
* @param model
* @param constructorDeclaration
* @param tableName
* @param targetField
* @param miningFunction
* @param kModulePackageName
*/
static void setConstructor(final Model model, final ConstructorDeclaration constructorDeclaration, final SimpleName tableName, final String targetField, final MINING_FUNCTION miningFunction, final String kModulePackageName) {
constructorDeclaration.setName(tableName);
final BlockStmt body = constructorDeclaration.getBody();
CommonCodegenUtils.setAssignExpressionValue(body, "targetField", new StringLiteralExpr(targetField));
CommonCodegenUtils.setAssignExpressionValue(body, "miningFunction", new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name()));
PMML_MODEL pmmlModel = PMML_MODEL.byName(model.getClass().getSimpleName());
CommonCodegenUtils.setAssignExpressionValue(body, "pmmlMODEL", new NameExpr(pmmlModel.getClass().getName() + "." + pmmlModel.name()));
CommonCodegenUtils.setAssignExpressionValue(body, "kModulePackageName", new StringLiteralExpr(kModulePackageName));
}
Aggregations