@@ -16,7 +16,6 @@ import Grammar;
1616import ParseTree ;
1717import Relation ;
1818import Set ;
19- import util ::Maybe ;
2019
2120import lang ::rascal ::grammar ::Util ;
2221
@@ -50,7 +49,7 @@ Dependencies deps(Graph[Production] g) {
5049 = deps (removeNodes (g , getNodes (g , p , getAncestors = removeAncestors )));
5150 list [Production ] getProds ()
5251 = toList (g .nodes );
53-
52+
5453 return deps (retainProds , removeProds , getProds );
5554}
5655
@@ -82,10 +81,14 @@ alias Graph[&Node] = tuple[
8281 rel [&Node , &Node ] edges ];
8382
8483@synopsis {
85- Representation of predicates to select nodes in a graph
84+ Representation of predicates to select nodes in a graph based on their own
85+ properties , their ancestors , and their descendants
8686}
8787
88- alias Predicate [&Node ] = bool (&Node n );
88+ alias Predicate [&Node ] = bool (
89+ &Node n ,
90+ set [&Node ] ancestors /* of `n` in the graph */ ,
91+ set [&Node ] descendants /* of `n` in the graph */ );
8992
9093@synopsis {
9194 Gets the nodes of graph `g` that satisfy predicate `p`, optionally including
@@ -94,13 +97,13 @@ alias Predicate[&Node] = bool(&Node n);
9497
9598set [&Node ] getNodes (Graph [&Node ] g , Predicate [&Node ] p ,
9699 bool getAncestors = false , bool getDescendants = false ) {
97-
100+
98101 // Compute ancestors/descendants of nodes
99102 rel [&Node , &Node ] descendants = g .edges +;
100103 rel [&Node , &Node ] ancestors = invert (descendants );
101104
102105 // Select nodes
103- nodes = {n | n <- g .nodes , p (n )};
106+ nodes = {n | n <- g .nodes , p (n , ancestors [ n ] ? {}, descendants [ n ] ? {} )};
104107 nodes += ({} | it + (ancestors [n ] ? {}) | getAncestors , n <- nodes );
105108 nodes += ({} | it + (descendants [n ] ? {}) | getDescendants , n <- nodes );
106109 return nodes ;
@@ -118,27 +121,4 @@ Graph[&Node] retainNodes(Graph[&Node] g, set[&Node] nodes)
118121}
119122
120123Graph [&Node ] removeNodes (Graph [&Node ] g , set [&Node ] nodes )
121- = <g .nodes - nodes , carrierX (g .edges , nodes )> ;
122-
123- @synopsis {
124- Gets the closest ancestors that satisfy predicate `p` in each branch upward
125- from node `n` in graph `g`, optionally including `\default` when none of the
126- ancestors in a branch satisfy `p`
127- }
128-
129- set [&Node ] getClosestAncestors (
130- Graph [&Node ] g , Predicate [&Node ] p , &Node n ,
131- set [&Node ] getting = {}, Maybe [&Node ] \default = nothing ()) {
132-
133- if (n in getting ) {
134- return {};
135- } else {
136- set [&Node ] parents = invert (g .edges )[n ];
137- if ({} == parents && just (_) := \default ) {
138- return {\default .val };
139- } else {
140- set [&Node ] recur (&Node parent ) = getClosestAncestors (g , p , parent , getting = getting + n , \default = \default );
141- return {*(p (parent ) ? {parent } : recur (parent )) | parent <- parents };
142- }
143- }
144- }
124+ = <g .nodes - nodes , carrierX (g .edges , nodes )> ;
0 commit comments