Search in sources :

Example 1 with ExternalException

use of org.kie.pmml.api.exceptions.ExternalException 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 2 with ExternalException

use of org.kie.pmml.api.exceptions.ExternalException in project drools by kiegroup.

the class PMMLRuntimeFactoryInternal method getPMMLFile.

/**
 * Load a <code>File</code> with the given <b>fullFileName</b> from the given
 * <code>InputStream</code>
 * @param fileName <b>full path</b> of file to load
 * @param inputStream
 * @return
 */
private static File getPMMLFile(String fileName, InputStream inputStream) {
    FileOutputStream outputStream = null;
    try {
        File toReturn = File.createTempFile(fileName, null);
        outputStream = new FileOutputStream(toReturn);
        byte[] byteArray = new byte[1024];
        int i;
        while ((i = inputStream.read(byteArray)) > 0) {
            outputStream.write(byteArray, 0, i);
        }
        return toReturn;
    } catch (Exception e) {
        throw new ExternalException(e);
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (Exception e) {
            logger.warn("Failed to close outputStream", e);
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ExternalException(org.kie.pmml.api.exceptions.ExternalException) File(java.io.File) FileUtils.getFile(org.apache.commons.io.FileUtils.getFile) ExternalException(org.kie.pmml.api.exceptions.ExternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 3 with ExternalException

use of org.kie.pmml.api.exceptions.ExternalException in project drools by kiegroup.

the class PMMLRuntimeFactoryInternal method getPMMLFileFromKieContainerByKieBase.

/**
 * Load a <code>File</code> with the given <b>pmmlFileName</b> from the <code>kjar</code> contained in the
 * <code>KieContainer</code> with the given <code>ReleaseId</code>
 * @param pmmlFileName
 * @param kieBase the name of the Kiebase configured inside the <b>kmodule.xml</b> of the loaded <b>kjar</b>
 * @param releaseId
 * @return
 */
private static File getPMMLFileFromKieContainerByKieBase(final String pmmlFileName, final String kieBase, final ReleaseId releaseId) {
    KieContainerImpl kieContainer = (KieContainerImpl) KIE_SERVICES.newKieContainer(releaseId);
    InternalResource internalResource = ((InternalKieModule) (kieContainer).getKieModuleForKBase(kieBase)).getResource(pmmlFileName);
    try (InputStream inputStream = internalResource.getInputStream()) {
        return getPMMLFile(pmmlFileName, inputStream);
    } catch (Exception e) {
        throw new ExternalException(e);
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) InputStream(java.io.InputStream) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) ExternalException(org.kie.pmml.api.exceptions.ExternalException) ExternalException(org.kie.pmml.api.exceptions.ExternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 4 with ExternalException

use of org.kie.pmml.api.exceptions.ExternalException in project drools by kiegroup.

the class PMMLCompilerService method getKiePMMLModelsFromResourceWithSources.

/**
 * @param kbuilderImpl
 * @param resource
 * @return
 */
public static List<KiePMMLModel> getKiePMMLModelsFromResourceWithSources(KnowledgeBuilderImpl kbuilderImpl, Resource resource) {
    PMMLCompiler pmmlCompiler = kbuilderImpl.getCachedOrCreate(PMML_COMPILER_CACHE_KEY, PMMLCompilerService::getCompiler);
    String[] classNamePackageName = getFactoryClassNamePackageName(resource);
    String factoryClassName = classNamePackageName[0];
    String packageName = classNamePackageName[1];
    try {
        final List<KiePMMLModel> toReturn = pmmlCompiler.getKiePMMLModelsWithSources(factoryClassName, packageName, resource.getInputStream(), getFileName(resource.getSourcePath()), new HasKnowledgeBuilderImpl(kbuilderImpl));
        populateWithPMMLRuleMappers(toReturn, resource);
        return toReturn;
    } catch (IOException e) {
        throw new ExternalException("ExternalException", e);
    }
}
Also used : KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) HasKnowledgeBuilderImpl(org.kie.pmml.evaluator.assembler.implementations.HasKnowledgeBuilderImpl) PMMLCompiler(org.kie.pmml.compiler.executor.PMMLCompiler) IOException(java.io.IOException) ExternalException(org.kie.pmml.api.exceptions.ExternalException)

Aggregations

ExternalException (org.kie.pmml.api.exceptions.ExternalException)4 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)3 InputStream (java.io.InputStream)2 KiePMMLModel (org.kie.pmml.commons.model.KiePMMLModel)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)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 FileUtils.getFile (org.apache.commons.io.FileUtils.getFile)1 PMML (org.dmg.pmml.PMML)1 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)1 KieContainerImpl (org.drools.compiler.kie.builder.impl.KieContainerImpl)1 InternalResource (org.drools.core.io.internal.InternalResource)1 KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)1 PACKAGE_CLASS_TEMPLATE (org.kie.pmml.commons.Constants.PACKAGE_CLASS_TEMPLATE)1