From c8c1f4932c536dffc02f0119ebe4c68520a2e5f3 Mon Sep 17 00:00:00 2001 From: Kai Nagel Date: Thu, 15 Feb 2024 11:21:28 +0100 Subject: [PATCH] make two static methods more easily accessible --- .../matsim/core/population/PersonUtils.java | 23 +++++++++ .../core/population/PopulationUtils.java | 21 +++++++++ .../org/matsim/vehicles/VehicleUtils.java | 47 ++++++++++++++----- 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/matsim/src/main/java/org/matsim/core/population/PersonUtils.java b/matsim/src/main/java/org/matsim/core/population/PersonUtils.java index 9c64e247d87..8959899a9b5 100644 --- a/matsim/src/main/java/org/matsim/core/population/PersonUtils.java +++ b/matsim/src/main/java/org/matsim/core/population/PersonUtils.java @@ -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() { @@ -225,4 +229,23 @@ public static TreeSet 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> 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> modeToVehicle ) { + VehicleUtils.insertVehicleIdsIntoPersonAttributes( person, modeToVehicle ); + } } diff --git a/matsim/src/main/java/org/matsim/core/population/PopulationUtils.java b/matsim/src/main/java/org/matsim/core/population/PopulationUtils.java index 574f2f76e93..cae206c534b 100644 --- a/matsim/src/main/java/org/matsim/core/population/PopulationUtils.java +++ b/matsim/src/main/java/org/matsim/core/population/PopulationUtils.java @@ -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 @@ -1266,5 +1269,23 @@ public static Person findPerson( Id 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> 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> modeToVehicle ) { + VehicleUtils.insertVehicleIdsIntoPersonAttributes( person, modeToVehicle ); + } } diff --git a/matsim/src/main/java/org/matsim/vehicles/VehicleUtils.java b/matsim/src/main/java/org/matsim/vehicles/VehicleUtils.java index 0141e814600..6c0a9b289ba 100644 --- a/matsim/src/main/java/org/matsim/vehicles/VehicleUtils.java +++ b/matsim/src/main/java/org/matsim/vehicles/VehicleUtils.java @@ -141,9 +141,9 @@ public static Map> 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) 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) with key 'vehicles' as person attribute to each person." + + "\n VehicleUtils.insertVehicleIdIntoAttributes does this for you."); } return personVehicles.getModeVehicles(); } @@ -167,23 +167,37 @@ public static Id getVehicleId(Person person, String mode) { Map> 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) 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) 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> modeToVehicle) { + @Deprecated + public static void insertVehicleIdsIntoAttributes(Person person, Map> 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> modeToVehicle) { Object attr = person.getAttributes().getAttribute(VEHICLE_ATTRIBUTE_KEY); // copy in case it's a UnmodifiableMap Map> modeToVehicleCopy = new HashMap<>(modeToVehicle); @@ -200,8 +214,17 @@ public static void insertVehicleIdsIntoAttributes(Person person, Map> 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> modeToVehicleType) { Object attr = person.getAttributes().getAttribute(VEHICLE_TYPES_ATTRIBUTE_KEY); Map> modeToTypesCopy = new HashMap<>(modeToVehicleType); @@ -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 ************ @@ -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) {