PMMLRuleMapperFactory { public static String getPMMLRuleMapperSource(final String fullRuleName) { final String packageName = fullRuleName.contains(".") ? fullRuleName.substring(0, fullRuleName.lastIndexOf('.')) : ""; CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(KIE_PMML_RULE_MAPPER_CLASS_NAME, packageName, KIE_PMML_RULE_MAPPER_TEMPLATE_JAVA, KIE_PMML_RULE_MAPPER_CLASS_NAME); ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) cloneCU.getTypes().get(0); FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("model").orElseThrow(() -> new RuntimeException("The template " + KIE_PMML_RULE_MAPPER_TEMPLATE_JAVA + " has been modified.")); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(fullRuleName); ruleNameField.getVariables().get(0).setInitializer(objectCreationExpr); return cloneCU.toString(); } private  PMMLRuleMapperFactory(); static String getPMMLRuleMapperSource(final String fullRuleName); static final String KIE_PMML_RULE_MAPPER_CLASS_NAME; }
@Test public void getPredictionRuleMapperSourceWithoutPackage() { final String fullRuleName = "FULL_RULE_NAME"; String retrieved = PMMLRuleMapperFactory.getPMMLRuleMapperSource(fullRuleName); assertNotNull(retrieved); String expected = String.format("public final static Model model = new %s();", fullRuleName); assertTrue(retrieved.contains(expected)); }
@Test public void getPredictionRuleMapperSourceWithPackage() { final String packageName = "PACKAGE"; final String ruleName = "RULE_NAME"; final String fullRuleName = packageName + "." + ruleName; String retrieved = PMMLRuleMapperFactory.getPMMLRuleMapperSource(fullRuleName); assertNotNull(retrieved); String expected = String.format("package %s;", packageName); assertTrue(retrieved.contains(expected)); expected = String.format("public final static Model model = new %s();", fullRuleName); assertTrue(retrieved.contains(expected)); }