| | 1 | package org.openstreetmap.josm.data.validation.tests; |
| | 2 | |
| | 3 | import static org.openstreetmap.josm.gui.MainApplication.getLayerManager; |
| | 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 5 | |
| | 6 | import java.util.ArrayDeque; |
| | 7 | import java.util.ArrayList; |
| | 8 | import java.util.Arrays; |
| | 9 | import java.util.Collection; |
| | 10 | import java.util.Deque; |
| | 11 | import java.util.HashMap; |
| | 12 | import java.util.HashSet; |
| | 13 | import java.util.Map; |
| | 14 | import java.util.Set; |
| | 15 | import java.util.stream.Collectors; |
| | 16 | |
| | 17 | import org.openstreetmap.josm.data.osm.IPrimitive; |
| | 18 | import org.openstreetmap.josm.data.osm.Node; |
| | 19 | import org.openstreetmap.josm.data.osm.NodeGraph; |
| | 20 | import org.openstreetmap.josm.data.osm.NodePair; |
| | 21 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 22 | import org.openstreetmap.josm.data.osm.Way; |
| | 23 | import org.openstreetmap.josm.data.validation.Severity; |
| | 24 | import org.openstreetmap.josm.data.validation.Test; |
| | 25 | import org.openstreetmap.josm.data.validation.TestError; |
| | 26 | import org.openstreetmap.josm.gui.progress.ProgressMonitor; |
| | 27 | import org.openstreetmap.josm.tools.Logging; |
| | 28 | |
| | 29 | |
| | 30 | /** |
| | 31 | * Test for detecting cycles in a directed graph, currently used for waterwoys only. |
| | 32 | * The graph consist of OSM dataset ways. |
| | 33 | * |
| | 34 | * @since xxx |
| | 35 | */ |
| | 36 | public class LoopDetector extends Test { |
| | 37 | public static final int LOOP_DETECTED = 4100; |
| | 38 | |
| | 39 | /** |
| | 40 | * Currently used directional waterways from the OSM wiki |
| | 41 | */ |
| | 42 | private static final Set<String> directionalWaterways = new HashSet<>( |
| | 43 | Arrays.asList("river", "stream", "tidal_channel", "drain", "ditch", "fish_pass", "fairway")); |
| | 44 | |
| | 45 | private final Set<Way> visitedWays = new HashSet<>(); |
| | 46 | |
| | 47 | public LoopDetector() { |
| | 48 | super(tr("Loop detector"), tr("Detects loops in connected directional ways.")); |
| | 49 | } |
| | 50 | |
| | 51 | @Override |
| | 52 | public boolean isPrimitiveUsable(OsmPrimitive p) { |
| | 53 | return p.isUsable() && (p instanceof Way) && ((Way) p).getNodesCount() > 1 && p.hasTag("waterway", directionalWaterways); |
| | 54 | } |
| | 55 | |
| | 56 | @Override |
| | 57 | public void startTest(ProgressMonitor progressMonitor) { |
| | 58 | super.startTest(progressMonitor); |
| | 59 | setShowElements(true); |
| | 60 | // TODO: osm partial selection validator doesn't work |
| | 61 | |
| | 62 | Collection<Collection<Way>> graphs = getGraphs(); |
| | 63 | |
| | 64 | // FIXME debug variables |
| | 65 | int i =0, g = 0; |
| | 66 | |
| | 67 | for (Collection<Way> graph : graphs) { |
| | 68 | NodeGraph nodeGraph = NodeGraph.createDirectedGraphFromWays(graph); |
| | 69 | Tarjan tarjan = new Tarjan(nodeGraph); |
| | 70 | Logging.debug("Graph looping... " + ++g); |
| | 71 | Collection<Collection<Node>> scc = tarjan.getSCC(); |
| | 72 | for (Collection<Node> possibleCycle : scc) { |
| | 73 | // contains a cycle (or loop) if the strongly connected components set size is larger than 1 |
| | 74 | if (possibleCycle.size() > 1) { |
| | 75 | Logging.debug("Cycle detected! " + ++i); |
| | 76 | errors.add( |
| | 77 | TestError.builder(this, Severity.ERROR, LOOP_DETECTED) |
| | 78 | .message(tr("Cycle in directional waterway network")) |
| | 79 | .primitives(possibleCycle) |
| | 80 | .build() |
| | 81 | ); |
| | 82 | } |
| | 83 | } |
| | 84 | progressMonitor.worked(1); |
| | 85 | } |
| | 86 | } |
| | 87 | |
| | 88 | @Override |
| | 89 | public void clear() { |
| | 90 | super.clear(); |
| | 91 | visitedWays.clear(); |
| | 92 | } |
| | 93 | |
| | 94 | /** |
| | 95 | * Returns all directional waterways which connects to at least one other usable way. |
| | 96 | * |
| | 97 | * @return all directional waterways which connects to at least one other usable way |
| | 98 | */ |
| | 99 | private Collection<Collection<Way>> getGraphs() { |
| | 100 | // 70127 waterway |
| | 101 | Set<Way> usableWaterways = getLayerManager() |
| | 102 | .getActiveDataSet() |
| | 103 | .getWays() |
| | 104 | .stream() |
| | 105 | .filter(this::isPrimitiveUsable) |
| | 106 | .collect(Collectors.toSet()); |
| | 107 | |
| | 108 | // HashSet doesn't make a difference here |
| | 109 | Collection<Collection<Way>> graphs = new ArrayList<>(); |
| | 110 | |
| | 111 | for (Way current : usableWaterways) { |
| | 112 | Collection<Way> graph = buildGraph(current); |
| | 113 | |
| | 114 | if (!graph.isEmpty()) |
| | 115 | graphs.add(graph); |
| | 116 | } |
| | 117 | |
| | 118 | Logging.debug("All usable waterways " + usableWaterways.size() + ", visited ways " + visitedWays.size() + ", graphs size " + graphs.size()); |
| | 119 | usableWaterways.removeAll(visitedWays); |
| | 120 | Logging.debug(String.format("Ignored ways (%s): %s", usableWaterways.size(), |
| | 121 | usableWaterways.stream().map(IPrimitive::getId).collect(Collectors.toList()))); |
| | 122 | |
| | 123 | return graphs; |
| | 124 | } |
| | 125 | |
| | 126 | /** |
| | 127 | * Returns a collection of ways which belongs to the same graph. |
| | 128 | * |
| | 129 | * @param way starting way to extend the graph from |
| | 130 | * @return a collection of ways which belongs to the same graph |
| | 131 | */ |
| | 132 | private Collection<Way> buildGraph(Way way) { |
| | 133 | if (visitedWays.contains(way)) |
| | 134 | return new HashSet<>(); |
| | 135 | |
| | 136 | final Set<Way> graph = new HashSet<>(); |
| | 137 | |
| | 138 | for (Node node : way.getNodes()) { |
| | 139 | final Collection<Way> referrers = node.referrers(Way.class).filter(this::isPrimitiveUsable).collect(Collectors.toSet()); |
| | 140 | referrers.remove(way); |
| | 141 | |
| | 142 | if (!referrers.isEmpty()) { |
| | 143 | visitedWays.add(way); |
| | 144 | for (Way referrer : referrers) { |
| | 145 | graph.addAll(buildGraph(referrer)); |
| | 146 | } |
| | 147 | } |
| | 148 | graph.addAll(referrers); |
| | 149 | } |
| | 150 | return graph; |
| | 151 | } |
| | 152 | |
| | 153 | /** |
| | 154 | * Tarjan's algorithm implementation for JOSM. |
| | 155 | * |
| | 156 | * @see <a href="https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm"> |
| | 157 | * Tarjan's strongly connected components algorithm</a> |
| | 158 | */ |
| | 159 | public static class Tarjan { |
| | 160 | |
| | 161 | /** |
| | 162 | * Helper class for storing algorithm runtime metadata. |
| | 163 | */ |
| | 164 | private static class TarjanHelper { |
| | 165 | private final int index; |
| | 166 | private int lowlink; |
| | 167 | |
| | 168 | public TarjanHelper(int index, int lowlink) { |
| | 169 | this.index = index; |
| | 170 | this.lowlink = lowlink; |
| | 171 | } |
| | 172 | } |
| | 173 | |
| | 174 | /** |
| | 175 | * A simple key-value registry to store visited nodes and its metadata |
| | 176 | */ |
| | 177 | private final Map<Node, TarjanHelper> registry = new HashMap<>(); |
| | 178 | |
| | 179 | private int index = 0; |
| | 180 | private final Collection<Collection<Node>> scc = new HashSet<>(); |
| | 181 | private final Deque<Node> stack = new ArrayDeque<>(); |
| | 182 | private final NodeGraph graph; |
| | 183 | |
| | 184 | public Tarjan(NodeGraph graph) { |
| | 185 | this.graph = graph; |
| | 186 | } |
| | 187 | |
| | 188 | /** |
| | 189 | * Returns the strongly connected components in the current graph. |
| | 190 | * |
| | 191 | * @return the strongly connected components in the current graph |
| | 192 | */ |
| | 193 | public Collection<Collection<Node>> getSCC() { |
| | 194 | for (Node node : graph.getNodes()) { |
| | 195 | if (!registry.containsKey(node)) { |
| | 196 | Logging.debug("Started SCC for n" + node.getId()); |
| | 197 | strongConnect(node); |
| | 198 | Logging.debug("...done"); |
| | 199 | } |
| | 200 | } |
| | 201 | return scc; |
| | 202 | } |
| | 203 | |
| | 204 | /** |
| | 205 | * Calculates strongly connected components available from the given node. |
| | 206 | * |
| | 207 | * @param v |
| | 208 | */ |
| | 209 | private void strongConnect(Node v) { |
| | 210 | registry.put(v, new TarjanHelper(index, index)); |
| | 211 | index++; |
| | 212 | stack.push(v); |
| | 213 | |
| | 214 | // FIXME: performance issue here, possibly because of constant getSuccessors() call |
| | 215 | for (Node w : getSuccessors(v)) { |
| | 216 | if (!registry.containsKey(w)) { |
| | 217 | strongConnect(w); |
| | 218 | TarjanHelper vHelper = registry.get(v); |
| | 219 | TarjanHelper wHelper = registry.get(w); |
| | 220 | vHelper.lowlink = Math.min(vHelper.lowlink, wHelper.lowlink); |
| | 221 | } else if (stack.contains(w)) { |
| | 222 | TarjanHelper vHelper = registry.get(v); |
| | 223 | TarjanHelper wHelper = registry.get(w); |
| | 224 | vHelper.lowlink = Math.min(vHelper.lowlink, wHelper.index); |
| | 225 | } |
| | 226 | } |
| | 227 | |
| | 228 | final TarjanHelper vHelper = registry.get(v); |
| | 229 | if (vHelper.lowlink == vHelper.index) { |
| | 230 | Collection<Node> currentSCC = new HashSet<>(); |
| | 231 | Node w; |
| | 232 | do { |
| | 233 | w = stack.remove(); |
| | 234 | currentSCC.add(w); |
| | 235 | } while (!w.equals(v)); |
| | 236 | scc.add(currentSCC); |
| | 237 | } |
| | 238 | } |
| | 239 | |
| | 240 | /** |
| | 241 | * Returns direct successors from the graph of the given node. |
| | 242 | * |
| | 243 | * @param node a node to start search from |
| | 244 | * @return a collection of nodes from the graph which are direct neighbors of the given node |
| | 245 | */ |
| | 246 | private Collection<Node> getSuccessors(Node node) { |
| | 247 | final Collection<Node> successors = new HashSet<>(); |
| | 248 | for (NodePair pair : graph.getEdges()) { |
| | 249 | if (pair.getA().equals(node)) { |
| | 250 | successors.add(pair.getB()); |
| | 251 | } |
| | 252 | } |
| | 253 | return successors; |
| | 254 | } |
| | 255 | } |
| | 256 | } |