1 package org.simantics.simulator.toolkit;
3 import org.eclipse.core.runtime.NullProgressMonitor;
4 import org.simantics.scl.runtime.SCLContext;
5 import org.simantics.scl.runtime.function.Function;
6 import org.simantics.simulator.IDynamicExperimentLocal;
9 * @author Antti Villberg
12 public class DynamicExperimentThreadSequenceRunner {
13 @SuppressWarnings({ "rawtypes", "unchecked" })
14 public static DynamicExperimentActionContext runAction(IDynamicExperimentLocal experiment, DynamicExperimentThread thread, Function action, final boolean simulateAndWaitCompletion) {
15 final DynamicExperimentActionContext context = new DynamicExperimentActionContext(experiment);
16 context.scheduleNextStep(action);
17 final Object sync = new Object();
18 final SCLContext sclContext = SCLContext.getCurrent();
20 thread.addListener(new DynamicExperimentThreadListener() {
23 public void beforeStep() {
24 if(!context.isStopped()) {
25 SCLContext.push(sclContext);
26 context.handleStep(experiment.getSimulationTime());
32 public void removeIfStopped() {
33 if(context.isStopped()) {
34 thread.removeListener(this);
35 if(simulateAndWaitCompletion) {
36 experiment.simulate(false);
41 experiment.shutdown(new NullProgressMonitor());
47 if(simulateAndWaitCompletion) {
48 experiment.simulate(true);
52 while(!context.isStopped())
55 } catch(InterruptedException e) {
59 if (context.exceptions != null && !context.exceptions.isEmpty()) {
60 StringBuilder builder = new StringBuilder();
61 builder.append("Action failures:");
62 for (Exception e : context.exceptions) {
64 builder.append(e.getMessage());
67 throw new RuntimeException(builder.toString());