@Override public void handleEvent(Event e) { manager.checkExceptions(); LOGGER.debug("new parcel -> switch to real time"); clock.switchToRealTime(); } }, PDPModelEventType.NEW_PARCEL);
void checkRealtime() { if (clock != null) { checkState(clock.getClockMode() == ClockMode.REAL_TIME, "Clock must be in real-time mode, but is in %s mode.", clock.getClockMode()); // make sure we stay in rt LOGGER.debug("Check real time -> stay in real time."); clock.switchToRealTime(); } }
clock.switchToRealTime();
void doHandleEvent(Event e) { synchronized (computingSimSolvers) { final boolean isComputingBefore = isComputing(); LOGGER.trace("receive: {}, computing: {}, clock is ticking: {}, {}", e, isComputingBefore, clock.isTicking(), computingSimSolvers); if (e.getEventType() == EventType.START_COMPUTING) { if (!isComputingBefore) { LOGGER.debug("start computing -> switch to real time"); clock.switchToRealTime(); } computingSimSolvers.add((RtSimSolverSchedulerBridge) e.getIssuer()); } else if (e.getEventType() == EventType.DONE_COMPUTING) { // done computing checkState(computingSimSolvers.remove(e.getIssuer())); // if (!isComputing()) { // stop(); // } } else { throw new IllegalArgumentException("Unexpected event: " + e); } } }
private void dispatchEvents(TimeLapse timeLapse) { TimedEvent e = null; while ((e = scenarioQueue.peek()) != null && e.getTime() <= timeLapse.getTime()) { scenarioQueue.poll(); if (status == null) { LOGGER.info("scenario started at virtual time:" + timeLapse.getTime()); status = EventType.SCENARIO_STARTED; disp.dispatchEvent(new Event(status, this)); } dispatch(e); } if ((e = scenarioQueue.peek()) != null && e.getTime() <= timeLapse.getTime() + timeLapse.getTickLength() && clock instanceof RealtimeClockController) { LOGGER.trace("Found an event in next tick, switch to RT"); ((RealtimeClockController) clock).switchToRealTime(); } if (e == null && status != EventType.SCENARIO_FINISHED) { status = EventType.SCENARIO_FINISHED; disp.dispatchEvent(new Event(status, this)); } }
private void dispatchEvents(TimeLapse timeLapse) { TimedEvent e = null; while ((e = scenarioQueue.peek()) != null && e.getTime() <= timeLapse.getTime()) { scenarioQueue.poll(); if (status == null) { LOGGER.info("scenario started at virtual time:" + timeLapse.getTime()); status = EventType.SCENARIO_STARTED; disp.dispatchEvent(new Event(status, this)); } dispatch(e); } if ((e = scenarioQueue.peek()) != null && e.getTime() <= timeLapse.getTime() + timeLapse.getTickLength() && clock instanceof RealtimeClockController) { LOGGER.trace("Found an event in next tick, switch to RT"); ((RealtimeClockController) clock).switchToRealTime(); } if (e == null && status != EventType.SCENARIO_FINISHED) { status = EventType.SCENARIO_FINISHED; disp.dispatchEvent(new Event(status, this)); } }
@Override public void tick(TimeLapse timeLapse) { if (timeLapse.getStartTime() == 200) { clock.switchToSimulatedTime(); } else if (timeLapse.getStartTime() == 600) { clock.switchToRealTime(); } }
verify(clock, times(0)).switchToRealTime(); verify(clock, times(0)).switchToSimulatedTime(); verify(clock, times(1)).switchToRealTime(); solvers.get(0).getEventAPI().addListener(eventHistory, EventType.NEW_SCHEDULE); solvers.get(1).solve(SolveArgs.create()); verify(clock, times(2)).switchToRealTime(); assertThat(eventHistory.getHistory()).isEmpty(); verify(clock, times(2)).switchToRealTime(); verify(clock, times(0)).switchToSimulatedTime(); verify(clock, times(3)).switchToRealTime(); verify(clock, times(0)).switchToSimulatedTime(); verify(clock, times(3)).switchToRealTime(); verify(clock, times(1)).switchToSimulatedTime();
@Override public void tick(TimeLapse timeLapse) { if (timeLapse.getTime() == 100) { rtClock.switchToRealTime(); } else if (timeLapse.getTime() == 200) { sim.register( Parcel.builder(new Point(0, 0), new Point(4, 4)).build()); simSolver.solve(SolveArgs.create() .useCurrentRoutes(ImmutableList.of(ImmutableList.<Parcel>of()))); } else if (timeLapse.getTime() == 500) { throw new IllegalStateException("This is a test"); } }
/** * Sets up a fake dependency provider and a working model instance. */ @Before public void setUp() { clock = spy((RealtimeClockController) TimeModel.builder() .withRealTime() .build(FakeDependencyProvider.empty())); doNothing().when(clock).switchToRealTime(); doNothing().when(clock).switchToSimulatedTime(); final RoadModelSnapshot planeSnapshot = RoadModelSnapshotTestUtil.createPlaneRoadModelSnapshot(new Point(0, 0), new Point(10, 10), SI.KILOMETER); final PDPRoadModel rm = mock(PDPRoadModel.class); when(rm.getSpeedUnit()).thenReturn(NonSI.KILOMETERS_PER_HOUR); when(rm.getDistanceUnit()).thenReturn(SI.KILOMETER); when(rm.getSnapshot()).thenReturn(planeSnapshot); dependencyProvider = FakeDependencyProvider.builder() .add(clock, RealtimeClockController.class) .add(rm, PDPRoadModel.class) .add(DefaultPDPModel.builder()) .build(); model = RtSolverModel.builder().build(dependencyProvider); ((TimeModel) clock).register(model); assertThat(model.mode).isEqualTo(Mode.UNKNOWN); TestUtil.testEnum(Mode.class); TestUtil.testEnum(RtSimSolver.EventType.class); }
@Override public void tick(TimeLapse timeLapse) { LOGGER.trace("{} tick", timeLapse); if (timeLapse.getTime() == 200) { rtClock.switchToRealTime(); } else if (timeLapse.getTime() == 300) { sim.register( Parcel.builder(new Point(0, 0), new Point(4, 4)).build()); simSolver.solve(SolveArgs.create() .useCurrentRoutes(ImmutableList.of(ImmutableList.<Parcel>of()))); assertNoSchedule(simSolver); } else if (timeLapse.getTime() == 400) { LOGGER.trace(AffinityLock.dumpLocks()); } else if (timeLapse.getTime() == 1000) { sim.register( Parcel.builder(new Point(0, 0), new Point(4, 4)).build()); simSolver.solve(SolveArgs.create() .useCurrentRoutes(ImmutableList.of(ImmutableList.<Parcel>of()))); assertNoSchedule(simSolver); assertThat(simSolver.isScheduleUpdated()).isFalse(); } else if (timeLapse.getTime() == 6000) { assertThat(simSolver.isScheduleUpdated()).isTrue(); final ImmutableList<ImmutableList<Parcel>> result = simSolver.getCurrentSchedule(); assertThat(result).hasSize(1); assertThat(result.get(0)).hasSize(2); assertThat(simSolver.isScheduleUpdated()).isFalse(); sim.stop(); } }