use of org.openksavi.sponge.SpongeException in project sponge by softelnet.
the class CoreRulesTest method evalEnableRuleWithException.
private static void evalEnableRuleWithException(ScriptKnowledgeBaseInterpreter interpreter, String ruleName) {
SpongeException exception = null;
try {
interpreter.eval("EPS.enable(" + ruleName + ")");
} catch (SpongeException e) {
exception = e;
}
assertNotNull(exception);
}
use of org.openksavi.sponge.SpongeException in project sponge by softelnet.
the class EventsTestTemplate method testRemovingEvent.
public static void testRemovingEvent(KnowledgeBaseType type) {
SpongeEngine engine = ScriptTestUtils.startWithKnowledgeBase(type, "events_removing");
try {
await().pollDelay(3, TimeUnit.SECONDS).atMost(30, TimeUnit.SECONDS).until(() -> engine.getOperations().getVariable(Number.class, "eventCounter").intValue() == engine.getOperations().getVariable(Number.class, "allowNumber").intValue());
TimeUnit.SECONDS.sleep(2);
assertEquals(engine.getOperations().getVariable(Number.class, "allowNumber").intValue(), engine.getOperations().getVariable(Number.class, "eventCounter").intValue());
assertFalse(engine.isError());
} catch (InterruptedException ie) {
throw new SpongeException(ie);
} finally {
engine.shutdown();
}
}
use of org.openksavi.sponge.SpongeException in project sponge by softelnet.
the class TriggersTestTemplate method testHelloWorld.
public static void testHelloWorld(KnowledgeBaseType type) {
SpongeEngine engine = ScriptTestUtils.startWithConfig(type, "hello_world");
try {
TimeUnit.SECONDS.sleep(1);
assertFalse(engine.isError());
} catch (InterruptedException e) {
throw new SpongeException(e);
} finally {
engine.shutdown();
}
}
use of org.openksavi.sponge.SpongeException in project sponge by softelnet.
the class AbstractRuleAdapterRuntime method runRule.
/**
* Attempts to run (fire) this rule for the specified node in the event tree.
*
* @param node event tree node.
* @return {@code true} if this rule has been run (fired) all times it ought to. In that case it will be finished.
*/
protected boolean runRule(TreeNode<NodeValue> node) {
if (node == null) {
return false;
}
if (isLeafLevel(node)) {
// If the leaf of the event tree.
prepareEventAliasMap(node);
if (logger.isDebugEnabled()) {
logger.debug("Running {}. Event tree: {}", adapter.getName(), eventTree);
}
// Running the rule for the calculated event sequence (there may be many such sequences for ALL mode).
adapter.getProcessor().onRun(node.getValue().getEvent());
EventMode eventMode = adapter.getEventMode(getEventIndex(node));
switch(eventMode) {
case FIRST:
return true;
case LAST:
case NONE:
// This line is executed only when a duration has been triggered.
return true;
case ALL:
if (adapter.isDurationTriggered()) {
return true;
} else {
// Mark the node to be removed from the tree because its event has already caused the rule to fire.
node.setValue(null);
// Return false because there could be new suitable event sequences in the future.
return false;
}
default:
throw new SpongeException("Unsupported value: " + eventMode);
}
} else {
// The node is not a leaf of the event tree.
return runRuleForNonFinalNode(node);
}
}
use of org.openksavi.sponge.SpongeException in project sponge by softelnet.
the class AbstractRuleAdapterRuntime method buildEventTree.
/**
* Continues building the event tree for the incoming event starting at the specified node.
*
* @param node event tree node.
* @param event incoming event.
*/
protected void buildEventTree(TreeNode<NodeValue> node, Event event) {
// Check if this event is the first event that starts the rule instance.
boolean isFirstNode = (node == null);
// Create a new node for the incoming event and add to the event tree. This node may be removed later when the event doesn't match.
TreeNode<NodeValue> newNode = new TreeNode<>(new NodeValue(event));
if (isFirstNode) {
// First event that starts the rule.
node = newNode;
// Add the new node to the event tree as root.
eventTree.setRoot(node);
} else {
// Recursively try to continue building the event tree, but only for modes FIRST, LAST and ALL.
node.getChildren().forEach(child -> {
// NONE events are processed in shouldAddToEventTreeForNMode(), not here.
if (adapter.getEventMode(getEventIndex(child)) != EventMode.NONE) {
buildEventTree(child, event);
}
});
// Return if reached the last level.
if (node.getLevel() + 1 >= adapter.getEventCount()) {
return;
}
// Add the new node to the event tree.
node.addChild(newNode);
}
// Should this event be added to the event tree in this place.
boolean rememberEvent = false;
EventMode eventMode = getEventMode(newNode);
if (eventMode != null) {
switch(eventMode) {
case FIRST:
case LAST:
case ALL:
rememberEvent = shouldAddToEventTreeForFlaModes(newNode, event);
break;
case NONE:
Mutable<TreeNode<NodeValue>> newNodeHolder = new MutableObject<>(newNode);
rememberEvent = shouldAddToEventTreeForNMode(node, newNodeHolder, event);
// shouldAddToEventTreeForNMode() may change newNode.
newNode = newNodeHolder.getValue();
break;
default:
throw new SpongeException("Unsupported value: " + eventMode);
}
}
// Remove the node for the incoming event if the event doesn't match.
if (!rememberEvent) {
if (isFirstNode) {
eventTree.setRoot(null);
} else {
node.removeChild(newNode);
}
}
}
Aggregations