Skip to content

Commit

Permalink
Merge pull request #3110 from matsim-org/maintenanceOfPersonVehicles
Browse files Browse the repository at this point in the history
make the methods to set PersonVehicles more easily accessible
  • Loading branch information
kainagel authored Feb 15, 2024
2 parents f16d044 + c8c1f49 commit 68a5246
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
23 changes: 23 additions & 0 deletions matsim/src/main/java/org/matsim/core/population/PersonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.vehicles.Vehicle;
import org.matsim.vehicles.VehicleType;
import org.matsim.vehicles.VehicleUtils;

public final class PersonUtils {
private PersonUtils() {
Expand Down Expand Up @@ -225,4 +229,23 @@ public static TreeSet<String> getTravelcards(Person person) {
public static boolean isSelected(Plan plan) {
return plan.getPerson().getSelectedPlan() == plan;
}

/**
* Attaches vehicle types to a person, so that the router knows which vehicle to use for which mode and person.
* @param modeToVehicleType mode string mapped to vehicle type ids. The provided map is copied and stored as unmodifiable map.
*/
public static void insertVehicleTypesIntoPersonAttributes(Person person, Map<String, Id<VehicleType>> modeToVehicleType ) {
VehicleUtils.insertVehicleTypesIntoPersonAttributes( person, modeToVehicleType );
}
/**
* Attaches vehicle ids to a person, so that the router knows which vehicle to use for which mode and person.
*
* @param modeToVehicle mode string mapped to vehicle ids. The provided map is copied and stored as unmodifiable map.
* If a mode key already exists in the persons's attributes it is overridden. Otherwise, existing
* and provided values are merged into one map
* We use PersonVehicle Class in order to have a dedicated PersonVehicleAttributeConverter to/from XML
*/
public static void insertVehicleIdsIntoPersonAttributes(Person person, Map<String, Id<Vehicle>> modeToVehicle ) {
VehicleUtils.insertVehicleIdsIntoPersonAttributes( person, modeToVehicle );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
import org.matsim.facilities.ActivityFacility;
import org.matsim.utils.objectattributes.attributable.Attributes;
import org.matsim.utils.objectattributes.attributable.AttributesUtils;
import org.matsim.vehicles.Vehicle;
import org.matsim.vehicles.VehicleType;
import org.matsim.vehicles.VehicleUtils;

/**
* @author nagel, ikaddoura
Expand Down Expand Up @@ -1266,5 +1269,23 @@ public static Person findPerson( Id<Person> personId, Scenario scenario ) {
return person ;
}

/**
* Attaches vehicle types to a person, so that the router knows which vehicle to use for which mode and person.
* @param modeToVehicleType mode string mapped to vehicle type ids. The provided map is copied and stored as unmodifiable map.
*/
public static void insertVehicleTypesIntoPersonAttributes(Person person, Map<String, Id<VehicleType>> modeToVehicleType ) {
VehicleUtils.insertVehicleTypesIntoPersonAttributes( person, modeToVehicleType );
}
/**
* Attaches vehicle ids to a person, so that the router knows which vehicle to use for which mode and person.
*
* @param modeToVehicle mode string mapped to vehicle ids. The provided map is copied and stored as unmodifiable map.
* If a mode key already exists in the persons's attributes it is overridden. Otherwise, existing
* and provided values are merged into one map
* We use PersonVehicle Class in order to have a dedicated PersonVehicleAttributeConverter to/from XML
*/
public static void insertVehicleIdsIntoPersonAttributes(Person person, Map<String, Id<Vehicle>> modeToVehicle ) {
VehicleUtils.insertVehicleIdsIntoPersonAttributes( person, modeToVehicle );
}

}
47 changes: 35 additions & 12 deletions matsim/src/main/java/org/matsim/vehicles/VehicleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ public static Map<String, Id<Vehicle>> getVehicleIds(Person person) {
var personVehicles = (PersonVehicles) person.getAttributes().getAttribute(VehicleUtils.VEHICLE_ATTRIBUTE_KEY);
if (personVehicles == null) {
throw new RuntimeException("Could not retrieve vehicle id from person: " + person.getId().toString() +
". \nIf you are not using config.qsim().getVehicleSource() with 'defaultVehicle' or 'modeVehicleTypesFromVehiclesData' you have to provide " +
"a vehicle for each mode for each person. Attach a PersonVehicles instance (containing a map of mode:String -> id:Id<Vehicle>) with key 'vehicles' as person attribute to each person." +
"\n VehicleUtils.insertVehicleIdIntoAttributes does this for you.");
". \nIf you are not using config.qsim().getVehicleSource() with 'defaultVehicle' or 'modeVehicleTypesFromVehiclesData' you have to provide " +
"a vehicle for each mode for each person. Attach a PersonVehicles instance (containing a map of mode:String -> id:Id<Vehicle>) with key 'vehicles' as person attribute to each person." +
"\n VehicleUtils.insertVehicleIdIntoAttributes does this for you.");
}
return personVehicles.getModeVehicles();
}
Expand All @@ -167,23 +167,37 @@ public static Id<Vehicle> getVehicleId(Person person, String mode) {
Map<String, Id<Vehicle>> vehicleIds = getVehicleIds(person);
if (!vehicleIds.containsKey(mode)) {
throw new RuntimeException("Could not retrieve vehicle id from person: " + person.getId().toString() + " for mode: " + mode +
". \nIf you are not using config.qsim().getVehicleSource() with 'defaultVehicle' or 'modeVehicleTypesFromVehiclesData' you have to provide " +
"a vehicle for each mode for each person. Attach a PersonVehicles instance (containing a map of mode:String -> id:Id<Vehicle>) with key 'vehicles' as person attribute to each person." +
"\n VehicleUtils.insertVehicleIdIntoAttributes does this for you."
". \nIf you are not using config.qsim().getVehicleSource() with 'defaultVehicle' or 'modeVehicleTypesFromVehiclesData' you have to provide " +
"a vehicle for each mode for each person. Attach a PersonVehicles instance (containing a map of mode:String -> id:Id<Vehicle>) with key 'vehicles' as person attribute to each person." +
"\n VehicleUtils.insertVehicleIdIntoAttributes does this for you."
);
}
return vehicleIds.get(mode);
}

/**
/**
* Attaches vehicle ids to a person, so that the router knows which vehicle to use for which mode and person.
*
* @param modeToVehicle mode string mapped to vehicle ids. The provided map is copied and stored as unmodifiable map.
* If a mode key already exists in the persons's attributes it is overridden. Otherwise, existing
* and provided values are merged into one map
* We use PersonVehicle Class in order to have a dedicated PersonVehicleAttributeConverter to/from XML
*
* @deprecated inline to more expressive method
*/
public static void insertVehicleIdsIntoAttributes(Person person, Map<String, Id<Vehicle>> modeToVehicle) {
@Deprecated
public static void insertVehicleIdsIntoAttributes(Person person, Map<String, Id<Vehicle>> modeToVehicle){
insertVehicleIdsIntoPersonAttributes( person, modeToVehicle );
}
/**
* Attaches vehicle ids to a person, so that the router knows which vehicle to use for which mode and person.
*
* @param modeToVehicle mode string mapped to vehicle ids. The provided map is copied and stored as unmodifiable map.
* If a mode key already exists in the persons's attributes it is overridden. Otherwise, existing
* and provided values are merged into one map
* We use PersonVehicle Class in order to have a dedicated PersonVehicleAttributeConverter to/from XML
*/
public static void insertVehicleIdsIntoPersonAttributes(Person person, Map<String, Id<Vehicle>> modeToVehicle) {
Object attr = person.getAttributes().getAttribute(VEHICLE_ATTRIBUTE_KEY);
// copy in case it's a UnmodifiableMap
Map<String, Id<Vehicle>> modeToVehicleCopy = new HashMap<>(modeToVehicle);
Expand All @@ -200,8 +214,17 @@ public static void insertVehicleIdsIntoAttributes(Person person, Map<String, Id<
/**
* Attaches vehicle types to a person, so that the router knows which vehicle to use for which mode and person.
* @param modeToVehicleType mode string mapped to vehicle type ids. The provided map is copied and stored as unmodifiable map.
* @deprecated please inline to more expressive method name
*/
@Deprecated
public static void insertVehicleTypesIntoAttributes(Person person, Map<String, Id<VehicleType>> modeToVehicleType) {
insertVehicleTypesIntoPersonAttributes( person, modeToVehicleType );
}
/**
* Attaches vehicle types to a person, so that the router knows which vehicle to use for which mode and person.
* @param modeToVehicleType mode string mapped to vehicle type ids. The provided map is copied and stored as unmodifiable map.
*/
public static void insertVehicleTypesIntoPersonAttributes(Person person, Map<String, Id<VehicleType>> modeToVehicleType) {
Object attr = person.getAttributes().getAttribute(VEHICLE_TYPES_ATTRIBUTE_KEY);

Map<String, Id<VehicleType>> modeToTypesCopy = new HashMap<>(modeToVehicleType);
Expand Down Expand Up @@ -273,7 +296,7 @@ public static Double getFuelConsumption(VehicleType vehicleType) {
}

public static void setFuelConsumption(VehicleType vehicleType, double literPerMeter) {
setFuelConsumption(vehicleType.getEngineInformation(), literPerMeter);
setFuelConsumption(vehicleType.getEngineInformation(), literPerMeter);
}

//******** EngineInformation attributes ************
Expand Down Expand Up @@ -307,11 +330,11 @@ public static void setHbefaEmissionsConcept( EngineInformation engineInformation
}

public static Double getEnergyConsumptionKWhPerMeter(EngineInformation engineInformation) {
return (Double) engineInformation.getAttributes().getAttribute(ENERGYCONSUMPTION);
}
return (Double) engineInformation.getAttributes().getAttribute(ENERGYCONSUMPTION);
}

public static void setEnergyConsumptionKWhPerMeter(EngineInformation engineInformation, double energyConsumptionKWhPerMeter) {
engineInformation.getAttributes().putAttribute(ENERGYCONSUMPTION, energyConsumptionKWhPerMeter);
engineInformation.getAttributes().putAttribute(ENERGYCONSUMPTION, energyConsumptionKWhPerMeter);
}

public static Double getEnergyCapacity(EngineInformation engineInformation) {
Expand Down

0 comments on commit 68a5246

Please sign in to comment.