Search in sources :

Example 1 with SpongeException

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

Example 2 with SpongeException

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

Example 3 with SpongeException

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

Example 4 with SpongeException

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

Example 5 with SpongeException

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);
        }
    }
}
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)

Aggregations

SpongeException (org.openksavi.sponge.SpongeException)25 SpongeEngine (org.openksavi.sponge.engine.SpongeEngine)4 EventMode (org.openksavi.sponge.rule.EventMode)4 ArrayList (java.util.ArrayList)3 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)3 TreeNode (org.openksavi.sponge.core.util.TreeNode)3 KnowledgeBaseType (org.openksavi.sponge.kb.KnowledgeBaseType)3 StringTokenizer (java.util.StringTokenizer)2 SpongeUtils (org.openksavi.sponge.core.util.SpongeUtils)2 KnowledgeBaseEngineOperations (org.openksavi.sponge.kb.KnowledgeBaseEngineOperations)2 KnowledgeBaseInterpreter (org.openksavi.sponge.kb.KnowledgeBaseInterpreter)2 KnowledgeBaseScript (org.openksavi.sponge.kb.KnowledgeBaseScript)2 Plugin (org.openksavi.sponge.plugin.Plugin)2 GroovyObject (groovy.lang.GroovyObject)1 MetaMethod (groovy.lang.MetaMethod)1 Script (groovy.lang.Script)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1