| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Predicate; |
| 20 |
import com.google.javascript.jscomp.graph.Annotation; |
| 21 |
import com.google.javascript.jscomp.graph.DiGraph; |
| 22 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge; |
| 23 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphNode; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class CheckPathsBetweenNodes<N, E> { |
| 35 |
|
| 36 |
private final Predicate<N> nodePredicate; |
| 37 |
private final Predicate<DiGraphEdge<N, E>> edgePredicate; |
| 38 |
private final boolean inclusive; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private static final Annotation BACK_EDGE = new Annotation() {}; |
| 56 |
private static final Annotation VISITED_EDGE = new Annotation() {}; |
| 57 |
|
| 58 |
|
| 59 |
private static final Annotation WHITE = null; |
| 60 |
|
| 61 |
private static final Annotation GRAY = new Annotation() {}; |
| 62 |
|
| 63 |
private static final Annotation BLACK = new Annotation() {}; |
| 64 |
|
| 65 |
private final DiGraph<N, E> graph; |
| 66 |
private final DiGraphNode<N, E> start; |
| 67 |
private final DiGraphNode<N, E> end; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
CheckPathsBetweenNodes(DiGraph<N, E> graph, DiGraphNode<N, E> a, |
| 86 |
DiGraphNode<N, E> b, Predicate<N> nodePredicate, |
| 87 |
Predicate<DiGraphEdge<N, E>> edgePredicate, boolean inclusive) { |
| 88 |
this.graph = graph; |
| 89 |
this.start = a; |
| 90 |
this.end = b; |
| 91 |
this.nodePredicate = nodePredicate; |
| 92 |
this.edgePredicate = edgePredicate; |
| 93 |
this.inclusive = inclusive; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
CheckPathsBetweenNodes(DiGraph<N, E> graph, DiGraphNode<N, E> a, |
| 100 |
DiGraphNode<N, E> b, Predicate<N> nodePredicate, |
| 101 |
Predicate<DiGraphEdge<N, E>> edgePredicate) { |
| 102 |
this(graph, a, b, nodePredicate, edgePredicate, true); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public boolean allPathsSatisfyPredicate() { |
| 111 |
setUp(); |
| 112 |
boolean result = checkAllPathsWithoutBackEdges(start, end); |
| 113 |
tearDown(); |
| 114 |
return result; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public boolean somePathsSatisfyPredicate() { |
| 122 |
setUp(); |
| 123 |
boolean result = checkSomePathsWithoutBackEdges(start, end); |
| 124 |
tearDown(); |
| 125 |
return result; |
| 126 |
} |
| 127 |
|
| 128 |
private void setUp() { |
| 129 |
graph.pushNodeAnnotations(); |
| 130 |
graph.pushEdgeAnnotations(); |
| 131 |
discoverBackEdges(this.start); |
| 132 |
} |
| 133 |
|
| 134 |
private void tearDown() { |
| 135 |
graph.popNodeAnnotations(); |
| 136 |
graph.popEdgeAnnotations(); |
| 137 |
} |
| 138 |
|
| 139 |
private void discoverBackEdges(DiGraphNode<N, E> u) { |
| 140 |
u.setAnnotation(GRAY); |
| 141 |
for (DiGraphEdge<N, E> e : u.getOutEdges()) { |
| 142 |
if (ignoreEdge(e)) { |
| 143 |
continue; |
| 144 |
} |
| 145 |
DiGraphNode<N, E> v = e.getDestination(); |
| 146 |
if (v.getAnnotation() == WHITE) { |
| 147 |
discoverBackEdges(v); |
| 148 |
} else if (v.getAnnotation() == GRAY) { |
| 149 |
e.setAnnotation(BACK_EDGE); |
| 150 |
} |
| 151 |
} |
| 152 |
u.setAnnotation(BLACK); |
| 153 |
} |
| 154 |
|
| 155 |
private boolean ignoreEdge(DiGraphEdge<N, E> e) { |
| 156 |
return !edgePredicate.apply(e); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private boolean checkAllPathsWithoutBackEdges(DiGraphNode<N, E> a, |
| 164 |
DiGraphNode<N, E> b) { |
| 165 |
if (nodePredicate.apply(a.getValue()) && |
| 166 |
(inclusive || (a != start && a != end))) { |
| 167 |
return true; |
| 168 |
} |
| 169 |
if (a == b) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
for (DiGraphEdge<N, E> e : a.getOutEdges()) { |
| 173 |
|
| 174 |
|
| 175 |
if (e.getAnnotation() == VISITED_EDGE) { |
| 176 |
continue; |
| 177 |
} |
| 178 |
e.setAnnotation(VISITED_EDGE); |
| 179 |
|
| 180 |
if (ignoreEdge(e)) { |
| 181 |
continue; |
| 182 |
} |
| 183 |
if (e.getAnnotation() == BACK_EDGE) { |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
DiGraphNode<N, E> next = e.getDestination(); |
| 188 |
if (!checkAllPathsWithoutBackEdges(next, b)) { |
| 189 |
return false; |
| 190 |
} |
| 191 |
} |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private boolean checkSomePathsWithoutBackEdges(DiGraphNode<N, E> a, |
| 200 |
DiGraphNode<N, E> b) { |
| 201 |
if (nodePredicate.apply(a.getValue()) && |
| 202 |
(inclusive || (a != start && a != end))) { |
| 203 |
return true; |
| 204 |
} |
| 205 |
if (a == b) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
for (DiGraphEdge<N, E> e : a.getOutEdges()) { |
| 209 |
|
| 210 |
|
| 211 |
if (e.getAnnotation() == VISITED_EDGE) { |
| 212 |
continue; |
| 213 |
} |
| 214 |
e.setAnnotation(VISITED_EDGE); |
| 215 |
|
| 216 |
if (ignoreEdge(e)) { |
| 217 |
continue; |
| 218 |
} |
| 219 |
if (e.getAnnotation() == BACK_EDGE) { |
| 220 |
continue; |
| 221 |
} |
| 222 |
|
| 223 |
DiGraphNode<N, E> next = e.getDestination(); |
| 224 |
if (checkSomePathsWithoutBackEdges(next, b)) { |
| 225 |
return true; |
| 226 |
} |
| 227 |
} |
| 228 |
return false; |
| 229 |
} |
| 230 |
} |