X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Ftimeline%2FTimeline.java;fp=plugins%2Fnet.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Ftimeline%2FTimeline.java;h=5fcb111e78f26c42cfd8c2709f1da32bc732714c;hb=8d10ce10f3f33a16b743e4be08634ac959aa9ca0;hp=9dbc31d4d40d4c0573ca647eb23bfc1388c37223;hpb=73e8cb804627bb71b3ac92796ecf4e29fb848521;p=Mograsim.git diff --git a/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/Timeline.java b/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/Timeline.java index 9dbc31d4..5fcb111e 100644 --- a/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/Timeline.java +++ b/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/Timeline.java @@ -5,7 +5,6 @@ import java.util.List; import java.util.PriorityQueue; import java.util.function.BooleanSupplier; import java.util.function.Consumer; -import java.util.function.LongSupplier; /** * Orders Events by the time they are due to be executed. Can execute Events individually. @@ -16,14 +15,44 @@ import java.util.function.LongSupplier; public class Timeline { private PriorityQueue events; - private LongSupplier time; + private TimeFunction time; private long lastTimeUpdated = 0; private long eventCounter = 0; private final List> eventAddedListener; - public final LongSupplier stepByStepExec = () -> lastTimeUpdated; - public final LongSupplier realTimeExec = () -> System.currentTimeMillis(); + public final TimeFunction stepByStepExec = new TimeFunction() + { + + @Override + public void setTime(long time) + { + lastTimeUpdated = time; + } + + @Override + public long getTime() + { + return lastTimeUpdated; + } + }; + public final TimeFunction realTimeExec = new TimeFunction() + { + private long offset = 0; + + @Override + public void setTime(long time) + { + offset = time; + } + + @Override + public long getTime() + { + return System.currentTimeMillis() - offset; + } + }; + private boolean isWorking; /** * Constructs a Timeline object. Per default the time function is set to step by step execution. @@ -93,6 +122,7 @@ public class Timeline lastTimeUpdated = getSimulationTime(); return ExecutionResult.NOTHING_DONE; } + working(); int checkStop = 0; while (hasNext() && !condition.getAsBoolean()) { @@ -106,8 +136,13 @@ public class Timeline // Don't check after every run checkStop = (checkStop + 1) % 10; if (checkStop == 0 && System.currentTimeMillis() >= stopMillis) + { + notWorking(); + lastTimeUpdated = getSimulationTime(); return ExecutionResult.EXEC_OUT_OF_TIME; + } } + notWorking(); lastTimeUpdated = getSimulationTime(); return hasNext() ? ExecutionResult.EXEC_UNTIL_EMPTY : ExecutionResult.EXEC_UNTIL_CONDITION; } @@ -117,8 +152,9 @@ public class Timeline * * @param time The return value of calling this function is the current simulation time. */ - public void setTimeFunction(LongSupplier time) + public void setTimeFunction(TimeFunction time) { + time.setTime(this.time.getTime()); this.time = time; } @@ -129,7 +165,7 @@ public class Timeline */ public long getSimulationTime() { - return time.getAsLong(); + return isWorking() ? lastTimeUpdated : time.getTime(); } /** @@ -153,7 +189,7 @@ public class Timeline { events.clear(); } - lastTimeUpdated = 0; + lastTimeUpdated = time.getTime(); } /** @@ -189,6 +225,12 @@ public class Timeline eventAddedListener.forEach(l -> l.accept(event)); } + //@formatter:off + private void working() { isWorking = true; } + private void notWorking() { isWorking = false; } + private boolean isWorking() { return isWorking; } + //@formatter:on + private class InnerEvent implements Runnable, Comparable { private final TimelineEventHandler function; @@ -249,8 +291,15 @@ public class Timeline return String.format("Simulation time: %s, Last update: %d, Events: %s", getSimulationTime(), lastTimeUpdated, eventsString); } - public enum ExecutionResult + public static enum ExecutionResult { NOTHING_DONE, EXEC_UNTIL_EMPTY, EXEC_UNTIL_CONDITION, EXEC_OUT_OF_TIME } + + public static interface TimeFunction + { + long getTime(); + + void setTime(long time); + } } \ No newline at end of file