PMMLRuleMappersFactory { public static String getPMMLRuleMappersSource(final String packageName, final List<String> generatedRuleMappers) { CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(KIE_PMML_RULE_MAPPERS_CLASS_NAME, packageName, KIE_PMML_RULE_MAPPERS_TEMPLATE_JAVA, KIE_PMML_RULE_MAPPERS_CLASS_NAME); ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) cloneCU.getTypes().get(0); FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("pmmlRuleMappers").orElseThrow(() -> new RuntimeException("The template " + KIE_PMML_RULE_MAPPERS_TEMPLATE_JAVA + " has been modified.")); final List<Expression> nodeList = generatedRuleMappers.stream() .map(generatedRuleMapper -> { ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(generatedRuleMapper); return objectCreationExpr; }) .collect(Collectors.toList()); NodeList<Expression> expressions = NodeList.nodeList(nodeList); MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions); ruleNameField.getVariables().get(0).setInitializer(methodCallExpr); return cloneCU.toString(); } private  PMMLRuleMappersFactory(); static String getPMMLRuleMappersSource(final String packageName, final List<String> generatedRuleMappers); static final String KIE_PMML_RULE_MAPPERS_CLASS_NAME; }
@Test public void getPredictionRuleMapperSource() { final String pmmlRuleMapper = "PMMLRuleMapperImpl"; final String packageName = "PACKAGE"; final List<String> generatedRuleMappers = IntStream.range(0, 4).mapToObj(index -> packageName + "." + "subPack" + index + "." + pmmlRuleMapper).collect(Collectors.toList()); String retrieved = PMMLRuleMappersFactory.getPMMLRuleMappersSource(packageName, generatedRuleMappers); assertNotNull(retrieved); String expected = String.format("package %s;", packageName); assertTrue(retrieved.contains(expected)); List<String> mod = generatedRuleMappers.stream().map(gen -> "new " + gen + "()").collect(Collectors.toList()); expected = "Arrays.asList(" + String.join(", ", mod) + ");"; assertTrue(retrieved.contains(expected)); }