1 package org.simantics.simulation.sequences.action;
3 import java.util.ArrayList;
4 import java.util.Collections;
6 import java.util.PriorityQueue;
8 import org.simantics.scl.runtime.SCLContext;
9 import org.simantics.scl.runtime.function.Function1;
10 import org.simantics.scl.runtime.tuple.Tuple0;
12 public abstract class AbstractActionContext implements ActionContext {
13 public static final double TIME_TOLERANCE = 1e-6;
16 volatile boolean stopped;
17 ArrayList<Function1<Tuple0, Object>> scheduledNow = new ArrayList<>();
18 ArrayList<Function1<Tuple0, Object>> scheduledNextStep = new ArrayList<>();
19 ArrayList<Function1<StopReason, Object>> scheduledWhenStopped = new ArrayList<>();
20 PriorityQueue<Task> scheduledAt = new PriorityQueue<>();
22 public List<Exception> exceptions;
24 private static class Task implements Comparable<Task> {
26 final Function1<Tuple0, Object> continuation;
28 public Task(double time, Function1<Tuple0, Object> continuation) {
30 this.continuation = continuation;
34 public int compareTo(Task o) {
35 return Double.compare(time, o.time);
40 public double time() {
45 public void scheduleNow(Function1<Tuple0, Object> continuation) {
46 scheduledNow.add(continuation);
50 public void scheduleNextStep(Function1<Tuple0, Object> continuation) {
51 scheduledNextStep.add(continuation);
55 public void scheduleAt(double time, Function1<Tuple0, Object> continuation) {
56 if(time <= currentTime)
57 scheduleNow(continuation);
59 scheduledAt.add(new Task(time, continuation));
63 public void scheduleWhenStopped(Function1<StopReason, Object> continuation) {
64 scheduledWhenStopped.add(continuation);
69 stop(StopReason.STOPPED);
72 public void stop(StopReason reason) {
77 public boolean isStopped() {
79 return stopped || (scheduledNextStep.isEmpty() && scheduledAt.isEmpty());
83 public double handleStep(double currentTime) {
85 this.currentTime = currentTime;
87 ArrayList<Function1<Tuple0, Object>> temp = scheduledNow;
88 scheduledNow = scheduledNextStep;
89 scheduledNextStep = temp;
90 Collections.reverse(scheduledNow);
93 SCLContext context = SCLContext.getCurrent();
94 Object oldActionContext = context.put("sequenceAction", this);
96 Task firstTask = scheduledAt.peek();
98 while(!scheduledNow.isEmpty()) {
100 Function1<Tuple0, Object> currentContinuation = scheduledNow.remove(scheduledNow.size()-1);
101 currentContinuation.apply(Tuple0.INSTANCE);
102 currentContinuation = null;
103 } catch (Exception e) {
104 if (this.exceptions == null)
105 this.exceptions = new ArrayList<>();
106 this.exceptions.add(new RuntimeException("Action failure at " + currentTime + ": " + e.getMessage(), e));
109 if(firstTask == null)
110 return Double.POSITIVE_INFINITY;
111 else if(firstTask.time > currentTime+TIME_TOLERANCE)
112 return firstTask.time;
114 firstTask.continuation.apply(Tuple0.INSTANCE);
115 synchronized (this) {
116 scheduledAt.remove();
118 firstTask = scheduledAt.peek();
122 context.put("sequenceAction", oldActionContext);
127 private void handleStop(StopReason reason) {
128 synchronized (this) {
129 List<Function1<StopReason, Object>> stopFunctions = new ArrayList<>(scheduledWhenStopped);
130 scheduledWhenStopped.clear();
132 SCLContext context = SCLContext.getCurrent();
133 Object oldActionContext = context.put("sequenceAction", this);
135 stopFunctions.forEach(f -> {
138 } catch (Exception e) {
139 if (this.exceptions == null)
140 this.exceptions = new ArrayList<>();
141 this.exceptions.add(new RuntimeException("Stop action failure at " + currentTime + ": " + e.getMessage(), e));
145 context.put("sequenceAction", oldActionContext);