Beispiel #1
0
 @Override
 public MoveProgress followPath(MovingRoadUser object, Queue<Point> path, TimeLapse time) {
   checkArgument(objLocs.containsKey(object), "Object: %s must have a location.", object);
   checkArgument(!path.isEmpty(), "Path can not be empty, found empty path for %s.", object);
   checkArgument(
       time.hasTimeLeft(), "Can not follow path when no time is left. For road user %s.", object);
   final Point dest = newArrayList(path).get(path.size() - 1);
   objDestinations.put(object, new DestinationPath(dest, path));
   final MoveProgress mp = doFollowPath(object, path, time);
   eventDispatcher.dispatchEvent(new MoveEvent(self, object, mp));
   return mp;
 }
  /** Tests a scenario with a limited number of ticks. */
  @Test
  public void finiteSimulation() {
    final NopHandler<?> handler = new NopHandler<>();
    @SuppressWarnings("unchecked")
    final Simulator sim =
        Simulator.builder()
            .setTickLength(1L)
            .setTimeUnit(SI.SECOND)
            .addModel(
                ScenarioController.builder(scenario)
                    .withEventHandler(EventA.class, (NopHandler<EventA>) handler)
                    .withEventHandler(EventB.class, (NopHandler<EventB>) handler)
                    .withEventHandler(EventC.class, (NopHandler<EventC>) handler)
                    .withNumberOfTicks(101))
            .build();

    final List<Long> ticks = new ArrayList<>();
    sim.addTickListener(
        new TickListener() {
          @Override
          public void tick(TimeLapse timeLapse) {
            ticks.add(timeLapse.getStartTime());
          }

          @Override
          public void afterTick(TimeLapse timeLapse) {}
        });

    final ScenarioController sc = sim.getModelProvider().getModel(ScenarioController.class);

    final ListenerEventHistory leh = new ListenerEventHistory();
    sc.getEventAPI().addListener(leh);
    assertThat(sc.isScenarioFinished()).isFalse();
    sim.start();

    assertThat(handler.getEvents())
        .containsExactly(
            EventA.create(0),
            EventB.create(0),
            EventB.create(0),
            EventA.create(1),
            EventC.create(5),
            EventC.create(100))
        .inOrder();

    assertThat(leh.getEventTypeHistory())
        .containsAllOf(SCENARIO_STARTED, SCENARIO_FINISHED)
        .inOrder();

    assertThat(sc.isScenarioFinished()).isTrue();
    sim.stop();
    final long before = sc.clock.getCurrentTime();
    sim.start(); // should have no effect

    assertThat(ticks).hasSize(101);

    assertThat(before).isEqualTo(sc.clock.getCurrentTime());
    final TimeLapse emptyTime = TimeLapseFactory.create(0, 1);
    emptyTime.consumeAll();
    sc.tick(emptyTime);
  }