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