1 /***
2 * Filter.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 10.12.2002 pko creation
9 *
10 * Copyright 2003 ELCA Informatique SA
11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland
12 * www.elca.ch
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public License
16 * as published by the Free Software Foundation; either version 2.1 of
17 * the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * USA
28 */
29
30 package ch.elca.dependency.graph;
31
32 import org.apache.regexp.RE;
33 import org.apache.regexp.RESyntaxException;
34 import java.util.Enumeration;
35
36 import org.apache.log4j.Logger;
37
38 import att.grappa.Edge;
39 import att.grappa.Graph;
40 import att.grappa.Node;
41
42 /***
43 * A <code>Filter</code> is responsible for filtering Nodes from a Graph.
44 *
45 * @see att.grappa.Graph
46 * @see att.grappa.Node
47 *
48 * @author Pawel Kowalski
49 * @version 1.0-beta
50 */
51 public class Filter extends AbstractGraphProcessor {
52
53 /***
54 * Log4j Logger.
55 */
56 private static final Logger LOG = Logger.getLogger(Filter.class);
57
58 /***
59 * A <code>regexp String</code> associated with this
60 * <code>Aggregator</code>.
61 */
62 protected String m_regexpString = null;
63
64 /***
65 * A <code>RE</code> (regular expression) associated with this
66 * <code>Aggregator</code>.
67 */
68 protected RE m_regexp = null;
69
70 /***
71 * A flag, telling whether the specified regular expression could
72 * be successfully used for building a <code>RE</code> Program.
73 */
74 protected boolean m_noRegexp = false;
75
76 /***
77 * Creates a new <code>Filter</code> instance.
78 */
79 public Filter() {
80 }
81
82 /***
83 * Creates a new <code>Filter</code> instance with a name and a String
84 * used to be used for building a RE (regular expression program).
85 *
86 * @see gnu.regexp.RE
87 *
88 * @param name a <code>String</code> value
89 * @param regexpString a <code>String</code> value
90 */
91 public Filter(String name, String regexpString) {
92 super(name);
93 setRegexp(regexpString);
94 }
95
96 /***
97 * Set a regular expression for filtering nodes.
98 *
99 * @param regexpString a <code>String</code> value
100 */
101 public void setRegexp(String regexpString) {
102 m_regexpString = regexpString;
103 try {
104 m_regexp = new RE(regexpString);
105 } catch (RESyntaxException e) {
106 LOG.error("Syntax error in Regexp: " + regexpString);
107 m_noRegexp = true;
108 }
109 }
110
111 /***
112 * Get the regexp used to filter nodes.
113 *
114 * @return a <code>String</code> value
115 */
116 public String getRegexp() {
117 return m_regexpString;
118 }
119
120 /***
121 * Filter nodes accordingl to a regexp. This is an
122 * Implementation of the abstract method inherited from the
123 * <code>AbstractGraphProcessor</code>.
124 *
125 * @see gnu.regexp.RE
126 *
127 * @param graph a <code>Graph</code> value
128 * @return a <code>Graph</code> value
129 */
130 public Graph process(Graph graph) {
131
132 // syntax error in regexp, ignore this filter
133 //
134 if (m_noRegexp) {
135 return graph;
136 }
137
138 LOG.debug("Process graph: " + graph.getName());
139
140 Node node = null;
141 Node tailNode = null;
142 Node headNode = null;
143 Edge edge = null;
144 String nodeName = "";
145
146 // enumerate over all nodes
147 //
148 for (Enumeration enum = graph.nodeElements(); enum.hasMoreElements(); ) {
149 node = (Node)enum.nextElement();
150 nodeName = node.getName();
151
152 // node that matches the regexp?
153 //
154 if (m_regexp.match(nodeName)) {
155
156 // remove all in-edges attached to this node
157 //
158 for (Enumeration edgeEnum = node.inEdgeElements(); edgeEnum.hasMoreElements(); ) {
159 edge = (Edge)edgeEnum.nextElement();
160 tailNode = edge.getTail();
161
162 // remove edge from graph and head/tail nodes
163 //
164 node.removeEdge(edge, true);
165 tailNode.removeEdge(edge, false);
166 graph.removeEdge(edge.getName());
167 }
168
169 // remove all out-edges attached to this node
170 //
171 for (Enumeration edgeEnum = node.outEdgeElements(); edgeEnum.hasMoreElements(); ) {
172 edge = (Edge)edgeEnum.nextElement();
173 headNode = edge.getHead();
174
175 // remove edge from graph and head/tail nodes
176 //
177 node.removeEdge(edge, false);
178 headNode.removeEdge(edge, true);
179 graph.removeEdge(edge.getName());
180 }
181
182 // remove node from graph
183 //
184 graph.removeNode(nodeName);
185 LOG.debug("Removed node: " + nodeName);
186 }
187 }
188 return graph;
189 }
190 }
This page was automatically generated by Maven