use of org.openksavi.sponge.rule.EventMode in project sponge by softelnet.
the class AbstractRuleAdapter method setEventSpecs.
public void setEventSpecs(List<Object> events) {
String[] eventNames = new String[events.size()];
String[] eventAliases = new String[events.size()];
EventMode[] modes = new EventMode[events.size()];
for (int i = 0; i < events.size(); i++) {
RuleEventSpec eventSpec = getKnowledgeBase().getInterpreter().getRuleEventSpec(events.get(i));
eventNames[i] = eventSpec.getEventName();
eventAliases[i] = eventSpec.getEventAlias();
modes[i] = eventSpec.getEventMode();
}
setEventNames(eventNames);
setEventAliases(eventAliases);
setEventModes(modes);
}
use of org.openksavi.sponge.rule.EventMode 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.rule.EventMode 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);
}
}
}
use of org.openksavi.sponge.rule.EventMode in project sponge by softelnet.
the class UnorderedRuleAdapterRuntime method resolveSubNodesToRun.
protected List<TreeNode<NodeValue>> resolveSubNodesToRun(TreeNode<NodeValue> node) {
List<TreeNode<NodeValue>> result = new ArrayList<>();
Map<Integer, TreeNode<NodeValue>> mapFirst = new LinkedHashMap<>();
Map<Integer, TreeNode<NodeValue>> mapLast = new LinkedHashMap<>();
boolean endLoop = false;
Iterator<TreeNode<NodeValue>> treeNodeIterator = node.getChildren().listIterator();
while (!endLoop && treeNodeIterator.hasNext()) {
TreeNode<NodeValue> child = treeNodeIterator.next();
int index = getEventIndex(child);
EventMode eventMode = adapter.getEventModes()[index];
switch(eventMode) {
case FIRST:
if (!mapFirst.containsKey(index)) {
mapFirst.put(index, child);
result.add(child);
}
break;
case LAST:
TreeNode<NodeValue> oldLast = mapLast.get(index);
mapLast.put(index, child);
if (oldLast != null) {
result.remove(oldLast);
}
result.add(child);
break;
case ALL:
result.add(child);
break;
case NONE:
throw new SpongeException(EventMode.NONE + " mode event should not be present in the event tree");
default:
throw new SpongeException("Unsupported value: " + eventMode);
}
}
return result;
}
use of org.openksavi.sponge.rule.EventMode in project sponge by softelnet.
the class OrderedRuleAdapterRuntime method validate.
@Override
public void validate() {
EventMode firstMode = adapter.getEventModes()[0];
if (firstMode != EventMode.FIRST) {
throw adapter.createValidationException("The mode of the first event in the sequence must be " + EventMode.FIRST + ".");
}
EventMode lastMode = adapter.getEventModes()[adapter.getEventModes().length - 1];
if (lastMode == null) {
throw adapter.createValidationException("The mode of the last event in the sequence is not set");
}
if ((lastMode == EventMode.LAST || lastMode == EventMode.NONE) && !adapter.hasDuration()) {
throw adapter.createValidationException("If the mode of the last event in the sequence is " + lastMode + " a duration should be set.");
}
}
Aggregations