MATSIM
EditPlans.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * EditPlans.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.withinday.utils;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Objects;
27 
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.matsim.api.core.v01.Id;
31 import org.matsim.api.core.v01.Scenario;
33 import org.matsim.api.core.v01.population.*;
34 import org.matsim.core.gbl.Gbl;
37 import org.matsim.core.mobsim.qsim.QSim;
43 
44 public final class EditPlans {
45  private static final Logger log = LogManager.getLogger( EditPlans.class ) ;
46 
47  private final QSim mobsim;
48  private final EditTrips editTrips;
49  private final PopulationFactory pf;
50  @Deprecated // population factory argument not needed. kai, apr'18
51  public EditPlans( QSim mobsim, TripRouter tripRouter, EditTrips editTrips, PopulationFactory pf ) {
52  this( mobsim, editTrips ) ;
53  }
54  @Deprecated // scenario argument not needed. kai, apr'18
55  public EditPlans( QSim mobsim, TripRouter tripRouter, EditTrips editTrips, Scenario sc) {
56  this( mobsim, editTrips ) ;
57  }
58  @Deprecated // tripRouter argument not needed. gleich-oct'19
59  public EditPlans( QSim mobsim, TripRouter tripRouter, EditTrips editTrips ) {
60  this( mobsim, editTrips ) ;
61  }
62  public EditPlans( QSim mobsim, EditTrips editTrips ) {
63  Gbl.assertNotNull( this.mobsim = mobsim );
64  Gbl.assertNotNull( this.editTrips = editTrips ) ;
65  Gbl.assertNotNull( this.pf = mobsim.getScenario().getPopulation().getFactory() ) ;
66  }
67  public boolean addActivityAtEnd(MobsimAgent agent, Activity activity, String routingMode) {
68  log.debug("entering addActivityAtEnd with routingMode=" + routingMode) ;
69 
71  List<PlanElement> planElements = plan.getPlanElements();
72 
73  boolean retVal1 = false;
74 
75  if (isAtRealActivity(agent)) {
76  retVal1 = planElements.add(pf.createLeg(routingMode));
77  }
78 
79  final boolean retVal = planElements.add(activity);
80  // (need the terminating activity in order to find the current trip. kai, nov'17)
81 
82  if (!isAtRealActivity(agent)) {
83  retVal1 = editTrips.replanCurrentTrip(agent,mobsim.getSimTimer().getTimeOfDay(),routingMode);
84  }
85 
86 
88  this.mobsim.rescheduleActivityEnd(agent);
89  return (retVal1 && retVal);
90  }
91  public PlanElement removeActivity(MobsimAgent agent, int index, String mode) {
93  List<PlanElement> planElements = plan.getPlanElements() ;
94 
95  checkIfNotInPastOrCurrent(agent, index);
96 
97  final Trip tripBefore = TripStructureUtils.findTripEndingAtActivity( (Activity) planElements.get(index),plan );
98  final Trip tripAfter = TripStructureUtils.findTripStartingAtActivity( (Activity)planElements.get(index),plan );
99  if ( mode==null ) {
100  final String mainModeBefore = TripStructureUtils.identifyMainMode( tripBefore.getTripElements() );
101  final String mainModeAfter = TripStructureUtils.identifyMainMode( tripAfter.getTripElements() );
102  if ( mainModeBefore.equals( mainModeAfter ) ) {
103  mode = mainModeBefore ;
104  } else {
105  throw new ReplanningException("mode not given and mode before removed activity != mode after removed activity; don't know which mode to use") ;
106  }
107  }
108  PlanElement pe = planElements.remove(index) ;
109  if ( checkIfTripHasAlreadyStarted( agent, tripBefore.getTripElements() ) ) {
110  editTrips.replanCurrentTrip(agent, mobsim.getSimTimer().getTimeOfDay() , mode);
111  } else {
112  editTrips.insertEmptyTrip(plan, tripBefore.getOriginActivity(), tripAfter.getDestinationActivity(), mode ) ;
113  }
115  this.mobsim.rescheduleActivityEnd(agent);
116  return pe ;
117  }
118  public final void rescheduleActivityEndtime( MobsimAgent agent, int index, double newEndTime ) {
119  Activity activity = (Activity) WithinDayAgentUtils.getModifiablePlan(agent).getPlanElements().get(index) ;
120  activity.setEndTime(newEndTime);
123  }
124  public final Activity replaceActivity(MobsimAgent agent, int index, Activity newAct, String upstreamMode, String downstreamMode ) {
125  System.err.println("here310");
127  System.err.println("here320");
130  System.err.println("here330");
131 
132 
133  List<PlanElement> planElements = plan.getPlanElements() ;
134 
135  // make sure we have indeed an activity position:
136  if ( ! ( planElements.get(index) instanceof Activity ) ) {
137  throw new ReplanningException("trying to replace a non-activity in the plan by an activity; this is not possible") ;
138  }
139  Activity origAct = (Activity) planElements.get(index) ;
140 
141  checkIfNotStageActivity(origAct);
142 
143  checkIfNotInPastOrCurrent(agent, index);
144 
145  // set the new activity:
146  planElements.set(index, newAct) ;
147  System.err.println("here340");
149 
150  // trip before (if any):
151  if ( index > 0 ) {
152  Trip tripBeforeAct = TripStructureUtils.findTripEndingAtActivity( newAct,plan );
153  Gbl.assertNotNull( tripBeforeAct ); // there could also just be a sequence of activities?!
154 
155  final List<PlanElement> currentTripElements = tripBeforeAct.getTripElements();
156  final String currentMode = TripStructureUtils.identifyMainMode( currentTripElements ) ;
157 
158  if ( checkIfTripHasAlreadyStarted(agent, currentTripElements) ) {
159  // trip has already started
160  checkIfSameMode(upstreamMode, currentMode);
161  this.editTrips.replanCurrentTrip(agent, this.mobsim.getSimTimer().getTimeOfDay(), currentMode );
162  } else {
163  // trip has not yet started
164  if ( upstreamMode == null ) {
165  upstreamMode = currentMode ;
166  }
167  this.editTrips.insertEmptyTrip( plan, tripBeforeAct.getOriginActivity(), newAct, upstreamMode );
168  }
169  }
170  // trip after (if any):
171  if ( index < planElements.size()-1 ) {
172  Trip tripAfterAct = TripStructureUtils.findTripStartingAtActivity(origAct,plan);
173  Gbl.assertIf( tripAfterAct!=null ); // there could also just be a sequence of activities?!
174  if ( downstreamMode==null ) {
175  final String currentMainMode = TripStructureUtils.identifyMainMode( tripAfterAct.getTripElements() );
176  EditTrips.insertEmptyTrip(plan, newAct, tripAfterAct.getDestinationActivity(), currentMainMode, pf);
177  } else {
178  EditTrips.insertEmptyTrip(plan, newAct, tripAfterAct.getDestinationActivity(), downstreamMode, pf);
179  }
180  }
182  this.mobsim.rescheduleActivityEnd(agent);
183  return origAct ;
184  }
185  public void insertActivity(MobsimAgent agent, int index, Activity activity, String upstreamMode, String downstreamMode ) {
187  List<PlanElement> planElements = plan.getPlanElements() ;
188 
189  checkIfNotInPastOrCurrent(agent, index) ;
190  planElements.add( index, activity ) ;
191  {
192  // activity before:
193  Activity actBefore = findRealActBefore(agent, index);
194  if ( actBefore != null ) {
196  // we are already under way
197  editTrips.replanCurrentTrip(agent, this.mobsim.getSimTimer().getTimeOfDay(), upstreamMode );
198  } else {
199  // we are not yet under way; inserting empty trip:
200  EditTrips.insertEmptyTrip(plan, actBefore, activity, upstreamMode, pf ) ;
201  }
202  }
203  }
204  {
205  // activity after:
206  Activity actAfter = findRealActAfter(agent, index);
207  if ( actAfter != null ) {
208  EditTrips.insertEmptyTrip(plan, activity, actAfter, downstreamMode, pf ) ;
209  }
210  }
212  this.mobsim.rescheduleActivityEnd(agent);
213  }
214 
215  // === convenience methods: ===
219  public PlanElement removeActivity(MobsimAgent agent, int index) {
220  return removeActivity( agent, index, null ) ;
221  }
225  public final Activity replaceActivity(MobsimAgent agent, int index, Activity newAct) {
226  return replaceActivity( agent, index, newAct, null, null ) ;
227  }
231  public void insertActivity(MobsimAgent agent, int index, Activity activity ) {
233  insertActivity( agent, index, activity, mode, mode ) ;
234  }
235 
236  // === internal utility methods: ===
237  private void checkIfNotStageActivity(Activity origAct) {
239  throw new ReplanningException("trying to replace a helper activity (stage activity) by a real activity; this is not possible") ;
240  }
241  }
242  private static boolean checkIfTripHasAlreadyStarted(MobsimAgent agent, final List<PlanElement> currentTripElements) {
243  return currentTripElements.contains( ((PlanAgent)agent).getCurrentPlanElement() ) ;
244  }
245  private static void checkIfNotInPastOrCurrent(MobsimAgent agent, int index) {
246  final Integer currentIndex = WithinDayAgentUtils.getCurrentPlanElementIndex(agent);
247 
248  // make sure we are not yet beyond the to-be-replanned activity:
249  if ( index <= currentIndex ) {
250  throw new ReplanningException("trying to replace an activity that lies in the past or is current; this is not possible") ;
251  }
252  }
253  private static void checkIfSameMode(String upstreamMode, final String currentMode) {
254  if ( upstreamMode!=null && !upstreamMode.equals(currentMode) ) {
255  throw new ReplanningException( "cannot change mode in trip that has already started. Don't set the mode in the request, "
256  + "or somehow make the agent to abort the current trip first." ) ;
257  }
258  }
259  // private static boolean locationsAreSame(Activity origAct, Activity newAct) {
260  // if ( origAct.getFacilityId() != null && newAct.getFacilityId() != null ) {
261  // return origAct.getFacilityId().equals( newAct.getFacilityId() ) ;
262  // }
263  // if ( origAct.getCoord() != null && newAct.getCoord() != null ) {
264  // return origAct.getCoord().equals( newAct.getCoord() ) ;
265  // }
266  // return false ;
267  // }
268  public static Activity findRealActAfter(MobsimAgent agent, int index) {
270  List<PlanElement> planElements = plan.getPlanElements() ;
271  return (Activity) planElements.get( findIndexOfRealActAfter(agent, index) ) ;
272  }
273  public static int findIndexOfRealActAfter(MobsimAgent agent, int index) {
275  List<PlanElement> planElements = plan.getPlanElements() ;
276 
277  int theIndex = -1 ;
278  for ( int ii=planElements.size()-1 ; ii>index; ii-- ) {
279  if ( planElements.get(ii) instanceof Activity ) {
280  Activity act = (Activity) planElements.get(ii) ;
282  theIndex = ii ;
283  }
284  }
285  }
286  return theIndex ;
287  }
288  public static Activity findRealActBefore(MobsimAgent agent, int index) {
290  List<PlanElement> planElements = plan.getPlanElements() ;
291 
292  Activity prevAct = null ;
293  for ( int ii=0 ; ii<index ; ii++ ) {
294  if ( planElements.get(ii) instanceof Activity ) {
295  Activity act = (Activity) planElements.get(ii) ;
297  prevAct = act ;
298  }
299  }
300  }
301  return prevAct;
302  }
303  // private Facility asFacility(Activity activity) {
304  // Facility hereFacility = new ActivityWrapperFacility( activity ) ;
305  // if ( activity.getFacilityId()!=null ) {
306  // ActivityFacility facility = this.mobsim.getScenario().getActivityFacilities().getFacilities().get( activity.getFacilityId() ) ;
307  // if ( facility != null ) {
308  // hereFacility = facility ;
309  // }
310  // }
311  // return hereFacility;
312  // }
313 
314  public void rescheduleActivityEnd(MobsimAgent agent) {
315  // this is mostly for retrofitting existing code. but maybe also useful by itself
316  this.mobsim.rescheduleActivityEnd(agent);
317  }
318  public boolean isAtRealActivity(MobsimAgent agent) {
320  if ( isRealActivity(pe) ) {
321  return true ;
322  } else {
323  return false ;
324  }
325  }
326  public boolean isRealActivity(PlanElement pe) {
327  return pe instanceof Activity && ! ( StageActivityTypeIdentifier.isStageActivity( ((Activity)pe).getType() ) );
328  }
329 
331  Trip trip ;
332  if ( isAtRealActivity( agent ) ) {
334  trip = editTrips.findTripAfterActivity(WithinDayAgentUtils.getModifiablePlan(agent), activity) ;
335  } else {
336  trip = EditTrips.findCurrentTrip(agent ) ;
337  }
339  }
341  List<PlanElement> pes = WithinDayAgentUtils.getModifiablePlan(agent).getPlanElements() ;
342  Integer index = WithinDayAgentUtils.getCurrentPlanElementIndex(agent) ;
343  for ( int ii=pes.size()-1 ; ii>index ; ii-- ) {
344  pes.remove(ii) ;
345  }
346  }
347  public void rescheduleCurrentActivityEndtime(MobsimAgent agent, double newEndTime) {
348  Integer index = WithinDayAgentUtils.getCurrentPlanElementIndex(agent) ;
349  this.rescheduleActivityEndtime(agent, index, newEndTime);
350  }
351 
352  public Activity createFinalActivity(String type, Id<Link> newLinkId) {
353  Activity newAct = this.pf.createActivityFromLinkId(type, newLinkId);
354  newAct.setEndTime( Double.POSITIVE_INFINITY ) ;
355  return newAct ;
356  }
357 // public Activity createAgentThatKeepsMatsimAlive( String type, Id<Link> newLinkId) {
358 // Activity newAct = this.pf.createActivityFromLinkId( type, newLinkId);;
359 // newAct.setEndTime( Double.MAX_VALUE ) ;
360 // return newAct ;
361 // }
362  public static Integer getCurrentPlanElementIndex( MobsimAgent agent ) {
364  }
365 
366  public static List<Leg> findLegsWithModeInFuture( MobsimAgent agent, String mode ) {
367  List<Leg> retVal = new ArrayList<>();
369  for (int ii = WithinDayAgentUtils.getCurrentPlanElementIndex(agent); ii < plan.getPlanElements().size(); ii++) {
370  PlanElement pe = plan.getPlanElements().get(ii);
371  if (pe instanceof Leg) {
372  if ( Objects.equals(mode, ((Leg)pe).getMode() )) {
373  retVal.add((Leg)pe);
374  }
375  }
376  }
377  return retVal;
378  }
379 
380 }
EditPlans(QSim mobsim, TripRouter tripRouter, EditTrips editTrips, Scenario sc)
Definition: EditPlans.java:55
static String identifyMainMode(final List<? extends PlanElement > tripElements)
final Activity replaceActivity(MobsimAgent agent, int index, Activity newAct)
Definition: EditPlans.java:225
EditPlans(QSim mobsim, EditTrips editTrips)
Definition: EditPlans.java:62
static Activity findRealActBefore(MobsimAgent agent, int index)
Definition: EditPlans.java:288
final PopulationFactory pf
Definition: EditPlans.java:49
static List< Leg > findLegsWithModeInFuture(MobsimAgent agent, String mode)
Definition: EditPlans.java:366
static void assertIf(boolean flag)
Definition: Gbl.java:207
static Trip findCurrentTrip(MobsimAgent agent)
Definition: EditTrips.java:122
boolean addActivityAtEnd(MobsimAgent agent, Activity activity, String routingMode)
Definition: EditPlans.java:67
void checkIfNotStageActivity(Activity origAct)
Definition: EditPlans.java:237
Activity createActivityFromLinkId(String actType, Id< Link > linkId)
static Integer getCurrentPlanElementIndex(MobsimAgent agent)
Activity createFinalActivity(String type, Id< Link > newLinkId)
Definition: EditPlans.java:352
static PlanElement getCurrentPlanElement(MobsimAgent agent)
void insertActivity(MobsimAgent agent, int index, Activity activity)
Definition: EditPlans.java:231
PlanElement removeActivity(MobsimAgent agent, int index, String mode)
Definition: EditPlans.java:91
static void checkIfNotInPastOrCurrent(MobsimAgent agent, int index)
Definition: EditPlans.java:245
void rescheduleCurrentActivityEndtime(MobsimAgent agent, double newEndTime)
Definition: EditPlans.java:347
void flushEverythingBeyondCurrent(MobsimAgent agent)
Definition: EditPlans.java:340
Trip findTripAfterActivity(Plan plan, Activity activity)
Definition: EditTrips.java:756
static Trip findTripEndingAtActivity(Activity activity, Plan plan)
void insertActivity(MobsimAgent agent, int index, Activity activity, String upstreamMode, String downstreamMode)
Definition: EditPlans.java:185
static final boolean isStageActivity(final String activityType)
static boolean checkIfTripHasAlreadyStarted(MobsimAgent agent, final List< PlanElement > currentTripElements)
Definition: EditPlans.java:242
static void checkIfSameMode(String upstreamMode, final String currentMode)
Definition: EditPlans.java:253
final void rescheduleActivityEndtime(MobsimAgent agent, int index, double newEndTime)
Definition: EditPlans.java:118
static int findIndexOfRealActAfter(MobsimAgent agent, int index)
Definition: EditPlans.java:273
List< PlanElement > getPlanElements()
String getModeOfCurrentOrNextTrip(MobsimAgent agent)
Definition: EditPlans.java:330
static void rescheduleActivityEnd(MobsimAgent agent, Mobsim mobsim)
static void assertNotNull(Object obj)
Definition: Gbl.java:212
static boolean insertEmptyTrip(Plan plan, Activity fromActivity, Activity toActivity, String mainMode, PopulationFactory pf)
Definition: EditTrips.java:604
EditPlans(QSim mobsim, TripRouter tripRouter, EditTrips editTrips)
Definition: EditPlans.java:59
static Integer getCurrentPlanElementIndex(MobsimAgent agent)
Definition: EditPlans.java:362
void rescheduleActivityEnd(MobsimAgent agent)
Definition: EditPlans.java:314
final Activity replaceActivity(MobsimAgent agent, int index, Activity newAct, String upstreamMode, String downstreamMode)
Definition: EditPlans.java:124
final void rescheduleActivityEnd(MobsimAgent agent)
Definition: QSim.java:185
static Activity findRealActAfter(MobsimAgent agent, int index)
Definition: EditPlans.java:268
boolean isRealActivity(PlanElement pe)
Definition: EditPlans.java:326
PlanElement removeActivity(MobsimAgent agent, int index)
Definition: EditPlans.java:219
boolean isAtRealActivity(MobsimAgent agent)
Definition: EditPlans.java:318
static int indexOfPlanElement(MobsimAgent agent, PlanElement pe)
void setEndTime(final double seconds)
EditPlans(QSim mobsim, TripRouter tripRouter, EditTrips editTrips, PopulationFactory pf)
Definition: EditPlans.java:51
final boolean replanCurrentTrip(MobsimAgent agent, double now, String routingMode)
Definition: EditTrips.java:144
static Trip findTripStartingAtActivity(final Activity activity, final Plan plan)