Skip to content

Commit

Permalink
ev: simplify DriveDischargingHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
michalmac committed Dec 27, 2023
1 parent 47d5d30 commit 72ea799
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.util.concurrent.ConcurrentLinkedQueue;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.Event;
import org.matsim.api.core.v01.events.HasLinkId;
import org.matsim.api.core.v01.events.HasVehicleId;
import org.matsim.api.core.v01.events.LinkLeaveEvent;
import org.matsim.api.core.v01.events.VehicleEntersTrafficEvent;
import org.matsim.api.core.v01.events.VehicleLeavesTrafficEvent;
Expand Down Expand Up @@ -122,28 +125,28 @@ public void setInternalInterface(InternalInterface internalInterface) {

@Override
public void doSimStep(double time) {
handleQueuedEvents(linkLeaveEvents, time, false);
handleQueuedEvents(trafficLeaveEvents, time, true);
}

private <E extends Event & HasVehicleId & HasLinkId> void handleQueuedEvents(Queue<E> queue, double time, boolean leftTraffic) {
// We want to process events in the main thread (instead of the event handling threads).
// This is to eliminate race conditions, where the battery is read/modified by many threads without proper synchronisation
while (!linkLeaveEvents.isEmpty()) {
var event = linkLeaveEvents.peek();
while (!queue.isEmpty()) {
var event = queue.peek();
if (event.getTime() == time) {
// There is a potential race condition wrt processing events between doSimStep() and handleEvent().
// To ensure a deterministic behaviour, we only process events from the previous time step.
break;
}

EvDrive evDrive = dischargeVehicle(event.getVehicleId(), event.getLinkId(), event.getTime(), time);
evDrive.movedOverNodeTime = event.getTime();
linkLeaveEvents.remove();
}

while (!trafficLeaveEvents.isEmpty()) {
var event = trafficLeaveEvents.peek();
if (event.getTime() == time) {
break;
var evDrive = dischargeVehicle(event.getVehicleId(), event.getLinkId(), event.getTime(), time);
if (leftTraffic) {
evDrives.remove(evDrive.vehicleId);
} else {
evDrive.movedOverNodeTime = event.getTime();
}

EvDrive evDrive = dischargeVehicle(event.getVehicleId(), event.getLinkId(), event.getTime(), time);
evDrives.remove(evDrive.vehicleId);
trafficLeaveEvents.remove();
queue.remove();
}
}

Expand Down

0 comments on commit 72ea799

Please sign in to comment.