Search in sources :

Example 1 with HasSourcesMap

use of org.kie.pmml.commons.model.HasSourcesMap in project drools by kiegroup.

the class PMMLCompilerService method addPMMLRuleMapper.

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);
        }
    }
}
Also used : HasRule(org.kie.pmml.commons.HasRule) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) HasNestedModels(org.kie.pmml.commons.model.HasNestedModels) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap)

Example 2 with HasSourcesMap

use of org.kie.pmml.commons.model.HasSourcesMap in project drools by kiegroup.

the class PMMLCompilerServiceTest method populateWithPMMLRuleMappers.

@Test
public void populateWithPMMLRuleMappers() {
    final List<KiePMMLModel> toPopulate = new ArrayList<>();
    toPopulate.add(new KiePMMLTestingModelWithSources("TEST", "kmodulePackageName", Collections.emptyMap()));
    toPopulate.add(new KiePMMLModelHasRule("TEST", Collections.emptyList()));
    toPopulate.add(new KiePMMLModelHasNestedModelsHasRule("TEST", Collections.emptyList()));
    toPopulate.add(new KiePMMLModelHasNestedModelsHasSourceMap("TEST", Collections.emptyList()));
    toPopulate.forEach(kiePMMLModel -> assertTrue(((HasSourcesMap) kiePMMLModel).getSourcesMap().isEmpty()));
    final File file = new File("foo.pmml");
    final Resource resource = new FileSystemResource(file);
    PMMLCompilerService.populateWithPMMLRuleMappers(toPopulate, resource);
    toPopulate.forEach(kiePmmlModel -> {
        if (kiePmmlModel instanceof HasRule || kiePmmlModel instanceof KiePMMLModelHasNestedModelsHasRule) {
            assertFalse(((HasSourcesMap) kiePmmlModel).getSourcesMap().isEmpty());
            String expected = kiePmmlModel.getKModulePackageName() + "." + KIE_PMML_RULE_MAPPERS_CLASS_NAME;
            assertTrue(((HasSourcesMap) kiePmmlModel).getSourcesMap().containsKey(expected));
            if (kiePmmlModel instanceof HasRule) {
                expected = kiePmmlModel.getKModulePackageName() + "." + KIE_PMML_RULE_MAPPER_CLASS_NAME;
                assertTrue(((HasSourcesMap) kiePmmlModel).getSourcesMap().containsKey(expected));
            }
        } else {
            assertTrue(((HasSourcesMap) kiePmmlModel).getSourcesMap().isEmpty());
        }
    });
}
Also used : KiePMMLTestingModelWithSources(org.kie.pmml.commons.testingutility.KiePMMLTestingModelWithSources) ArrayList(java.util.ArrayList) FileSystemResource(org.drools.core.io.impl.FileSystemResource) Resource(org.kie.api.io.Resource) FileSystemResource(org.drools.core.io.impl.FileSystemResource) HasRule(org.kie.pmml.commons.HasRule) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap) File(java.io.File) Test(org.junit.Test)

Example 3 with HasSourcesMap

use of org.kie.pmml.commons.model.HasSourcesMap in project drools by kiegroup.

the class PMMLCompilerImpl method getKiePMMLModelsWithSources.

@Override
public List<KiePMMLModel> getKiePMMLModelsWithSources(final String factoryClassName, final String packageName, final InputStream inputStream, final String fileName, final HasClassLoader hasClassloader) {
    logger.trace("getModels {} {}", inputStream, hasClassloader);
    try {
        PMML commonPMMLModel = KiePMMLUtil.load(inputStream, fileName);
        Set<String> expectedClasses = commonPMMLModel.getModels().stream().map(model -> {
            String modelPackageName = getSanitizedPackageName(String.format(PACKAGE_CLASS_TEMPLATE, packageName, model.getModelName()));
            return modelPackageName + "." + getSanitizedClassName(model.getModelName());
        }).collect(Collectors.toSet());
        final List<KiePMMLModel> toReturn = getModelsWithSources(packageName, commonPMMLModel, hasClassloader);
        final Set<String> generatedClasses = new HashSet<>();
        Map<String, Boolean> expectedClassModelTypeMap = expectedClasses.stream().collect(Collectors.toMap(expectedClass -> expectedClass, expectedClass -> {
            HasSourcesMap retrieved = getHasSourceMap(toReturn, expectedClass);
            generatedClasses.addAll(retrieved.getSourcesMap().keySet());
            return retrieved.isInterpreted();
        }));
        if (!generatedClasses.containsAll(expectedClasses)) {
            expectedClasses.removeAll(generatedClasses);
            String missingClasses = String.join(", ", expectedClasses);
            throw new KiePMMLException("Expected generated class " + missingClasses + " not found");
        }
        Map<String, String> factorySourceMap = getFactorySourceCode(factoryClassName, packageName, expectedClassModelTypeMap);
        KiePMMLFactoryModel kiePMMLFactoryModel = new KiePMMLFactoryModel(factoryClassName, packageName, factorySourceMap);
        toReturn.add(kiePMMLFactoryModel);
        return toReturn;
    } catch (KiePMMLInternalException e) {
        throw new KiePMMLException("KiePMMLInternalException", e);
    } catch (KiePMMLException e) {
        throw e;
    } catch (Exception e) {
        throw new ExternalException("ExternalException", e);
    }
}
Also used : HasClassLoader(org.kie.pmml.commons.model.HasClassLoader) LoggerFactory(org.slf4j.LoggerFactory) ExternalException(org.kie.pmml.api.exceptions.ExternalException) HashSet(java.util.HashSet) KiePMMLUtil(org.kie.pmml.compiler.commons.utils.KiePMMLUtil) KiePMMLFactoryModel(org.kie.pmml.commons.model.KiePMMLFactoryModel) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) Map(java.util.Map) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) KiePMMLModelUtils.getSanitizedPackageName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedPackageName) KiePMMLModelRetriever.getFromCommonDataAndTransformationDictionaryAndModel(org.kie.pmml.compiler.commons.implementations.KiePMMLModelRetriever.getFromCommonDataAndTransformationDictionaryAndModel) PMML(org.dmg.pmml.PMML) Logger(org.slf4j.Logger) Set(java.util.Set) KiePMMLModelRetriever.getFromCommonDataAndTransformationDictionaryAndModelWithSources(org.kie.pmml.compiler.commons.implementations.KiePMMLModelRetriever.getFromCommonDataAndTransformationDictionaryAndModelWithSources) Collectors(java.util.stream.Collectors) List(java.util.List) KiePMMLFactoryFactory.getFactorySourceCode(org.kie.pmml.compiler.commons.factories.KiePMMLFactoryFactory.getFactorySourceCode) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap) CommonCompilationDTO(org.kie.pmml.compiler.api.dto.CommonCompilationDTO) Optional(java.util.Optional) PACKAGE_CLASS_TEMPLATE(org.kie.pmml.commons.Constants.PACKAGE_CLASS_TEMPLATE) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLModelUtils.getSanitizedClassName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedClassName) InputStream(java.io.InputStream) ExternalException(org.kie.pmml.api.exceptions.ExternalException) ExternalException(org.kie.pmml.api.exceptions.ExternalException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLFactoryModel(org.kie.pmml.commons.model.KiePMMLFactoryModel) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) PMML(org.dmg.pmml.PMML) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap) HashSet(java.util.HashSet)

Example 4 with HasSourcesMap

use of org.kie.pmml.commons.model.HasSourcesMap in project drools by kiegroup.

the class ModelImplementationProvider method getKiePMMLModelWithSourcesCompiled.

/**
 * Method provided only to have <b>drools</b> models working when invoked by a <code>MiningModel</code>
 * Default implementation provided for <b>not-drools</b> models.
 * @param compilationDTO
 * @return
 * @throws KiePMMLInternalException
 */
default KiePMMLModelWithSources getKiePMMLModelWithSourcesCompiled(final CompilationDTO<T> compilationDTO) {
    KiePMMLModelWithSources toReturn = getKiePMMLModelWithSources(compilationDTO);
    final Map<String, String> sourcesMap = ((HasSourcesMap) toReturn).getSourcesMap();
    try {
        compilationDTO.compileAndLoadClass(sourcesMap);
    } catch (Exception e) {
        throw new KiePMMLException(e);
    }
    return toReturn;
}
Also used : KiePMMLModelWithSources(org.kie.pmml.commons.model.KiePMMLModelWithSources) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 5 with HasSourcesMap

use of org.kie.pmml.commons.model.HasSourcesMap in project drools by kiegroup.

the class MiningModelImplementationProviderTest method commonGetKiePMMLModelWithSources.

private void commonGetKiePMMLModelWithSources(String source) throws Exception {
    final PMML pmml = getPMML(source);
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    final MiningModel miningmodel = (MiningModel) pmml.getModels().get(0);
    final CommonCompilationDTO<MiningModel> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, miningmodel, new HasKnowledgeBuilderMock(knowledgeBuilder));
    final KiePMMLMiningModelWithSources retrieved = (KiePMMLMiningModelWithSources) PROVIDER.getKiePMMLModelWithSources(compilationDTO);
    assertNotNull(retrieved);
    commonVerifyIsDeepCloneable(retrieved);
    assertNotNull(retrieved.getNestedModels());
    assertFalse(retrieved.getNestedModels().isEmpty());
    final Map<String, String> sourcesMap = new HashMap<>(retrieved.getSourcesMap());
    assertFalse(sourcesMap.isEmpty());
    try {
        KieMemoryCompiler.compile(sourcesMap, Thread.currentThread().getContextClassLoader());
        fail("Expecting compilation error without nested models sources");
    } catch (Exception e) {
    // Expected
    }
    retrieved.getNestedModels().forEach(nestedModel -> sourcesMap.putAll(((HasSourcesMap) nestedModel).getSourcesMap()));
    try {
        KieMemoryCompiler.compile(sourcesMap, Thread.currentThread().getContextClassLoader());
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : KiePMMLMiningModelWithSources(org.kie.pmml.models.mining.model.KiePMMLMiningModelWithSources) HasKnowledgeBuilderMock(org.kie.pmml.models.mining.compiler.HasKnowledgeBuilderMock) KiePMMLMiningModel(org.kie.pmml.models.mining.model.KiePMMLMiningModel) MiningModel(org.dmg.pmml.mining.MiningModel) HashMap(java.util.HashMap) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PMML(org.dmg.pmml.PMML) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap)

Aggregations

HasSourcesMap (org.kie.pmml.commons.model.HasSourcesMap)5 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)3 KiePMMLModel (org.kie.pmml.commons.model.KiePMMLModel)3 PMML (org.dmg.pmml.PMML)2 KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)2 HasRule (org.kie.pmml.commons.HasRule)2 File (java.io.File)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 MiningModel (org.dmg.pmml.mining.MiningModel)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 FileSystemResource (org.drools.core.io.impl.FileSystemResource)1 Test (org.junit.Test)1