MATSIM
LanesConsistencyChecker.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * LanesConsistencyChecker
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 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.lanes;
21 
22 import java.util.LinkedList;
23 import java.util.List;
24 
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.matsim.api.core.v01.Id;
30 
31 
36 public final class LanesConsistencyChecker {
37 
38  private static final Logger log = LogManager.getLogger(LanesConsistencyChecker.class);
39  private Network network;
40  private Lanes lanes;
41  private boolean removeMalformed = false;
42 
43  public LanesConsistencyChecker(Network net, Lanes laneDefs) {
44  this.network = net;
45  this.lanes = laneDefs;
46  }
47 
48  public void checkConsistency() {
49  log.info("checking consistency...");
50  List<Id<Link>> linksWithMalformedLanes = new LinkedList<>();
51  for (LanesToLinkAssignment l2l : this.lanes.getLanesToLinkAssignments().values()){
52  if (!isLaneOnLinkConsistent(l2l)){
53  linksWithMalformedLanes.add(l2l.getLinkId());
54  }
55  }
56 
57  if (this.removeMalformed){
58  for (Id<Link> linkId : linksWithMalformedLanes) {
59  this.lanes.getLanesToLinkAssignments().remove(linkId);
60  log.info("remove lanes on link " + linkId);
61  }
62  }
63  log.info("checked consistency. Lanes on " + linksWithMalformedLanes.size() + " links have been removed.");
64  }
65 
67  //check if link exists for each assignment of one or more lanes to a link
68  if (!this.network.getLinks().containsKey(l2l.getLinkId())) {
69  log.error("No link found for lanesToLinkAssignment on link Id(linkIdRef): " + l2l.getLinkId());
70  return false;
71  }
72  //check length
73  else {
74  Link link = this.network.getLinks().get(l2l.getLinkId());
75  for (Lane l : l2l.getLanes().values()){
76  if (link.getLength() < l.getStartsAtMeterFromLinkEnd()) {
77  log.error("Link Id " + link.getId() + " is shorter than an assigned lane with id " + l.getId());
78  return false;
79  }
80  }
81  }
82 
83  //check toLinks or toLanes specified in the lanes
84  for (Lane lane : l2l.getLanes().values()) {
85  if (lane.getToLaneIds() != null) {
86  for (Id<Lane> toLaneId : lane.getToLaneIds()){
87  if (! l2l.getLanes().containsKey(toLaneId)){
88  log.error("Error: toLane not existing:");
89  log.error(" Lane Id: " + lane.getId() + " on Link Id: " + l2l.getLinkId() +
90  " leads to Lane Id: " + toLaneId + " that is not existing!");
91  return false;
92  // TODO just delete this toLane?
93  }
94  }
95  }
96  //check availability of toLink in network
97  else if (lane.getToLinkIds() != null){
98  for (Id<Link> toLinkId : lane.getToLinkIds()) {
99  if (! this.network.getLinks().containsKey(toLinkId)){
100  log.error("No link found in network for toLinkId " + toLinkId + " of laneId " + lane.getId() + " of link id " + l2l.getLinkId());
101  return false;
102  // TODO just delete this toLink?
103  } else {
104  Link link = this.network.getLinks().get(l2l.getLinkId());
105  if (! link.getToNode().getOutLinks().containsKey(toLinkId)){
106  log.error("The given toLink " + toLinkId + " is not reachable from lane " + lane.getId() + " on link " + link.getId());
107  return false;
108  // TODO just delete this toLink?
109  }
110  }
111  }
112  }
113  }
114 
115  // comment this out, because not every out-link of a node has to be reached by every in-link. theresa, aug'17
116 // //second check matching of link's outlinks and lane's toLinks
117 // Link link = this.network.getLinks().get(l2l.getLinkId());
118 // log.info("Link id: " + l2l.getLinkId());
119 // Set<Id<Link>> toLinksFromLanes = new HashSet<>();
120 // for (Lane lane : l2l.getLanes().values()){
121 // if (lane.getToLinkIds() != null){
122 // toLinksFromLanes.addAll(lane.getToLinkIds());
123 // }
124 // }
125 //
126 // for (Link nodeOutLink : link.getToNode().getOutLinks().values()){
127 // log.info("\t\thas outlink: " + nodeOutLink.getId());
128 // if (!toLinksFromLanes.contains(nodeOutLink.getId())){
129 // log.error("Error: Lane Outlink: ");
130 // log.error("\t\tThe lanes of link " + link.getId() + " do not lead to all of the outlinks of the links toNode " + link.getToNode().getId() + " . The outlink " + nodeOutLink.getId()
131 // + " is not reachable from the lanes of this link. ");
132 // for (Lane lane : l2l.getLanes().values()){
133 // log.error("\t\tLane id: " + lane.getId());
134 // if (lane.getToLinkIds() != null){
135 // for (Id<Link> id : lane.getToLinkIds()){
136 // log.error("\t\t\t\thas toLinkId: " + id);
137 // }
138 // }
139 // log.error("End: Lane Outlink Error Message");
140 // }
141 // return false;
142 // }
143 // }
144  return true;
145  }
146 
147  public boolean isRemoveMalformed() {
148  return removeMalformed;
149  }
150 
151  public void setRemoveMalformed(boolean removeMalformed) {
152  this.removeMalformed = removeMalformed;
153  }
154 
155 }
SortedMap< Id< Link >, LanesToLinkAssignment > getLanesToLinkAssignments()
boolean isLaneOnLinkConsistent(LanesToLinkAssignment l2l)
Map< Id< Link >, ? extends Link > getLinks()
Map< Id< Link >, ? extends Link > getOutLinks()
LanesConsistencyChecker(Network net, Lanes laneDefs)