X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Ftimeline%2FTimeline.java;h=c22e1a4a7db8eaf35495a5f594ff038b568c4963;hb=6c67a9ff8361cd9fc082f40e2676f2c8b5911fe4;hp=8107db9627d1d70034db863a4da738a4cf647a0c;hpb=fb169b120e97337093a8707c62bbc4bb06098a9c;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/timeline/Timeline.java b/era.mi/src/era/mi/logic/timeline/Timeline.java index 8107db96..c22e1a4a 100644 --- a/era.mi/src/era/mi/logic/timeline/Timeline.java +++ b/era.mi/src/era/mi/logic/timeline/Timeline.java @@ -20,13 +20,7 @@ public class Timeline public Timeline(int initCapacity) { - events = new PriorityQueue(initCapacity, (a, b) -> - { - long difference = a.getTiming() - b.getTiming(); - if (difference == 0) - return 0; - return difference < 0 ? -1 : 1; - }); + events = new PriorityQueue(initCapacity); eventAddedListener = new ArrayList<>(); } @@ -38,9 +32,9 @@ public class Timeline public void executeNext() { - InnerEvent first = events.poll(); - currentTime = first.getTiming(); - first.run(); + InnerEvent first = events.peek(); + if (first != null) + executeUpTo(first.getTiming(), -1); } public void executeAll() @@ -53,8 +47,8 @@ public class Timeline * Executes all events up to a given simulation timestamp. The simulation process can be constrained by a real world timestamp. * * @param timestamp the simulation timestamp up to which the events will be processed - * @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop. - * @return if it was possible to fulfil the goal in the given real world time. + * @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop. A value of -1 means no timeout. + * @return if it was possible to fulfill the goal in the given real world time. * @author Christian Femers */ public ExecutionResult executeUpTo(long timestamp, long stopMillis) @@ -90,7 +84,7 @@ public class Timeline { if (!hasNext()) return -1; - return events.peek().timing; + return events.peek().getTiming(); } public void reset() @@ -119,14 +113,12 @@ public class Timeline { long timing = currentTime + relativeTiming; TimelineEvent event = new TimelineEvent(timing); - events.add(new InnerEvent(function, event, timing)); + events.add(new InnerEvent(function, event)); eventAddedListener.forEach(l -> l.accept(event)); } - private class InnerEvent + private class InnerEvent implements Runnable, Comparable { - - final long timing; private final TimelineEventHandler function; private final TimelineEvent event; @@ -136,18 +128,18 @@ public class Timeline * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed; */ - InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing) + InnerEvent(TimelineEventHandler function, TimelineEvent event) { this.function = function; this.event = event; - this.timing = timing; } public long getTiming() { - return timing; + return event.getTiming(); } + @Override public void run() { function.handle(event); @@ -158,6 +150,15 @@ public class Timeline { return event.toString(); } + + @Override + public int compareTo(InnerEvent o) + { + long difference = getTiming() - o.getTiming(); + if (difference == 0) + return 0; + return difference < 0 ? -1 : 1; + } } @Override