MATSIM
PersonUtils.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * PersonUtils.java
5  * *
6  * *********************************************************************** *
7  * *
8  * copyright : (C) 2019 by the members listed in the COPYING, *
9  * LICENSE and WARRANTY file. *
10  * email : info at matsim dot org *
11  * *
12  * *********************************************************************** *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * See also COPYING, LICENSE and WARRANTY file *
19  * *
20  * *********************************************************************** */
21 
22 package org.matsim.core.population;
23 
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.TreeSet;
27 
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.matsim.api.core.v01.Id;
33 import org.matsim.vehicles.Vehicle;
36 
37 public final class PersonUtils {
38  private PersonUtils() {
39  } // do not instantiate
40 
41  private final static String SEX_ATTRIBUTE = "sex";
42  private final static String HAS_LICENSE = "hasLicense";
43  private static final String CAR_AVAIL = "carAvail";
44  private static final String EMPLOYED = "employed";
45  private static final String AGE = "age";
46  private static final String TRAVEL_CARDS = "travelcards";
47  private static final String PERSONAL_INCOME_ATTRIBUTE_NAME = "income";
48  private static final String PERSONAL_SCORING_MODE_CONSTANTS_ATTRIBUTE_NAME = "modeConstants";
49  private final static Logger log = LogManager.getLogger(Person.class);
50 
51  @Deprecated // use methods of interface Person
52  //yyy there is no such method in the Person interface. paul, feb'25
53  public static Plan createAndAddPlan(Person person, final boolean selected) {
54  Plan p = PopulationUtils.createPlan(person);
55  person.addPlan(p);
56  if (selected) {
57  person.setSelectedPlan(p);
58  }
59  return p;
60  }
61 
62  public static void removeUnselectedPlans(Person person) {
63  for (Iterator<? extends Plan> iter = person.getPlans().iterator(); iter.hasNext(); ) {
64  Plan plan = iter.next();
65  if (!PersonUtils.isSelected(plan)) {
66  iter.remove();
67  }
68  }
69  }
70 
74  public static String getSex(Person person) {
75  return (String) person.getAttributes().getAttribute(SEX_ATTRIBUTE);
76  }
77 
81  public static Integer getAge(Person person) {
82  return (Integer) person.getAttributes().getAttribute(AGE);
83  }
84 
88  public static String getLicense(Person person) {
89  return (String) person.getAttributes().getAttribute(HAS_LICENSE);
90  }
91 
95  public static Double getIncome(Person person) {
96  return (Double) person.getAttributes().getAttribute(PERSONAL_INCOME_ATTRIBUTE_NAME);
97  }
98 
106  public static Map<String, String> getModeConstants(Person person) {
107  try {
108 
109  return (Map<String, String>) person.getAttributes().getAttribute(PERSONAL_SCORING_MODE_CONSTANTS_ATTRIBUTE_NAME);
110  } catch (Exception e) {
111  log.error("Error retrieving personalScoringModeConstants from attribute " +
112  PERSONAL_SCORING_MODE_CONSTANTS_ATTRIBUTE_NAME + ". Should be a Map<String,String>.");
113  log.error(e.getMessage());
114  throw new RuntimeException(e.getMessage());
115  }
116  }
117 
121  public static boolean hasLicense(Person person) {
122  return ("yes".equals(getLicense(person))) || ("true".equals(getLicense(person)));
123  }
124 
128  public static String getCarAvail(Person person) {
129  return (String) person.getAttributes().getAttribute(CAR_AVAIL);
130  }
131 
136  public static boolean canUseCar(Person person) {
137  return !"no".equals(PersonUtils.getLicense(person)) && !"never".equals(PersonUtils.getCarAvail(person));
138  }
139 
143  public static Boolean isEmployed(Person person) {
144  return (Boolean) person.getAttributes().getAttribute(EMPLOYED);
145  }
146 
147 
151  public static void setAge(Person person, final Integer age) {
152  if (age != null) {
153  person.getCustomAttributes().put(AGE, age);
154  person.getAttributes().putAttribute(AGE, age);
155  }
156  }
157 
161  public static void setSex(Person person, final String sex) {
162  if (sex != null) {
163  person.getCustomAttributes().put(SEX_ATTRIBUTE, sex);
164  person.getAttributes().putAttribute(SEX_ATTRIBUTE, sex);
165  }
166  }
167 
171  public static void setLicence(Person person, final String licence) {
172  if (licence != null) {
173 
174  person.getCustomAttributes().put(HAS_LICENSE, licence);
175  person.getAttributes().putAttribute(HAS_LICENSE, licence);
176  }
177  }
178 
182  public static void setCarAvail(Person person, final String carAvail) {
183  if (carAvail != null) {
184  person.getCustomAttributes().put(CAR_AVAIL, carAvail);
185  person.getAttributes().putAttribute(CAR_AVAIL, carAvail);
186  }
187  }
188 
192  public static void setEmployed(Person person, final Boolean employed) {
193  if (employed != null) {
194  person.getCustomAttributes().put(EMPLOYED, employed);
195  person.getAttributes().putAttribute(EMPLOYED, employed);
196  }
197  }
198 
202  public static void setIncome(Person person, final double income) {
203  person.getCustomAttributes().put(PERSONAL_INCOME_ATTRIBUTE_NAME, income); // deprecated, should not be necessary anymore
204  person.getAttributes().putAttribute(PERSONAL_INCOME_ATTRIBUTE_NAME, income);
205  }
206 
207  public static void setModeConstants(Person person, Map<String, String> mode2scoringConstant) {
208  person.getAttributes().putAttribute(PERSONAL_SCORING_MODE_CONSTANTS_ATTRIBUTE_NAME, mode2scoringConstant);
209  }
210 
211  @Deprecated // yyyy is there a way to use person.getAttributes instead?? kai, nov'16
212  public static void addTravelcard(Person person, final String type) {
213  if (getTravelcards(person) == null) {
214  person.getCustomAttributes().put(TRAVEL_CARDS, new TreeSet<String>());
215  }
216  if (getTravelcards(person).contains(type)) {
217  log.info(person + "[type=" + type + " already exists]");
218  } else {
219  getTravelcards(person).add(type.intern());
220  }
221  }
222 
223  @Deprecated // use PersonAttributes
224  public static TreeSet<String> getTravelcards(Person person) {
225  return (TreeSet<String>) person.getCustomAttributes().get(TRAVEL_CARDS);
226  }
227 
228  public static boolean isSelected(Plan plan) {
229  return plan.getPerson().getSelectedPlan() == plan;
230  }
231 
237  public static void insertVehicleTypesIntoPersonAttributes(Person person, Map<String, Id<VehicleType>> modeToVehicleType) {
238  VehicleUtils.insertVehicleTypesIntoPersonAttributes(person, modeToVehicleType);
239  }
240 
249  public static void insertVehicleIdsIntoPersonAttributes(Person person, Map<String, Id<Vehicle>> modeToVehicle) {
250  VehicleUtils.insertVehicleIdsIntoPersonAttributes(person, modeToVehicle);
251  }
252 }
static final String PERSONAL_INCOME_ATTRIBUTE_NAME
static void insertVehicleTypesIntoPersonAttributes(Person person, Map< String, Id< VehicleType >> modeToVehicleType)
static void addTravelcard(Person person, final String type)
static boolean hasLicense(Person person)
Map< String, Object > getCustomAttributes()
static void setLicence(Person person, final String licence)
static String getCarAvail(Person person)
static void setIncome(Person person, final double income)
static final String PERSONAL_SCORING_MODE_CONSTANTS_ATTRIBUTE_NAME
static Map< String, String > getModeConstants(Person person)
static Plan createAndAddPlan(Person person, final boolean selected)
static Boolean isEmployed(Person person)
static void insertVehicleTypesIntoPersonAttributes(Person person, Map< String, Id< VehicleType >> modeToVehicleType)
static String getSex(Person person)
static void insertVehicleIdsIntoPersonAttributes(Person person, Map< String, Id< Vehicle >> modeToVehicle)
static void setAge(Person person, final Integer age)
static void insertVehicleIdsIntoPersonAttributes(Person person, Map< String, Id< Vehicle >> modeToVehicle)
Object putAttribute(final String attribute, final Object value)
static boolean canUseCar(Person person)
abstract void setSelectedPlan(T selectedPlan)
static void setCarAvail(Person person, final String carAvail)
static Integer getAge(Person person)
static void setModeConstants(Person person, Map< String, String > mode2scoringConstant)
static void setSex(Person person, final String sex)
static String getLicense(Person person)
static TreeSet< String > getTravelcards(Person person)
static boolean isSelected(Plan plan)
static void removeUnselectedPlans(Person person)
static void setEmployed(Person person, final Boolean employed)
static Double getIncome(Person person)
abstract List<? extends T > getPlans()