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