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