use of org.openksavi.sponge.rule.EventMode in project sponge by softelnet.
the class OrderedRuleAdapterRuntime method runRuleForNonFinalNode.
@Override
protected boolean runRuleForNonFinalNode(TreeNode<NodeValue> node) {
int maxLevel = adapter.getEventCount() - 1;
boolean fired = false;
TreeNode<NodeValue> child;
EventMode eventMode = adapter.getEventModes()[node.getLevel() + 1];
switch(eventMode) {
case FIRST:
// For FIRST mode consider only the first event in the next level.
if (node.hasChildren()) {
if ((child = node.getChildren().get(0)) != null) {
return runRule(child);
}
}
break;
case LAST:
// For LAST mode consider only the last event in the next level.
if (node.hasChildren()) {
if ((child = Iterables.getLast(node.getChildren())) != null) {
return runRule(child);
}
}
break;
case ALL:
// For ALL mode consider all events in the next level.
Iterator<TreeNode<NodeValue>> treeNodeIterator = node.getChildren().listIterator();
while (treeNodeIterator.hasNext()) {
child = treeNodeIterator.next();
if (runRule(child)) {
fired = true;
}
// Remove the child node from the tree because its event has already caused the rule to fire.
if (child.getValue() == null) {
treeNodeIterator.remove();
}
}
break;
case NONE:
// If duration has been triggered and there is no child node and the next level is the leaf level (maxLevel).
if (adapter.isDurationTriggered() && !node.hasChildren() && node.getLevel() + 1 == maxLevel) {
// Add a new node with no event since there should be none.
node.addChild(new TreeNode<>(new NodeValue(null)));
}
// Consider the first (and only) node.
if (node.hasChildren()) {
child = node.getChildren().get(0);
if (child != null) {
return runRule(child);
}
}
break;
default:
throw new SpongeException("Unsupported value: " + eventMode);
}
return fired;
}
use of org.openksavi.sponge.rule.EventMode 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);
}
Aggregations