Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
add two EventsHandler to build connections between driver - vehicle -…
Browse files Browse the repository at this point in the history
… carrier
  • Loading branch information
kt86 committed Aug 7, 2024
1 parent 034b2fe commit 81ee1c5
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
*********************************************************************** *
* project: org.matsim.*
* *
* *********************************************************************** *
* *
* copyright : (C) 2024 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* ***********************************************************************
*/

package org.matsim.freight.logistics.analysis;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.VehicleEntersTrafficEvent;
import org.matsim.api.core.v01.events.VehicleLeavesTrafficEvent;
import org.matsim.api.core.v01.events.handler.VehicleEntersTrafficEventHandler;
import org.matsim.api.core.v01.events.handler.VehicleLeavesTrafficEventHandler;
import org.matsim.api.core.v01.population.Person;
import org.matsim.vehicles.Vehicle;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Driver2VehicleEventHandler implements VehicleEntersTrafficEventHandler, VehicleLeavesTrafficEventHandler {

private final Map<Id<Person>, Id<Vehicle>> driversVehicles = new ConcurrentHashMap<>();

@Override
public void reset(int iteration) {
driversVehicles.clear();
}

@Override
public void handleEvent(VehicleEntersTrafficEvent event) {
driversVehicles.put(event.getPersonId(), event.getVehicleId());
}

@Override
public void handleEvent(VehicleLeavesTrafficEvent event) {
driversVehicles.remove(event.getPersonId());
}

/**
* @param personId the unique driver identifier.
* @return vehicle id of the driver's vehicle
*/
public Id<Vehicle> getVehicleOfDriver(Id<Person> personId) {
return driversVehicles.get(personId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
*********************************************************************** *
* project: org.matsim.*
* *
* *********************************************************************** *
* *
* copyright : (C) 2024 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* ***********************************************************************
*/

package org.matsim.freight.logistics.analysis;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.VehicleEntersTrafficEvent;
import org.matsim.api.core.v01.events.VehicleLeavesTrafficEvent;
import org.matsim.api.core.v01.population.Person;
import org.matsim.freight.carriers.Carrier;
import org.matsim.freight.carriers.events.CarrierTourEndEvent;
import org.matsim.freight.carriers.events.CarrierTourStartEvent;
import org.matsim.freight.carriers.events.eventhandler.CarrierTourEndEventHandler;
import org.matsim.freight.carriers.events.eventhandler.CarrierTourStartEventHandler;
import org.matsim.vehicles.Vehicle;

public class Vehicle2CarrierEventHandler implements CarrierTourStartEventHandler, CarrierTourEndEventHandler {

private final Map<Id<Vehicle>, Id<Carrier>> vehicle2carrier = new ConcurrentHashMap<>();

@Override
public void reset(int iteration) {
vehicle2carrier.clear();
}

@Override
public void handleEvent(CarrierTourStartEvent event) {
vehicle2carrier.put(event.getVehicleId(), event.getCarrierId());
}

@Override
public void handleEvent(CarrierTourEndEvent event) {
vehicle2carrier.remove(event.getVehicleId());
}

/**
* @param vehicleId the unique vehicle Id
* @return id of the vehicle's carrier
*/
public Id<Carrier> getCarrierOfVehicle(Id<Vehicle> vehicleId) {
return vehicle2carrier.get(vehicleId);
}

}

0 comments on commit 81ee1c5

Please sign in to comment.