1 package era.mi.logic.timeline;
3 import java.util.ArrayList;
5 import java.util.PriorityQueue;
6 import java.util.function.BooleanSupplier;
7 import java.util.function.Consumer;
8 import java.util.function.LongSupplier;
11 * Orders Events by the time they are due to be executed. Can execute Events individually.
13 * @author Fabian Stemmler
18 private PriorityQueue<InnerEvent> events;
19 private LongSupplier time;
20 private long lastTimeUpdated = 0;
22 private final List<Consumer<TimelineEvent>> eventAddedListener;
24 public Timeline(int initCapacity)
26 events = new PriorityQueue<InnerEvent>(initCapacity);
28 eventAddedListener = new ArrayList<>();
29 time = () -> lastTimeUpdated;
33 * @param timestamp exclusive
34 * @return true if the first event is later than the timestamp
36 public BooleanSupplier laterThan(long timestamp)
38 return () -> timeCmp(events.peek().getTiming(), timestamp) > 0;
41 public boolean hasNext()
43 return !events.isEmpty();
47 * Executes all events at the next timestamp, at which there are any
49 public void executeNext()
51 InnerEvent first = events.peek();
53 executeUntil(laterThan(first.getTiming()), -1);
56 public void executeAll()
63 * Executes all events until a given condition is met. The simulation process can be constrained by a real world timestamp.
65 * @param condition the condition until which the events are be processed
66 * @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop. A value of -1 means no timeout.
67 * @return State of the event execution
69 * <code>NOTHING_DONE</code> if the {@link Timeline} was already empty
70 * <code>EXEC_OUT_OF_TIME</code> if the given maximum time was reached
71 * <code>EXEC_UNTIL_CONDITION</code> if the condition was met
72 * <code>EXEC_UNTIL_EMPTY</code> if events were executed until the {@link Timeline} was empty
74 * @author Christian Femers, Fabian Stemmler
76 public ExecutionResult executeUntil(BooleanSupplier condition, long stopMillis)
80 lastTimeUpdated = getSimulationTime();
81 return ExecutionResult.NOTHING_DONE;
84 InnerEvent first = events.peek();
85 while (hasNext() && !condition.getAsBoolean())
88 lastTimeUpdated = first.getTiming();
90 // Don't check after every run
91 checkStop = (checkStop + 1) % 10;
92 if (checkStop == 0 && System.currentTimeMillis() >= stopMillis)
93 return ExecutionResult.EXEC_OUT_OF_TIME;
94 first = events.peek();
96 lastTimeUpdated = getSimulationTime();
97 return hasNext() ? ExecutionResult.EXEC_UNTIL_EMPTY : ExecutionResult.EXEC_UNTIL_CONDITION;
100 public void setTimeFunction(LongSupplier time)
105 public long getSimulationTime()
107 return time.getAsLong();
110 public long nextEventTime()
114 return events.peek().getTiming();
123 public void addEventAddedListener(Consumer<TimelineEvent> listener)
125 eventAddedListener.add(listener);
128 public void removeEventAddedListener(Consumer<TimelineEvent> listener)
130 eventAddedListener.remove(listener);
134 * Adds an Event to the {@link Timeline}
136 * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.
137 * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.
139 public void addEvent(TimelineEventHandler function, int relativeTiming)
141 long timing = getSimulationTime() + relativeTiming;
142 TimelineEvent event = new TimelineEvent(timing);
143 events.add(new InnerEvent(function, event));
144 eventAddedListener.forEach(l -> l.accept(event));
147 private class InnerEvent implements Runnable, Comparable<InnerEvent>
149 private final TimelineEventHandler function;
150 private final TimelineEvent event;
153 * Creates an {@link InnerEvent}
155 * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs
156 * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;
158 InnerEvent(TimelineEventHandler function, TimelineEvent event)
160 this.function = function;
164 public long getTiming()
166 return event.getTiming();
172 function.handle(event);
176 public String toString()
178 return event.toString();
182 public int compareTo(InnerEvent o)
184 return timeCmp(getTiming(), o.getTiming());
188 private static int timeCmp(long a, long b)
190 return Long.signum(a - b);
194 public String toString()
196 return String.format("Simulation time: %s, Last update: %d, Events: %s", getSimulationTime(), lastTimeUpdated, events.toString());
199 public enum ExecutionResult
201 NOTHING_DONE, EXEC_UNTIL_EMPTY, EXEC_UNTIL_CONDITION, EXEC_OUT_OF_TIME