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.
11 * @author Fabian Stemmler
16 private PriorityQueue<InnerEvent> events;
17 private long currentTime = 0;
19 private final List<Consumer<TimelineEvent>> eventAddedListener;
21 public Timeline(int initCapacity)
23 events = new PriorityQueue<InnerEvent>(initCapacity, (a, b) ->
25 long difference = a.getTiming() - b.getTiming();
28 return difference < 0 ? -1 : 1;
31 eventAddedListener = new ArrayList<>();
34 public boolean hasNext()
36 return !events.isEmpty();
39 public void executeNext()
41 InnerEvent first = events.poll();
42 currentTime = first.getTiming();
46 public void executeAll()
53 * Executes all events up to a given simulation timestamp. The simulation process can be constrained by a real world timestamp.
55 * @param timestamp the simulation timestamp up to which the events will be processed
56 * @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop.
57 * @return if it was possible to fulfil the goal in the given real world time.
58 * @author Christian Femers
60 public ExecutionResult executeUpTo(long timestamp, long stopMillis)
64 currentTime = timestamp;
65 return ExecutionResult.NOTHING_DONE;
68 InnerEvent first = events.peek();
69 while (first != null && first.getTiming() <= timestamp)
72 currentTime = first.getTiming();
74 // Don't check after every run
75 checkStop = (checkStop + 1) % 10;
76 if (checkStop == 0 && System.currentTimeMillis() >= stopMillis)
77 return ExecutionResult.RAN_OUT_OF_TIME;
78 first = events.peek();
80 currentTime = timestamp;
81 return ExecutionResult.DONE_IN_TIME;
84 public long getSimulationTime()
89 public long nextEventTime()
94 return events.peek().timing;
103 public void addEventAddedListener(Consumer<TimelineEvent> listener)
105 eventAddedListener.add(listener);
108 public void removeEventAddedListener(Consumer<TimelineEvent> listener)
110 eventAddedListener.remove(listener);
114 * Adds an Event to the {@link Timeline}
116 * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.
117 * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.
119 public void addEvent(TimelineEventHandler function, int relativeTiming)
121 long timing = currentTime + relativeTiming;
122 TimelineEvent event = new TimelineEvent(timing);
123 events.add(new InnerEvent(function, event, timing));
124 eventAddedListener.forEach(l -> l.accept(event));
127 private class InnerEvent
130 private final long timing;
131 private final TimelineEventHandler function;
132 private final TimelineEvent event;
135 * Creates an {@link InnerEvent}
137 * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs
138 * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;
140 InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)
142 this.function = function;
144 this.timing = timing;
147 public long getTiming()
154 function.handle(event);
158 public String toString()
160 return event.toString();
165 public String toString()
167 return "simulation time: " + currentTime + ", " + events.toString();
170 public static long toNanoseconds(long ticks)
172 return ticks; // TODO: Alter this when it has been determined how ticks should relate to real time.
175 public enum ExecutionResult
177 NOTHING_DONE, DONE_IN_TIME, RAN_OUT_OF_TIME