Search in sources :

Example 6 with EventMode

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

Example 7 with EventMode

use of org.openksavi.sponge.rule.EventMode in project sponge by softelnet.

the class BaseKnowledgeBaseInterpreter method getCustomRuleEventSpec.

/**
 * Resolves event specification "&lt;name&gt; &lt;alias&gt; : &lt;mode&gt;". 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);
}
Also used : Arrays(java.util.Arrays) KnowledgeBaseInterpreter(org.openksavi.sponge.kb.KnowledgeBaseInterpreter) Logger(org.slf4j.Logger) Plugin(org.openksavi.sponge.plugin.Plugin) LoggerFactory(org.slf4j.LoggerFactory) GenericRuleEventSpec(org.openksavi.sponge.core.rule.GenericRuleEventSpec) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) KnowledgeBaseEngineOperations(org.openksavi.sponge.kb.KnowledgeBaseEngineOperations) EventMode(org.openksavi.sponge.rule.EventMode) KnowledgeBaseType(org.openksavi.sponge.kb.KnowledgeBaseType) List(java.util.List) RuleAdapter(org.openksavi.sponge.rule.RuleAdapter) RuleEventSpec(org.openksavi.sponge.rule.RuleEventSpec) StringTokenizer(java.util.StringTokenizer) SpongeException(org.openksavi.sponge.SpongeException) Processor(org.openksavi.sponge.Processor) SpongeUtils(org.openksavi.sponge.core.util.SpongeUtils) EventMode(org.openksavi.sponge.rule.EventMode) SpongeException(org.openksavi.sponge.SpongeException) GenericRuleEventSpec(org.openksavi.sponge.core.rule.GenericRuleEventSpec) SpongeException(org.openksavi.sponge.SpongeException)

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