1 package era.mi.logic.timeline;
3 import java.util.ArrayList;
5 import java.util.PriorityQueue;
6 import java.util.function.Consumer;
9 * Orders Events by the time they are due to be executed. Can execute Events individually.
10 * @author Fabian Stemmler
15 private PriorityQueue<InnerEvent> events;
16 private long currentTime = 0;
18 private final List<Consumer<TimelineEvent>> eventAddedListener;
20 public Timeline(int initCapacity)
22 events = new PriorityQueue<InnerEvent>(initCapacity, (a, b) -> {
23 long difference = a.getTiming() - b.getTiming();
26 return difference < 0 ? -1 : 1;
29 eventAddedListener = new ArrayList<>();
32 public boolean hasNext()
34 return !events.isEmpty();
37 public void executeNext()
39 InnerEvent first = events.poll();
40 currentTime = first.getTiming();
44 public void executeAll()
51 * Executes all events up to a given simulation timestamp. The simulation
52 * process can be constrained by a real world timestamp.
54 * @param timestamp the simulation timestamp up to which the events will be
56 * @param stopMillis the System.currentTimeMillis() when simulation definitely
58 * @return if it was possible to fulfil the goal in the given real world time.
59 * @author Christian Femers
61 public ExecutionResult executeUpTo(long timestamp, long stopMillis)
65 currentTime = timestamp;
66 return ExecutionResult.NOTHING_DONE;
69 InnerEvent first = events.peek();
70 while (first != null && first.getTiming() <= timestamp)
73 currentTime = first.getTiming();
75 // Don't check after every run
76 checkStop = (checkStop + 1) % 10;
77 if (checkStop == 0 && System.currentTimeMillis() >= stopMillis)
78 return ExecutionResult.RAN_OUT_OF_TIME;
79 first = events.peek();
81 currentTime = timestamp;
82 return ExecutionResult.DONE_IN_TIME;
85 public long getSimulationTime()
90 public long nextEventTime()
95 return events.peek().timing;
104 public void addEventAddedListener(Consumer<TimelineEvent> listener)
106 eventAddedListener.add(listener);
108 public void removeEventAddedListener(Consumer<TimelineEvent> listener)
110 eventAddedListener.remove(listener);
114 * Adds an Event to the {@link Timeline}
115 * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.
116 * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.
118 public void addEvent(TimelineEventHandler function, int relativeTiming)
120 long timing = currentTime + relativeTiming;
121 TimelineEvent event = new TimelineEvent(timing);
122 events.add(new InnerEvent(function, event, timing));
123 eventAddedListener.forEach(l -> l.accept(event));
126 private class InnerEvent
129 private final long timing;
130 private final TimelineEventHandler function;
131 private final TimelineEvent event;
134 * Creates an {@link InnerEvent}
135 * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs
136 * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;
138 InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)
140 this.function = function;
142 this.timing = timing;
145 public long getTiming()
152 function.handle(event);
156 public String toString()
158 return event.toString();
163 public String toString()
165 return "simulation time: " + currentTime + ", " + events.toString();
168 public static long toNanoseconds(long ticks)
170 return ticks; //TODO: Alter this when it has been determined how ticks should relate to real time.
173 public enum ExecutionResult
175 NOTHING_DONE, DONE_IN_TIME, RAN_OUT_OF_TIME