1 /***
2 * GraphUtils.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 java.util.Enumeration;
33 import java.util.Hashtable;
34
35 import att.grappa.Graph;
36 import att.grappa.Node;
37 import att.grappa.Edge;
38 import att.grappa.Element;
39 import att.grappa.Subgraph;
40
41 /***
42 * Class <code>GraphUtils</code> is a utility class for manipulating,
43 * copying of Graphs (specialized for the att.grappa package).
44 *
45 * @author Pawel Kowalski
46 * @version 1.0-beta
47 */
48 public class GraphUtils {
49
50 /***
51 * Copy a Graph.
52 *
53 * @param inGraph a <code>Graph</code> value
54 * @return a <code>Graph</code> value
55 *
56 * @tbd save Graph's counter before manipulating
57 */
58 public static Graph copy(Graph inGraph) {
59
60 Hashtable outElements = new Hashtable();
61 Graph outGraph = new Graph(inGraph.getName(), inGraph.isDirected(), inGraph.isStrict());
62 outElements.put(inGraph.getName(), outGraph);
63
64 Subgraph outSubgraph = null;
65 Node outNode = null;
66 Edge outEdge = null;
67 Element inElement = null;
68 Subgraph inSubgraph = null;
69 Node inNode = null;
70 Edge inEdge = null;
71 Subgraph parentSubgraph = null;
72 Node tailNode = null;
73 Node headNode = null;
74
75 int tmpVal = 0;
76
77 // save original counter var
78 //
79 // >>>>>>>>> this needs to be improved
80 //
81
82 // mark all elements as unprocessed
83 //
84 for (Enumeration enum = inGraph.elements(); enum.hasMoreElements(); ) {
85 ((Element)enum.nextElement()).counter = 1;
86 }
87
88 // mark graph as processed
89 //
90 inGraph.counter = 0;
91
92 // process all elements
93 //
94 for (int done = 1; done != 0; ) {
95
96 for (Enumeration enum = inGraph.elements(); enum.hasMoreElements(); ) {
97 inElement = (Element)enum.nextElement();
98
99 // create a new Subgraph
100 //
101 if (inElement instanceof Subgraph) {
102 inSubgraph = (Subgraph)inElement;
103
104 // subgraph created already, continue
105 //
106 if (outElements.get(inSubgraph.getName()) != null) {
107 continue;
108 }
109
110 // parent does not exist yet, continue
111 //
112 if ((parentSubgraph = (Subgraph)outElements.get(inSubgraph.getSubgraph().getName())) == null) {
113 continue;
114 }
115
116 outSubgraph = new Subgraph(parentSubgraph, inSubgraph.getName());
117 copyPublicMembers(outSubgraph, inSubgraph);
118 outElements.put(inSubgraph.getName(), outSubgraph);
119 inSubgraph.counter = 0;
120 }
121
122 // create a new Node
123 //
124 else if (inElement instanceof Node) {
125 inNode = (Node)inElement;
126
127 // node created already, continue
128 //
129 if (outElements.get(inNode.getName()) != null) {
130 continue;
131 }
132
133 // parent does not exist yet, continue
134 //
135 if ((parentSubgraph = (Subgraph)outElements.get(inNode.getSubgraph().getName())) == null) {
136 continue;
137 }
138
139 outNode = new Node(parentSubgraph, inNode.getName());
140 copyPublicMembers(outNode, inNode);
141 outElements.put(inNode.getName(), outNode);
142 inNode.counter = 0;
143 }
144
145 // create a new Edge
146 //
147 else if (inElement instanceof Edge) {
148 inEdge = (Edge)inElement;
149
150 // edge created already, continue
151 //
152 if (outElements.get(inEdge.getName()) != null) {
153 continue;
154 }
155
156 // parent does not exist yet, continue
157 //
158 if ((parentSubgraph = (Subgraph)outElements.get(inEdge.getSubgraph().getName())) == null) {
159 continue;
160 }
161
162 // do we have the tailNode already?
163 //
164 if ((tailNode = (Node)outElements.get(inEdge.getTail().getName())) == null) {
165 continue;
166 }
167
168 // do we have the headNode already?
169 //
170 if ((headNode = (Node)outElements.get(inEdge.getHead().getName())) == null) {
171 continue;
172 }
173
174 outEdge = new Edge(parentSubgraph, tailNode, headNode, inEdge.getName());
175 copyPublicMembers(outEdge, inEdge);
176 outElements.put(inEdge.getName(), outEdge);
177 inEdge.counter = 0;
178 }
179 }
180
181 // check, whether all elements have been processed
182 //
183 tmpVal = 0;
184 for (Enumeration enum = inGraph.elements(); enum.hasMoreElements(); ) {
185 tmpVal |= ((Element)enum.nextElement()).counter;
186 }
187 done = tmpVal;
188 }
189
190 // restore counter var
191 //
192 // >>>>>>>>> this needs to be improved
193 //
194
195 // copy all graph memebers
196 //
197 copyPublicMembers((Graph)outGraph, (Graph)inGraph);
198
199 return outGraph;
200 }
201
202 /***
203 * Copy Graph's all public members.
204 *
205 * @see att.grappa.Graph
206 *
207 * @param outGraph a <code>Graph</code> value
208 * @param inGraph a <code>Graph</code> value
209 *
210 * @tbd improve this method
211 */
212 private static void copyPublicMembers(Graph outGraph, Graph inGraph) {
213
214 // copy all subgraph members
215 //
216 copyPublicMembers((Subgraph)outGraph, (Subgraph)inGraph);
217
218 // copy all graph attributes
219 //
220 String key = null;
221 for (Enumeration enum = inGraph.getGrappaAttributeKeys(); enum.hasMoreElements(); ) {
222 key = (String)enum.nextElement();
223 Object value = inGraph.getGrappaAttributeValue(key);
224 if (value != null) {
225 outGraph.setGrappaAttribute(key, value.toString());
226 }
227 }
228
229 outGraph.setEditable(inGraph.isEditable());
230 outGraph.setErrorWriter(inGraph.getErrorWriter());
231 outGraph.setMenuable(inGraph.isMenuable());
232 outGraph.setSelectable(inGraph.isSelectable());
233 outGraph.setSynchronizePaint(inGraph.getSynchronizePaint());
234 outGraph.setToolTipText(inGraph.getToolTipText());
235 }
236
237 /***
238 * Copy all public members of a Graph's Subgraph
239 *
240 * @see att.grappa.Subgraph
241 *
242 * @param outSubgraph a <code>Subgraph</code> value
243 * @param inSubgraph a <code>Subgraph</code> value
244 */
245 private static void copyPublicMembers(Subgraph outSubgraph, Subgraph inSubgraph) {
246
247 // copy all element members
248 //
249 copyPublicMembers((Element)outSubgraph, (Element)inSubgraph);
250
251 // copy subgraph members
252 //
253 outSubgraph.setShowEdgeLabels(inSubgraph.getShowEdgeLabels());
254 outSubgraph.setShowNodeLabels(inSubgraph.getShowNodeLabels());
255 outSubgraph.setShowSubgraphLabels(inSubgraph.getShowSubgraphLabels());
256
257 String key = null;
258
259 // Node Attributes
260 //
261 for (Enumeration enum = inSubgraph.getNodeAttributeKeys(); enum.hasMoreElements(); ) {
262 key = (String)enum.nextElement();
263 outSubgraph.setNodeAttribute(key, inSubgraph.getNodeAttributeValue(key));
264 }
265
266 // Edge Attributes
267 //
268 for (Enumeration enum = inSubgraph.getEdgeAttributeKeys(); enum.hasMoreElements(); ) {
269 key = (String)enum.nextElement();
270 outSubgraph.setEdgeAttribute(key, inSubgraph.getEdgeAttributeValue(key));
271 }
272 }
273
274 /***
275 * Copy all public members of a Graph's Node
276 *
277 * @see att.grappa.Edge
278 *
279 * @param outNode a <code>Node</code> value
280 * @param inNode a <code>Node</code> value
281 */
282 private static void copyPublicMembers(Node outNode, Node inNode) {
283
284 // copy all element members
285 //
286 copyPublicMembers((Element)outNode, (Element)inNode);
287
288 // nothing else to be copied...
289 //
290 }
291
292 /***
293 * Copy all public members of a Graph's Edge
294 *
295 * @see att.grappa.Edge
296 *
297 * @param outEdge an <code>Edge</code> value
298 * @param inEdge an <code>Edge</code> value
299 */
300 private static void copyPublicMembers(Edge outEdge, Edge inEdge) {
301
302 // copy all element members
303 //
304 copyPublicMembers((Element)outEdge, (Element)inEdge);
305
306 // nothing else to be copied...
307 //
308 }
309
310 /***
311 * Copy all public members of a Graph's Element
312 *
313 * @see att.grappa.Element
314 *
315 * @param outElement an <code>Element</code> value
316 * @param inElement an <code>Element</code> value
317 */
318 private static void copyPublicMembers(Element outElement, Element inElement) {
319
320 // copy all element members
321 //
322 String key = null;
323 for (Enumeration enum = inElement.getLocalAttributeKeys(); enum.hasMoreElements(); ) {
324 key = (String)enum.nextElement();
325 outElement.setAttribute(key, inElement.getAttributeValue(key));
326 }
327
328 outElement.counter = inElement.counter;
329 outElement.highlight = inElement.highlight;
330 outElement.linewidth = inElement.linewidth;
331 outElement.object = inElement.object;
332 outElement.printAllAttributes = inElement.printAllAttributes;
333 outElement.printDefaultAttributes = inElement.printDefaultAttributes;
334 outElement.usePrintList = inElement.usePrintList;
335 outElement.visible = inElement.visible;
336 }
337 }
This page was automatically generated by Maven