1 package era.mi.logic.timeline;
3 import java.util.PriorityQueue;
6 * Orders Events by the time they are due to be executed. Can execute Events individually.
7 * @author Fabian Stemmler
12 private PriorityQueue<InnerEvent> events;
13 private long currentTime = 0;
15 public Timeline(int initCapacity)
17 events = new PriorityQueue<InnerEvent>(initCapacity, (a, b) -> {
18 long difference = a.getTiming() - b.getTiming();
21 return difference < 0 ? -1 : 1;
25 public boolean hasNext()
27 return !events.isEmpty();
30 public void executeNext()
32 InnerEvent first = events.poll();
33 currentTime = first.getTiming();
37 public void executeAll()
44 * Executes all events up to a given simulation timestamp. The simulation
45 * process can be constrained by a real world timestamp.
47 * @param timestamp the simulation timestamp up to which the events will be
49 * @param stopMillis the System.currentTimeMillis() when simulation definitely
51 * @return if it was possible to fulfil 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()
90 * Adds an Event to the {@link Timeline}
91 * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.
92 * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.
94 public void addEvent(TimelineEventHandler function, int relativeTiming)
96 long timing = currentTime + relativeTiming;
97 events.add(new InnerEvent(function, new TimelineEvent(timing), timing));
100 private class InnerEvent
103 private final long timing;
104 private final TimelineEventHandler function;
105 private final TimelineEvent event;
108 * Creates an {@link InnerEvent}
109 * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs
110 * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;
112 InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)
114 this.function = function;
116 this.timing = timing;
119 public long getTiming()
126 function.handle(event);
130 public String toString()
132 return event.toString();
137 public String toString()
139 return "simulation time: " + currentTime + ", " + events.toString();
142 public static long toNanoseconds(long ticks)
144 return ticks; //TODO: Alter this when it has been determined how ticks should relate to real time.
147 public enum ExecutionResult
149 NOTHING_DONE, DONE_IN_TIME, RAN_OUT_OF_TIME