X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Ftimeline%2FTimeline.java;h=12cf5bd15dca6796789cc8f8e92f782ea4a9d3b7;hb=HEAD;hp=9dbc31d4d40d4c0573ca647eb23bfc1388c37223;hpb=58babf45ae7d259a296656451d796dbe601377a4;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..12cf5bd1 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 long lastTimeUpdated = 0; + private TimeFunction time; + private long processedUntil = 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) + { + processedUntil = time; + } + + @Override + public long getTime() + { + return processedUntil; + } + }; + 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. @@ -90,9 +119,10 @@ public class Timeline { if (events.isEmpty()) { - lastTimeUpdated = getSimulationTime(); + processedUntil = getSimulationTime(); return ExecutionResult.NOTHING_DONE; } + working(); int checkStop = 0; while (hasNext() && !condition.getAsBoolean()) { @@ -101,14 +131,18 @@ public class Timeline { event = events.remove(); } - lastTimeUpdated = event.getTiming(); + processedUntil = event.getTiming(); event.run(); // Don't check after every run checkStop = (checkStop + 1) % 10; if (checkStop == 0 && System.currentTimeMillis() >= stopMillis) + { + notWorking(); return ExecutionResult.EXEC_OUT_OF_TIME; + } } - lastTimeUpdated = getSimulationTime(); + notWorking(); + processedUntil = getSimulationTime(); return hasNext() ? ExecutionResult.EXEC_UNTIL_EMPTY : ExecutionResult.EXEC_UNTIL_CONDITION; } @@ -117,8 +151,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 +164,7 @@ public class Timeline */ public long getSimulationTime() { - return time.getAsLong(); + return isWorking() ? processedUntil : time.getTime(); } /** @@ -153,7 +188,7 @@ public class Timeline { events.clear(); } - lastTimeUpdated = 0; + processedUntil = time.getTime(); } /** @@ -189,6 +224,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; @@ -246,11 +287,26 @@ public class Timeline { eventsString = events.toString(); } - return String.format("Simulation time: %s, Last update: %d, Events: %s", getSimulationTime(), lastTimeUpdated, eventsString); + return String.format("Simulation time: %s, Last update: %d, Events: %s", getSimulationTime(), processedUntil, 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); + } + + /** + * Sets the time of the {@link TimeFunction} to the timestamp of the latest processed event + */ + public void synchTime() + { + time.setTime(processedUntil); + } } \ No newline at end of file