Skip to content

Commit

Permalink
Merge pull request #3019 from matsim-org/refactor-events-in-ev
Browse files Browse the repository at this point in the history
simplify DriveDischargingHandler
  • Loading branch information
michalmac authored Dec 27, 2023
2 parents edb23db + 72ea799 commit 7e682d9
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 127 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,30 +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
var linkLeaveIterator = linkLeaveEvents.iterator();
while (linkLeaveIterator.hasNext()) {
var event = linkLeaveIterator.next();
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();
linkLeaveIterator.remove();
}

var trafficLeaveIterator = trafficLeaveEvents.iterator();
while (trafficLeaveIterator.hasNext()) {
var event = trafficLeaveIterator.next();
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);
trafficLeaveIterator.remove();
queue.remove();
}
}

Expand Down
28 changes: 14 additions & 14 deletions matsim/src/main/java/org/matsim/api/core/v01/events/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,28 @@ public Event(final double time) {
}

public Map<String, String> getAttributes() {
Map<String, String> attr = new LinkedHashMap<String, String>();
Map<String, String> 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;
}
Expand All @@ -75,7 +75,7 @@ public final double getTime() {
public void setTime(double time) {
this.time = time;
}

public String toString() {
Map<String,String> attr = this.getAttributes() ;
StringBuilder eventXML = new StringBuilder("\t<event ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -49,29 +49,28 @@ public LinkEnterEvent(final double time, final Id<Vehicle> vehicleId, final Id<L
public String getEventType() {
return EVENT_TYPE;
}

/**
* Please use getVehicleId() instead.
* Please use getVehicleId() instead.
* Vehicle-driver relations can be made by {@link VehicleEntersTrafficEvent} and {@link VehicleLeavesTrafficEvent}.
*/
@Deprecated
public Id<Person> getDriverId() {
throw new RuntimeException( LinkLeaveEvent.missingDriverIdMessage ) ;
}
}
@Override
public Id<Link> getLinkId() {
return this.linkId;
}

public Id<Vehicle> getVehicleId() {
return vehicleId;
}

@Override
public Map<String, String> getAttributes() {
Map<String, String> attr = super.getAttributes();
attr.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString());
attr.put(ATTRIBUTE_LINK, this.linkId.toString());
return attr;
Map<String, String> atts = super.getAttributes();
// linkId, vehicleId handled by superclass
return atts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand All @@ -69,12 +69,11 @@ public Id<Link> getLinkId() {
public Id<Vehicle> getVehicleId() {
return vehicleId;
}

@Override
public Map<String, String> getAttributes() {
Map<String, String> attr = super.getAttributes();
attr.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString());
attr.put(ATTRIBUTE_LINK, this.linkId.toString());
return attr;
Map<String, String> atts = super.getAttributes();
// linkId, vehicleId handled by superclass
return atts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -45,28 +45,27 @@ public PersonArrivalEvent(final double time, final Id<Person> agentId, final Id<
this.legMode = legMode;
this.personId = agentId;
}

public Id<Person> getPersonId() {
return this.personId;
}

public Id<Link> getLinkId() {
return this.linkId;
}

public String getLegMode() {
return this.legMode;
}

public String getEventType() {
return EVENT_TYPE;
}

@Override
public Map<String, String> getAttributes() {
Map<String, String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -47,24 +47,24 @@ public PersonDepartureEvent(final double time, final Id<Person> agentId, final I
this.personId = agentId;
this.routingMode = routingMode;
}

@Override
public Id<Person> getPersonId() {
return this.personId;
}

public Id<Link> getLinkId() {
return this.linkId;
}

public String getLegMode() {
return this.legMode;
}

public String getRoutingMode() {
return routingMode;
}

@Override
public String getEventType() {
return EVENT_TYPE;
Expand All @@ -73,8 +73,7 @@ public String getEventType() {
@Override
public Map<String, String> getAttributes() {
Map<String, String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -43,7 +43,7 @@ public PersonEntersVehicleEvent(final double time, final Id<Person> personId, fi
this.personId = personId;
this.vehicleId = vehicleId;
}

public Id<Vehicle> getVehicleId() {
return this.vehicleId;
}
Expand All @@ -55,18 +55,17 @@ public void setVehicleId(Id<Vehicle> vehicleId) {
@Override
public Id<Person> getPersonId() {
return this.personId;
}
}

@Override
public String getEventType() {
return EVENT_TYPE;
}

@Override
public Map<String, String> getAttributes() {
Map<String, String> attrs = super.getAttributes();
attrs.put(ATTRIBUTE_PERSON, this.personId.toString());
attrs.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString());
return attrs;
Map<String, String> atts = super.getAttributes();
// personId, vehicleId handled by superclass
return atts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person> personId;
private Id<Vehicle> vehicleId;

Expand All @@ -44,12 +44,12 @@ public class PersonLeavesVehicleEvent extends Event implements HasPersonId {
this.personId = personId;
this.vehicleId = vehicleId;
}

@Override
public Id<Person> getPersonId() {
return this.personId;
}

public Id<Vehicle> getVehicleId() {
return this.vehicleId;
}
Expand All @@ -62,12 +62,11 @@ public void setVehicleId(Id<Vehicle> vehicleId) {
public String getEventType() {
return EVENT_TYPE;
}

@Override
public Map<String, String> getAttributes() {
Map<String, String> attrs = super.getAttributes();
attrs.put(ATTRIBUTE_PERSON, this.personId.toString());
attrs.put(ATTRIBUTE_VEHICLE, this.vehicleId.toString());
// personId, vehicleId handled by superclass
return attrs;
}
}
Loading

0 comments on commit 7e682d9

Please sign in to comment.