Search in sources :

Example 16 with SpongeException

use of org.openksavi.sponge.SpongeException in project sponge by softelnet.

the class DefaultInteractiveMode method getScriptKnowledgeBaseInterpreter.

protected ScriptKnowledgeBaseInterpreter getScriptKnowledgeBaseInterpreter() {
    KnowledgeBase knowledgeBase = kbName != null ? engine.getKnowledgeBaseManager().getKnowledgeBase(kbName) : engine.getKnowledgeBaseManager().getMainKnowledgeBase();
    KnowledgeBaseInterpreter interpreter = knowledgeBase.getInterpreter();
    if (!(interpreter instanceof ScriptKnowledgeBaseInterpreter)) {
        throw new SpongeException("Knowledge base '" + kbName + "' is not script-based.");
    }
    return (ScriptKnowledgeBaseInterpreter) interpreter;
}
Also used : KnowledgeBase(org.openksavi.sponge.kb.KnowledgeBase) SpongeException(org.openksavi.sponge.SpongeException) ScriptKnowledgeBaseInterpreter(org.openksavi.sponge.kb.ScriptKnowledgeBaseInterpreter) KnowledgeBaseInterpreter(org.openksavi.sponge.kb.KnowledgeBaseInterpreter) ScriptKnowledgeBaseInterpreter(org.openksavi.sponge.kb.ScriptKnowledgeBaseInterpreter)

Example 17 with SpongeException

use of org.openksavi.sponge.SpongeException in project sponge by softelnet.

the class BaseKnowledgeBaseInterpreter method getCustomRuleEventSpec.

/**
 * Resolves event specification "<name> <alias> : <mode>". Uses default value when one not provided.
 *
 * @param eventSpecString event specification.
 * @return rule event specification, i.e. a triple of (name, alias, mode).
 */
protected RuleEventSpec getCustomRuleEventSpec(String eventSpecString) {
    if (eventSpecString == null) {
        throw new SpongeException("Event specification is null");
    }
    List<String> mainList = Arrays.stream(eventSpecString.split(":")).map(s -> s.trim()).filter(s -> !s.isEmpty()).collect(Collectors.toList());
    if (mainList.isEmpty()) {
        throw new SpongeException("Event specification is empty");
    } else if (mainList.size() > 2) {
        throw new SpongeException("Event specification has too many elements separated by ':'");
    }
    ImmutablePair<String, String> nameAlias = resolveEventNameAndAlias(mainList.get(0));
    EventMode eventMode = RuleAdapter.DEFAULT_MODE;
    if (mainList.size() == 2) {
        try {
            eventMode = EventMode.valueOf(mainList.get(1).toUpperCase());
        } catch (Exception e) {
            throw new SpongeException("Event mode is incorrect: " + mainList.get(1));
        }
    }
    return new GenericRuleEventSpec(nameAlias.getLeft(), nameAlias.getRight(), eventMode);
}
Also used : Arrays(java.util.Arrays) KnowledgeBaseInterpreter(org.openksavi.sponge.kb.KnowledgeBaseInterpreter) Logger(org.slf4j.Logger) Plugin(org.openksavi.sponge.plugin.Plugin) LoggerFactory(org.slf4j.LoggerFactory) GenericRuleEventSpec(org.openksavi.sponge.core.rule.GenericRuleEventSpec) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) KnowledgeBaseEngineOperations(org.openksavi.sponge.kb.KnowledgeBaseEngineOperations) EventMode(org.openksavi.sponge.rule.EventMode) KnowledgeBaseType(org.openksavi.sponge.kb.KnowledgeBaseType) List(java.util.List) RuleAdapter(org.openksavi.sponge.rule.RuleAdapter) RuleEventSpec(org.openksavi.sponge.rule.RuleEventSpec) StringTokenizer(java.util.StringTokenizer) SpongeException(org.openksavi.sponge.SpongeException) Processor(org.openksavi.sponge.Processor) SpongeUtils(org.openksavi.sponge.core.util.SpongeUtils) EventMode(org.openksavi.sponge.rule.EventMode) SpongeException(org.openksavi.sponge.SpongeException) GenericRuleEventSpec(org.openksavi.sponge.core.rule.GenericRuleEventSpec) SpongeException(org.openksavi.sponge.SpongeException)

Example 18 with SpongeException

use of org.openksavi.sponge.SpongeException in project sponge by softelnet.

the class BaseKnowledgeBaseInterpreter method resolveEventNameAndAlias.

protected ImmutablePair<String, String> resolveEventNameAndAlias(String eventSpecString) {
    if (eventSpecString == null) {
        throw new SpongeException("Event specification is null");
    }
    if (eventSpecString.trim().length() < 1) {
        throw new SpongeException("Event specification is empty");
    }
    StringTokenizer st = new StringTokenizer(eventSpecString, " \t\n\r\f", false);
    String eventName = st.nextToken();
    String eventAlias = st.hasMoreTokens() ? st.nextToken() : eventName;
    return new ImmutablePair<>(eventName, eventAlias);
}
Also used : StringTokenizer(java.util.StringTokenizer) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) SpongeException(org.openksavi.sponge.SpongeException)

Example 19 with SpongeException

use of org.openksavi.sponge.SpongeException in project sponge by softelnet.

the class EngineScriptKnowledgeBaseInterpreter method resolveVariableName.

public String resolveVariableName(Object value) {
    Bindings bindings = getScriptEngine().getBindings(ScriptContext.ENGINE_SCOPE);
    Iterator<Entry<String, Object>> iterator = bindings.entrySet().stream().filter(entry -> entry.getValue() == value).iterator();
    if (!iterator.hasNext()) {
        throw new SpongeException("Variable for processor " + value + " not found.");
    }
    Map.Entry<String, Object> variableEntry = iterator.next();
    if (iterator.hasNext()) {
        throw new SpongeException("Multiple variables for processor " + value + " have been found.");
    }
    return variableEntry.getKey();
}
Also used : Compilable(javax.script.Compilable) Iterator(java.util.Iterator) Bindings(javax.script.Bindings) Reader(java.io.Reader) ScriptContext(javax.script.ScriptContext) KnowledgeBaseEngineOperations(org.openksavi.sponge.kb.KnowledgeBaseEngineOperations) KnowledgeBaseType(org.openksavi.sponge.kb.KnowledgeBaseType) Invocable(javax.script.Invocable) Map(java.util.Map) SpongeException(org.openksavi.sponge.SpongeException) Entry(java.util.Map.Entry) CompiledScript(javax.script.CompiledScript) ScriptEngine(javax.script.ScriptEngine) SpongeUtils(org.openksavi.sponge.core.util.SpongeUtils) ScriptException(javax.script.ScriptException) Entry(java.util.Map.Entry) SpongeException(org.openksavi.sponge.SpongeException) Bindings(javax.script.Bindings) Map(java.util.Map)

Example 20 with SpongeException

use of org.openksavi.sponge.SpongeException in project sponge by softelnet.

the class GroovyKnowledgeBaseInterpreter method doInvokeFunction.

protected Object doInvokeFunction(String name, boolean optional, Object defaultValue, Object[] args) {
    Object result = null;
    boolean invoked = false;
    for (Script script : scripts) {
        MetaMethod method = script.getMetaClass().getMetaMethod(name, args != null ? args : new Object[0]);
        if (method != null) {
            if (invoked) {
                // scripting languages.
                break;
            }
            result = script.invokeMethod(name, args);
            invoked = true;
        }
    }
    if (!invoked) {
        if (optional) {
            return defaultValue;
        } else {
            throw new SpongeException("Missing function '" + name + "'");
        }
    }
    return result;
}
Also used : MetaMethod(groovy.lang.MetaMethod) Script(groovy.lang.Script) KnowledgeBaseScript(org.openksavi.sponge.kb.KnowledgeBaseScript) SpongeException(org.openksavi.sponge.SpongeException) MutableObject(org.apache.commons.lang3.mutable.MutableObject) GroovyObject(groovy.lang.GroovyObject)

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