use of org.openksavi.sponge.SpongeException in project sponge by softelnet.
the class NonScriptKotlinKnowledgeBaseInterpreter method createProcessorInstanceByProcessorClass.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ProcessorInstanceHolder createProcessorInstanceByProcessorClass(KnowledgeBase knowledgeBase, Object processorClass, Class<?> javaClass) {
if (processorClass instanceof KClass) {
// Kotlin-based processor.
KClass kclass = (KClass) processorClass;
Class<?> destJavaClass = JvmClassMappingKt.getJavaClass(kclass);
if (!javaClass.isAssignableFrom(destJavaClass)) {
throw new SpongeException("Unsupported processor specification: " + destJavaClass.getName() + " can't be used as " + javaClass.getName());
}
try {
return new GenericProcessorInstanceHolder((Processor) destJavaClass.newInstance(), KotlinUtils.createProcessorName(kclass), true);
} catch (Throwable e) {
throw SpongeUtils.wrapException(destJavaClass.getName(), e);
}
}
return null;
}
use of org.openksavi.sponge.SpongeException in project sponge by softelnet.
the class DefaultProcessorManager method createProcessorInstance.
@Override
public <T extends Processor> T createProcessorInstance(ProcessorDefinition definition, Class<T> cls) {
Validate.isInstanceOf(BaseProcessorDefinition.class, definition, "Processor definition must be or extend %s", BaseProcessorDefinition.class);
BaseProcessorDefinition baseDefinition = (BaseProcessorDefinition) definition;
if (baseDefinition.isJavaDefined()) {
try {
if (baseDefinition.getProcessorClass() == null) {
throw new SpongeException("No corresponding Java class for processor: " + definition.getName());
}
return (T) baseDefinition.getProcessorClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw SpongeUtils.wrapException(e);
}
} else {
return definition.getKnowledgeBase().getInterpreter().createProcessorInstance(definition.getName(), cls);
}
}
use of org.openksavi.sponge.SpongeException in project sponge by softelnet.
the class DefaultProcessorManager method createProcessorInstanceByProcessorClass.
protected ProcessorInstanceHolder createProcessorInstanceByProcessorClass(KnowledgeBase knowledgeBase, Object processorClass, Class javaClass) {
Validate.notNull(processorClass, "Processor class cannot be null");
ProcessorInstanceHolder result = knowledgeBase.getInterpreter().createProcessorInstanceByProcessorClass(knowledgeBase, processorClass, javaClass);
if (result == null) {
// Try to create an instance using the default (Java-based) knowledge base interpreter.
result = getEngine().getKnowledgeBaseManager().getDefaultKnowledgeBase().getInterpreter().createProcessorInstanceByProcessorClass(knowledgeBase, processorClass, javaClass);
}
if (result == null) {
throw new SpongeException("Unsupported processor class: " + processorClass);
}
return result;
}
use of org.openksavi.sponge.SpongeException in project sponge by softelnet.
the class DefaultKnowledgeBaseManager method createScriptKnowledgeBaseFromConfiguration.
protected DefaultScriptKnowledgeBase createScriptKnowledgeBaseFromConfiguration(String name, String typeCode, Configuration[] fileNodes) {
List<KnowledgeBaseScript> scripts = new ArrayList<>();
for (Configuration fileNode : fileNodes) {
String fileName = fileNode.getValue();
if (StringUtils.isEmpty(fileName)) {
throw new SpongeException("Knowledge base file name must not be empty");
}
scripts.add(new FileKnowledgeBaseScript(fileName, fileNode.getAttribute(CFG_KB_FILE_ATTR_CHARSET, null), fileNode.getBooleanAttribute(CFG_KB_FILE_ATTR_REQUIRED, KnowledgeBaseScript.DEFAULT_REQUIRED)));
}
DefaultScriptKnowledgeBase knowledgeBase;
if (scripts.isEmpty()) {
if (StringUtils.isEmpty(typeCode)) {
throw new SpongeException("Knowledge base type for script knowledge bases with no files must not be empty");
}
knowledgeBase = new DefaultScriptKnowledgeBase(name, getKnowledgeBaseInterpreterFactory(typeCode).getSupportedType());
} else {
KnowledgeBaseType inferredKnowledgeBaseType = inferKnowledgeBaseType(name, scripts);
if (!StringUtils.isEmpty(typeCode) && !inferredKnowledgeBaseType.getTypeCode().equals(typeCode)) {
throw new SpongeException("The inferred knowledge base type '" + inferredKnowledgeBaseType.getTypeCode() + "' is different that the specified '" + typeCode + "'");
}
knowledgeBase = new DefaultScriptKnowledgeBase(name, inferredKnowledgeBaseType);
}
scripts.forEach(script -> knowledgeBase.addScript(script));
return knowledgeBase;
}
use of org.openksavi.sponge.SpongeException in project sponge by softelnet.
the class DefaultPluginManager method createAndConfigurePlugin.
/**
* Creates and configures a plugin.
*
* @param pluginConfig plugin configuration.
* @return a plugin.
*/
public Plugin createAndConfigurePlugin(Configuration pluginConfig) {
String pluginName = pluginConfig.getAttribute(PluginManagerConstants.CFG_PLUGIN_NAME, null);
if (StringUtils.isBlank(pluginName)) {
throw new SpongeException("Plugin should have a name");
}
String className = pluginConfig.getAttribute(PluginManagerConstants.CFG_PLUGIN_CLASS, null);
if (StringUtils.isBlank(className)) {
throw new SpongeException("Plugin configuration should specify a class: " + pluginName);
}
String knowledgeBaseName = pluginConfig.getAttribute(PluginManagerConstants.CFG_PLUGIN_KB_NAME, null);
Plugin plugin = createPluginStub(pluginName, knowledgeBaseName, className);
if (existsPlugin(plugin.getName())) {
throw new SpongeException("Plugin '" + plugin.getName() + "' already exists.");
}
if (plugin.getName() != null && plugin.getName().equals(KnowledgeBaseConstants.VAR_ENGINE_OPERATIONS)) {
throw new SpongeException("Invalid plugin name: " + plugin.getName());
}
plugin.setConfiguration(pluginConfig.getChildConfiguration(PluginManagerConstants.CFG_PLUGIN_CONFIGURATION), true);
return plugin;
}
Aggregations