1 package net.mograsim.logic.core.timeline;
\r
3 import java.util.ArrayList;
\r
4 import java.util.List;
\r
5 import java.util.PriorityQueue;
\r
6 import java.util.function.BooleanSupplier;
\r
7 import java.util.function.Consumer;
\r
8 import java.util.function.LongSupplier;
\r
11 * Orders Events by the time they are due to be executed. Can execute Events individually.
\r
13 * @author Fabian Stemmler
\r
16 public class Timeline
\r
18 private PriorityQueue<InnerEvent> events;
\r
19 private LongSupplier time;
\r
20 private long lastTimeUpdated = 0;
\r
22 private final List<Consumer<TimelineEvent>> eventAddedListener;
\r
24 public final LongSupplier stepByStepExec = () -> lastTimeUpdated;
\r
25 public final LongSupplier realTimeExec = () -> System.currentTimeMillis();
\r
28 * Constructs a Timeline object. Per default the time function is set to step by step execution.
\r
30 * @param initCapacity The initial capacity of the event queue.
\r
32 public Timeline(int initCapacity)
\r
34 events = new PriorityQueue<>(initCapacity);
\r
35 eventAddedListener = new ArrayList<>();
\r
36 time = stepByStepExec;
\r
40 * @param timestamp exclusive
\r
41 * @return <code>true</code> if the first event in queue is later than the given timestamp
\r
43 public BooleanSupplier laterThan(long timestamp)
\r
45 return () -> timeCmp(events.peek().getTiming(), timestamp) > 0;
\r
49 * @return <code>true</code> if there is at least one event enqueued. <code>false</code> otherwise
\r
51 public boolean hasNext()
\r
53 return !events.isEmpty();
\r
57 * Executes all events at the next timestamp, at which there are any
\r
59 public void executeNext()
\r
61 InnerEvent first = events.peek();
\r
63 executeUntil(laterThan(first.getTiming()), -1);
\r
67 * Executes all events enqueued in the {@link Timeline}. Use very carefully! Events may generate new events, causing an infinite loop.
\r
69 public void executeAll()
\r
76 * Executes all events until a given condition is met. The simulation process can be constrained by a real world timestamp.
\r
78 * @param condition the condition until which the events are be processed
\r
79 * @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop. A value of -1 means no timeout.
\r
80 * @return State of the event execution
\r
82 * <code>NOTHING_DONE</code> if the {@link Timeline} was already empty
\r
83 * <code>EXEC_OUT_OF_TIME</code> if the given maximum time was reached
\r
84 * <code>EXEC_UNTIL_CONDITION</code> if the condition was met
\r
85 * <code>EXEC_UNTIL_EMPTY</code> if events were executed until the {@link Timeline} was empty
\r
88 public ExecutionResult executeUntil(BooleanSupplier condition, long stopMillis)
\r
90 if (events.isEmpty())
\r
92 lastTimeUpdated = getSimulationTime();
\r
93 return ExecutionResult.NOTHING_DONE;
\r
96 InnerEvent first = events.peek();
\r
97 while (hasNext() && !condition.getAsBoolean())
\r
100 lastTimeUpdated = first.getTiming();
\r
102 // Don't check after every run
\r
103 checkStop = (checkStop + 1) % 10;
\r
104 if (checkStop == 0 && System.currentTimeMillis() >= stopMillis)
\r
105 return ExecutionResult.EXEC_OUT_OF_TIME;
\r
106 first = events.peek();
\r
108 lastTimeUpdated = getSimulationTime();
\r
109 return hasNext() ? ExecutionResult.EXEC_UNTIL_EMPTY : ExecutionResult.EXEC_UNTIL_CONDITION;
\r
113 * Sets the function, which defines the current simulation time at any time.
\r
115 * @param time The return value of calling this function is the current simulation time.
\r
117 public void setTimeFunction(LongSupplier time)
\r
123 * Calculates the current simulation time.
\r
125 * @return The simulation time as defined by the time function.
\r
127 public long getSimulationTime()
\r
129 return time.getAsLong();
\r
133 * Retrieves the timestamp of the next event.
\r
135 * @return The timestamp of the next enqueued event, if the {@link Timeline} is not empty, -1 otherwise.
\r
137 public long nextEventTime()
\r
141 return events.peek().getTiming();
\r
145 * Clears the {@link Timeline} of enqueued events.
\r
147 public void reset()
\r
150 lastTimeUpdated = 0;
\r
154 * Adds a listener, that is called when a {@link TimelineEvent} is added.
\r
156 public void addEventAddedListener(Consumer<TimelineEvent> listener)
\r
158 eventAddedListener.add(listener);
\r
162 * Removes the listener, if possible. It will no longer be called when a {@link TimelineEvent} is added.
\r
164 public void removeEventAddedListener(Consumer<TimelineEvent> listener)
\r
166 eventAddedListener.remove(listener);
\r
170 * Adds an Event to the {@link Timeline}
\r
172 * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.
\r
173 * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.
\r
175 public void addEvent(TimelineEventHandler function, int relativeTiming)
\r
177 long timing = getSimulationTime() + relativeTiming;
\r
178 TimelineEvent event = new TimelineEvent(timing);
\r
179 events.add(new InnerEvent(function, event));
\r
180 eventAddedListener.forEach(l -> l.accept(event));
\r
183 private class InnerEvent implements Runnable, Comparable<InnerEvent>
\r
185 private final TimelineEventHandler function;
\r
186 private final TimelineEvent event;
\r
189 * Creates an {@link InnerEvent}
\r
191 * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs
\r
192 * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;
\r
194 InnerEvent(TimelineEventHandler function, TimelineEvent event)
\r
196 this.function = function;
\r
197 this.event = event;
\r
200 public long getTiming()
\r
202 return event.getTiming();
\r
208 function.handle(event);
\r
212 public String toString()
\r
214 return event.toString();
\r
218 public int compareTo(InnerEvent o)
\r
220 return timeCmp(getTiming(), o.getTiming());
\r
224 public static int timeCmp(long a, long b)
\r
226 return Long.signum(a - b);
\r
230 public String toString()
\r
232 return String.format("Simulation time: %s, Last update: %d, Events: %s", getSimulationTime(), lastTimeUpdated, events.toString());
\r
235 public enum ExecutionResult
\r
237 NOTHING_DONE, EXEC_UNTIL_EMPTY, EXEC_UNTIL_CONDITION, EXEC_OUT_OF_TIME
\r