KiePMMLDroolsModelFactoryUtils { public static CompilationUnit getKiePMMLModelCompilationUnit(final DataDictionary dataDictionary, final Model model, final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap, final String packageName, final String javaTemplate, final String modelClassName) { logger.trace("getKiePMMLModelCompilationUnit {} {} {}", dataDictionary, model, packageName); String className = getSanitizedClassName(model.getModelName()); CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, packageName, javaTemplate, modelClassName); ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className) .orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className)); MINING_FUNCTION miningFunction = MINING_FUNCTION.byName(model.getMiningFunction().value()); final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName()))); String targetField = getTargetFieldName(dataDictionary, model).orElse(null); List<KiePMMLOutputField> outputFields = getOutputFields(model); setConstructor(model, constructorDeclaration, modelTemplate.getName(), targetField, miningFunction); addOutputFieldsPopulation(constructorDeclaration.getBody(), outputFields); addFieldTypeMapPopulation(constructorDeclaration.getBody(), fieldTypeMap); final MethodDeclaration getKModulePackageNameMethod = modelTemplate.getMethodsByName(GETKMODULEPACKAGENAME_METHOD).get(0); populateGetKModulePackageName(getKModulePackageNameMethod, packageName); return cloneCU; } private  KiePMMLDroolsModelFactoryUtils(); static CompilationUnit getKiePMMLModelCompilationUnit(final DataDictionary dataDictionary,
                                                                 final Model model,
                                                                 final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap,
                                                                 final String packageName,
                                                                 final String javaTemplate,
                                                                 final String modelClassName);  }
@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"; Model 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"; CompilationUnit retrieved = KiePMMLDroolsModelFactoryUtils.getKiePMMLModelCompilationUnit(dataDictionary, model, fieldTypeMap , packageName, TEMPLATE_SOURCE, TEMPLATE_CLASS_NAME); assertEquals(packageName, 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())); assertTrue(commonEvaluateAssignExpr(constructorDeclaration.getBody(), assignExpressionMap)); int expectedMethodCallExprs = assignExpressionMap.size() + fieldTypeMap.size() + 1; commonEvaluateFieldTypeMap(constructorDeclaration.getBody(), fieldTypeMap, expectedMethodCallExprs); }