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