Skip to content

Commit

Permalink
handle multiple detours on the same schedule correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Jan 14, 2025
1 parent 904a28f commit 7b38e60
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public Id<Vehicle> getVehicleId() {
return vehicleId;
}

public Id<TransitStopFacility> getNewStop() {
return newStop;
}

@Override
public Map<String, String> getAttributes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package ch.sbb.matsim.contrib.railsim.integration;

import ch.sbb.matsim.contrib.railsim.RailsimModule;
import ch.sbb.matsim.contrib.railsim.events.RailsimDetourEvent;
import ch.sbb.matsim.contrib.railsim.events.RailsimTrainStateEvent;
import ch.sbb.matsim.contrib.railsim.qsimengine.RailsimQSimModule;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -337,34 +338,21 @@ void testMicroStationRerouting() {
// collect all arrivals and departures
Map<Id<Vehicle>, List<Id<TransitStopFacility>>> train2arrivalStops = new HashMap<>();
Map<Id<Vehicle>, List<Id<TransitStopFacility>>> train2departureStops = new HashMap<>();
Map<Id<Vehicle>, List<RailsimDetourEvent>> train2detours = new HashMap<>();

for (Event event : collector.getEvents()) {
if (event.getEventType().equals(VehicleArrivesAtFacilityEvent.EVENT_TYPE)) {

VehicleArrivesAtFacilityEvent vehicleArrivesEvent = (VehicleArrivesAtFacilityEvent) event;

if (train2arrivalStops.get(vehicleArrivesEvent.getVehicleId()) == null) {

List<Id<TransitStopFacility>> stops = new ArrayList<>();
stops.add(vehicleArrivesEvent.getFacilityId());
train2arrivalStops.put(vehicleArrivesEvent.getVehicleId(), stops);
} else {
train2arrivalStops.get(vehicleArrivesEvent.getVehicleId()).add(vehicleArrivesEvent.getFacilityId());
}
if (event instanceof VehicleArrivesAtFacilityEvent vehicleArrivesEvent) {
train2arrivalStops.computeIfAbsent(vehicleArrivesEvent.getVehicleId(), k -> new ArrayList<>())
.add(vehicleArrivesEvent.getFacilityId());
}

if (event.getEventType().equals(VehicleDepartsAtFacilityEvent.EVENT_TYPE)) {

VehicleDepartsAtFacilityEvent vehicleDepartsEvent = (VehicleDepartsAtFacilityEvent) event;

if (train2departureStops.get(vehicleDepartsEvent.getVehicleId()) == null) {
if (event instanceof VehicleDepartsAtFacilityEvent vehicleDepartsEvent) {
train2departureStops.computeIfAbsent(vehicleDepartsEvent.getVehicleId(), k -> new ArrayList<>())
.add(vehicleDepartsEvent.getFacilityId());
}

List<Id<TransitStopFacility>> stops = new ArrayList<>();
stops.add(vehicleDepartsEvent.getFacilityId());
train2departureStops.put(vehicleDepartsEvent.getVehicleId(), stops);
} else {
train2departureStops.get(vehicleDepartsEvent.getVehicleId()).add(vehicleDepartsEvent.getFacilityId());
}
if (event instanceof RailsimDetourEvent detour) {
train2detours.computeIfAbsent(detour.getVehicleId(), k -> new ArrayList<>()).add(detour);
}
}

Expand All @@ -380,7 +368,10 @@ void testMicroStationRerouting() {
Assertions.assertEquals("AB", train2arrivalStops.get(Id.createVehicleId("train3")).get(0).toString(), "Wrong stop facility. This is the start stop facility.");

// The original events don't contain the re-routing. They appear to other agents as if the train drove the original schedule.
Assertions.assertEquals("CE", train2arrivalStops.get(Id.createVehicleId("train3")).get(1).toString(), "Wrong stop facility. This is the rerouted stop Id. Train 3 is rerouted from CE to CD.");
Assertions.assertEquals("CE", train2arrivalStops.get(Id.createVehicleId("train3")).get(1).toString(), "Wrong stop facility. This is the original id");
// Detour facility
Assertions.assertEquals("CD", train2detours.get(Id.createVehicleId("train3")).get(0).getNewStop().toString());


Assertions.assertEquals("JK", train2arrivalStops.get(Id.createVehicleId("train3")).get(2).toString(), "Wrong stop facility. This is the final stop facility.");
}
Expand Down

0 comments on commit 7b38e60

Please sign in to comment.