/** * 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(); }
/** * 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(); }
/** * Add a {@link TimedEventHandler} to the controller that handles * {@link TimedEvent}s of the specified type. * @param type The type of event to handle. * @param handler The handler that handles the event. * @param <T> The type of event to handle. * @return A new {@link Builder} instance. * @throws IllegalArgumentException If an interface class is provided. */ @CheckReturnValue public <T extends TimedEvent> Builder withEventHandler(Class<T> type, TimedEventHandler<T> handler) { checkHandlerType(type); return create( getScenario(), ImmutableMap .<Class<? extends TimedEvent>, TimedEventHandler<?>>builder() .putAll(getEventHandlers()).put(type, handler).build(), getNumberOfTicks(), getStopModelBuilder(), isIgnoreRedundantHandlers()); }
.withEventHandler(AddParcelEvent.class, AddParcelEvent.defaultHandler()) .withEventHandler(TimeOutEvent.class, TimeOutEvent.ignoreHandler()) .withEventHandler(AddVehicleEvent.class, new TimedEventHandler<AddVehicleEvent>() { @Override
.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();
ScenarioController .builder(scenario) .withEventHandler(AddParcelEvent.class, AddParcelEvent.defaultHandler()) .withEventHandler(AddVehicleEvent.class, new TimedEventHandler<AddVehicleEvent>() { @Override
ScenarioController .builder(scenario) .withEventHandler(AddParcelEvent.class, AddParcelEvent.defaultHandler()) .withEventHandler(AddVehicleEvent.class, new TimedEventHandler<AddVehicleEvent>() { @Override
ScenarioController .builder(testScenario) .withEventHandler(AddParcelEvent.class, AddParcelEvent.defaultHandler()) .withEventHandler(TimeOutEvent.class, TimeOutEvent.ignoreHandler()) .withEventHandler(AddVehicleEvent.class, new TimedEventHandler<AddVehicleEvent>() { @Override
ScenarioController .builder(s) .withEventHandler(AddDepotEvent.class, AddDepotEvent.defaultHandler()) .withEventHandler(AddParcelEvent.class, AddParcelEvent.defaultHandler()) .withEventHandler(TimeOutEvent.class, TimeOutEvent.ignoreHandler()) .withEventHandler(AddVehicleEvent.class, new TimedEventHandler<AddVehicleEvent>() { @Override
.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();
.get(ClockController.class); final Scenario s = getScenario(); final Set<Class<?>> required = collectClasses(s.getEvents()); final Map<Class<? extends TimedEvent>, TimedEventHandler<?>> m = newLinkedHashMap(getEventHandlers()); final Set<Class<? extends TimedEvent>> covered = newLinkedHashSet(getEventHandlers().keySet()); checkState(isIgnoreRedundantHandlers() || covered.isEmpty(), "Found redundant event handlers for event type(s): %s, no event with " + "these type(s) was found. All added handlers: %s, all event types" s.getProblemInstanceId()); return new ScenarioController(sim, clockController, s, ImmutableMap.copyOf(m), getNumberOfTicks());
.get(ClockController.class); final Scenario s = getScenario(); final Set<Class<?>> required = collectClasses(s.getEvents()); final Map<Class<? extends TimedEvent>, TimedEventHandler<?>> m = newLinkedHashMap(getEventHandlers()); final Set<Class<? extends TimedEvent>> covered = newLinkedHashSet(getEventHandlers().keySet()); checkState(isIgnoreRedundantHandlers() || covered.isEmpty(), "Found redundant event handlers for event type(s): %s, no event with " + "these type(s) was found. All added handlers: %s, all event types" s.getProblemInstanceId()); return new ScenarioController(sim, clockController, s, ImmutableMap.copyOf(m), getNumberOfTicks());
ScenarioController.builder( scenario) .withIgnoreRedundantHandlers(true) .withEventHandlers(config.getEventHandlers()); containsStatisticsProvider(scenContrBuilder.getChildren()) || containsStatisticsProvider(config.getModels());
.addModel( ScenarioController.builder(scenario) .withEventHandler(EventA.class, aHandler) .withEventHandler(EventB.class, bHandler) .withEventHandler(EventC.class, cHandler)) .build();
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(); 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();
ScenarioController.builder( scenario) .withIgnoreRedundantHandlers(true) .withEventHandlers(config.getEventHandlers()); containsStatisticsProvider(scenContrBuilder.getChildren()) || containsStatisticsProvider(config.getModels());
public static void main(String[] args) throws IOException { for (int i = 0; i < 100; i++) { final FabriRechtScenario scenario = FabriRechtParser.fromJson(Files .toString(new File("files/test/fabri-recht/lc101.scenario"), Charsets.UTF_8), 8, 20); Simulator.builder() .addModel( ScenarioController.builder(scenario) .withEventHandler(AddVehicleEvent.class, new TimedEventHandler<AddVehicleEvent>() { @Override public void handleTimedEvent(AddVehicleEvent event, SimulatorAPI simulator) { simulator.register(new Truck(event.getVehicleDTO())); } })) .addModel( View.builder() .with(PlaneRoadModelRenderer.builder()) .with(RoadUserRenderer.builder()) .with(PDPModelRenderer.builder()) .withSpeedUp(50) .withAutoClose() .withAutoPlay()) .build().start(); } } }
.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();
static Simulator.Builder init( TimedEventHandler<AddVehicleEvent> vehicleHandler, Iterable<? extends TimedEvent> events) { final Scenario scenario = Scenario.builder() .addEvent(AddDepotEvent.create(-1, new Point(5, 5))) .addEvent(AddVehicleEvent.create(-1, VehicleDTO.builder().build())) .addEvent(AddVehicleEvent.create(-1, VehicleDTO.builder().build())) .addEvents(events) .build(); final ScenarioController.Builder sb = ScenarioController.builder(scenario) .withEventHandler(AddParcelEvent.class, AddParcelEvent.defaultHandler()) .withEventHandler(AddVehicleEvent.class, vehicleHandler) .withEventHandler(AddDepotEvent.class, AddDepotEvent.defaultHandler()) .withEventHandler(TimeOutEvent.class, TimeOutEvent.ignoreHandler()) .withOrStopCondition(StatsStopConditions.vehiclesDoneAndBackAtDepot()) .withOrStopCondition(StatsStopConditions.timeOutEvent()); return Simulator.builder() .addModel(PDPRoadModel.builder(RoadModelBuilders.plane()) .withAllowVehicleDiversion(true)) .addModel(DefaultPDPModel.builder()) .addModel(TimeModel.builder() .withRealTime() .withStartInClockMode(ClockMode.SIMULATED) .withTickLength(100)) .addModel(sb) .addModel(StatsTracker.builder()); }
/** * Adds an additional stop condition to the controller in OR fashion. The * first stop condition is defined by {@link Scenario#getStopCondition()}. * @param stp The builder that constructs the {@link StopCondition}. * @return A new {@link Builder} instance. * @see StopConditions */ @CheckReturnValue public Builder withOrStopCondition(StopCondition stp) { final StopModelBuilder smb; if (getStopModelBuilder().stopCondition().equals( StopConditions.alwaysFalse())) { smb = StopModelBuilder.create(stp); } else { smb = StopModelBuilder.create(StopConditions.or(getStopModelBuilder() .stopCondition(), stp)); } return create(getScenario(), getEventHandlers(), getNumberOfTicks(), smb, isIgnoreRedundantHandlers()); }