Added GUITest, ManualSwitch and one method to Timeline
[Mograsim.git] / era.mi / src / era / mi / logic / timeline / Timeline.java
1 package era.mi.logic.timeline;
2
3 import java.util.PriorityQueue;
4
5 /**
6  * Orders Events by the time they are due to be executed. Can execute Events individually.
7  * @author Fabian Stemmler
8  *
9  */
10 public class Timeline
11 {
12         private PriorityQueue<InnerEvent> events;
13         private long currentTime = 0;
14         
15         public Timeline(int initCapacity)
16         {
17                 events = new PriorityQueue<InnerEvent>(initCapacity, (a, b) -> {
18                         long difference = a.getTiming() - b.getTiming();
19                         if(difference == 0)
20                                 return 0;
21                         return difference < 0 ? -1 : 1;
22                 });
23         }
24         
25         public boolean hasNext()
26         {
27                 return !events.isEmpty();
28         }
29
30         public void executeNext()
31         {
32                 InnerEvent first = events.poll();
33                 currentTime = first.getTiming();
34                 first.run();
35         }
36         
37         public void executeAll()
38         {
39                 while (hasNext())
40                         executeNext();
41         }
42         
43         /**
44          * Executes all events up to a given simulation timestamp. The simulation
45          * process can be constrained by a real world timestamp.
46          * 
47          * @param timestamp  the simulation timestamp up to which the events will be
48          *                   processed
49          * @param stopMillis the System.currentTimeMillis() when simulation definitely
50          *                   needs to stop.
51          * @return if it was possible to fulfil the goal in the given real world time.
52          * @author Christian Femers
53          */
54         public ExecutionResult executeUpTo(long timestamp, long stopMillis)
55         {
56                 if (events.isEmpty())
57                 {
58                         currentTime = timestamp;
59                         return ExecutionResult.NOTHING_DONE;
60                 }
61                 int checkStop = 0;
62                 InnerEvent first = events.peek();
63                 while (first != null && first.getTiming() <= timestamp)
64                 {
65                         events.remove();
66                         currentTime = first.getTiming();
67                         first.run();
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();
73                 }
74                 currentTime = timestamp;
75                 return ExecutionResult.DONE_IN_TIME;
76         }
77
78         public long getSimulationTime()
79         {
80                 return currentTime;
81         }
82         
83         public void reset()
84         {
85                 events.clear();
86                 currentTime = 0;
87         }
88         
89         /**
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.
93          */
94         public void addEvent(TimelineEventHandler function, int relativeTiming)
95         {
96                 long timing = currentTime + relativeTiming;
97                 events.add(new InnerEvent(function, new TimelineEvent(timing), timing));
98         }
99         
100         private class InnerEvent
101         {
102
103                 private final long timing;
104                 private final TimelineEventHandler function;
105                 private final TimelineEvent event;
106                 
107                 /**
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;
111                  */
112                 InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)
113                 {
114                         this.function = function;
115                         this.event = event;
116                         this.timing = timing;
117                 }
118
119                 public long getTiming()
120                 {
121                         return timing;
122                 }
123                 
124                 public void run()
125                 {
126                         function.handle(event);
127                 }
128                 
129                 @Override
130                 public String toString()
131                 {
132                         return event.toString();
133                 }
134         }
135         
136         @Override
137         public String toString()
138         {
139                 return "simulation time: " + currentTime + ", " + events.toString();
140         }
141         
142         public static long toNanoseconds(long ticks)
143         {
144                 return ticks; //TODO: Alter this when it has been determined how ticks should relate to real time.
145         }
146         
147         public enum ExecutionResult
148         {
149                 NOTHING_DONE, DONE_IN_TIME, RAN_OUT_OF_TIME 
150         }
151 }