Coverage details for edu.uci.ics.jung.graph.filters.UnassembledGraph

LineHitsSource
1 /*
2  * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  */
8 package edu.uci.ics.jung.graph.filters;
9  
10 import java.util.Iterator;
11 import java.util.Set;
12  
13 import edu.uci.ics.jung.exceptions.FatalException;
14 import edu.uci.ics.jung.graph.*;
15 import edu.uci.ics.jung.utils.UserData;
16  
17 /**
18  * This class represents an unassembled graph. It does not implement Graph. It
19  * represents the collection of vertices and edges that were generated by the
20  * filter. VERTICES that are members of this DO NOT fulfill the vertex contract,
21  * as they claim their graph is the source graph.
22  * <p>
23  * The filter process looks like this: <br>
24  *
25  * <pre>
26  *
27  * SOURCE GRAPH - [ Filter1 ] - UnassembledGraph - [Filter2] - UnassembledGraph - [Assemble] - Graph
28  *
29  * </pre>
30  *
31  * @author danyelf
32  */
33 public class UnassembledGraph {
34  
35     protected String name;
36  
37     /**
38      * Holds a reference to the filter that generated this UnassembledGraph
39      */
40     protected Filter filter;
41  
42     // CONSTRAINT: all vertices in these sets are members of OriginalGraph
43     /**
44      * Holds a reference to the filter that generated this UnassembledGraph
45      */
46     protected Set vertexSet;
47  
48     protected Set edgeSet;
49  
50     // If there was a graph before this one, this is it
51     protected UnassembledGraph previousGraph;
52  
53     // This is the last real graph in the sequence
54     protected Graph originalGraph;
55  
5675    public UnassembledGraph(Filter f, Set vertices, Set edges, Graph original) {
5775        this.filter = f;
5875        this.vertexSet = vertices;
5975        this.edgeSet = edges;
6075        this.previousGraph = null;
6175        this.originalGraph = original;
6275        this.name = filter.getName();
6375        checkData();
6475    }
65  
66     /**
67      * A constructor that uses non-Filters (for example, GraphCluterers) to
68      * build themselves.
69      *
70      * @param name
71      * A name to refer to the caller (e.g.
72      * "EdgeBetweenessCluster(3)")
73      * @param vertices
74      * The set of vertices in this UnassembledGraph
75      * @param edges
76      * The set of edges in this UnassembledGraph
77      * @param original
78      * The graph from which this data comes
79      */
800    public UnassembledGraph(String name, Set vertices, Set edges, Graph original) {
810        this.filter = null;
820        this.vertexSet = vertices;
830        this.edgeSet = edges;
840        this.previousGraph = null;
850        this.originalGraph = original;
860        this.name = name;
870        checkData();
880    }
89  
90     public UnassembledGraph(Filter f, Set vertices, Set edges,
914            UnassembledGraph previous) {
924        this.filter = f;
934        this.vertexSet = vertices;
944        this.edgeSet = edges;
954        this.previousGraph = previous;
964        this.originalGraph = previous.getOriginalGraph();
974        this.name = filter.getName();
984        checkData();
994    }
100  
101     /**
102      * Returns the name of the filter that generated this UnassembledGraph.
103      */
104     public String getFilterName() {
1057        String rv = name;
1067        if (previousGraph != null) {
1072            rv += ":" + previousGraph.getFilterName();
108         }
1097        return rv;
110     }
111  
112     private void checkData() {
11379        for (Iterator iter = vertexSet.iterator(); iter.hasNext();) {
114586            Vertex e = (Vertex) iter.next();
115586            if (e.getGraph() != originalGraph)
1160                    throw new FatalException("Vertex not in original");
117         }
118  
11979        for (Iterator iter = edgeSet.iterator(); iter.hasNext();) {
1201474            Edge e = (Edge) iter.next();
1211474            if (e.getGraph() != originalGraph)
1220                    throw new FatalException("Edge not in original");
123         }
12479    }
125  
126     /**
127      * Returns the original graph that was subsetted for this UnsassembledGraph.
128      */
129     public Graph getOriginalGraph() {
1309        return originalGraph;
131     }
132  
133     /**
134      * Returns the set of vertices (from <tt>getOriginalGraph()</tt>) that
135      * passed the filter.
136      */
137     public Set getUntouchedVertices() {
13898        return vertexSet;
139     }
140  
141     /**
142      * Returns the set of edges (from <tt>getOriginalGraph()</tt>) that
143      * passed the filter.
144      */
145     public Set getUntouchedEdges() {
146239        return edgeSet;
147     }
148  
149     public String toString() {
1500        if (previousGraph == null) {
1510            return "UNASSEMBLED" + "<" + originalGraph + ">";
152         } else {
1530            return "UNASSEMBLED" + "<" + previousGraph + ">";
154         }
155     }
156  
157     /**
158      * Constructs a new graph based on the source graph. Assumes that edges
159      * should only be copied if they contain all the original edge's vertices;
160      * all vertices that pass the filter are copied.
161      * <p>
162      * Here's the general theory:
163      * <ul>
164      * <li>A Graph object of the appropriate type can be obtained by calling
165      * <tt>{@link edu.uci.ics.jung.graph.ArchetypeGraph#newInstance() Graph.newInstance()}</tt>
166      * on the original.</li>
167      * <li>Then, the graph is populated with all vertices that the filters have
168      * passed (that is, the vertices in
169      * <tt>{@link #getUntouchedVertices() getUntouchedVertices()}</tt>.
170      * Vertices are copied in with
171      * <tt>{@link edu.uci.ics.jung.graph.ArchetypeVertex#copy( ArchetypeGraph ) copy(Graph)}</tt>.
172      * </li>
173      * <li>Last, the graph is populated with all edges that have both passed,
174      * and have both ends in vertices that passed.</li>
175      * </ul>
176      */
177     public Graph assemble(boolean shouldPreserveRecord) {
178         // constructs AG from the various edges
17952        Graph subgraph = (Graph) originalGraph.newInstance();
180  
18152        if (shouldPreserveRecord) {
1825            subgraph.addUserDatum(GraphAssemblyRecord.FILTER_GRAPH_KEY,
183                     new GraphAssemblyRecord(this), UserData.REMOVE);
184         }
185  
18652        Set localVertices = getUntouchedVertices();
187  
188         // now, we scoop up vertices
18952        for (Iterator iter = localVertices.iterator(); iter.hasNext();) {
190415            ArchetypeVertex newV = (ArchetypeVertex) iter.next();
191415            newV.copy(subgraph);
192         }
193  
194         // and only the edges that actually match
19552        for (Iterator iter = getUntouchedEdges().iterator(); iter.hasNext();) {
1961287            ArchetypeEdge newE = (ArchetypeEdge) iter.next();
1971287            if (localVertices.containsAll(newE.getIncidentVertices())) {
198486                newE.copy(subgraph);
199             }
200         }
201  
20252        return subgraph;
203     }
204  
205     public Graph assemble() {
20647        return assemble(false);
207     }
208  
209 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.