MATSIM
PlanRouter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PlanRouter.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2012 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 package org.matsim.core.router;
21 
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.matsim.api.core.v01.Id;
37 import org.matsim.vehicles.Vehicle;
38 
39 import java.util.List;
40 
48 public final class PlanRouter implements PlanAlgorithm, PersonAlgorithm {
49  private static final Logger log = LogManager.getLogger( PlanRouter.class ) ;
50  private final TripRouter tripRouter;
53 
61  public PlanRouter( final TripRouter tripRouter, final ActivityFacilities facilities, final TimeInterpretation timeInterpretation) {
62  this.tripRouter = tripRouter;
63  this.facilities = facilities;
64  this.timeInterpretation = timeInterpretation;
65  }
66 
70  public PlanRouter( final TripRouter routingHandler, final TimeInterpretation timeInterpretation) {
71  this( routingHandler , null, timeInterpretation );
72  }
73 
74  @Override
75  public void run(final Plan plan) {
76  final List<Trip> trips = TripStructureUtils.getTrips( plan );
77  TimeTracker timeTracker = new TimeTracker(timeInterpretation);
78 
79  for (Trip oldTrip : trips) {
80  final String routingMode = TripStructureUtils.identifyMainMode( oldTrip.getTripElements() );
81  timeTracker.addActivity(oldTrip.getOriginActivity());
82 
83  if (log.isDebugEnabled()) log.debug("about to call TripRouter with routingMode=" + routingMode);
84  final List<? extends PlanElement> newTripElements = tripRouter.calcRoute( //
85  routingMode, //
86  FacilitiesUtils.toFacility(oldTrip.getOriginActivity(), facilities), //
87  FacilitiesUtils.toFacility(oldTrip.getDestinationActivity(), facilities), //
88  timeTracker.getTime().seconds(), //
89  plan.getPerson(), //
90  oldTrip.getTripAttributes() //
91  );
92 
93  putVehicleFromOldTripIntoNewTripIfMeaningful(oldTrip, newTripElements);
94 
95  TripRouter.insertTrip( plan, oldTrip.getOriginActivity(), newTripElements, oldTrip.getDestinationActivity());
96 
97  timeTracker.addElements(newTripElements);
98  }
99  }
100 
108  public static void putVehicleFromOldTripIntoNewTripIfMeaningful(Trip oldTrip, List<? extends PlanElement> newTrip) {
109  Id<Vehicle> oldVehicleId = getUniqueVehicleId(oldTrip);
110  if (oldVehicleId != null) {
111  for (Leg leg : TripStructureUtils.getLegs(newTrip)) {
112  if (leg.getRoute() instanceof NetworkRoute) {
113  if (((NetworkRoute) leg.getRoute()).getVehicleId() == null) {
114  ((NetworkRoute) leg.getRoute()).setVehicleId(oldVehicleId);
115  }
116  }
117  }
118  }
119  }
120 
121  private static Id<Vehicle> getUniqueVehicleId(Trip trip) {
122  Id<Vehicle> vehicleId = null;
123  for (Leg leg : trip.getLegsOnly()) {
124  if (leg.getRoute() instanceof NetworkRoute) {
125  if (vehicleId != null && (!vehicleId.equals(((NetworkRoute) leg.getRoute()).getVehicleId()))) {
126  return null; // The trip uses several vehicles.
127  }
128  vehicleId = ((NetworkRoute) leg.getRoute()).getVehicleId();
129  }
130  }
131  return vehicleId;
132  }
133 
134  @Override
135  public void run(final Person person) {
136  for (Plan plan : person.getPlans()) {
137  run( plan );
138  }
139  }
140 
141 }
142 
synchronized List<? extends PlanElement > calcRoute(final String mainMode, final Facility fromFacility, final Facility toFacility, final double departureTime, final Person person, final Attributes routingAttributes)
PlanRouter(final TripRouter tripRouter, final ActivityFacilities facilities, final TimeInterpretation timeInterpretation)
Definition: PlanRouter.java:61
static String identifyMainMode(final List<? extends PlanElement > tripElements)
final ActivityFacilities facilities
Definition: PlanRouter.java:51
static List< PlanElement > insertTrip(final Plan plan, final Activity origin, final List<? extends PlanElement > trip, final Activity destination)
static void putVehicleFromOldTripIntoNewTripIfMeaningful(Trip oldTrip, List<? extends PlanElement > newTrip)
void run(final Person person)
static List< Trip > getTrips(final Plan plan)
static Id< Vehicle > getUniqueVehicleId(Trip trip)
PlanRouter(final TripRouter routingHandler, final TimeInterpretation timeInterpretation)
Definition: PlanRouter.java:70
final TimeInterpretation timeInterpretation
Definition: PlanRouter.java:52
OptionalTime addActivity(Activity activity)
void run(final Plan plan)
Definition: PlanRouter.java:75
boolean equals(Object obj)
Definition: Id.java:139
static Facility toFacility(final Activity toWrap, ActivityFacilities activityFacilities)
static List< Leg > getLegs(final Plan plan)
OptionalTime addElements(List<? extends PlanElement > elements)
abstract List<? extends T > getPlans()