Debian Codegraph @

src/com/google/javascript/jscomp/CheckPathsBetweenNodes.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * See constructor, {@link #CheckPathsBetweenNodes(DiGraph,
27 * DiGraphNode, DiGraphNode, Predicate, Predicate)}, for a
28 * description of this algorithm.
29 *
30 *
31 * @param <N> The node type.
32 * @param <E> The edge type.
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 // This algorithm works in two stages. First, the depth-first search (DFS)
41 // tree is calculated with A as the root. During when constructing the DFS
42 // tree, back edges are recorded. A back edge is a non-tree edge (X -> Y)
43 // where X is an descendant of Y in the DFS tree. The second step does a
44 // recursive traversal of the graph. Back edges are ignored during the
45 // recursive traversal, thus no cycles are encountered. Any recursive branch
46 // that encounters B without yet satisfying the predicate represents a path
47 // from the entry node to the exit without any nodes that satisfy the
48 // predicate.
49 //
50 // The implementation of discoverBackEdges follows the DFS-Visit algorithm in
51 // "Introduction to Algorithms" by Cormen, Leiseron, Rivest, and Stein, 2nd
52 // ed., on page 541. The calculation of back edges is described on page 546.
53
54 // A non-tree edge in the DFS that connects a node to one of its ancestors.
55 private static final Annotation BACK_EDGE = new Annotation() {};
56 private static final Annotation VISITED_EDGE = new Annotation() {};
57
58 // Not yet visited.
59 private static final Annotation WHITE = null;
60 // Being visited.
61 private static final Annotation GRAY = new Annotation() {};
62 // Finished visiting.
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 * Given a graph G with nodes A and B, this algorithm determines if all paths
71 * from A to B contain at least one node satisfying a given predicate.
72 *
73 * Note that nodePredicate is not necessarily called for all nodes in G nor is
74 * edgePredicate called for all edges in G.
75 *
76 * @param graph Graph G to analyze.
77 * @param a The node A.
78 * @param b The node B.
79 * @param nodePredicate Predicate which at least one node on each path from an
80 * A node to B (inclusive) must match.
81 * @param edgePredicate Edges to consider as part of the graph. Edges in
82 * graph that don't match edgePredicate will be ignored.
83 * @param inclusive Includes node A and B in the test for the node predicate.
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 * Inclusive check.
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 * @return true iff all paths contain at least one node that satisfy the
108 * predicate
109 */
110 public boolean allPathsSatisfyPredicate() {
111 setUp();
112 boolean result = checkAllPathsWithoutBackEdges(start, end);
113 tearDown();
114 return result;
115 }
116
117 /**
118 * @return true iff some paths contain at least one node that satisfy the
119 * predicate
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 * Verify that all non-looping paths from {@code a} to {@code b} pass
161 * through at least one node where {@code nodePredicate} is true.
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 // Once we visited that edge once, we no longer need to
174 // re-visit it again.
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 * Verify that some non-looping paths from {@code a} to {@code b} pass
197 * through at least one node where {@code nodePredicate} is true.
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 // Once we visited that edge once, we no longer need to
210 // re-visit it again.
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 }