MATSIM
WithinDayAgentUtils.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ExperimentalBasicWithindayAgent.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2013 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.mobsim.qsim.agents;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
38 
64 public final class WithinDayAgentUtils {
65 
67  // do not instantiate: static methods only
68  }
69 
70  private static final Logger log = LogManager.getLogger( WithinDayAgentUtils.class );
71 
72  public static Integer getCurrentPlanElementIndex(MobsimAgent agent) {
73  // if (agent instanceof PersonDriverAgentImpl) {
74  // return ((PersonDriverAgentImpl) agent).getCurrentPlanElementIndex() ;
75  // } else
76 
77  // commenting out the above so the new code runs through the existing tests. kai, nov'17
78 
79  if ( agent instanceof PlanAgent ) {
80  return ((PlanAgent)agent).getCurrentPlan().getPlanElements().indexOf( ((PlanAgent)agent).getCurrentPlanElement() ) ;
81  } else {
82  throw new RuntimeException("Sorry, agent is from type " + agent.getClass().toString() +
83  " which does not support getCurrentPlanElementIndex(...). Aborting!");
84  }
85  }
86 
93  public static Integer getCurrentRouteLinkIdIndex(MobsimAgent agent) {
94 
95  if (agent instanceof HasModifiablePlan) {
96 
97  return ((HasModifiablePlan) agent).getCurrentLinkIndex();
98 
99 // } else if ( agent instanceof PlanAgent ) {
100 //
101 // // the following does not work because of loop routes, see above
102 // Leg currentLeg = (Leg) ((PlanAgent)agent).getCurrentPlanElement() ;
103 // if ( ! (currentLeg.getRoute() instanceof NetworkRoute ) ) {
104 // throw new RuntimeException("agent currently not on network route; asking for link id index does not make sense") ;
105 // }
106 // NetworkRoute route = (NetworkRoute) currentLeg.getRoute();
107 // int index = route.getLinkIds().indexOf( agent.getCurrentLinkId() );
108 //
109 // // if agent is on arrival link, we need special treatment:
110 // if ( index==-1 && agent.getCurrentLinkId().equals( route.getEndLinkId() ) ) {
111 // index = route.getLinkIds().size() ;
112 // }
113 //
114 // // and in the end it points even one further (always points to the _next_ entry, if there is any)
115 // index++ ;
116 //
117 // return index ;
118 
119  } else {
120  throw new RuntimeException("Sorry, agent is from type " + agent.getClass().toString() +
121  " which does not support getCurrentRouteLinkIdIndex(...). Aborting!");
122  }
123  }
124 
125  // public static final void calculateAndSetDepartureTime(MobsimAgent agent, Activity act) {
126  // if (agent instanceof PersonDriverAgentImpl) {
127  // ((PersonDriverAgentImpl) agent).calculateAndSetDepartureTime(act);
128  // } else {
129  // throw new RuntimeException("Sorry, agent is from type " + agent.getClass().toString() +
130  // " which does not support calculateAndSetDepartureTime(...). Aborting!");
131  // }
132  // }
133 
134  public static void resetCaches(MobsimAgent agent) {
135  if (agent instanceof HasModifiablePlan) {
136  ((HasModifiablePlan) agent).resetCaches();
137  } else {
138  throw new RuntimeException("Sorry, agent is from type " + agent.getClass().toString() +
139  " which does not support resetCaches(...). Aborting!");
140  }
141  }
142  public static void rescheduleActivityEnd(MobsimAgent agent, Mobsim mobsim ) {
143  if ( mobsim instanceof ActivityEndRescheduler ) {
144  ((ActivityEndRescheduler) mobsim).rescheduleActivityEnd(agent);
145  } else {
146  throw new RuntimeException("mobsim does not support activity end rescheduling; aborting ...") ;
147  }
148  }
149 
150  public static Leg getModifiableCurrentLeg(MobsimAgent agent) {
151  PlanElement currentPlanElement = getCurrentPlanElement(agent);
152  if (!(currentPlanElement instanceof Leg)) {
153  return null;
154  }
155  return (Leg) currentPlanElement;
156  }
157 
158  public static Plan getModifiablePlan(MobsimAgent agent) {
159  if (agent instanceof HasModifiablePlan) {
160  return ((HasModifiablePlan) agent).getModifiablePlan();
161  } else {
162  throw new RuntimeException("Sorry, agent is from type " + agent.getClass().toString() +
163  " which does not support getModifiablePlan(...). Aborting!");
164  }
165  }
166 
169  }
170 
171  public static boolean isOnReplannableCarLeg(MobsimAgent agent) {
172  // if (plan == null) {
173  // log.info( " we don't have a modifiable plan; returning ... ") ;
174  // return false;
175  // }
176  if ( !(getCurrentPlanElement(agent) instanceof Leg) ) {
177  log.info( "agent not on leg; returning ... ") ;
178  return false ;
179  }
180  if (!((Leg) getCurrentPlanElement(agent)).getMode().equals(TransportMode.car)) {
181  log.info( "not a car leg; can only replan car legs; returning ... ") ;
182  return false;
183  }
184  return true ;
185  }
186 
187  public static Plan printPlan(MobsimAgent agent1) {
188  final Plan plan = getModifiablePlan(agent1);
189  return printPlan(plan) ;
190  }
191 
192  public static Plan printPlan(Plan plan) {
193  System.err.println( "plan=" + plan );
194  for ( int ii=0 ; ii<plan.getPlanElements().size() ; ii++ ) {
195  System.err.println( "\t" + ii + ":\t" + plan.getPlanElements().get(ii) );
196  }
197  return plan;
198  }
199 
207  public static boolean replaceLegBlindly(Plan plan, Leg oldLeg, Leg newLeg) {
208 
209  if (plan == null) return false;
210  if (oldLeg == null) return false;
211  if (newLeg == null) return false;
212 
213  int index = plan.getPlanElements().indexOf(oldLeg);
214  // yyyy I can't say how safe this is. There is no guarantee that the same entry is not used twice in the plan. This will in
215  // particular be a problem if we override the "equals" contract, in the sense that two legs are equal if
216  // certain (or all) elements are equal. kai, oct'10
217 
218  if (index == -1) return false;
219 
220  plan.getPlanElements().remove(index);
221  plan.getPlanElements().add(index,newLeg);
222 
223  return true;
224  }
225 
233  public static boolean replaceActivityBlindly( Plan plan, Activity oldActivity, Activity newActivity) {
234 
235  if (plan == null) return false;
236  if (oldActivity == null) return false;
237  if (newActivity == null) return false;
238 
239  int index = plan.getPlanElements().indexOf(oldActivity);
240  // yyyy I can't say how safe this is. There is no guarantee that the same entry is not used twice in the plan. This will in
241  // particular be a problem if we override the "equals" contract, in the sense that two activities are equal if
242  // certain (or all) elements are equal. kai, oct'10
243 
244  if (index == -1) return false;
245 
246  // /*
247  // * If the new Activity takes place on a different Link
248  // * we have to replan the Routes from an to that Activity.
249  // */
250  // if (oldActivity.getLinkId() != newActivity.getLinkId())
251  // {
252  //
253  // }
254 
255  plan.getPlanElements().remove(index);
256  plan.getPlanElements().add(index, newActivity);
257 
258  return true;
259  }
260 
261  // === search methods: ===
262  public static int indexOfPlanElement(MobsimAgent agent, PlanElement pe) {
263  Plan plan = getModifiablePlan(agent) ;
264  List<PlanElement> planElements = plan.getPlanElements() ;
265 
266  return planElements.indexOf(pe) ;
267  }
268 
269  public static int indexOfNextActivityWithType( MobsimAgent agent, String type ) {
270  Plan plan = getModifiablePlan(agent) ;
271  List<PlanElement> planElements = plan.getPlanElements() ;
272 
273  for ( int index = getCurrentPlanElementIndex(agent) ; index < planElements.size() ; index++ ) {
274  PlanElement pe = planElements.get(index) ;
275  if ( pe instanceof Activity ) {
276  if ( ((Activity)pe).getType().equals(type) ) {
277  return index ;
278  }
279  }
280  }
281  return -1 ;
282  }
283 
284  public static Activity findNextActivityWithType( MobsimAgent agent, String type ) {
285  int index = indexOfNextActivityWithType( agent, type ) ;
286  return (Activity) getModifiablePlan(agent).getPlanElements().get(index) ;
287  }
288 
289  public static List<PlanElement> subList( MobsimAgent agent, int fromIndex, int toIndex) {
290  return getModifiablePlan(agent).getPlanElements().subList( fromIndex, toIndex ) ;
291  }
292 
293  public static List<PlanElement> convertInteractionActivities(List<? extends PlanElement> elements) {
294  List<PlanElement> updatedElements = new ArrayList<>(elements.size());
295 
296  for (int i = 0; i < elements.size(); i++) {
297  PlanElement element = elements.get(i);
298 
299  if (element instanceof Activity) {
300  element = PopulationUtils.convertInteractionToStandardActivity((Activity) element);
301  }
302 
303  updatedElements.add(element);
304  }
305 
306  return updatedElements;
307  }
308 }
static Integer getCurrentRouteLinkIdIndex(MobsimAgent agent)
static boolean replaceActivityBlindly(Plan plan, Activity oldActivity, Activity newActivity)
static boolean replaceLegBlindly(Plan plan, Leg oldLeg, Leg newLeg)
static Integer getCurrentPlanElementIndex(MobsimAgent agent)
static Activity convertInteractionToStandardActivity(Activity activity)
static PlanElement getCurrentPlanElement(MobsimAgent agent)
static int indexOfNextActivityWithType(MobsimAgent agent, String type)
static List< PlanElement > convertInteractionActivities(List<? extends PlanElement > elements)
static List< PlanElement > subList(MobsimAgent agent, int fromIndex, int toIndex)
List< PlanElement > getPlanElements()
static void rescheduleActivityEnd(MobsimAgent agent, Mobsim mobsim)
static Activity findNextActivityWithType(MobsimAgent agent, String type)
static int indexOfPlanElement(MobsimAgent agent, PlanElement pe)