Search in sources :

Example 11 with SpongeException

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;
}
Also used : KClass(kotlin.reflect.KClass) SpongeException(org.openksavi.sponge.SpongeException) GenericProcessorInstanceHolder(org.openksavi.sponge.core.engine.GenericProcessorInstanceHolder)

Example 12 with SpongeException

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);
    }
}
Also used : SpongeException(org.openksavi.sponge.SpongeException) BaseProcessorDefinition(org.openksavi.sponge.core.BaseProcessorDefinition)

Example 13 with SpongeException

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;
}
Also used : SpongeException(org.openksavi.sponge.SpongeException) ProcessorInstanceHolder(org.openksavi.sponge.engine.ProcessorInstanceHolder)

Example 14 with SpongeException

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;
}
Also used : KnowledgeBaseType(org.openksavi.sponge.kb.KnowledgeBaseType) Configuration(org.openksavi.sponge.config.Configuration) DefaultScriptKnowledgeBase(org.openksavi.sponge.core.kb.DefaultScriptKnowledgeBase) SpongeException(org.openksavi.sponge.SpongeException) ArrayList(java.util.ArrayList) FileKnowledgeBaseScript(org.openksavi.sponge.core.kb.FileKnowledgeBaseScript) FileKnowledgeBaseScript(org.openksavi.sponge.core.kb.FileKnowledgeBaseScript) KnowledgeBaseScript(org.openksavi.sponge.kb.KnowledgeBaseScript)

Example 15 with SpongeException

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;
}
Also used : SpongeException(org.openksavi.sponge.SpongeException) Plugin(org.openksavi.sponge.plugin.Plugin)

Aggregations

SpongeException (org.openksavi.sponge.SpongeException)25 SpongeEngine (org.openksavi.sponge.engine.SpongeEngine)4 EventMode (org.openksavi.sponge.rule.EventMode)4 ArrayList (java.util.ArrayList)3 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)3 TreeNode (org.openksavi.sponge.core.util.TreeNode)3 KnowledgeBaseType (org.openksavi.sponge.kb.KnowledgeBaseType)3 StringTokenizer (java.util.StringTokenizer)2 SpongeUtils (org.openksavi.sponge.core.util.SpongeUtils)2 KnowledgeBaseEngineOperations (org.openksavi.sponge.kb.KnowledgeBaseEngineOperations)2 KnowledgeBaseInterpreter (org.openksavi.sponge.kb.KnowledgeBaseInterpreter)2 KnowledgeBaseScript (org.openksavi.sponge.kb.KnowledgeBaseScript)2 Plugin (org.openksavi.sponge.plugin.Plugin)2 GroovyObject (groovy.lang.GroovyObject)1 MetaMethod (groovy.lang.MetaMethod)1 Script (groovy.lang.Script)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1