Skip to content

Commit

Permalink
add utility class for ev events reading
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel committed Mar 21, 2024
1 parent 0d2ce6f commit b89d5fa
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -68,4 +69,13 @@ public Map<String, String> getAttributes() {
attr.put(ATTRIBUTE_CHARGE, charge + "");
return attr;
}

public static ChargingEndEvent convert(GenericEvent genericEvent) {
Map<String, String> attributes = genericEvent.getAttributes();
double time = genericEvent.getTime();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(ChargingEndEvent.ATTRIBUTE_VEHICLE));
Id<Charger> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -68,4 +69,13 @@ public Map<String, String> getAttributes() {
attr.put(ATTRIBUTE_CHARGE, charge + "");
return attr;
}

public static ChargingStartEvent convert(GenericEvent genericEvent) {
Map<String, String> attributes = genericEvent.getAttributes();
double time = genericEvent.getTime();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(ChargingStartEvent.ATTRIBUTE_VEHICLE));
Id<Charger> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -80,4 +83,14 @@ public Map<String, String> getAttributes() {
attr.put(ATTRIBUTE_END_CHARGE, endCharge + "");
return attr;
}

public static EnergyChargedEvent convert(GenericEvent genericEvent) {
Map<String, String> attributes = genericEvent.getAttributes();
double time = genericEvent.getTime();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(EnergyChargedEvent.ATTRIBUTE_VEHICLE));
Id<Charger> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -60,4 +61,12 @@ public Map<String, String> getAttributes() {
attr.put(ATTRIBUTE_VEHICLE, vehicleId.toString());
return attr;
}

public static QueuedAtChargerEvent convert(GenericEvent genericEvent) {
Map<String, String> attributes = genericEvent.getAttributes();
double time = genericEvent.getTime();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(QueuedAtChargerEvent.ATTRIBUTE_VEHICLE));
Id<Charger> chargerId = Id.create(attributes.get(QueuedAtChargerEvent.ATTRIBUTE_CHARGER), Charger.class);
return new QueuedAtChargerEvent(time, chargerId, vehicleId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -60,4 +61,12 @@ public Map<String, String> getAttributes() {
attr.put(ATTRIBUTE_VEHICLE, vehicleId.toString());
return attr;
}

public static QuitQueueAtChargerEvent convert(GenericEvent genericEvent) {
Map<String, String> attributes = genericEvent.getAttributes();
double time = genericEvent.getTime();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(QuitQueueAtChargerEvent.ATTRIBUTE_VEHICLE));
Id<Charger> chargerId = Id.create(attributes.get(QuitQueueAtChargerEvent.ATTRIBUTE_CHARGER), Charger.class);
return new QuitQueueAtChargerEvent(time, chargerId, vehicleId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand All @@ -38,4 +41,14 @@ public DrivingEnergyConsumptionEvent(double time, Id<Vehicle> vehicleId, Id<Link
public String getEventType() {
return EVENT_TYPE;
}

public static DrivingEnergyConsumptionEvent convert(GenericEvent genericEvent) {
Map<String, String> attributes = genericEvent.getAttributes();
double time = genericEvent.getTime();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(DrivingEnergyConsumptionEvent.ATTRIBUTE_VEHICLE));
Id<Link> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand All @@ -38,4 +41,14 @@ public IdlingEnergyConsumptionEvent(double time, Id<Vehicle> vehicleId, Id<Link>
public String getEventType() {
return EVENT_TYPE;
}

public static IdlingEnergyConsumptionEvent convert(GenericEvent genericEvent) {
Map<String, String> attributes = genericEvent.getAttributes();
double time = genericEvent.getTime();
Id<Vehicle> vehicleId = Id.createVehicleId(attributes.get(IdlingEnergyConsumptionEvent.ATTRIBUTE_VEHICLE));
Id<Link> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit b89d5fa

Please sign in to comment.