MATSIM
Static Public Member Functions | Private Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils Class Reference

Static Public Member Functions

static boolean isValid (Network network)
 
static void clean (Network network)
 
static List< List< Id< Link > > > getDisallowedLinkIdSequences (Network network, String mode)
 

Private Member Functions

 DisallowedNextLinksUtils ()
 

Static Private Member Functions

static List< String > getErrors (Map< Id< Link >, ? extends Link > links, Id< Link > linkId, DisallowedNextLinks disallowedNextLinks)
 
static List< String > isNextLinkSequenceOf (Map< Id< Link >, ? extends Link > links, Link link, List< Id< Link >> nextLinkIds)
 
static boolean isNextLinkOf (Link link, Id< Link > nextLinkId)
 
static List< String > isInAllowedModes (Map< Id< Link >, ? extends Link > links, String mode, Link link, List< Id< Link >> nextLinkIds)
 

Static Private Attributes

static final Logger LOG = LogManager.getLogger(DisallowedNextLinksUtils.class)
 

Detailed Description

Some methods to validate DisallowedNextLinks attributes of a network.

Author
hrewald

Definition at line 22 of file DisallowedNextLinksUtils.java.

Constructor & Destructor Documentation

◆ DisallowedNextLinksUtils()

org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.DisallowedNextLinksUtils ( )
private

Definition at line 26 of file DisallowedNextLinksUtils.java.

26  {
27  throw new IllegalStateException("Utility class");
28  }

Member Function Documentation

◆ isValid()

static boolean org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.isValid ( Network  network)
static

Check network for errors in the definition of disallowed next links and log them, if any.

See also
DisallowedNextLinks
Parameters
network
Returns
true if no errors regarding disallowed next links were detected

Definition at line 39 of file DisallowedNextLinksUtils.java.

References org.matsim.core.network.NetworkUtils.getDisallowedNextLinks(), org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.getErrors(), and org.matsim.api.core.v01.network.Network.getLinks().

Referenced by org.matsim.core.network.algorithms.NetworkMergeDoubleLinks.handleTurnRestrictions().

39  {
40 
41  Map<Id<Link>, ? extends Link> links = network.getLinks();
42  List<String> errors = links.entrySet().parallelStream()
43  .map(e -> {
44  DisallowedNextLinks disallowedNextLinks = NetworkUtils.getDisallowedNextLinks(e.getValue());
45  return disallowedNextLinks != null ? Map.entry(e.getKey(), disallowedNextLinks) : null;
46  })
47  .filter(e -> e != null)
48  .flatMap(e -> getErrors(links, e.getKey(), e.getValue()).stream())
49  .toList();
50 
51  errors.forEach(LOG::warn);
52  return errors.isEmpty();
53  }
Here is the call graph for this function:

◆ clean()

static void org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.clean ( Network  network)
static

Remove link sequences of DisallowedNextLinks which contain missing links or wrong modes.

Parameters
network

Definition at line 61 of file DisallowedNextLinksUtils.java.

References org.matsim.core.network.turnRestrictions.DisallowedNextLinks.addDisallowedLinkSequence(), org.matsim.api.core.v01.network.Link.getAllowedModes(), org.matsim.core.network.turnRestrictions.DisallowedNextLinks.getAsMap(), org.matsim.core.network.NetworkUtils.getDisallowedNextLinks(), org.matsim.api.core.v01.network.Network.getLinks(), org.matsim.core.network.turnRestrictions.DisallowedNextLinks.isEmpty(), org.matsim.core.network.turnRestrictions.DisallowedNextLinks.removeDisallowedLinkSequences(), and org.matsim.core.network.NetworkUtils.removeDisallowedNextLinks().

61  {
62  Map<Id<Link>, ? extends Link> links = network.getLinks();
63 
64  links.values().forEach(link -> {
65 
66  DisallowedNextLinks dnl = NetworkUtils.getDisallowedNextLinks(link);
67  if (dnl == null) {
68  return;
69  }
70 
71  // remove link sequences for modes, that are not allowed on this link
72  for (Entry<String, List<List<Id<Link>>>> entry : dnl.getAsMap().entrySet()) {
73  final String mode = entry.getKey();
74  final int linkSequencesCount = entry.getValue().size();
75 
76  if (!link.getAllowedModes().contains(mode)) {
77  dnl.removeDisallowedLinkSequences(mode);
78  LOG.info("Link {}: Removed all {} disallowed next link sequences of mode {}"
79  + " because {} is not allowed", link.getId(), linkSequencesCount, mode, mode);
80  }
81  }
82 
83  // keep only valid link sequences
84  for (Entry<String, List<List<Id<Link>>>> entry : dnl.getAsMap().entrySet()) {
85  final String mode = entry.getKey();
86  final List<List<Id<Link>>> linkSequences = entry.getValue();
87 
88  // find valid link sequences
89  List<List<Id<Link>>> validLinkSequences = linkSequences.stream()
90  // links of sequence exist in network
91  .filter(linkIds -> linkIds.stream().allMatch(links::containsKey))
92  // links all have mode in allowed modes
93  .filter(linkIds -> linkIds.stream()
94  .map(links::get)
96  .allMatch(allowedModes -> allowedModes.contains(mode)))
97  .toList();
98 
99  // update mode with valid link sequences
100  final int invalidLinkSequencesCount = linkSequences.size() - validLinkSequences.size();
101  if (invalidLinkSequencesCount > 0) {
102  dnl.removeDisallowedLinkSequences(mode);
103  validLinkSequences.forEach(linkIds -> dnl.addDisallowedLinkSequence(mode, linkIds));
104  LOG.info("Link {}: Removed {} disallowed next link sequences for mode {}",
105  link.getId(), invalidLinkSequencesCount, mode);
106  }
107  }
108 
109  // remove attribute completely, if it contains no link sequences anymore.
110  if (dnl.isEmpty()) {
111  NetworkUtils.removeDisallowedNextLinks(link);
112  }
113 
114  });
115  }
Here is the call graph for this function:

◆ getErrors()

static List<String> org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.getErrors ( Map< Id< Link >, ? extends Link links,
Id< Link linkId,
DisallowedNextLinks  disallowedNextLinks 
)
staticprivate

Definition at line 119 of file DisallowedNextLinksUtils.java.

References org.matsim.core.network.turnRestrictions.DisallowedNextLinks.getAsMap(), org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.isInAllowedModes(), and org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.isNextLinkSequenceOf().

Referenced by org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.isValid().

120  {
121 
122  List<String> errors = new ArrayList<>();
123 
124  Link link = links.get(linkId);
125  for (Entry<String, List<List<Id<Link>>>> entry : disallowedNextLinks.getAsMap().entrySet()) {
126  String mode = entry.getKey();
127  List<List<Id<Link>>> linkSequences = entry.getValue();
128 
129  for (List<Id<Link>> linkSequence : linkSequences) {
130  // check for (1) link sequences being a valid sequence and (2) links existing
131  errors.addAll(isNextLinkSequenceOf(links, link, linkSequence));
132 
133  // check for allowedModes on this and next links
134  errors.addAll(isInAllowedModes(links, mode, link, linkSequence));
135  }
136  }
137  return errors;
138  }
Here is the call graph for this function:

◆ isNextLinkSequenceOf()

static List<String> org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.isNextLinkSequenceOf ( Map< Id< Link >, ? extends Link links,
Link  link,
List< Id< Link >>  nextLinkIds 
)
staticprivate

Definition at line 140 of file DisallowedNextLinksUtils.java.

References org.matsim.api.core.v01.Identifiable< T >.getId(), and org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.isNextLinkOf().

Referenced by org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.getErrors().

141  {
142 
143  List<String> messages = new ArrayList<>();
144 
145  Link lastLink = link;
146  for (Id<Link> nextLinkId : nextLinkIds) {
147 
148  // all link ids in disallowedNextLinks need be subsequent links
149  if (!isNextLinkOf(lastLink, nextLinkId)) {
150  messages.add(String.format("Link %s had a next link sequence that is not valid sequence: %s",
151  link.getId(), nextLinkIds));
152  }
153  lastLink = links.get(nextLinkId);
154 
155  // all link ids in disallowedNextLinks need to exist
156  if (lastLink == null) {
157  messages.add(String.format("Link %s had a next link sequence with (a) missing link(s): %s",
158  link.getId(), nextLinkId));
159  }
160  }
161 
162  return messages;
163  }
Here is the call graph for this function:

◆ isNextLinkOf()

static boolean org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.isNextLinkOf ( Link  link,
Id< Link nextLinkId 
)
staticprivate

Definition at line 165 of file DisallowedNextLinksUtils.java.

References org.matsim.api.core.v01.network.Node.getOutLinks(), and org.matsim.api.core.v01.network.Link.getToNode().

Referenced by org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.isNextLinkSequenceOf().

165  {
166  Node toNode = link.getToNode();
167  return toNode.getOutLinks().get(nextLinkId) != null;
168  }
Here is the call graph for this function:

◆ isInAllowedModes()

static List<String> org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.isInAllowedModes ( Map< Id< Link >, ? extends Link links,
String  mode,
Link  link,
List< Id< Link >>  nextLinkIds 
)
staticprivate

Definition at line 170 of file DisallowedNextLinksUtils.java.

References org.matsim.api.core.v01.network.Link.getAllowedModes(), and org.matsim.api.core.v01.Identifiable< T >.getId().

Referenced by org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.getErrors().

171  {
172 
173  List<String> messages = new ArrayList<>();
174 
175  if (!link.getAllowedModes().contains(mode)) {
176  messages.add(String.format("Link %s does not allow mode %s",
177  link.getId(), mode));
178  }
179 
180  for (Id<Link> nextLinkId : nextLinkIds) {
181  Link nextLink = links.get(nextLinkId);
182  if (nextLink != null && !nextLink.getAllowedModes().contains(mode)) {
183  messages.add(String.format("Next link %s does not allow mode %s",
184  nextLink.getId(), mode));
185  }
186  }
187 
188  return messages;
189  }
Here is the call graph for this function:

◆ getDisallowedLinkIdSequences()

static List<List<Id<Link> > > org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.getDisallowedLinkIdSequences ( Network  network,
String  mode 
)
static

Returns a list of link id sequences that are not allowed to be traveled due to turn restrictions.

Parameters
network
modeuse turn restrictions of that mode
Returns

Definition at line 199 of file DisallowedNextLinksUtils.java.

References org.matsim.core.network.turnRestrictions.DisallowedNextLinks.getDisallowedLinkSequences(), org.matsim.core.network.NetworkUtils.getDisallowedNextLinks(), and org.matsim.api.core.v01.network.Network.getLinks().

199  {
200  return network.getLinks().values().stream()
201  .map(link -> {
202  DisallowedNextLinks dnl = NetworkUtils.getDisallowedNextLinks(link);
203  List<List<Id<Link>>> disallowedLinkSequences = Collections.emptyList();
204  if (dnl != null) {
205  disallowedLinkSequences = dnl.getDisallowedLinkSequences(mode);
206  }
207  return Map.entry(link.getId(), disallowedLinkSequences);
208  })
209  .filter(e -> !e.getValue().isEmpty())
210  .map(e -> {
211  List<List<Id<Link>>> linkSequences = new ArrayList<>(e.getValue().size());
212  for (List<Id<Link>> disallowedNextLinks : e.getValue()) {
213  List<Id<Link>> linkIds = new ArrayList<>(disallowedNextLinks.size() + 1);
214  linkIds.add(e.getKey()); // add this link at start of link id sequence
215  linkIds.addAll(disallowedNextLinks);
216  linkSequences.add(linkIds);
217  }
218  return linkSequences;
219  })
220  .flatMap(List::stream)
221  .toList();
222  }
Here is the call graph for this function:

Member Data Documentation

◆ LOG

final Logger org.matsim.core.network.turnRestrictions.DisallowedNextLinksUtils.LOG = LogManager.getLogger(DisallowedNextLinksUtils.class)
staticprivate

Definition at line 24 of file DisallowedNextLinksUtils.java.


The documentation for this class was generated from the following file: