]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulator.toolkit/src/org/simantics/simulator/toolkit/DynamicExperimentThreadSequenceRunner.java
Remove unnecessary System.out print from DynamicExperimentThread
[simantics/platform.git] / bundles / org.simantics.simulator.toolkit / src / org / simantics / simulator / toolkit / DynamicExperimentThreadSequenceRunner.java
1 package org.simantics.simulator.toolkit;
2
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;
7
8 /**
9  * @author Antti Villberg
10  * @since 1.34.0
11  */
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();
19
20         thread.addListener(new DynamicExperimentThreadListener() {
21
22             @Override
23             public void beforeStep() {
24                 if(!context.isStopped()) {
25                     SCLContext.push(sclContext);
26                     context.handleStep(experiment.getSimulationTime());
27                     SCLContext.pop();
28                 }
29                 removeIfStopped();
30             }
31
32             public void removeIfStopped() {
33                 if(context.isStopped()) {
34                     thread.removeListener(this);
35                     if(simulateAndWaitCompletion) {
36                         experiment.simulate(false);
37                         synchronized(sync) {
38                             sync.notify();
39                         }
40                     }
41                     experiment.shutdown(new NullProgressMonitor());
42                 }
43             }
44
45         });
46
47         if(simulateAndWaitCompletion) {
48             experiment.simulate(true);
49
50             try {
51                 synchronized(sync) {
52                     while(!context.isStopped())
53                         sync.wait(1000L);
54                 }
55             } catch(InterruptedException e) {
56                 context.stop();
57             }
58
59             if (context.exceptions != null && !context.exceptions.isEmpty()) {
60                 StringBuilder builder = new StringBuilder();
61                 builder.append("Action failures:");
62                 for (Exception e : context.exceptions) {
63                     builder.append("\n");
64                     builder.append(e.getMessage());
65                 }
66
67                 throw new RuntimeException(builder.toString());
68             }
69         }
70         return context;
71     }
72 }