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