MATSIM
NetworkTurnInfoBuilder.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * NetworkTurnInfoBuilder
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.network.algorithms;
21 
22 import com.google.inject.Inject;
23 import org.matsim.api.core.v01.Id;
24 import org.matsim.api.core.v01.Scenario;
29 import org.matsim.lanes.Lane;
30 import org.matsim.lanes.Lanes;
32 
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39 
46 public final class NetworkTurnInfoBuilder implements NetworkTurnInfoBuilderI {
47 
48  private final Scenario scenario;
49 
50  @Inject
52  {
53  this.scenario = scenario;
54  }
55 
56  @Override
57  public Map<Id<Link>, List<TurnInfo>> createAllowedTurnInfos(){
58  Map<Id<Link>, List<TurnInfo>> allowedInLinkTurnInfoMap = new HashMap<>();
59 
60  createAndAddTurnInfo(TransportMode.car, allowedInLinkTurnInfoMap);
61 
62  if ( scenario.getConfig().network().getLaneDefinitionsFile()!=null || //
63  scenario.getConfig().qsim().isUseLanes()) {
64  Lanes ld = scenario.getLanes();
65  Map<Id<Link>, List<TurnInfo>> lanesTurnInfoMap = createTurnInfos(ld);
66  mergeTurnInfoMaps(allowedInLinkTurnInfoMap, lanesTurnInfoMap);
67  }
68  return allowedInLinkTurnInfoMap;
69  }
70 
71 
72  private Map<Id<Link>, List<TurnInfo>> createTurnInfos(Lanes laneDefs) {
73  Map<Id<Link>, List<TurnInfo>> inLinkIdTurnInfoMap = new HashMap<>();
74  Set<Id<Link>> toLinkIds = new HashSet<>();
75  for (LanesToLinkAssignment l2l : laneDefs.getLanesToLinkAssignments().values()) {
76  toLinkIds.clear();
77  for (Lane lane : l2l.getLanes().values()) {
78  if (lane.getToLinkIds() != null
79  && (lane.getToLaneIds() == null || lane.getToLaneIds().isEmpty())) { // make sure that it is a lane at the end of a link
80  toLinkIds.addAll(lane.getToLinkIds());
81  }
82  }
83  if (!toLinkIds.isEmpty()) {
84  List<TurnInfo> turnInfoList = new ArrayList<TurnInfo>();
85  for (Id<Link> toLinkId : toLinkIds) {
86  turnInfoList.add(new TurnInfo(l2l.getLinkId(), toLinkId));
87  }
88  inLinkIdTurnInfoMap.put(l2l.getLinkId(), turnInfoList);
89  }
90  }
91 
92  return inLinkIdTurnInfoMap;
93  }
94 
95 
96 
101  private void createAndAddTurnInfo(String mode, Map<Id<Link>, List<TurnInfo>> inLinkTurnInfoMap) {
102  TurnInfo turnInfo = null;
103  Set<String> modes = null;
104  List<TurnInfo> turnInfosForInLink = null;
105  for (Node node : scenario.getNetwork().getNodes().values()) {
106  for (Link inLink : node.getInLinks().values()) {
107  turnInfosForInLink = inLinkTurnInfoMap.get(inLink.getId());
108  if (turnInfosForInLink == null) {
109  turnInfosForInLink = new ArrayList<TurnInfo>();
110  inLinkTurnInfoMap.put(inLink.getId(), turnInfosForInLink);
111  }
112 
113  for (Link outLink : node.getOutLinks().values()) {
114  if (! inLink.getAllowedModes().isEmpty() && ! outLink.getAllowedModes().isEmpty()) {
115  if (inLink.getAllowedModes().contains(mode) && outLink.getAllowedModes().contains(mode)) {
116  modes = new HashSet<String>();
117  modes.add(mode);
118  turnInfo = new TurnInfo(inLink.getId(), outLink.getId(), modes);
119  turnInfosForInLink.add(turnInfo);
120  }
121  }
122  else { // we have no mode information at all
123  turnInfo = new TurnInfo(inLink.getId(), outLink.getId());
124  turnInfosForInLink.add(turnInfo);
125  }
126  }
127  }
128  }
129  }
130 
137  public final void mergeTurnInfoMaps(Map<Id<Link>, List<TurnInfo>> allowedInLinkTurnInfoMap,
138  Map<Id<Link>, List<TurnInfo>> restrictingTurnInfoMap) {
139  for (Map.Entry<Id<Link>, List<TurnInfo>> e : allowedInLinkTurnInfoMap.entrySet()) {
140  Id<Link> inLinkId = e.getKey();
141  List<TurnInfo> restrictingTurnInfos = restrictingTurnInfoMap.get(inLinkId);
142 
143  if (restrictingTurnInfos != null) { // there are restrictions for the inLink
144  List<TurnInfo> allowedTurnInfos = new ArrayList<TurnInfo>(e.getValue());
145  for (TurnInfo allowedForOutlink : allowedTurnInfos) {
146  TurnInfo restrictionForOutlink = getTurnInfoForOutlinkId(
147  restrictingTurnInfos, allowedForOutlink.getToLinkId());
148  if (restrictionForOutlink == null) { // there is no turn at all allowed from the inLink to the outLink
149  allowedInLinkTurnInfoMap.get(inLinkId).remove(allowedForOutlink);
150  }
151  else { // turns are restricted to some modes or allowed without any mode information
152  if (restrictionForOutlink.getModes() != null && allowedForOutlink.getModes() != null){
153  Set<String> commonModes = this.calculateCommonModes(
154  restrictionForOutlink, allowedForOutlink);
155  Set<String> allowedModes = allowedForOutlink.getModes();
156  for (String mode : allowedModes) {
157  if (!commonModes.contains(mode)) {
158  allowedForOutlink.getModes().remove(mode);
159  }
160  }
161  }
162  }
163  }
164  }
165  }
166  }
167 
171  static TurnInfo getTurnInfoForOutlinkId(List<TurnInfo> turnInfoList, Id<Link> outLinkId) {
172  for (TurnInfo ti : turnInfoList) {
173  if (ti.getToLinkId().equals(outLinkId)) {
174  return ti;
175  }
176  }
177  return null;
178  }
179 
180  private Set<String> calculateCommonModes(TurnInfo first, TurnInfo second) {
181  Set<String> modes = new HashSet<String>();
182  for (String mode : first.getModes()) {
183  if (second.getModes().contains(mode)) {
184  modes.add(mode);
185  }
186  }
187  return modes;
188  }
189 
190 
191 }
static< T > Id< T > get(int index, final Class< T > type)
Definition: Id.java:112
Map< Id< Node >, ? extends Node > getNodes()
SortedMap< Id< Link >, LanesToLinkAssignment > getLanesToLinkAssignments()
final NetworkConfigGroup network()
Definition: Config.java:411
void createAndAddTurnInfo(String mode, Map< Id< Link >, List< TurnInfo >> inLinkTurnInfoMap)
QSimConfigGroup qsim()
Definition: Config.java:447
Set< String > calculateCommonModes(TurnInfo first, TurnInfo second)
Map< Id< Link >, List< TurnInfo > > createTurnInfos(Lanes laneDefs)
final void mergeTurnInfoMaps(Map< Id< Link >, List< TurnInfo >> allowedInLinkTurnInfoMap, Map< Id< Link >, List< TurnInfo >> restrictingTurnInfoMap)