summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
6ab8014)
The timeline method executeUpto(timestamp, timeout), has been changed to
executeUntil(condition, timeout). The timeline method setTimeFunction
was added (e.g. to allow for real-time simulation). The timeFunction is
called whenever the timeline needs to find out the current simulation
time.
while (running.get())
{
// always execute to keep timeline from "hanging behind" for too long
while (running.get())
{
// always execute to keep timeline from "hanging behind" for too long
- timeline.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);
+ timeline.executeUntil(timeline.laterThan(System.currentTimeMillis()), System.currentTimeMillis() + 10);
long sleepTime;
if (timeline.hasNext())
sleepTime = timeline.nextEventTime() - System.currentTimeMillis();
long sleepTime;
if (timeline.hasNext())
sleepTime = timeline.nextEventTime() - System.currentTimeMillis();
@Override
public boolean clicked(double x, double y)
{
@Override
public boolean clicked(double x, double y)
{
+ timeline.addEvent((e) -> toggle(), (int) (System.currentTimeMillis() - timeline.getSimulationTime()));
public static void main(String[] args)
{
Timeline t = new Timeline(11);
public static void main(String[] args)
{
Timeline t = new Timeline(11);
+ t.setTimeFunction(() -> System.currentTimeMillis()); // real time simulation
LogicUIStandalone ui = new LogicUIStandalone(t);
addComponentsAndWires(ui.getLogicUICanvas(), t);
ui.run();
LogicUIStandalone ui = new LogicUIStandalone(t);
addComponentsAndWires(ui.getLogicUICanvas(), t);
ui.run();
while (!ui.isDisposed())
{
// always execute to keep timeline from "hanging behind" for too long
while (!ui.isDisposed())
{
// always execute to keep timeline from "hanging behind" for too long
- timeline.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);
+ timeline.executeUntil(timeline.laterThan(System.currentTimeMillis()), System.currentTimeMillis() + 10);
long sleepTime;
if (timeline.hasNext())
sleepTime = timeline.nextEventTime() - System.currentTimeMillis();
long sleepTime;
if (timeline.hasNext())
sleepTime = timeline.nextEventTime() - System.currentTimeMillis();
package era.mi.logic.tests;
package era.mi.logic.tests;
+import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
assertEquals(-1, a.getSignedValue());
}
assertEquals(-1, a.getSignedValue());
}
+ boolean flag = false;
+
+ @Test
+ void simpleTimelineTest()
+ {
+ Timeline t = new Timeline(3);
+ flag = false;
+ t.addEvent((e) ->
+ {
+ if (!flag)
+ fail();
+ flag = false;
+ }, 15);
+ t.addEvent((e) ->
+ {
+ if (flag)
+ fail();
+ flag = true;
+ }, 10);
+ t.addEvent((e) ->
+ {
+ if (flag)
+ fail();
+ flag = true;
+ }, 20);
+ t.addEvent((e) ->
+ {
+ fail("Only supposed to execute until timestamp 20, not 25");
+ }, 25);
+
+ t.executeUntil(t.laterThan(20), 100);
+
+ if (!flag)
+ fail();
+ }
+
@Test
void multipleInputs()
{
@Test
void multipleInputs()
{
- ExecutionResult er = gt.getTimeline().executeUpTo((lastFrame - begin) * 3, lastFrame + 14);
+ ExecutionResult er = gt.getTimeline().executeUntil(gt.getTimeline().laterThan((lastFrame - begin) * 3), lastFrame + 14);
// if (t.hasNext())
// t.executeNext();
if (er != ExecutionResult.NOTHING_DONE)
// if (t.hasNext())
// t.executeNext();
if (er != ExecutionResult.NOTHING_DONE)
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
+import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
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.
/**
* Orders Events by the time they are due to be executed. Can execute Events individually.
public class Timeline
{
private PriorityQueue<InnerEvent> events;
public class Timeline
{
private PriorityQueue<InnerEvent> events;
- private long currentTime = 0;
+ private LongSupplier time;
+ private long lastTimeUpdated = 0;
private final List<Consumer<TimelineEvent>> eventAddedListener;
private final List<Consumer<TimelineEvent>> eventAddedListener;
events = new PriorityQueue<InnerEvent>(initCapacity);
eventAddedListener = new ArrayList<>();
events = new PriorityQueue<InnerEvent>(initCapacity);
eventAddedListener = new ArrayList<>();
+ time = () -> lastTimeUpdated;
+ }
+
+ /**
+ * @param timestamp exclusive
+ * @return true if the first event is later than the timestamp
+ */
+ public BooleanSupplier laterThan(long timestamp)
+ {
+ return () -> timeCmp(events.peek().getTiming(), timestamp) > 0;
}
public boolean hasNext()
}
public boolean hasNext()
return !events.isEmpty();
}
return !events.isEmpty();
}
+ /**
+ * Executes all events at the next timestamp, at which there are any
+ */
public void executeNext()
{
InnerEvent first = events.peek();
if (first != null)
public void executeNext()
{
InnerEvent first = events.peek();
if (first != null)
- executeUpTo(first.getTiming(), -1);
+ executeUntil(laterThan(first.getTiming()), -1);
}
public void executeAll()
}
public void executeAll()
- * Executes all events up to a given simulation timestamp. The simulation process can be constrained by a real world timestamp.
+ * Executes all events until a given condition is met. 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 condition the condition until which the events are be processed
* @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop. A value of -1 means no timeout.
* @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
+ * @return State of the event execution
+ * @formatter:off
+ * <code>NOTHING_DONE</code> if the {@link Timeline} was already empty
+ * <code>EXEC_OUT_OF_TIME</code> if the given maximum time was reached
+ * <code>EXEC_UNTIL_CONDITION</code> if the condition was met
+ * <code>EXEC_UNTIL_EMPTY</code> if events were executed until the {@link Timeline} was empty
+ * @formatter:on
+ * @author Christian Femers, Fabian Stemmler
- public ExecutionResult executeUpTo(long timestamp, long stopMillis)
+ public ExecutionResult executeUntil(BooleanSupplier condition, long stopMillis)
{
if (events.isEmpty())
{
{
if (events.isEmpty())
{
- currentTime = timestamp;
+ lastTimeUpdated = getSimulationTime();
return ExecutionResult.NOTHING_DONE;
}
int checkStop = 0;
InnerEvent first = events.peek();
return ExecutionResult.NOTHING_DONE;
}
int checkStop = 0;
InnerEvent first = events.peek();
- while (first != null && first.getTiming() <= timestamp)
+ while (hasNext() && !condition.getAsBoolean())
- currentTime = first.getTiming();
+ lastTimeUpdated = first.getTiming();
first.run();
// Don't check after every run
checkStop = (checkStop + 1) % 10;
if (checkStop == 0 && System.currentTimeMillis() >= stopMillis)
first.run();
// Don't check after every run
checkStop = (checkStop + 1) % 10;
if (checkStop == 0 && System.currentTimeMillis() >= stopMillis)
- return ExecutionResult.RAN_OUT_OF_TIME;
+ return ExecutionResult.EXEC_OUT_OF_TIME;
- currentTime = timestamp;
- return ExecutionResult.DONE_IN_TIME;
+ lastTimeUpdated = getSimulationTime();
+ return hasNext() ? ExecutionResult.EXEC_UNTIL_EMPTY : ExecutionResult.EXEC_UNTIL_CONDITION;
+ }
+
+ public void setTimeFunction(LongSupplier time)
+ {
+ this.time = time;
}
public long getSimulationTime()
{
}
public long getSimulationTime()
{
+ return time.getAsLong();
}
public long nextEventTime()
}
public long nextEventTime()
public void reset()
{
events.clear();
public void reset()
{
events.clear();
}
public void addEventAddedListener(Consumer<TimelineEvent> listener)
}
public void addEventAddedListener(Consumer<TimelineEvent> listener)
*/
public void addEvent(TimelineEventHandler function, int relativeTiming)
{
*/
public void addEvent(TimelineEventHandler function, int relativeTiming)
{
- long timing = currentTime + relativeTiming;
+ long timing = getSimulationTime() + relativeTiming;
TimelineEvent event = new TimelineEvent(timing);
events.add(new InnerEvent(function, event));
eventAddedListener.forEach(l -> l.accept(event));
TimelineEvent event = new TimelineEvent(timing);
events.add(new InnerEvent(function, event));
eventAddedListener.forEach(l -> l.accept(event));
@Override
public int compareTo(InnerEvent o)
{
@Override
public int compareTo(InnerEvent o)
{
- long difference = getTiming() - o.getTiming();
- if (difference == 0)
- return 0;
- return difference < 0 ? -1 : 1;
+ return timeCmp(getTiming(), o.getTiming());
- @Override
- public String toString()
+ private static int timeCmp(long a, long b)
- return "simulation time: " + currentTime + ", " + events.toString();
+ return Long.signum(a - b);
- public static long toNanoseconds(long ticks)
+ @Override
+ public String toString()
- return ticks; // TODO: Alter this when it has been determined how ticks should relate to real time.
+ return String.format("Simulation time: %s, Last update: %d, Events: %s", getSimulationTime(), lastTimeUpdated, events.toString());
}
public enum ExecutionResult
{
}
public enum ExecutionResult
{
- NOTHING_DONE, DONE_IN_TIME, RAN_OUT_OF_TIME
+ NOTHING_DONE, EXEC_UNTIL_EMPTY, EXEC_UNTIL_CONDITION, EXEC_OUT_OF_TIME
}
}
\ No newline at end of file
}
}
\ No newline at end of file