/** Checks whether the start events are generated. */
  @Test
  public void testStartEventGenerated() {
    final NopHandler<EventA> aHandler = new NopHandler<>();
    final NopHandler<EventB> bHandler = new NopHandler<>();
    final NopHandler<EventC> cHandler = new NopHandler<>();

    final Simulator sim =
        Simulator.builder()
            .setTickLength(1L)
            .setTimeUnit(SI.SECOND)
            .addModel(
                ScenarioController.builder(scenario)
                    .withEventHandler(EventA.class, aHandler)
                    .withEventHandler(EventB.class, bHandler)
                    .withEventHandler(EventC.class, cHandler))
            .build();

    controller = sim.getModelProvider().getModel(ScenarioController.class);

    final ListenerEventHistory leh = new ListenerEventHistory();
    controller.getEventAPI().addListener(leh);

    controller.clock.tick();

    assertThat(aHandler.getEvents()).containsExactly(EventA.create(0L));
    assertThat(bHandler.getEvents()).containsExactly(EventB.create(0L), EventB.create(0L));
    assertThat(cHandler.getEvents()).isEmpty();
    assertThat(leh.getEventTypeHistory())
        .containsExactly(
            EventType.SCENARIO_STARTED,
            EventType.SCENARIO_EVENT,
            EventType.SCENARIO_EVENT,
            EventType.SCENARIO_EVENT);
  }
  /** Test run of whole scenario. */
  @Test
  public void runningWholeScenario() {
    final NopHandler<?> handler = new NopHandler<>();
    @SuppressWarnings("unchecked")
    final Simulator sim =
        Simulator.builder()
            .setTickLength(1L)
            .setTimeUnit(SI.SECOND)
            .addModel(
                ScenarioController.builder(scenario)
                    .withNumberOfTicks(-1)
                    .withEventHandler(EventA.class, (NopHandler<EventA>) handler)
                    .withEventHandler(EventB.class, (NopHandler<EventB>) handler)
                    .withEventHandler(EventC.class, (NopHandler<EventC>) handler))
            .build();

    controller = sim.getModelProvider().getModel(ScenarioController.class);
    controller
        .getEventAPI()
        .addListener(
            new Listener() {
              @Override
              public void handleEvent(Event e) {
                if (e.getEventType() == ScenarioController.EventType.SCENARIO_FINISHED) {
                  sim.stop();
                }
              }
            });
    sim.start();
    assertThat(handler.getEvents()).hasSize(scenario.getEvents().size());
    assertThat(controller.isScenarioFinished()).isTrue();
  }
  /** Test for stop condition. */
  @Test
  public void testStopCondition() {
    final Simulator sim =
        Simulator.builder()
            .setTickLength(1L)
            .addModel(
                ScenarioController.builder(scenario)
                    .withEventHandler(EventA.class, new NopHandler<EventA>())
                    .withEventHandler(EventB.class, new NopHandler<EventB>())
                    .withEventHandler(EventC.class, new NopHandler<EventC>())
                    .withAndStopCondition(StopConditions.alwaysTrue()))
            .build();

    sim.start();

    assertThat(sim.getCurrentTime()).isEqualTo(1L);

    final Simulator sim2 =
        Simulator.builder()
            .setTickLength(1L)
            .addModel(
                ScenarioController.builder(scenario)
                    .withEventHandler(EventA.class, new NopHandler<EventA>())
                    .withEventHandler(EventB.class, new NopHandler<EventB>())
                    .withEventHandler(EventC.class, new NopHandler<EventC>())
                    .withAndStopCondition(StopConditions.limitedTime(100)))
            .build();

    sim2.start();

    assertThat(sim2.getCurrentTime()).isEqualTo(101L);
  }
Exemple #4
0
  /**
   * Run the example.
   *
   * @param testing if <code>true</code> turns on testing mode.
   */
  public static void run(boolean testing) {
    // configure the GUI. We use separate renderers for the road model and
    // for the drivers. By default the road model is rendered as a square
    // (indicating its boundaries), and the drivers are rendered as red
    // dots.
    View.Builder viewBuilder =
        View.builder().with(PlaneRoadModelRenderer.builder()).with(RoadUserRenderer.builder());

    if (testing) {
      viewBuilder =
          viewBuilder
              .withSpeedUp(TEST_SPEEDUP)
              .withAutoClose()
              .withAutoPlay()
              .withSimulatorEndTime(TEST_STOP_TIME);
    }

    // initialize a new Simulator instance
    final Simulator sim =
        Simulator.builder()
            // set the length of a simulation 'tick'
            .setTickLength(TICK_LENGTH)
            // set the random seed we use in this 'experiment'
            .setRandomSeed(RANDOM_SEED)
            // add a PlaneRoadModel, a model which facilitates the moving of
            // RoadUsers on a plane. The plane is bounded by two corner points:
            // (0,0) and (10,10)
            .addModel(
                RoadModelBuilders.plane()
                    .withMinPoint(MIN_POINT)
                    .withMaxPoint(MAX_POINT)
                    .withMaxSpeed(VEHICLE_SPEED_KMH))
            // in case a GUI is not desired simply don't add it.
            .addModel(viewBuilder)
            .build();

    // add a number of drivers on the road
    for (int i = 0; i < NUM_VEHICLES; i++) {
      // when an object is registered in the simulator it gets
      // automatically 'hooked up' with models that it's interested in. An
      // object declares to be interested in an model by implementing an
      // interface.
      sim.register(new Driver(sim.getRandomGenerator()));
    }

    // if a GUI is added, it starts it, if no GUI is specified it will
    // run the simulation without visualization.
    sim.start();
  }
  /** Tests proper dispatching of setup events. */
  @Test
  public void testSetupEvents() {
    final Scenario s =
        Scenario.builder()
            .addEvent(EventA.create(0))
            .addEvent(EventB.create(-1))
            .addEvent(EventB.create(2))
            .addEvent(EventA.create(2))
            .addEvent(EventC.create(-1))
            .addEvent(EventC.create(100))
            .build();

    final NopHandler<?> handler = new NopHandler<>();

    @SuppressWarnings("unchecked")
    final Simulator sim =
        Simulator.builder()
            .setTickLength(1L)
            .setTimeUnit(SI.SECOND)
            .addModel(
                ScenarioController.builder(s)
                    .withNumberOfTicks(1)
                    .withEventHandler(EventA.class, (NopHandler<EventA>) handler)
                    .withEventHandler(EventB.class, (NopHandler<EventB>) handler)
                    .withEventHandler(EventC.class, (NopHandler<EventC>) handler))
            .build();

    final ListenerEventHistory leh = new ListenerEventHistory();
    final ScenarioController sc = sim.getModelProvider().getModel(ScenarioController.class);
    sc.getEventAPI().addListener(leh);
    sim.start();

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

    assertThat(leh.getEventTypeHistory())
        .containsExactly(SCENARIO_EVENT, SCENARIO_EVENT, SCENARIO_STARTED, SCENARIO_EVENT)
        .inOrder();
  }
  /** Test for {@link PDPModelRenderer} in combination with a realtime clock. */
  @Test
  public void test() {
    final Simulator sim =
        Simulator.builder()
            .addModel(TimeModel.builder().withRealTime())
            .addModel(RoadModelBuilders.plane())
            .addModel(DefaultPDPModel.builder())
            .addModel(
                View.builder()
                    .with(PlaneRoadModelRenderer.builder())
                    .with(
                        RoadUserRenderer.builder()
                            .withColorAssociation(Depot.class, new RGB(255, 200, 0))
                            .withCircleAroundObjects())
                    .with(PDPModelRenderer.builder().withDestinationLines())
                    .withAutoPlay()
                    .withAutoClose()
                    .withSimulatorEndTime(5000))
            .build();

    for (int i = 0; i < 10; i++) {
      if (i != 5) {
        sim.register(Parcel.builder(new Point(i, i + 1), new Point(5, 5)).build());
        sim.register(new TestVehicle(new Point(i, 10 - i), new Point(i, i + 1)));
      }
    }
    sim.register(new Depot(new Point(5, 5)));

    sim.start();
  }
  /** 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);
  }