PMMLCompilerService { static void addPMMLRuleMapper(final KiePMMLModel kiePMMLModel, final List<String> generatedRuleMappers, final String sourcePath) { if (!(kiePMMLModel instanceof HasSourcesMap)) { String errorMessage = String.format("Expecting HasSourcesMap instance, retrieved %s inside %s", kiePMMLModel.getClass().getName(), sourcePath); throw new KiePMMLException(errorMessage); } if (kiePMMLModel instanceof HasRule) { String pkgUUID = ((HasRule)kiePMMLModel).getPkgUUID(); String rulesFileName = kiePMMLModel.getKModulePackageName() + "." + RULES_FILE_NAME + pkgUUID; String pmmlRuleMapper = kiePMMLModel.getKModulePackageName() + "." + KIE_PMML_RULE_MAPPER_CLASS_NAME; String ruleMapperSource = PMMLRuleMapperFactory.getPMMLRuleMapperSource(rulesFileName); ((HasRule) kiePMMLModel).addSourceMap(pmmlRuleMapper, ruleMapperSource); generatedRuleMappers.add(pmmlRuleMapper); } if (kiePMMLModel instanceof HasNestedModels) { for (KiePMMLModel nestedModel : ((HasNestedModels) kiePMMLModel).getNestedModels()) { addPMMLRuleMapper(nestedModel, generatedRuleMappers, sourcePath); } } } private  PMMLCompilerService(); static List<KiePMMLModel> getKiePMMLModelsFromResourcesWithConfigurationsFromPlugin(KnowledgeBuilderImpl kbuilderImpl, Collection<ResourceWithConfiguration> resourceWithConfigurations); static List<KiePMMLModel> getKiePMMLModelsCompiledFromResourcesWithConfigurations(KnowledgeBuilderImpl kbuilderImpl, Collection<ResourceWithConfiguration> resourceWithConfigurations); static List<KiePMMLModel> getKiePMMLModelsCompiledFromResource(KnowledgeBuilderImpl kbuilderImpl,
                                                                          Resource resource); static List<KiePMMLModel> getKiePMMLModelsFromResourceFromPlugin(KnowledgeBuilderImpl kbuilderImpl,
                                                                            Resource resource);  }
@Test public void addPMMLRuleMapperHasSourcesMap() { KiePMMLModelHasSourceMap kiePmmlModel = new KiePMMLModelHasSourceMap("TEST", Collections.emptyList()); assertTrue(kiePmmlModel.sourcesMap.isEmpty()); final List<String> generatedRuleMappers = IntStream.range(0, 3).mapToObj(i -> "apackage.Rule_" + i).collect(Collectors.toList()); PMMLCompilerService.addPMMLRuleMapper(kiePmmlModel, generatedRuleMappers, "source_path"); assertTrue(kiePmmlModel.sourcesMap.isEmpty()); }
@Test public void addPMMLRuleMapperHasRule() { KiePMMLModelHasRule kiePmmlModel = new KiePMMLModelHasRule("TEST", Collections.emptyList()); assertTrue(kiePmmlModel.sourcesMap.isEmpty()); final List<String> generatedRuleMappers = new ArrayList<>(); PMMLCompilerService.addPMMLRuleMapper(kiePmmlModel, generatedRuleMappers, "source_path"); String expected = kiePmmlModel.getKModulePackageName() + "." + KIE_PMML_RULE_MAPPER_CLASS_NAME; assertTrue(kiePmmlModel.sourcesMap.containsKey(expected)); expected = kiePmmlModel.getKModulePackageName() + "." + KIE_PMML_RULE_MAPPER_CLASS_NAME; assertTrue(generatedRuleMappers.contains(expected)); }
@Test public void addPMMLRuleMapperKiePMMLModelHasNestedModelsHasRule() { KiePMMLModelHasNestedModelsHasRule kiePmmlModel = new KiePMMLModelHasNestedModelsHasRule("TEST", Collections.emptyList()); assertTrue(kiePmmlModel.sourcesMap.isEmpty()); final List<String> generatedRuleMappers = new ArrayList<>(); PMMLCompilerService.addPMMLRuleMapper(kiePmmlModel, generatedRuleMappers, "source_path"); assertTrue(kiePmmlModel.sourcesMap.isEmpty()); assertEquals(kiePmmlModel.nestedModels.size(), generatedRuleMappers.size()); generatedRuleMappers.forEach(ret -> assertEquals(kiePmmlModel.getKModulePackageName() + "." + KIE_PMML_RULE_MAPPER_CLASS_NAME, ret)); kiePmmlModel.nestedModels.forEach(nestedModel -> { assertTrue(((HasSourcesMap) nestedModel).getSourcesMap().containsKey(nestedModel.getKModulePackageName() + "." +KIE_PMML_RULE_MAPPER_CLASS_NAME)); }); }
@Test public void addPMMLRuleMapperKiePMMLModelHasNestedModelsHasSourceMap() { KiePMMLModelHasNestedModelsHasSourceMap kiePmmlModel = new KiePMMLModelHasNestedModelsHasSourceMap("TEST", Collections.emptyList()); assertTrue(kiePmmlModel.sourcesMap.isEmpty()); final List<String> generatedRuleMappers = new ArrayList<>(); PMMLCompilerService.addPMMLRuleMapper(kiePmmlModel, generatedRuleMappers, "source_path"); assertTrue(kiePmmlModel.sourcesMap.isEmpty()); assertTrue(generatedRuleMappers.isEmpty()); kiePmmlModel.nestedModels.forEach(nestedModel -> { assertTrue(((HasSourcesMap) nestedModel).getSourcesMap().isEmpty()); }); }
@Test(expected = KiePMMLException.class) public void addPMMLRuleMapperNoHasSourceMap() { final KiePMMLModel kiePmmlModel = new KiePMMLModel("name", Collections.emptyList()) { @Override public Object evaluate(Object knowledgeBase, Map<String, Object> requestData) { return null; } }; PMMLCompilerService.addPMMLRuleMapper(kiePmmlModel, new ArrayList<>(), "source_path"); }