Search in sources :

Example 1 with EventMode

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);
}
Also used : EventMode(org.openksavi.sponge.rule.EventMode) RuleEventSpec(org.openksavi.sponge.rule.RuleEventSpec)

Example 2 with EventMode

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);
    }
}
Also used : EventMode(org.openksavi.sponge.rule.EventMode) SpongeException(org.openksavi.sponge.SpongeException)

Example 3 with EventMode

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);
        }
    }
}
Also used : EventMode(org.openksavi.sponge.rule.EventMode) SpongeException(org.openksavi.sponge.SpongeException) TreeNode(org.openksavi.sponge.core.util.TreeNode) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 4 with EventMode

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;
}
Also used : SpongeException(org.openksavi.sponge.SpongeException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) EventMode(org.openksavi.sponge.rule.EventMode) TreeNode(org.openksavi.sponge.core.util.TreeNode)

Example 5 with EventMode

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.");
    }
}
Also used : EventMode(org.openksavi.sponge.rule.EventMode)

Aggregations

EventMode (org.openksavi.sponge.rule.EventMode)7 SpongeException (org.openksavi.sponge.SpongeException)5 TreeNode (org.openksavi.sponge.core.util.TreeNode)3 RuleEventSpec (org.openksavi.sponge.rule.RuleEventSpec)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 Collectors (java.util.stream.Collectors)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Processor (org.openksavi.sponge.Processor)1 GenericRuleEventSpec (org.openksavi.sponge.core.rule.GenericRuleEventSpec)1 SpongeUtils (org.openksavi.sponge.core.util.SpongeUtils)1 KnowledgeBaseEngineOperations (org.openksavi.sponge.kb.KnowledgeBaseEngineOperations)1 KnowledgeBaseInterpreter (org.openksavi.sponge.kb.KnowledgeBaseInterpreter)1 KnowledgeBaseType (org.openksavi.sponge.kb.KnowledgeBaseType)1 Plugin (org.openksavi.sponge.plugin.Plugin)1 RuleAdapter (org.openksavi.sponge.rule.RuleAdapter)1