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);
25 eventAddedListener = new ArrayList<>();
28 public boolean hasNext()
30 return !events.isEmpty();
33 public void executeNext()
35 InnerEvent first = events.peek();
37 executeUpTo(first.getTiming(), -1);
40 public void executeAll()
47 * Executes all events up to a given simulation timestamp. The simulation process can be constrained by a real world timestamp.
49 * @param timestamp the simulation timestamp up to which the events will be processed
50 * @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop. A value of -1 means no timeout.
51 * @return if it was possible to fulfill the goal in the given real world time.
52 * @author Christian Femers
54 public ExecutionResult executeUpTo(long timestamp, long stopMillis)
58 currentTime = timestamp;
59 return ExecutionResult.NOTHING_DONE;
62 InnerEvent first = events.peek();
63 while (first != null && first.getTiming() <= timestamp)
66 currentTime = first.getTiming();
68 // Don't check after every run
69 checkStop = (checkStop + 1) % 10;
70 if (checkStop == 0 && System.currentTimeMillis() >= stopMillis)
71 return ExecutionResult.RAN_OUT_OF_TIME;
72 first = events.peek();
74 currentTime = timestamp;
75 return ExecutionResult.DONE_IN_TIME;
78 public long getSimulationTime()
83 public long nextEventTime()
87 return events.peek().getTiming();
96 public void addEventAddedListener(Consumer<TimelineEvent> listener)
98 eventAddedListener.add(listener);
101 public void removeEventAddedListener(Consumer<TimelineEvent> listener)
103 eventAddedListener.remove(listener);
107 * Adds an Event to the {@link Timeline}
109 * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.
110 * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.
112 public void addEvent(TimelineEventHandler function, int relativeTiming)
114 long timing = currentTime + relativeTiming;
115 TimelineEvent event = new TimelineEvent(timing);
116 events.add(new InnerEvent(function, event));
117 eventAddedListener.forEach(l -> l.accept(event));
120 private class InnerEvent implements Runnable, Comparable<InnerEvent>
122 private final TimelineEventHandler function;
123 private final TimelineEvent event;
126 * Creates an {@link InnerEvent}
128 * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs
129 * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;
131 InnerEvent(TimelineEventHandler function, TimelineEvent event)
133 this.function = function;
137 public long getTiming()
139 return event.getTiming();
145 function.handle(event);
149 public String toString()
151 return event.toString();
155 public int compareTo(InnerEvent o)
157 long difference = getTiming() - o.getTiming();
160 return difference < 0 ? -1 : 1;
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