From ff3693303baa6372216d283a57876733a05b6232 Mon Sep 17 00:00:00 2001 From: Michal Maciejewski Date: Wed, 27 Dec 2023 17:05:25 +0100 Subject: [PATCH 1/5] ev: do not use iterator for polling from queue --- .../ev/discharging/DriveDischargingHandler.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java index 4619ea69bd6..ef34f50b316 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java @@ -124,28 +124,26 @@ public void setInternalInterface(InternalInterface internalInterface) { public void doSimStep(double time) { // 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 - var linkLeaveIterator = linkLeaveEvents.iterator(); - while (linkLeaveIterator.hasNext()) { - var event = linkLeaveIterator.next(); + while (!linkLeaveEvents.isEmpty()) { + var event = linkLeaveEvents.peek(); if (event.getTime() == time) { break; } EvDrive evDrive = dischargeVehicle(event.getVehicleId(), event.getLinkId(), event.getTime(), time); evDrive.movedOverNodeTime = event.getTime(); - linkLeaveIterator.remove(); + linkLeaveEvents.remove(); } - var trafficLeaveIterator = trafficLeaveEvents.iterator(); - while (trafficLeaveIterator.hasNext()) { - var event = trafficLeaveIterator.next(); + while (!trafficLeaveEvents.isEmpty()) { + var event = trafficLeaveEvents.peek(); if (event.getTime() == time) { break; } EvDrive evDrive = dischargeVehicle(event.getVehicleId(), event.getLinkId(), event.getTime(), time); evDrives.remove(evDrive.vehicleId); - trafficLeaveIterator.remove(); + trafficLeaveEvents.remove(); } } From 03fb135f3d67d6fc631a18d80b11315f0123ccfe Mon Sep 17 00:00:00 2001 From: Michal Maciejewski Date: Wed, 27 Dec 2023 17:32:28 +0100 Subject: [PATCH 2/5] core: use pattern matching for instanceof in Event.getAttributes() --- .../org/matsim/api/core/v01/events/Event.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/Event.java b/matsim/src/main/java/org/matsim/api/core/v01/events/Event.java index 6476397214c..09852740b7a 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/Event.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/Event.java @@ -39,28 +39,28 @@ public Event(final double time) { } public Map getAttributes() { - Map attr = new LinkedHashMap(); + Map attr = new LinkedHashMap<>(); attr.put(ATTRIBUTE_TIME, Double.toString(this.time)); attr.put(ATTRIBUTE_TYPE, getEventType()); - if ( this instanceof HasPersonId && ((HasPersonId) this).getPersonId()!=null) { - attr.put( HasPersonId.ATTRIBUTE_PERSON, ((HasPersonId) this).getPersonId().toString() ) ; + if (this instanceof HasPersonId hasPersonId && hasPersonId.getPersonId() != null) { + attr.put(HasPersonId.ATTRIBUTE_PERSON, hasPersonId.getPersonId().toString()); // many derived types do this by themselves, for historical reasons. Since the information is put into a map, it still exists only once under that key. kai, // mar'19 } - if ( this instanceof HasFacilityId && ((HasFacilityId) this).getFacilityId()!=null ) { - attr.put( HasFacilityId.ATTRIBUTE_FACILITY, ((HasFacilityId) this).getFacilityId().toString() ); + if (this instanceof HasFacilityId hasFacilityId && hasFacilityId.getFacilityId() != null) { + attr.put(HasFacilityId.ATTRIBUTE_FACILITY, hasFacilityId.getFacilityId().toString()); } - if ( this instanceof HasLinkId && ((HasLinkId) this).getLinkId()!=null) { - attr.put( HasLinkId.ATTRIBUTE_LINK, ((HasLinkId) this).getLinkId().toString() ); + if (this instanceof HasLinkId hasLinkId && hasLinkId.getLinkId() != null) { + attr.put(HasLinkId.ATTRIBUTE_LINK, hasLinkId.getLinkId().toString()); } - if ( this instanceof BasicLocation && ((BasicLocation) this).getCoord()!=null ) { - if ( ((BasicLocation) this).getCoord()!=null ) { - attr.put( ATTRIBUTE_X, String.valueOf( ((BasicLocation) this).getCoord().getX() ) ) ; - attr.put( ATTRIBUTE_Y, String.valueOf( ((BasicLocation) this).getCoord().getY() ) ) ; + if (this instanceof BasicLocation basicLocation && basicLocation.getCoord() != null) { + if (((BasicLocation)this).getCoord() != null) { + attr.put(ATTRIBUTE_X, String.valueOf(basicLocation.getCoord().getX())); + attr.put(ATTRIBUTE_Y, String.valueOf(basicLocation.getCoord().getY())); } } - if ( this instanceof HasVehicleId && ((HasVehicleId) this).getVehicleId()!=null ) { - attr.put( HasVehicleId.ATTRIBUTE_VEHICLE, ((HasVehicleId) this).getVehicleId().toString() ); + if (this instanceof HasVehicleId hasVehicleId && hasVehicleId.getVehicleId() != null) { + attr.put(HasVehicleId.ATTRIBUTE_VEHICLE, hasVehicleId.getVehicleId().toString()); } return attr; } @@ -75,7 +75,7 @@ public final double getTime() { public void setTime(double time) { this.time = time; } - + public String toString() { Map attr = this.getAttributes() ; StringBuilder eventXML = new StringBuilder("\t Date: Wed, 27 Dec 2023 17:34:59 +0100 Subject: [PATCH 3/5] core: implement HasVehicleId in VehicleLeavesTrafficEvent --- .../matsim/api/core/v01/events/VehicleEntersTrafficEvent.java | 2 +- .../matsim/api/core/v01/events/VehicleLeavesTrafficEvent.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleEntersTrafficEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleEntersTrafficEvent.java index cf467b6c3c2..3e3968ccd4d 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleEntersTrafficEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleEntersTrafficEvent.java @@ -27,7 +27,7 @@ import org.matsim.api.core.v01.population.Person; import org.matsim.vehicles.Vehicle; -public class VehicleEntersTrafficEvent extends Event implements HasPersonId, HasLinkId, HasVehicleId{ +public class VehicleEntersTrafficEvent extends Event implements HasPersonId, HasLinkId, HasVehicleId { public static final String EVENT_TYPE = "vehicle enters traffic"; public static final String ATTRIBUTE_NETWORKMODE = "networkMode"; diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleLeavesTrafficEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleLeavesTrafficEvent.java index e39910597bb..00f00a4ec80 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleLeavesTrafficEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleLeavesTrafficEvent.java @@ -27,7 +27,7 @@ import org.matsim.api.core.v01.population.Person; import org.matsim.vehicles.Vehicle; -public class VehicleLeavesTrafficEvent extends Event implements HasPersonId, HasLinkId { +public class VehicleLeavesTrafficEvent extends Event implements HasPersonId, HasLinkId, HasVehicleId { public static final String EVENT_TYPE = "vehicle leaves traffic"; public static final String ATTRIBUTE_VEHICLE = "vehicle"; From 47d5d307895ab04250b779d6e6e8a2fbffa8b6f9 Mon Sep 17 00:00:00 2001 From: Michal Maciejewski Date: Wed, 27 Dec 2023 21:07:38 +0100 Subject: [PATCH 4/5] core: simplify getAttributes() in core events --- .../api/core/v01/events/LinkEnterEvent.java | 17 +++++++------ .../api/core/v01/events/LinkLeaveEvent.java | 13 +++++----- .../core/v01/events/PersonArrivalEvent.java | 15 ++++++------ .../core/v01/events/PersonDepartureEvent.java | 15 ++++++------ .../v01/events/PersonEntersVehicleEvent.java | 15 ++++++------ .../v01/events/PersonLeavesVehicleEvent.java | 13 +++++----- .../api/core/v01/events/PersonStuckEvent.java | 17 ++++++------- .../core/v01/events/VehicleAbortsEvent.java | 19 +++++++-------- .../v01/events/VehicleEntersTrafficEvent.java | 24 ++++++++----------- .../v01/events/VehicleLeavesTrafficEvent.java | 21 +++++++--------- 10 files changed, 75 insertions(+), 94 deletions(-) diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/LinkEnterEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/LinkEnterEvent.java index 2f6ec7165f3..63250efa333 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/LinkEnterEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/LinkEnterEvent.java @@ -27,7 +27,7 @@ import java.util.Map; -public class LinkEnterEvent extends Event implements HasLinkId, HasVehicleId{ +public class LinkEnterEvent extends Event implements HasLinkId, HasVehicleId { public static final String EVENT_TYPE = "entered link"; @@ -49,29 +49,28 @@ public LinkEnterEvent(final double time, final Id vehicleId, final Id getDriverId() { throw new RuntimeException( LinkLeaveEvent.missingDriverIdMessage ) ; - } + } @Override public Id getLinkId() { return this.linkId; } - + public Id getVehicleId() { return vehicleId; } @Override public Map getAttributes() { - Map attr = super.getAttributes(); - attr.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString()); - attr.put(ATTRIBUTE_LINK, this.linkId.toString()); - return attr; + Map atts = super.getAttributes(); + // linkId, vehicleId handled by superclass + return atts; } } diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/LinkLeaveEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/LinkLeaveEvent.java index a2524206ece..06018363204 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/LinkLeaveEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/LinkLeaveEvent.java @@ -27,7 +27,7 @@ import java.util.Map; -public class LinkLeaveEvent extends Event implements HasLinkId, HasVehicleId{ +public class LinkLeaveEvent extends Event implements HasLinkId, HasVehicleId { public static final String EVENT_TYPE = "left link"; public static final String ATTRIBUTE_LINK = "link"; @@ -53,7 +53,7 @@ public String getEventType() { } /** - * Please use getVehicleId() instead. + * Please use getVehicleId() instead. * Vehicle-driver relations can be made by Wait2Link (now: VehicleEntersTraffic) and VehicleLeavesTraffic Events. */ @Deprecated @@ -69,12 +69,11 @@ public Id getLinkId() { public Id getVehicleId() { return vehicleId; } - + @Override public Map getAttributes() { - Map attr = super.getAttributes(); - attr.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString()); - attr.put(ATTRIBUTE_LINK, this.linkId.toString()); - return attr; + Map atts = super.getAttributes(); + // linkId, vehicleId handled by superclass + return atts; } } diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/PersonArrivalEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/PersonArrivalEvent.java index a6c49d62198..6e947aaff11 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/PersonArrivalEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/PersonArrivalEvent.java @@ -26,10 +26,10 @@ import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.population.Person; -public class PersonArrivalEvent extends Event implements HasPersonId { +public class PersonArrivalEvent extends Event implements HasPersonId, HasLinkId { public static final String EVENT_TYPE = "arrival"; - + public static final String ATTRIBUTE_PERSON = "person"; public static final String ATTRIBUTE_LINK = "link"; public static final String ATTRIBUTE_LEGMODE = "legMode"; @@ -45,7 +45,7 @@ public PersonArrivalEvent(final double time, final Id agentId, final Id< this.legMode = legMode; this.personId = agentId; } - + public Id getPersonId() { return this.personId; } @@ -53,20 +53,19 @@ public Id getPersonId() { public Id getLinkId() { return this.linkId; } - + public String getLegMode() { return this.legMode; } - + public String getEventType() { return EVENT_TYPE; } - + @Override public Map getAttributes() { Map attr = super.getAttributes(); - attr.put(ATTRIBUTE_PERSON, this.personId.toString()); - attr.put(ATTRIBUTE_LINK, (this.linkId == null ? null : this.linkId.toString())); + // linkId, personId handled by superclass if (this.legMode != null) { attr.put(ATTRIBUTE_LEGMODE, this.legMode); } diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/PersonDepartureEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/PersonDepartureEvent.java index 0dc5555778f..425d8ad4e78 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/PersonDepartureEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/PersonDepartureEvent.java @@ -26,7 +26,7 @@ import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.population.Person; -public class PersonDepartureEvent extends Event implements HasPersonId { +public class PersonDepartureEvent extends Event implements HasPersonId, HasLinkId { public static final String EVENT_TYPE = "departure"; @@ -47,24 +47,24 @@ public PersonDepartureEvent(final double time, final Id agentId, final I this.personId = agentId; this.routingMode = routingMode; } - + @Override public Id getPersonId() { return this.personId; } - + public Id getLinkId() { return this.linkId; } - + public String getLegMode() { return this.legMode; } - + public String getRoutingMode() { return routingMode; } - + @Override public String getEventType() { return EVENT_TYPE; @@ -73,8 +73,7 @@ public String getEventType() { @Override public Map getAttributes() { Map attr = super.getAttributes(); - attr.put(ATTRIBUTE_PERSON, this.personId.toString()); - attr.put(ATTRIBUTE_LINK, (this.linkId == null ? null : this.linkId.toString())); + // linkId, personId handled by superclass if (this.legMode != null) { attr.put(ATTRIBUTE_LEGMODE, this.legMode); } diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/PersonEntersVehicleEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/PersonEntersVehicleEvent.java index e862e1c8095..a3e051f44c4 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/PersonEntersVehicleEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/PersonEntersVehicleEvent.java @@ -29,7 +29,7 @@ /** * @author mrieser */ -public class PersonEntersVehicleEvent extends Event implements HasPersonId { +public class PersonEntersVehicleEvent extends Event implements HasPersonId, HasVehicleId { public static final String EVENT_TYPE = "PersonEntersVehicle"; public static final String ATTRIBUTE_PERSON = "person"; @@ -43,7 +43,7 @@ public PersonEntersVehicleEvent(final double time, final Id personId, fi this.personId = personId; this.vehicleId = vehicleId; } - + public Id getVehicleId() { return this.vehicleId; } @@ -55,8 +55,8 @@ public void setVehicleId(Id vehicleId) { @Override public Id getPersonId() { return this.personId; - } - + } + @Override public String getEventType() { return EVENT_TYPE; @@ -64,9 +64,8 @@ public String getEventType() { @Override public Map getAttributes() { - Map attrs = super.getAttributes(); - attrs.put(ATTRIBUTE_PERSON, this.personId.toString()); - attrs.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString()); - return attrs; + Map atts = super.getAttributes(); + // personId, vehicleId handled by superclass + return atts; } } diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/PersonLeavesVehicleEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/PersonLeavesVehicleEvent.java index c241f9753e3..27a3b5b2bdd 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/PersonLeavesVehicleEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/PersonLeavesVehicleEvent.java @@ -30,12 +30,12 @@ * * @author mrieser */ -public class PersonLeavesVehicleEvent extends Event implements HasPersonId { +public class PersonLeavesVehicleEvent extends Event implements HasPersonId, HasVehicleId { public static final String EVENT_TYPE = "PersonLeavesVehicle"; public static final String ATTRIBUTE_PERSON = "person"; public static final String ATTRIBUTE_VEHICLE = "vehicle"; - + private final Id personId; private Id vehicleId; @@ -44,12 +44,12 @@ public class PersonLeavesVehicleEvent extends Event implements HasPersonId { this.personId = personId; this.vehicleId = vehicleId; } - + @Override public Id getPersonId() { return this.personId; } - + public Id getVehicleId() { return this.vehicleId; } @@ -62,12 +62,11 @@ public void setVehicleId(Id vehicleId) { public String getEventType() { return EVENT_TYPE; } - + @Override public Map getAttributes() { Map attrs = super.getAttributes(); - attrs.put(ATTRIBUTE_PERSON, this.personId.toString()); - attrs.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString()); + // personId, vehicleId handled by superclass return attrs; } } diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/PersonStuckEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/PersonStuckEvent.java index 150495b08e9..b2439dd8b96 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/PersonStuckEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/PersonStuckEvent.java @@ -26,10 +26,10 @@ import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.population.Person; -public class PersonStuckEvent extends Event implements HasPersonId { +public class PersonStuckEvent extends Event implements HasPersonId, HasLinkId { public static final String EVENT_TYPE = "stuckAndAbort"; - + public static final String ATTRIBUTE_LINK = "link"; public static final String ATTRIBUTE_LEGMODE = "legMode"; public static final String ATTRIBUTE_PERSON = "person"; @@ -44,11 +44,11 @@ public PersonStuckEvent(final double time, final Id agentId, final Id
  • getPersonId() { return this.personId; } - + public Id getLinkId() { return this.linkId; } @@ -56,22 +56,19 @@ public Id getLinkId() { public String getLegMode() { return this.legMode; } - + @Override public String getEventType() { return EVENT_TYPE; } - + @Override public Map getAttributes() { Map attr = super.getAttributes(); - if (this.linkId != null) { - attr.put(ATTRIBUTE_LINK, this.linkId.toString()); - } + // personId, linkId handled by superclass if (this.legMode != null) { attr.put(ATTRIBUTE_LEGMODE, this.legMode); } - attr.put(ATTRIBUTE_PERSON, this.personId.toString()); return attr; } } diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleAbortsEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleAbortsEvent.java index 39e6782cace..a623a758bd0 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleAbortsEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleAbortsEvent.java @@ -26,10 +26,10 @@ import org.matsim.api.core.v01.network.Link; import org.matsim.vehicles.Vehicle; -public class VehicleAbortsEvent extends Event { +public class VehicleAbortsEvent extends Event implements HasLinkId, HasVehicleId { public static final String EVENT_TYPE = "vehicle aborts"; - + public static final String ATTRIBUTE_LINK = "link"; public static final String ATTRIBUTE_VEHICLE = "vehicle"; @@ -41,11 +41,11 @@ public VehicleAbortsEvent(final double time, final Id vehicleId, final this.vehicleId = vehicleId; this.linkId = linkId; } - + public Id getVehicleId() { return this.vehicleId; } - + public Id getLinkId() { return this.linkId; } @@ -54,12 +54,11 @@ public Id getLinkId() { public String getEventType() { return EVENT_TYPE; } - + @Override public Map getAttributes() { - Map attr = super.getAttributes(); - attr.put(ATTRIBUTE_LINK, this.linkId.toString()); - attr.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString()); - return attr; + Map atts = super.getAttributes(); + // linkId, vehicleId handled by superclass + return atts; } -} \ No newline at end of file +} diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleEntersTrafficEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleEntersTrafficEvent.java index 3e3968ccd4d..2316507f115 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleEntersTrafficEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleEntersTrafficEvent.java @@ -39,7 +39,7 @@ public class VehicleEntersTrafficEvent extends Event implements HasPersonId, Has private final String networkMode; private final double relativePositionOnLink; - + public VehicleEntersTrafficEvent(final double time, final Id driverId, final Id linkId, Id vehicleId, String networkMode, double relativePositionOnLink) { super(time); this.driverId = driverId; @@ -48,42 +48,38 @@ public VehicleEntersTrafficEvent(final double time, final Id driverId, f this.networkMode = networkMode; this.relativePositionOnLink = relativePositionOnLink; } - + @Override public Id getPersonId() { return this.driverId; - } - + } + @Override public Id getLinkId() { return this.linkId; } - + public Id getVehicleId() { return vehicleId; } - + @Override public String getEventType() { return EVENT_TYPE; } - + public String getNetworkMode() { return networkMode; } - + public double getRelativePositionOnLink() { return relativePositionOnLink; } - + @Override public Map getAttributes() { Map attr = super.getAttributes(); -// attr.put(ATTRIBUTE_DRIVER, this.driverId.toString()); -// attr.put(ATTRIBUTE_LINK, (this.linkId == null ? null : this.linkId.toString())); -// if (this.vehicleId != null) { -// attr.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString()); -// } + // personId, linkId, vehicleId handled by superclass if (this.networkMode != null) { attr.put(ATTRIBUTE_NETWORKMODE, networkMode); } diff --git a/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleLeavesTrafficEvent.java b/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleLeavesTrafficEvent.java index 00f00a4ec80..600c499063d 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleLeavesTrafficEvent.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/events/VehicleLeavesTrafficEvent.java @@ -53,30 +53,30 @@ public VehicleLeavesTrafficEvent(final double time, final Id driverId, f this.relativePositionOnLink = relativePositionOnLink; } - + @Override public Id getPersonId() { return this.driverId; - } - + } + @Override public Id getLinkId() { return this.linkId; } - + public Id getVehicleId() { return vehicleId; } - + @Override public String getEventType() { return EVENT_TYPE; } - + public String getNetworkMode() { return networkMode; } - + public double getRelativePositionOnLink() { return relativePositionOnLink; } @@ -84,16 +84,11 @@ public double getRelativePositionOnLink() { @Override public Map getAttributes() { Map attr = super.getAttributes(); - attr.put(ATTRIBUTE_DRIVER, this.driverId.toString()); - attr.put(ATTRIBUTE_LINK, (this.linkId == null ? null : this.linkId.toString())); - if (this.vehicleId != null) { - attr.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString()); - } + // personId, linkId, vehicleId handled by superclass if (this.networkMode != null) { attr.put(ATTRIBUTE_NETWORKMODE, networkMode); } attr.put(ATTRIBUTE_POSITION, Double.toString(this.relativePositionOnLink)); - return attr; } } From 72ea7991c63208e03891a0186a777c195f887d5f Mon Sep 17 00:00:00 2001 From: Michal Maciejewski Date: Wed, 27 Dec 2023 21:10:12 +0100 Subject: [PATCH 5/5] ev: simplify DriveDischargingHandler --- .../discharging/DriveDischargingHandler.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java index ef34f50b316..1f87d091b7b 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java @@ -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; @@ -122,28 +125,28 @@ public void setInternalInterface(InternalInterface internalInterface) { @Override public void doSimStep(double time) { + handleQueuedEvents(linkLeaveEvents, time, false); + handleQueuedEvents(trafficLeaveEvents, time, true); + } + + private void handleQueuedEvents(Queue 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(); } }