MATSIM
NetworkRoutingModule.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2015 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 package org.matsim.core.router;
20 
21 import java.util.Arrays;
22 import java.util.List;
23 
31 import org.matsim.core.gbl.Gbl;
38 
45 @Deprecated // use NetworkRoutingInclAccessEgressModule instead
46 public final class NetworkRoutingModule implements RoutingModule {
47  // I think it makes sense to NOT add the bushwhacking mode directly into here ...
48  // ... since it makes sense be able to to route from facility.getLinkId() to facility.getLinkId(). kai, dec'15
49 
50  private final String mode;
52 
53  private final Network network;
55 
56 
58  final String mode,
59  final PopulationFactory populationFactory,
60  final Network network,
61  final LeastCostPathCalculator routeAlgo) {
62  Gbl.assertNotNull(network);
63 // Gbl.assertIf( network.getLinks().size()>0 ) ; // otherwise network for mode probably not defined
64  // makes many tests fail.
65  this.network = network;
66  this.routeAlgo = routeAlgo;
67  this.mode = mode;
68  this.populationFactory = populationFactory;
69  }
70 
71  @Override
72  public List<? extends PlanElement> calcRoute(RoutingRequest request) {
73  final Facility fromFacility = request.getFromFacility();
74  final Facility toFacility = request.getToFacility();
75  final double departureTime = request.getDepartureTime();
76  final Person person = request.getPerson();
77 
78  Leg newLeg = this.populationFactory.createLeg( this.mode );
79 
80  Gbl.assertNotNull(fromFacility);
81  Gbl.assertNotNull(toFacility);
82 
83  Link fromLink = this.network.getLinks().get(fromFacility.getLinkId());
84  if ( fromLink==null ) {
85  //if an activity takes place on a link which is not part of the modal network, use coord as fallback
86  Gbl.assertNotNull( fromFacility.getCoord() ) ;
87  fromLink = NetworkUtils.getNearestLink( network, fromFacility.getCoord()) ;
88  }
89  Link toLink = this.network.getLinks().get(toFacility.getLinkId());
90  if ( toLink==null ) {
91  //if an activity takes place on a link which is not part of the modal network, use coord as fallback
92  Gbl.assertNotNull( toFacility.getCoord() ) ;
93  toLink = NetworkUtils.getNearestLink(network, toFacility.getCoord());
94  }
95  Gbl.assertNotNull(fromLink);
96  Gbl.assertNotNull(toLink);
97 
98  if (toLink != fromLink) {
99  // (a "true" route)
100  Node startNode = fromLink.getToNode(); // start at the end of the "current" link
101  Node endNode = toLink.getFromNode(); // the target is the start of the link
102 
103  /* The NetworkInclAccessEgressModule actually looks up the vehicle in the scenario and passes it to the routeAlgo as well as to the resulting route.
104  * Here, we do not hold the scenario as a field (yet) and can not perform the lookup.
105  * So i don't add it here (yet), in order not to break anything. But probably should be done in future.
106  * ts, june '21
107  */
108  Path path = this.routeAlgo.calcLeastCostPath(startNode, endNode, departureTime, person, null);
109  if (path == null)
110  throw new RuntimeException("No route found from node " + startNode.getId() + " to node " + endNode.getId() + " by mode " + this.mode + ".");
111  NetworkRoute route = this.populationFactory.getRouteFactories().createRoute(NetworkRoute.class, fromLink.getId(), toLink.getId());
112  route.setLinkIds(fromLink.getId(), NetworkUtils.getLinkIds(path.links), toLink.getId());
113  route.setTravelTime(path.travelTime);
114  route.setTravelCost(path.travelCost);
115  route.setDistance(RouteUtils.calcDistance(route, 1.0, 1.0, this.network));
116  newLeg.setRoute(route);
117  newLeg.setTravelTime(path.travelTime);
118  } else {
119  // create an empty route == staying on place if toLink == endLink
120  // note that we still do a route: someone may drive from one location to another on the link. kai, dec'15
121  NetworkRoute route = this.populationFactory.getRouteFactories().createRoute(NetworkRoute.class, fromLink.getId(), toLink.getId());
122  route.setTravelTime(0);
123  route.setDistance(0.0);
124  newLeg.setRoute(route);
125  newLeg.setTravelTime(0);
126  }
127  newLeg.setDepartureTime(departureTime);
128 
129  return Arrays.asList( newLeg );
130  }
131 
132  @Override
133  public String toString() {
134  return "[NetworkRoutingModule: mode="+this.mode+"]";
135  }
136 
137 }
NetworkRoutingModule(final String mode, final PopulationFactory populationFactory, final Network network, final LeastCostPathCalculator routeAlgo)
void setDistance(final double distance)
void setDepartureTime(final double seconds)
Path calcLeastCostPath(Node fromNode, Node toNode, double starttime, final Person person, final Vehicle vehicle)
List<? extends PlanElement > calcRoute(RoutingRequest request)
static Link getNearestLink(Network network, final Coord coord)
static void assertNotNull(Object obj)
Definition: Gbl.java:212
static List< Id< Link > > getLinkIds(final String links)
static double calcDistance(final NetworkRoute networkRoute, final double relPosOnDepartureLink, final double relPosOnArrivalLink, final Network network)
Map< Id< Link >, ? extends Link > getLinks()
void setTravelTime(final double seconds)
void setTravelTime(final double travelTime)