Search in sources :

Example 6 with SpongeException

use of org.openksavi.sponge.SpongeException 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 7 with SpongeException

use of org.openksavi.sponge.SpongeException 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 8 with SpongeException

use of org.openksavi.sponge.SpongeException in project sponge by softelnet.

the class Py4JTest method startCPython.

protected Pair<Process, String> startCPython(SpongeEngine engine, String script, boolean readOutput) throws Exception {
    ProcessBuilder pb = new ProcessBuilder(engine.getConfigurationManager().getProperty("pythonExecutable"), script);
    Process process = null;
    try {
        process = pb.start();
        logger.debug("{}", pb.environment());
    } catch (Exception e) {
        logger.error(getClass().getSimpleName() + " tests require Python 2.7! Python must have Py4J installed, e.g. pip install py4j.");
        throw e;
    }
    String outputText = null;
    if (readOutput) {
        try (BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader errors = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
            outputText = output.lines().collect(Collectors.joining("\n"));
            logger.info("Python process output:\n{}", outputText);
            String errorsString = errors.lines().collect(Collectors.joining("\n"));
            if (!errorsString.isEmpty()) {
                throw new SpongeException("Python script error: " + errorsString);
            }
        }
    }
    return new ImmutablePair<>(process, outputText);
}
Also used : InputStreamReader(java.io.InputStreamReader) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) SpongeException(org.openksavi.sponge.SpongeException) BufferedReader(java.io.BufferedReader) SpongeException(org.openksavi.sponge.SpongeException)

Example 9 with SpongeException

use of org.openksavi.sponge.SpongeException in project sponge by softelnet.

the class MidiPlugin method connectInputDevice.

/**
 * Sets and updates the input MIDI device in the plugin. If there has already been connected an another input device then replaces it.
 *
 * @param deviceName the input MIDI device name.
 */
public void connectInputDevice(String deviceName) {
    MidiDevice device = getDeviceByName(deviceName);
    if (device == null) {
        throw new SpongeException("MIDI device named '" + deviceName + "' not found");
    }
    setInputDevice(device);
    updateInputDevice();
}
Also used : SpongeException(org.openksavi.sponge.SpongeException) MidiDevice(javax.sound.midi.MidiDevice)

Example 10 with SpongeException

use of org.openksavi.sponge.SpongeException in project sponge by softelnet.

the class MidiSpongeEventReceiver method send.

@Override
public void send(MidiMessage message, long timeStamp) {
    try {
        if (sound) {
            midiPlugin.sound(message);
        }
        MidiMessageEvent<?> event;
        if (message instanceof ShortMessage) {
            event = MidiUtils.createShortMessageEvent(midiPlugin, (ShortMessage) message, timeStamp);
        } else if (message instanceof SysexMessage) {
            event = new MidiSysexMessageEvent(midiPlugin.getMidiSysexMessageEventName(), midiPlugin.getEngine().getDefaultParameters().getEventClonePolicy(), (SysexMessage) message, timeStamp);
        } else if (message instanceof MetaMessage) {
            event = new MidiMetaMessageEvent(midiPlugin.getMidiMetaMessageEventName(), midiPlugin.getEngine().getDefaultParameters().getEventClonePolicy(), (MetaMessage) message, timeStamp);
        } else {
            throw new SpongeException("Unknown MIDI message type: " + message.getClass());
        }
        midiPlugin.getEngine().getOperations().event(event).send();
    } catch (Exception e) {
        logger.error("send", e);
    }
}
Also used : ShortMessage(javax.sound.midi.ShortMessage) SpongeException(org.openksavi.sponge.SpongeException) SysexMessage(javax.sound.midi.SysexMessage) MetaMessage(javax.sound.midi.MetaMessage) MidiMetaMessageEvent(org.openksavi.sponge.midi.event.MidiMetaMessageEvent) SpongeException(org.openksavi.sponge.SpongeException) MidiSysexMessageEvent(org.openksavi.sponge.midi.event.MidiSysexMessageEvent)

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