Added era.mi; Project containing provisional simulator core
[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                         //Is this really necessary? If only ints are allowed as relative timing, the difference should always be an int
19                         long difference = a.getTiming() - b.getTiming();
20                         if(difference == 0)
21                                 return 0;
22                         return difference < 0 ? -1 : 1;
23                 });
24         }
25         
26         public boolean hasNext()
27         {
28                 return !events.isEmpty();
29         }
30
31         public void executeNext()
32         {
33                 InnerEvent first = events.poll();
34                 currentTime = first.getTiming();
35                 first.run();
36         }
37
38         public long getSimulationTime()
39         {
40                 return currentTime;
41         }
42         
43         public void reset()
44         {
45                 events.clear();
46                 currentTime = 0;
47         }
48         
49         /**
50          * Adds an Event to the {@link Timeline}
51          * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.
52          * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.
53          */
54         public void addEvent(TimelineEventHandler function, int relativeTiming)
55         {
56                 long timing = currentTime + relativeTiming;
57                 events.add(new InnerEvent(function, new TimelineEvent(timing), timing));
58         }
59         
60         private class InnerEvent
61         {
62
63                 private final long timing;
64                 private final TimelineEventHandler function;
65                 private final TimelineEvent event;
66                 
67                 /**
68                  * Creates an {@link InnerEvent}
69                  * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs
70                  * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;
71                  */
72                 InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)
73                 {
74                         this.function = function;
75                         this.event = event;
76                         this.timing = timing;
77                 }
78
79                 public long getTiming()
80                 {
81                         return timing;
82                 }
83                 
84                 public void run()
85                 {
86                         function.handle(event);
87                 }
88                 
89         }
90 }