boolean diversion, boolean realtime) { final TimeModel.Builder timeModelBuilder = TimeModel.builder() .withTickLength(ts) .withTimeUnit(SI.MILLI(SI.SECOND)); .add(realtime ? timeModelBuilder.withRealTime() : timeModelBuilder) .add( PDPRoadModel.builder(
/** * Create a time model that synchronized ticks to the real time. An * additional type is provided by this model: * {@link RealtimeClockController}. * @return A new builder instance. */ @CheckReturnValue public RealtimeBuilder withRealTime() { return RealtimeBuilder.create(getTickLength(), getTimeUnit(), ClockMode.REAL_TIME); }
.addModel(TimeModel.builder().withTickLength(100)) .addModel( RoadModelBuilders.plane()
.addModel(TimeModel.builder().withTickLength(TICK_LENGTH)) .addModel( RoadModelBuilders.plane()
.addModel(TimeModel.builder().withTickLength(TICK_LENGTH)) .addModel( RoadModelBuilders.plane()
.withSpeedUnit(SI.METERS_PER_SECOND) .withMaxSpeed(1000d)) .addModel(TimeModel.builder().withTickLength(500)) .build(); model = sim.getModelProvider().getModel(CollisionPlaneRoadModel.class);
.removeModelsOfType(TimeModel.AbstractBuilder.class) .addModel(TimeModel.builder() .withRealTime() .withStartInClockMode(ClockMode.SIMULATED)) .clearEvents()
public void test() { final Simulator sim = Simulator.builder() .addModel(TimeModel.builder().withRealTime()) .addModel(RoadModelBuilders.plane()) .addModel(DefaultPDPModel.builder())
.addModel(TimeModel.builder().withTickLength(7L)) .addModel(RoadModelBuilders.staticGraph( DotGraphIO.getLengthDataGraphSupplier(p)))
public void testBuilderClockMode() { final RealtimeModel tm1 = (RealtimeModel) TimeModel.builder() .withRealTime() .build(FakeDependencyProvider.empty()); assertThat(tm1.getClockMode()).isEqualTo(ClockMode.REAL_TIME); .withRealTime() .withStartInClockMode(ClockMode.SIMULATED) .withTimeUnit(NonSI.HOUR) @SuppressWarnings("unused") final RealtimeBuilder b = TimeModel.builder() .withRealTime() .withStartInClockMode(ClockMode.STOPPED); } catch (final IllegalArgumentException e) {
/** * 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); }
TestUtil.testEnum(VehicleChecker.class); final Simulator sim = Simulator.builder() .addModel(TimeModel.builder().withRealTime()) .addModel(PDPRoadModel.builder(RoadModelBuilders.plane())) .addModel(DefaultPDPModel.builder())
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()); }
static Scenario createScenario(long... delays) { final long endTime = 15 * 60 * 1000; final VehicleDTO vehicle = VehicleDTO.builder() .startPosition(new Point(5, 5)) .availabilityTimeWindow(TimeWindow.create(0, endTime)) .build(); final Scenario.Builder scenario = Scenario.builder() .addEvent(AddDepotEvent.create(-1, new Point(5, 5))) .addEvent(AddVehicleEvent.create(-1, vehicle)) .addEvent(AddVehicleEvent.create(-1, vehicle)) .addEvent(TimeOutEvent.create(endTime)) .addModel(PDPRoadModel.builder(RoadModelBuilders.plane()) .withAllowVehicleDiversion(true)) .addModel(DefaultPDPModel.builder()) .addModel(TimeModel.builder().withTickLength(250)) .setStopCondition(StopConditions.and( StatsStopConditions.vehiclesDoneAndBackAtDepot(), StatsStopConditions.timeOutEvent())); final long[] dls = new long[3]; System.arraycopy(delays, 0, dls, 0, delays.length); scenario .addEvent(createParcel(0, dls[0], new Point(1, 1), new Point(9, 1))); scenario .addEvent(createParcel(1, dls[1], new Point(1, 2), new Point(9, 2))); scenario .addEvent(createParcel(2, dls[2], new Point(9, 9), new Point(1, 9))); return scenario.build(); }
/** * Tests {@link ScenarioIO#readerAdapter(com.google.common.base.Function)}. * @throws IOException When IO fails. */ @Test public void testReaderAdapter() throws IOException { final Scenario s = Scenario.builder() .addModel(TimeModel.builder().withTickLength(7L)) .build(); final Path tmpDir = Files.createTempDirectory("rinsim-scenario-io-test"); final Path file = Paths.get(tmpDir.toString(), "test.scen"); ScenarioIO.write(s, file); final Scenario out = ScenarioIO.reader().apply(file); final Scenario convertedOut = verifyNotNull(ScenarioIO.readerAdapter(ScenarioConverters.toRealtime()) .apply(file)); assertThat(s).isEqualTo(out); assertThat(s).isNotEqualTo(convertedOut); assertThat(convertedOut.getModelBuilders()) .contains(TimeModel.builder() .withRealTime() .withStartInClockMode(ClockMode.SIMULATED) .withTickLength(7L)); Files.delete(file); Files.delete(tmpDir); }
/** * Tests detection and correct error message. * @throws IOException If something goes wrong with the filesystem. */ @Test public void testGraphRmbDirectIO() throws IOException { final Graph<LengthData> g = new TableGraph<>(); g.addConnection(new Point(0, 0), new Point(1, 0)); g.addConnection(new Point(1, 1), new Point(1, 0)); final Scenario s = Scenario.builder() .addModel(TimeModel.builder().withTickLength(7L)) .addModel(RoadModelBuilders.staticGraph(Suppliers.ofInstance(g))) .build(); boolean fail = false; try { ScenarioIO.write(s); } catch (final IllegalArgumentException e) { fail = true; assertThat(e.getMessage()) .isEqualTo("A graph cannot be serialized embedded in a scenario."); } assertThat(fail).isTrue(); } }
@Override public ImmutableSet<ModelBuilder<?, ?>> getModelBuilders() { return ImmutableSet.<ModelBuilder<?, ?>>builder() .add( TimeModel.builder() .withTickLength(1L) .withTimeUnit(NonSI.MINUTE)) .add( RoadModelBuilders.plane() .withMinPoint(getMin()) .withMaxPoint(getMax()) .withDistanceUnit(SI.KILOMETER) .withMaxSpeed(MAX_SPEED) .withSpeedUnit( SI.KILOMETRE.divide(NonSI.MINUTE).asType(Velocity.class))) .add( DefaultPDPModel.builder() .withTimeWindowPolicy(TimeWindowPolicies.TARDY_ALLOWED)) .add( StatsTracker.builder()) .build(); }
/** * Tests that IAE is thrown when there are too many time models. */ @Test public void testTooManyTimeModels() { final Scenario s = Scenario.builder() .addModel(TimeModel.builder()) .addModel(TimeModel.builder().withRealTime()) .build(); boolean fail = false; try { ScenarioConverters.toRealtime().apply(s); } catch (final IllegalArgumentException e) { fail = true; assertThat(e.getMessage()) .isEqualTo("More than one time model is not supported."); } assertThat(fail).isTrue(); } }
@Override @Nullable public Scenario apply(@Nullable Scenario input) { final Scenario in = verifyNotNull(input); final Optional<TimeModel.AbstractBuilder<?>> timeModel = getTimeModel(in); RealtimeBuilder rtb = TimeModel.builder() .withRealTime() .withStartInClockMode(ClockMode.SIMULATED); if (timeModel.isPresent()) { // copy properties from existing time model rtb = rtb.withTickLength(timeModel.get().getTickLength()) .withTimeUnit(timeModel.get().getTimeUnit()); } // else: in this case we don't copy properties, we use the defaults return Scenario.builder(in) .removeModelsOfType(TimeModel.AbstractBuilder.class) .addModel(rtb) .build(); }
/** * Tests the removal of model builders. */ @Test public void testRemoveModelsOfType() { final Scenario.Builder builder = Scenario.builder(); builder.addModel(TimeModel.builder()) .addModel(TimeModel.builder().withRealTime()) .addModel(RoadModelBuilders.plane()) .addModel(CommModel.builder()); assertThat(builder.modelBuilders).hasSize(4); builder.removeModelsOfType(RoadModelBuilders.PlaneRMB.class); assertThat(builder.modelBuilders).hasSize(3); assertThat(builder.modelBuilders).containsExactly(TimeModel.builder(), TimeModel.builder().withRealTime(), CommModel.builder()); builder.removeModelsOfType(RoadModelBuilders.AbstractGraphRMB.class); builder.removeModelsOfType(TimeModel.AbstractBuilder.class); assertThat(builder.modelBuilders).hasSize(1); assertThat(builder.modelBuilders).containsExactly(CommModel.builder()); builder.removeModelsOfType(CommModel.Builder.class); assertThat(builder.modelBuilders).isEmpty(); }