/** * 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); }
@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(); }
/** * 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); }
stateMachine = StateMachine .create( builder.getClockMode() == ClockMode.REAL_TIME ? INIT_RT : INIT_ST) .addTransition(INIT_RT, Trigger.SIMULATE, INIT_ST) .addTransition(INIT_RT, Trigger.START, realtimeState)
.addModel(TimeModel.builder() .withRealTime() .withStartInClockMode(ClockMode.SIMULATED)) .clearEvents() .addEvents(events)
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()); }
final RealtimeModel tm1 = (RealtimeModel) TimeModel.builder() .withRealTime() .build(FakeDependencyProvider.empty()); assertThat(tm1.getClockMode()).isEqualTo(ClockMode.REAL_TIME); .withStartInClockMode(ClockMode.SIMULATED) .withTimeUnit(NonSI.HOUR) .withTickLength(1) .build(FakeDependencyProvider.empty()); final RealtimeBuilder b = TimeModel.builder() .withRealTime() .withStartInClockMode(ClockMode.STOPPED); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()).contains("Can not use");
/** * Test for ui with real time clock. */ @Test public void testRealtime() { final Simulator sim = Simulator.builder() .addModel(TimeModel.builder() .withRealTime() .withTickLength(100L)) .addModel(RoadModelBuilders.plane()) .addModel(View.builder() .withTitleAppendix("ViewTest") .withAutoClose() .withAutoPlay() .withSimulatorEndTime(1000) .with(PlaneRoadModelRenderer.builder()) .with(TestRenderer.builder())) .build(); sim.start(); }
@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 that when a time model already exists, its properties are copied. */ @Test public void testCopyProperties() { final Scenario s = Scenario.builder() .addModel(TimeModel.builder() .withTickLength(754L) .withTimeUnit(NonSI.DAY)) .addModel(CommModel.builder()) .build(); final Scenario converted = verifyNotNull(ScenarioConverters.toRealtime().apply(s)); assertThat(converted.getModelBuilders()) .contains(TimeModel.builder() .withRealTime() .withStartInClockMode(ClockMode.SIMULATED) .withTickLength(754L) .withTimeUnit(NonSI.DAY)); }
@Before public void setUp() { sim = Simulator.builder() .addModel(TimeModel.builder() .withRealTime() .withTickLength(100L) .withTimeUnit(SI.MILLI(SI.SECOND))) .addModel(PDPRoadModel.builder(RoadModelBuilders.plane())) .addModel(DefaultPDPModel.builder()) .addModel(RtSolverModel.builder() .withSingleMode() .withThreadGrouping(useThreadGrouping)) .build(); model = sim.getModelProvider().getModel(RtSolverModel.class); rtClock = sim.getModelProvider().getModel(TimeModel.class) .get(RealtimeClockController.class); sim.register(new Depot(new Point(5, 5))); sim.register(new RouteFollowingVehicle(VehicleDTO.builder().build(), true)); }
@Override public RealtimeBuilder withTimeUnit(Unit<Duration> timeUnit) { return create(getTickLength(), timeUnit, getClockMode()); }
/** * @return The models to test. */ @Parameters public static Collection<Object[]> data() { return asList(new Object[][] { {TimeModel.builder().withRealTime().withTickLength(100L)} }); }
/** * 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); }
/** * Sets the {@link ClockMode} the model should start with. By default the * mode is {@link ClockMode#REAL_TIME}. * @param mode The mode to start with, can be either * {@link ClockMode#REAL_TIME} or {@link ClockMode#STOPPED}. * @return A new builder instance. */ @CheckReturnValue public RealtimeBuilder withStartInClockMode(ClockMode mode) { checkArgument(mode != ClockMode.STOPPED, "Can not use %s as starting mode in %s.", ClockMode.STOPPED, toString()); return create(getTickLength(), getTimeUnit(), mode); }
/** * Tests that when no time model is available, a default is added. */ @Test public void testEmpty() { final Scenario empty = Scenario.builder().build(); assertThat(empty.getModelBuilders()).isEmpty(); final Scenario convertedEmpty = verifyNotNull(ScenarioConverters.toRealtime().apply(empty)); assertThat(convertedEmpty.getModelBuilders()) .contains(TimeModel.builder() .withRealTime() .withStartInClockMode(ClockMode.SIMULATED)); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof TimeModel.RealtimeBuilder) { TimeModel.RealtimeBuilder that = (TimeModel.RealtimeBuilder) o; return (this.tickLength == that.getTickLength()) && (this.timeUnit.equals(that.getTimeUnit())) && (this.clockMode.equals(that.getClockMode())); } return false; }
@Override public RealtimeBuilder withTickLength(long tickLength) { return create(tickLength, getTimeUnit(), getClockMode()); }
/** * @return The models to test. */ @Parameters public static Collection<Object[]> data() { return asList(new Object[][] { {TimeModel.builder().withRealTime().withTickLength(100L)} }); }