1 package org.matsim.core.network.turnRestrictions;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import java.util.Map.Entry;
9 import org.apache.logging.log4j.LogManager;
10 import org.apache.logging.log4j.Logger;
27 throw new IllegalStateException(
"Utility class");
41 Map<Id<Link>, ? extends
Link> links = network.
getLinks();
42 List<String> errors = links.entrySet().parallelStream()
45 return disallowedNextLinks != null ? Map.entry(e.getKey(), disallowedNextLinks) : null;
47 .filter(e -> e != null)
48 .flatMap(e ->
getErrors(links, e.getKey(), e.getValue()).stream())
51 errors.forEach(LOG::warn);
52 return errors.isEmpty();
62 Map<Id<Link>, ? extends
Link> links = network.
getLinks();
64 links.values().forEach(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();
76 if (!link.getAllowedModes().contains(mode)) {
78 LOG.info(
"Link {}: Removed all {} disallowed next link sequences of mode {}" 79 +
" because {} is not allowed", link.getId(), linkSequencesCount, mode, mode);
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();
89 List<List<Id<Link>>> validLinkSequences = linkSequences.stream()
91 .filter(linkIds -> linkIds.stream().allMatch(links::containsKey))
93 .filter(linkIds -> linkIds.stream()
96 .allMatch(allowedModes -> allowedModes.contains(mode)))
100 final int invalidLinkSequencesCount = linkSequences.size() - validLinkSequences.size();
101 if (invalidLinkSequencesCount > 0) {
104 LOG.info(
"Link {}: Removed {} disallowed next link sequences for mode {}",
105 link.getId(), invalidLinkSequencesCount, mode);
122 List<String> errors =
new ArrayList<>();
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();
129 for (List<
Id<Link>> linkSequence : linkSequences) {
143 List<String> messages =
new ArrayList<>();
145 Link lastLink = link;
146 for (
Id<Link> nextLinkId : nextLinkIds) {
150 messages.add(String.format(
"Link %s had a next link sequence that is not valid sequence: %s",
151 link.
getId(), nextLinkIds));
153 lastLink = links.get(nextLinkId);
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));
167 return toNode.
getOutLinks().get(nextLinkId) != null;
173 List<String> messages =
new ArrayList<>();
176 messages.add(String.format(
"Link %s does not allow mode %s",
177 link.
getId(), mode));
180 for (
Id<Link> nextLinkId : nextLinkIds) {
181 Link nextLink = links.get(nextLinkId);
183 messages.add(String.format(
"Next link %s does not allow mode %s",
184 nextLink.
getId(), mode));
200 return network.
getLinks().values().stream()
203 List<List<Id<Link>>> disallowedLinkSequences = Collections.emptyList();
207 return Map.entry(link.getId(), disallowedLinkSequences);
209 .filter(e -> !e.getValue().isEmpty())
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());
215 linkIds.addAll(disallowedNextLinks);
216 linkSequences.add(linkIds);
218 return linkSequences;
220 .flatMap(List::stream)
List< List< Id< Link > > > getDisallowedLinkSequences(String mode)
static boolean isValid(Network network)
static void clean(Network network)
DisallowedNextLinksUtils()
List< List< Id< Link > > > removeDisallowedLinkSequences(String mode)
static List< List< Id< Link > > > getDisallowedLinkIdSequences(Network network, String mode)
static void removeDisallowedNextLinks(Link link)
boolean addDisallowedLinkSequence(String mode, List< Id< Link >> linkSequence)
static DisallowedNextLinks getDisallowedNextLinks(Link link)
Map< Id< Link >, ? extends Link > getLinks()
static List< String > getErrors(Map< Id< Link >, ? extends Link > links, Id< Link > linkId, DisallowedNextLinks disallowedNextLinks)
static List< String > isInAllowedModes(Map< Id< Link >, ? extends Link > links, String mode, Link link, List< Id< Link >> nextLinkIds)
static boolean isNextLinkOf(Link link, Id< Link > nextLinkId)
Map< String, List< List< Id< Link > > > > getAsMap()
Map< Id< Link >, ? extends Link > getOutLinks()
static List< String > isNextLinkSequenceOf(Map< Id< Link >, ? extends Link > links, Link link, List< Id< Link >> nextLinkIds)
Set< String > getAllowedModes()