Skip to content

Commit

Permalink
Freight: Adding missing converters for freight events (#2769)
Browse files Browse the repository at this point in the history
* added converters for missing freight events

* added test for reading in carrier events

* Revert "added test for reading in carrier events"

This reverts commit 2ef2813.
  • Loading branch information
nixlaos authored Sep 18, 2023
1 parent 38c4e11 commit 2a54b08
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ public class CarrierEventsReaders {

public static Map<String, MatsimEventsReader.CustomEventMapper> createCustomEventMappers() {
return Map.of(
CarrierServiceStartEvent.EVENT_TYPE, CarrierServiceStartEvent::convert,
CarrierServiceEndEvent.EVENT_TYPE, CarrierServiceEndEvent::convert,
CarrierShipmentDeliveryStartEvent.EVENT_TYPE, CarrierShipmentDeliveryStartEvent::convert,
CarrierShipmentDeliveryEndEvent.EVENT_TYPE, CarrierShipmentDeliveryEndEvent::convert,
CarrierShipmentPickupStartEvent.EVENT_TYPE, CarrierShipmentPickupStartEvent::convert,
CarrierShipmentPickupEndEvent.EVENT_TYPE, CarrierShipmentPickupEndEvent::convert,
CarrierTourStartEvent.EVENT_TYPE, CarrierTourStartEvent::convert, //
CarrierTourEndEvent.EVENT_TYPE, CarrierTourEndEvent::convert
// more will follow later, KMT feb'23
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Map;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.GenericEvent;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.freight.carrier.Carrier;
import org.matsim.contrib.freight.carrier.CarrierService;
import org.matsim.vehicles.Vehicle;
Expand Down Expand Up @@ -67,4 +69,17 @@ public Map<String, String> getAttributes() {
attr.put(ATTRIBUTE_SERVICE_DURATION, String.valueOf(serviceDuration));
return attr;
}

public static CarrierServiceEndEvent convert(GenericEvent event) {
Map<String, String> attributes = event.getAttributes();
double time = Double.parseDouble(attributes.get(ATTRIBUTE_TIME));
Id<Carrier> carrierId = Id.create(attributes.get(ATTRIBUTE_CARRIER_ID), Carrier.class);
Id<CarrierService> carrierServiceId = Id.create(attributes.get(ATTRIBUTE_SERVICE_ID), CarrierService.class);
Id<Link> locationLinkId = Id.createLinkId(attributes.get(ATTRIBUTE_LINK));
CarrierService service = CarrierService.Builder.newInstance(carrierServiceId, locationLinkId)
.setServiceDuration(Double.parseDouble(attributes.get(ATTRIBUTE_SERVICE_DURATION)))
.build();
Id<Vehicle> vehicleId = Id.create(attributes.get(ATTRIBUTE_VEHICLE), Vehicle.class);
return new CarrierServiceEndEvent(time, carrierId, service, vehicleId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package org.matsim.contrib.freight.events;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.GenericEvent;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.freight.carrier.Carrier;
import org.matsim.contrib.freight.carrier.CarrierService;
import org.matsim.vehicles.Vehicle;
Expand Down Expand Up @@ -77,4 +79,18 @@ public Map<String, String> getAttributes() {
attr.put(ATTRIBUTE_CAPACITYDEMAND, String.valueOf(capacityDemand));
return attr;
}

public static CarrierServiceStartEvent convert(GenericEvent event) {
Map<String, String> attributes = event.getAttributes();
double time = Double.parseDouble(attributes.get(ATTRIBUTE_TIME));
Id<Carrier> carrierId = Id.create(attributes.get(ATTRIBUTE_CARRIER_ID), Carrier.class);
Id<CarrierService> carrierServiceId = Id.create(attributes.get(ATTRIBUTE_SERVICE_ID), CarrierService.class);
Id<Link> locationLinkId = Id.createLinkId(attributes.get(ATTRIBUTE_LINK));
CarrierService service = CarrierService.Builder.newInstance(carrierServiceId, locationLinkId)
.setServiceDuration(Double.parseDouble(attributes.get(ATTRIBUTE_SERVICE_DURATION)))
.setCapacityDemand(Integer.parseInt(attributes.get(ATTRIBUTE_CAPACITYDEMAND)))
.build();
Id<Vehicle> vehicleId = Id.create(attributes.get(ATTRIBUTE_VEHICLE), Vehicle.class);
return new CarrierServiceStartEvent(time, carrierId, service, vehicleId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package org.matsim.contrib.freight.events;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.GenericEvent;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.freight.carrier.Carrier;
import org.matsim.contrib.freight.carrier.CarrierShipment;
import org.matsim.vehicles.Vehicle;
Expand Down Expand Up @@ -76,4 +78,18 @@ public Map<String, String> getAttributes() {
return attr;
}

public static CarrierShipmentDeliveryEndEvent convert(GenericEvent event) {
var attributes = event.getAttributes();
double time = Double.parseDouble(attributes.get(ATTRIBUTE_TIME));
Id<Carrier> carrierId = Id.create(attributes.get(ATTRIBUTE_CARRIER_ID), Carrier.class);
Id<CarrierShipment> shipmentId = Id.create(attributes.get(ATTRIBUTE_SHIPMENT_ID), CarrierShipment.class);
Id<Link> shipmentTo = Id.createLinkId(attributes.get(ATTRIBUTE_LINK));
int size = Integer.parseInt(attributes.get(ATTRIBUTE_CAPACITYDEMAND));
CarrierShipment shipment = CarrierShipment.Builder.newInstance(shipmentId, null, shipmentTo, size)
.setDeliveryServiceTime(Double.parseDouble(attributes.get(ATTRIBUTE_SERVICE_DURATION)))
.build();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(ATTRIBUTE_VEHICLE));
return new CarrierShipmentDeliveryEndEvent(time, carrierId, shipment, vehicleId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package org.matsim.contrib.freight.events;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.GenericEvent;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.freight.carrier.Carrier;
import org.matsim.contrib.freight.carrier.CarrierShipment;
import org.matsim.vehicles.Vehicle;
Expand Down Expand Up @@ -75,4 +77,18 @@ public Map<String, String> getAttributes() {
return attr;
}

public static CarrierShipmentDeliveryStartEvent convert(GenericEvent event) {
var attributes = event.getAttributes();
double time = Double.parseDouble(attributes.get(ATTRIBUTE_TIME));
Id<Carrier> carrierId = Id.create(attributes.get(ATTRIBUTE_CARRIER_ID), Carrier.class);
Id<CarrierShipment> shipmentId = Id.create(attributes.get(ATTRIBUTE_SHIPMENT_ID), CarrierShipment.class);
Id<Link> shipmentTo = Id.createLinkId(attributes.get(ATTRIBUTE_LINK));
int size = Integer.parseInt(attributes.get(ATTRIBUTE_CAPACITYDEMAND));
CarrierShipment shipment = CarrierShipment.Builder.newInstance(shipmentId, null, shipmentTo, size)
.setDeliveryServiceTime(Double.parseDouble(attributes.get(ATTRIBUTE_SERVICE_DURATION)))
.build();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(ATTRIBUTE_VEHICLE));
return new CarrierShipmentDeliveryStartEvent(time, carrierId, shipment, vehicleId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package org.matsim.contrib.freight.events;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.GenericEvent;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.freight.carrier.Carrier;
import org.matsim.contrib.freight.carrier.CarrierShipment;
import org.matsim.vehicles.Vehicle;
Expand Down Expand Up @@ -69,4 +71,18 @@ public Map<String, String> getAttributes() {
attr.put(ATTRIBUTE_CAPACITYDEMAND, String.valueOf(capacityDemand));
return attr;
}

public static CarrierShipmentPickupEndEvent convert(GenericEvent event) {
Map<String, String> attributes = event.getAttributes();
double time = Double.parseDouble(attributes.get(ATTRIBUTE_TIME));
Id<Carrier> carrierId = Id.create(attributes.get(ATTRIBUTE_CARRIER_ID), Carrier.class);
Id<CarrierShipment> shipmentId = Id.create(attributes.get(ATTRIBUTE_SHIPMENT_ID), CarrierShipment.class);
Id<Link> shipmentFrom = Id.createLinkId(attributes.get(ATTRIBUTE_LINK));
int shipmentSize = Integer.parseInt(attributes.get(ATTRIBUTE_CAPACITYDEMAND));
CarrierShipment shipment = CarrierShipment.Builder.newInstance(shipmentId, shipmentFrom, null, shipmentSize)
.setPickupServiceTime(Double.parseDouble(attributes.get(ATTRIBUTE_PICKUP_DURATION)))
.build();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(ATTRIBUTE_VEHICLE));
return new CarrierShipmentPickupEndEvent(time, carrierId, shipment, vehicleId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package org.matsim.contrib.freight.events;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.GenericEvent;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.freight.carrier.Carrier;
import org.matsim.contrib.freight.carrier.CarrierShipment;
import org.matsim.vehicles.Vehicle;
Expand Down Expand Up @@ -69,4 +71,18 @@ public Map<String, String> getAttributes() {
attr.put(ATTRIBUTE_CAPACITYDEMAND, String.valueOf(capacityDemand));
return attr;
}

public static CarrierShipmentPickupStartEvent convert(GenericEvent event) {
Map<String, String> attributes = event.getAttributes();
double time = Double.parseDouble(attributes.get(ATTRIBUTE_TIME));
Id<Carrier> carrierId = Id.create(attributes.get(ATTRIBUTE_CARRIER_ID), Carrier.class);
Id<CarrierShipment> shipmentId = Id.create(attributes.get(ATTRIBUTE_SHIPMENT_ID), CarrierShipment.class);
Id<Link> shipmentFrom = Id.createLinkId(attributes.get(ATTRIBUTE_LINK));
int shipmentSize = Integer.parseInt(attributes.get(ATTRIBUTE_CAPACITYDEMAND));
CarrierShipment shipment = CarrierShipment.Builder.newInstance(shipmentId, shipmentFrom, null, shipmentSize)
.setPickupServiceTime(Double.parseDouble(attributes.get(ATTRIBUTE_PICKUP_DURATION)))
.build();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(ATTRIBUTE_VEHICLE));
return new CarrierShipmentPickupStartEvent(time, carrierId, shipment, vehicleId);
}
}

0 comments on commit 2a54b08

Please sign in to comment.