covered, m.entrySet(), required, s.getProblemClass(), s.getProblemInstanceId()); return new ScenarioController(sim, clockController, s, ImmutableMap.copyOf(m), getNumberOfTicks());
/** * Dispatch all setup events (the ones that define initial settings). For * example, a vehicle that is added during setup (at time < 0) will receive * its first tick at time 0. If the vehicle is added at the beginning of the * simulation (time 0) the first tick it will receive will be the second * (globally) tick. */ protected void dispatchSetupEvents() { TimedEvent e = null; while ((e = scenarioQueue.peek()) != null && e.getTime() < 0) { scenarioQueue.poll(); dispatch(e); } }
@Override public void tick(TimeLapse timeLapse) { if (endOfScenario) { return; } if (ticks == 0) { stopClock(timeLapse); } if (LOGGER.isDebugEnabled() && ticks >= 0) { LOGGER.debug("ticks to end: " + ticks); } if (ticks > 0) { ticks--; } dispatchEvents(timeLapse); if (ticks == 0 && status == EventType.SCENARIO_FINISHED) { stopClock(timeLapse); endOfScenario = true; } }
/** * Tests that a not handled event results in a {@link IllegalStateException}. */ @Test public void testEventNotHandled() { final ScenarioController.Builder b = ScenarioController.builder(scenario) .withNumberOfTicks(3); boolean fail = false; try { b.build(dependencyProvider); } catch (final IllegalStateException e) { assertThat(e.getMessage()).containsMatch("No handler found for event"); fail = true; } assertThat(fail).isTrue(); }
.setTimeUnit(SI.SECOND) .addModel( ScenarioController.builder(scenario) .withEventHandler(EventA.class, (NopHandler<EventA>) handler) .withEventHandler(EventB.class, (NopHandler<EventB>) handler) sc.getEventAPI().addListener(leh); assertThat(sc.isScenarioFinished()).isFalse(); sim.start(); .inOrder(); assertThat(sc.isScenarioFinished()).isTrue(); sim.stop(); final long before = sc.clock.getCurrentTime(); final TimeLapse emptyTime = TimeLapseFactory.create(0, 1); emptyTime.consumeAll(); sc.tick(emptyTime);
.setTimeUnit(SI.SECOND) .addModel( ScenarioController.builder(scenario) .withNumberOfTicks(-1) .withEventHandler(EventA.class, (NopHandler<EventA>) handler) controller.getEventAPI().addListener(new Listener() { @Override public void handleEvent(Event e) { sim.start(); assertThat(handler.getEvents()).hasSize(scenario.getEvents().size()); assertThat(controller.isScenarioFinished()).isTrue();
.setTimeUnit(SI.SECOND) .addModel( ScenarioController.builder(scenario) .withEventHandler(EventA.class, aHandler) .withEventHandler(EventB.class, bHandler) controller.getEventAPI().addListener(leh);
StatsTracker(ScenarioController scenContr, Clock c, RoadModel rm, PDPModel pm) { clock = c; roadModel = rm; eventDispatcher = new EventDispatcher(StatsProvider.EventTypes.values()); theListener = new TheListener(); scenContr.getEventAPI().addListener(theListener, SCENARIO_STARTED, SCENARIO_FINISHED, SCENARIO_EVENT); roadModel.getEventAPI().addListener(theListener, MOVE); clock.getEventAPI().addListener(theListener, STARTED, STOPPED); pm.getEventAPI() .addListener(theListener, START_PICKUP, END_PICKUP, START_DELIVERY, END_DELIVERY, NEW_PARCEL, NEW_VEHICLE); }
@Override public void handleEvent(Event e) { if (clock.getCurrentTime() == 0) { dispatchSetupEvents(); } if (sc.endOfScenario) { clock.stop(); } } }, Clock.ClockEventType.STARTED);
/** * Tests that handling an interface is rejected. */ @Test public void testHandleInterface() { boolean fail = false; try { ScenarioController.builder(scenario) .withEventHandler(TimedEvent.class, new NopHandler<>()).toString(); } catch (final IllegalArgumentException e) { fail = true; assertThat(e.getMessage()).containsMatch("Must handle a concrete class"); } assertThat(fail).isTrue(); }
.setTimeUnit(SI.SECOND) .addModel( ScenarioController.builder(s) .withNumberOfTicks(1) .withEventHandler(EventA.class, (NopHandler<EventA>) handler) final ScenarioController sc = sim.getModelProvider().getModel( ScenarioController.class); sc.getEventAPI().addListener(leh); sim.start();
StatsTracker(ScenarioController scenContr, Clock c, RoadModel rm, PDPModel pm) { clock = c; roadModel = rm; eventDispatcher = new EventDispatcher(StatsProvider.EventTypes.values()); theListener = new TheListener(); scenContr.getEventAPI().addListener(theListener, SCENARIO_STARTED, SCENARIO_FINISHED, SCENARIO_EVENT); roadModel.getEventAPI().addListener(theListener, MOVE); clock.getEventAPI().addListener(theListener, STARTED, STOPPED); pm.getEventAPI() .addListener(theListener, START_PICKUP, END_PICKUP, START_DELIVERY, END_DELIVERY, NEW_PARCEL, NEW_VEHICLE); }
@Override public void handleEvent(Event e) { if (clock.getCurrentTime() == 0) { dispatchSetupEvents(); } if (sc.endOfScenario) { clock.stop(); } } }, Clock.ClockEventType.STARTED);
ScenarioController.builder( scenario) .withIgnoreRedundantHandlers(true)
@Override public void tick(TimeLapse timeLapse) { if (endOfScenario) { return; } if (ticks == 0) { stopClock(timeLapse); } if (LOGGER.isDebugEnabled() && ticks >= 0) { LOGGER.debug("ticks to end: " + ticks); } if (ticks > 0) { ticks--; } dispatchEvents(timeLapse); if (ticks == 0 && status == EventType.SCENARIO_FINISHED) { stopClock(timeLapse); endOfScenario = true; } }
/** * Dispatch all setup events (the ones that define initial settings). For * example, a vehicle that is added during setup (at time < 0) will receive * its first tick at time 0. If the vehicle is added at the beginning of the * simulation (time 0) the first tick it will receive will be the second * (globally) tick. */ protected void dispatchSetupEvents() { TimedEvent e = null; while ((e = scenarioQueue.peek()) != null && e.getTime() < 0) { scenarioQueue.poll(); dispatch(e); } }
covered, m.entrySet(), required, s.getProblemClass(), s.getProblemInstanceId()); return new ScenarioController(sim, clockController, s, ImmutableMap.copyOf(m), getNumberOfTicks());
ScenarioController.builder( scenario) .withIgnoreRedundantHandlers(true)
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)); } }
.addModel( ScenarioController .builder(scenario) .withEventHandler(AddParcelEvent.class, AddParcelEvent.defaultHandler())