MATSIM
CurrentLegReplanner.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * CurrentLegReplanner.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2010 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.withinday.replanning.replanners;
22 
23 import org.matsim.api.core.v01.Id;
24 import org.matsim.api.core.v01.Scenario;
36 
37 /*
38  * The CurrentLegReplanner can be used while an Agent travels from
39  * one Activity to another one.
40  *
41  * MATSim Routers use Plan as Input Data. To be able to use them, we have to create
42  * a new Plan from the current Position to the location of the next Activity.
43  *
44  * This Replanner is called, if a person is somewhere on a Route between two Activities.
45  * First the current Route is splitted into two parts - the already passed links and
46  * the ones which are still to go.
47  * Next a new Plan is created with an Activity at the current Position and an Endposition
48  * that is equal to the one from the original plan.
49  * This Plan is handed over to the Router and finally the new route is merged with the
50  * Links that already have been passed by the Person.
51  */
53 
56 
58  LeastCostPathCalculator pathCalculator) {
59  super(id, scenario, internalInterface);
60  this.pathCalculator = pathCalculator;
61  this.populationFactory = scenario.getPopulation().getFactory() ;
62  }
63 
64  /*
65  * Replan Route every time the End of a Link is reached.
66  *
67  * Idea:
68  * - create a new Activity at the current Location
69  * - create a new Route from the current Location to the Destination
70  * - merge already passed parts of the current Route with the new created Route
71  */
72  @Override
73  public boolean doReplanning(MobsimAgent withinDayAgent) {
74 
75  Plan executedPlan = WithinDayAgentUtils.getModifiablePlan(withinDayAgent);
76 
77  // If we don't have an executed plan
78  if (executedPlan == null) return false;
79 
80  PlanElement currentPlanElement = WithinDayAgentUtils.getCurrentPlanElement(withinDayAgent);
81  if (!(currentPlanElement instanceof Leg)) return false;
82  Leg currentLeg = (Leg) currentPlanElement;
83  int currentLinkIndex = WithinDayAgentUtils.getCurrentRouteLinkIdIndex(withinDayAgent);
84 
86 
87  editRoutes.replanCurrentLegRoute(currentLeg, executedPlan.getPerson(), currentLinkIndex, this.time.seconds() ) ;
88 
89  // Finally reset the cached Values of the PersonAgent - they may have changed!
90  WithinDayAgentUtils.resetCaches(withinDayAgent);
91 
92  return true;
93  }
94 
95 }
static Integer getCurrentRouteLinkIdIndex(MobsimAgent agent)
static PlanElement getCurrentPlanElement(MobsimAgent agent)
boolean replanCurrentLegRoute(Leg leg, Person person, int currentLinkIndex, double time)