From b89d5fa72d40036b5932b02ba7c3bdc4024d7505 Mon Sep 17 00:00:00 2001 From: nkuehnel Date: Thu, 21 Mar 2024 12:14:21 +0100 Subject: [PATCH] add utility class for ev events reading --- .../contrib/ev/charging/ChargingEndEvent.java | 10 ++++++ .../ev/charging/ChargingStartEvent.java | 10 ++++++ .../ev/charging/EnergyChargedEvent.java | 13 ++++++++ .../ev/charging/QueuedAtChargerEvent.java | 9 ++++++ .../ev/charging/QuitQueueAtChargerEvent.java | 9 ++++++ .../DrivingEnergyConsumptionEvent.java | 13 ++++++++ .../IdlingEnergyConsumptionEvent.java | 13 ++++++++ .../contrib/ev/util/EvEventsReader.java | 31 +++++++++++++++++++ 8 files changed, 108 insertions(+) create mode 100644 contribs/ev/src/main/java/org/matsim/contrib/ev/util/EvEventsReader.java diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/ChargingEndEvent.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/ChargingEndEvent.java index e1222d14be2..74543d6e783 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/ChargingEndEvent.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/ChargingEndEvent.java @@ -23,6 +23,7 @@ import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.events.Event; +import org.matsim.api.core.v01.events.GenericEvent; import org.matsim.contrib.ev.infrastructure.Charger; import org.matsim.vehicles.Vehicle; @@ -68,4 +69,13 @@ public Map getAttributes() { attr.put(ATTRIBUTE_CHARGE, charge + ""); return attr; } + + public static ChargingEndEvent convert(GenericEvent genericEvent) { + Map attributes = genericEvent.getAttributes(); + double time = genericEvent.getTime(); + Id vehicleId = Id.createVehicleId(attributes.get(ChargingEndEvent.ATTRIBUTE_VEHICLE)); + Id chargerId = Id.create(attributes.get(ChargingEndEvent.ATTRIBUTE_CHARGER), Charger.class); + double charge = Double.parseDouble(attributes.get(ChargingEndEvent.ATTRIBUTE_CHARGE)); + return new ChargingEndEvent(time, chargerId, vehicleId, charge); + } } diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/ChargingStartEvent.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/ChargingStartEvent.java index 3305f40ecdd..f6a4a28f774 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/ChargingStartEvent.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/ChargingStartEvent.java @@ -23,6 +23,7 @@ import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.events.Event; +import org.matsim.api.core.v01.events.GenericEvent; import org.matsim.contrib.ev.infrastructure.Charger; import org.matsim.vehicles.Vehicle; @@ -68,4 +69,13 @@ public Map getAttributes() { attr.put(ATTRIBUTE_CHARGE, charge + ""); return attr; } + + public static ChargingStartEvent convert(GenericEvent genericEvent) { + Map attributes = genericEvent.getAttributes(); + double time = genericEvent.getTime(); + Id vehicleId = Id.createVehicleId(attributes.get(ChargingStartEvent.ATTRIBUTE_VEHICLE)); + Id chargerId = Id.create(attributes.get(ChargingStartEvent.ATTRIBUTE_CHARGER), Charger.class); + double charge = Double.parseDouble(attributes.get(ChargingStartEvent.ATTRIBUTE_CHARGE)); + return new ChargingStartEvent(time, chargerId, vehicleId, charge); + } } diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/EnergyChargedEvent.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/EnergyChargedEvent.java index c60dfae9b67..1fb8e29396e 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/EnergyChargedEvent.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/EnergyChargedEvent.java @@ -24,6 +24,9 @@ import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.events.Event; +import org.matsim.api.core.v01.events.GenericEvent; +import org.matsim.api.core.v01.network.Link; +import org.matsim.contrib.ev.discharging.IdlingEnergyConsumptionEvent; import org.matsim.contrib.ev.infrastructure.Charger; import org.matsim.vehicles.Vehicle; @@ -80,4 +83,14 @@ public Map getAttributes() { attr.put(ATTRIBUTE_END_CHARGE, endCharge + ""); return attr; } + + public static EnergyChargedEvent convert(GenericEvent genericEvent) { + Map attributes = genericEvent.getAttributes(); + double time = genericEvent.getTime(); + Id vehicleId = Id.createVehicleId(attributes.get(EnergyChargedEvent.ATTRIBUTE_VEHICLE)); + Id chargerId = Id.create(attributes.get(EnergyChargedEvent.ATTRIBUTE_CHARGER), Charger.class); + double energy = Double.parseDouble(attributes.get(EnergyChargedEvent.ATTRIBUTE_ENERGY)); + double endCharge = Double.parseDouble(attributes.get(EnergyChargedEvent.ATTRIBUTE_END_CHARGE)); + return new EnergyChargedEvent(time, chargerId, vehicleId, energy, endCharge); + } } diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/QueuedAtChargerEvent.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/QueuedAtChargerEvent.java index b24089b7d15..77f90a67166 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/QueuedAtChargerEvent.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/QueuedAtChargerEvent.java @@ -23,6 +23,7 @@ import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.events.Event; +import org.matsim.api.core.v01.events.GenericEvent; import org.matsim.contrib.ev.infrastructure.Charger; import org.matsim.vehicles.Vehicle; @@ -60,4 +61,12 @@ public Map getAttributes() { attr.put(ATTRIBUTE_VEHICLE, vehicleId.toString()); return attr; } + + public static QueuedAtChargerEvent convert(GenericEvent genericEvent) { + Map attributes = genericEvent.getAttributes(); + double time = genericEvent.getTime(); + Id vehicleId = Id.createVehicleId(attributes.get(QueuedAtChargerEvent.ATTRIBUTE_VEHICLE)); + Id chargerId = Id.create(attributes.get(QueuedAtChargerEvent.ATTRIBUTE_CHARGER), Charger.class); + return new QueuedAtChargerEvent(time, chargerId, vehicleId); + } } diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/QuitQueueAtChargerEvent.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/QuitQueueAtChargerEvent.java index d38ad2323e1..3eab78fbde8 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/QuitQueueAtChargerEvent.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/charging/QuitQueueAtChargerEvent.java @@ -23,6 +23,7 @@ import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.events.Event; +import org.matsim.api.core.v01.events.GenericEvent; import org.matsim.contrib.ev.infrastructure.Charger; import org.matsim.vehicles.Vehicle; @@ -60,4 +61,12 @@ public Map getAttributes() { attr.put(ATTRIBUTE_VEHICLE, vehicleId.toString()); return attr; } + + public static QuitQueueAtChargerEvent convert(GenericEvent genericEvent) { + Map attributes = genericEvent.getAttributes(); + double time = genericEvent.getTime(); + Id vehicleId = Id.createVehicleId(attributes.get(QuitQueueAtChargerEvent.ATTRIBUTE_VEHICLE)); + Id chargerId = Id.create(attributes.get(QuitQueueAtChargerEvent.ATTRIBUTE_CHARGER), Charger.class); + return new QuitQueueAtChargerEvent(time, chargerId, vehicleId); + } } diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DrivingEnergyConsumptionEvent.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DrivingEnergyConsumptionEvent.java index 3289446a72e..e18b3ed0bbd 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DrivingEnergyConsumptionEvent.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DrivingEnergyConsumptionEvent.java @@ -21,9 +21,12 @@ package org.matsim.contrib.ev.discharging; 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.vehicles.Vehicle; +import java.util.Map; + /** * @author Michal Maciejewski (michalm) */ @@ -38,4 +41,14 @@ public DrivingEnergyConsumptionEvent(double time, Id vehicleId, Id attributes = genericEvent.getAttributes(); + double time = genericEvent.getTime(); + Id vehicleId = Id.createVehicleId(attributes.get(DrivingEnergyConsumptionEvent.ATTRIBUTE_VEHICLE)); + Id linkId = Id.createLinkId(attributes.get(DrivingEnergyConsumptionEvent.ATTRIBUTE_LINK)); + double energy = Double.parseDouble(attributes.get(DrivingEnergyConsumptionEvent.ATTRIBUTE_ENERGY)); + double endCharge = Double.parseDouble(attributes.get(DrivingEnergyConsumptionEvent.ATTRIBUTE_END_CHARGE)); + return new DrivingEnergyConsumptionEvent(time, vehicleId, linkId, energy, endCharge); + } } diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/IdlingEnergyConsumptionEvent.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/IdlingEnergyConsumptionEvent.java index fcb625ad7b6..d1e31c33841 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/IdlingEnergyConsumptionEvent.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/IdlingEnergyConsumptionEvent.java @@ -21,9 +21,12 @@ package org.matsim.contrib.ev.discharging; 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.vehicles.Vehicle; +import java.util.Map; + /** * @author Michal Maciejewski (michalm) */ @@ -38,4 +41,14 @@ public IdlingEnergyConsumptionEvent(double time, Id vehicleId, Id public String getEventType() { return EVENT_TYPE; } + + public static IdlingEnergyConsumptionEvent convert(GenericEvent genericEvent) { + Map attributes = genericEvent.getAttributes(); + double time = genericEvent.getTime(); + Id vehicleId = Id.createVehicleId(attributes.get(IdlingEnergyConsumptionEvent.ATTRIBUTE_VEHICLE)); + Id linkId = Id.createLinkId(attributes.get(IdlingEnergyConsumptionEvent.ATTRIBUTE_LINK)); + double energy = Double.parseDouble(attributes.get(IdlingEnergyConsumptionEvent.ATTRIBUTE_ENERGY)); + double endCharge = Double.parseDouble(attributes.get(IdlingEnergyConsumptionEvent.ATTRIBUTE_END_CHARGE)); + return new IdlingEnergyConsumptionEvent(time, vehicleId, linkId, energy, endCharge); + } } diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/util/EvEventsReader.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/util/EvEventsReader.java new file mode 100644 index 00000000000..46854443c9e --- /dev/null +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/util/EvEventsReader.java @@ -0,0 +1,31 @@ +package org.matsim.contrib.ev.util; + +import org.matsim.contrib.ev.charging.*; +import org.matsim.contrib.ev.discharging.DrivingEnergyConsumptionEvent; +import org.matsim.contrib.ev.discharging.IdlingEnergyConsumptionEvent; +import org.matsim.core.api.experimental.events.EventsManager; +import org.matsim.core.events.MatsimEventsReader; + +/** + * @author nkuehnel / MOIA + */ +public final class EvEventsReader { + + private EvEventsReader(){} + + public static void registerEvEventMappers(MatsimEventsReader reader) { + reader.addCustomEventMapper(DrivingEnergyConsumptionEvent.EVENT_TYPE, DrivingEnergyConsumptionEvent::convert); + reader.addCustomEventMapper(IdlingEnergyConsumptionEvent.EVENT_TYPE, IdlingEnergyConsumptionEvent::convert); + reader.addCustomEventMapper(EnergyChargedEvent.EVENT_TYPE, EnergyChargedEvent::convert); + reader.addCustomEventMapper(ChargingStartEvent.EVENT_TYPE, ChargingStartEvent::convert); + reader.addCustomEventMapper(ChargingEndEvent.EVENT_TYPE, ChargingEndEvent::convert); + reader.addCustomEventMapper(QueuedAtChargerEvent.EVENT_TYPE, QueuedAtChargerEvent::convert); + reader.addCustomEventMapper(QuitQueueAtChargerEvent.EVENT_TYPE, QuitQueueAtChargerEvent::convert); + } + + public static MatsimEventsReader createEvEventsReader(EventsManager eventsManager) { + MatsimEventsReader reader = new MatsimEventsReader(eventsManager); + registerEvEventMappers(reader); + return reader; + } +}