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);
}
}
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);
}
}
}
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);
}
}
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);
}
}
Aggregations