Coverage details for edu.uci.ics.jung.graph.impl.HypergraphBPG

LineHitsSource
1 /*
2  * Copyright (c) 2003, the JUNG Project and the Regents of the University
3  * of California
4  * All rights reserved.
5  *
6  * This software is open-source under the BSD license; see either
7  * "license.txt" or
8  * http://jung.sourceforge.net/license.txt for a description.
9  */
10 /*
11  * Created on Dec 11, 2003
12  */
13 package edu.uci.ics.jung.graph.impl;
14  
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.Map;
20 import java.util.Set;
21  
22 import org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.collections.Transformer;
24  
25 import edu.uci.ics.jung.graph.ArchetypeEdge;
26 import edu.uci.ics.jung.graph.ArchetypeGraph;
27 import edu.uci.ics.jung.graph.ArchetypeVertex;
28 import edu.uci.ics.jung.graph.Hyperedge;
29 import edu.uci.ics.jung.graph.Hypergraph;
30 import edu.uci.ics.jung.graph.Hypervertex;
31 import edu.uci.ics.jung.utils.UserDataContainer;
32  
33 /**
34  * Implements a hypergraph built over an underlying
35  * Bipartite graph, using the equivalence explained in
36  * the FAQ. Fully implements the Hypergraph interface;
37  * its vertices and edges fully implement their interfaces.
38  *
39  * Use and create in the standard way; the underlying graph
40  * is invisible to the user (but can be extracted
41  * with a call to getBipartiteGraphEquivalent() ).
42  *
43  * @author danyelf
44  * @deprecated As of version 1.7, replaced by <code>SetHypergraph</code>.
45  * @see SetHypergraph
46  */
47 public class HypergraphBPG extends AbstractArchetypeGraph implements Hypergraph {
48  
49     protected BipartiteGraph bpg;
50  
518    public HypergraphBPG() {
528        initialize();
538    }
54  
55     protected void initialize()
56     {
578        this.bpg = new BipartiteGraph();
588        hyperedges = new HashMap();
598        hypervertices = new HashMap();
608        vertexToHyperVertex = new XToHyperX(hypervertices);
618        vertexToHyperEdge = new XToHyperX(hyperedges);
628        super.initialize();
638    }
64     
65     /**
66      * @see edu.uci.ics.jung.graph.ArchetypeGraph#newInstance()
67      */
68     public ArchetypeGraph newInstance() {
691        return new HypergraphBPG();
70     }
71  
721    public static final BipartiteGraph.Choice VERTEX = BipartiteGraph.CLASSA;
731    public static final BipartiteGraph.Choice EDGE = BipartiteGraph.CLASSB;
74  
75     Map hypervertices, hyperedges;
76     private Transformer vertexToHyperVertex, vertexToHyperEdge;
77  
78     /**
79      * Adds <code>v</code> to this graph.
80      */
81     public Hypervertex addVertex(Hypervertex v) {
8221        HypervertexBPG hv = (HypervertexBPG) v;
8321        this.bpg.addVertex(hv.underlying_vertex(), VERTEX);
8421        hypervertices.put(hv.underlying_vertex(), hv);
8521        hv.setGraph(this);
8621        mGraphListenerHandler.handleAdd(v);
8721        return v;
88     }
89  
90     /**
91      * Adds a single edge to the graph
92      *
93      * @see edu.uci.ics.jung.graph.Hypergraph#addEdge(edu.uci.ics.jung.graph.Hyperedge)
94      */
95     public Hyperedge addEdge(Hyperedge e) {
9614        HyperedgeBPG he = (HyperedgeBPG) e;
9714        this.bpg.addVertex( he.underlying_vertex(), EDGE );
9814        hyperedges.put( he.underlying_vertex(), he );
9914        he.setGraph( this );
10014        mGraphListenerHandler.handleAdd(e);
10114        return e;
102     }
103  
104     /**
105      * Returns a set of all the vertices in the graph.
106      *
107      * @see edu.uci.ics.jung.graph.ArchetypeGraph#getVertices()
108      */
109     public Set getVertices() {
11017        return new HashSet( hypervertices.values());
111     }
112  
113     /**
114      * Returns the set of all edges in the graph.
115      *
116      * @see edu.uci.ics.jung.graph.ArchetypeGraph#getEdges()
117      */
118     public Set getEdges() {
11917        return new HashSet( hyperedges.values() );
120     }
121  
122     /**
123      * Returns a count of the number of vertices in the graph.
124      *
125      * @see edu.uci.ics.jung.graph.ArchetypeGraph#numVertices()
126      */
127     public int numVertices() {
1280        return hypervertices.size();
129     }
130  
131     /**
132      * Returns a count of the number of edges in the graph.
133      *
134      * @see edu.uci.ics.jung.graph.ArchetypeGraph#numEdges()
135      */
136     public int numEdges() {
1370        return hyperedges.size();
138     }
139  
140     public void removeVertex(Hypervertex v) {
1410        HypervertexBPG hv = (HypervertexBPG) v;
1420        hypervertices.remove( hv.underlying_vertex() );
1430        bpg.removeVertex( hv.underlying_vertex() );
1440        hv.setGraph(null);
1450        mGraphListenerHandler.handleRemove(v);
1460    }
147  
148     public void removeEdge(Hyperedge e) {
1490        HyperedgeBPG he = (HyperedgeBPG) e;
1500        hyperedges.remove( he.underlying_vertex() );
1510        bpg.removeVertex( he.underlying_vertex() );
1520        he.setGraph(null);
1530        mGraphListenerHandler.handleRemove(e);
1540    }
155  
156     public void addVertices(Set vertices)
157     {
1580        for (Iterator iter = vertices.iterator(); iter.hasNext(); )
1590            addVertex((HypervertexBPG)iter.next());
1600    }
161     
162     public void addEdges(Set edges)
163     {
1640        for (Iterator iter = edges.iterator(); iter.hasNext(); )
1650            addEdge((HyperedgeBPG)iter.next());
1660    }
167     
168     /**
169      * @see edu.uci.ics.jung.graph.ArchetypeGraph#removeVertices(java.util.Set)
170      */
171     public void removeVertices(Set vertices) {
1720        for (Iterator iter = vertices.iterator(); iter.hasNext();) {
1730            HypervertexBPG v = (HypervertexBPG) iter.next();
1740            removeVertex( v );
175         }
1760    }
177  
178     /**
179      * @see edu.uci.ics.jung.graph.ArchetypeGraph#removeEdges(java.util.Set)
180      */
181     public void removeEdges(Set edges) {
1820        for (Iterator iter = edges.iterator(); iter.hasNext();) {
1830            HyperedgeBPG e = (HyperedgeBPG) iter.next();
1840            removeEdge( e );
185         }
1860    }
187  
188     /**
189      * @see edu.uci.ics.jung.graph.ArchetypeGraph#removeAllEdges()
190      */
191     public void removeAllEdges() {
1920        removeEdges( new HashSet( hyperedges.values() ));
1930    }
194  
195     /**
196      * @see edu.uci.ics.jung.graph.ArchetypeGraph#removeAllVertices()
197      */
198     public void removeAllVertices() {
1990        removeVertices( new HashSet( hypervertices.values()) );
2000    }
201  
202     /**
203      * @see edu.uci.ics.jung.graph.ArchetypeGraph#copy()
204      */
205     public ArchetypeGraph copy() {
2061        HypergraphBPG cln = (HypergraphBPG) this.newInstance();
2071        cln.bpg = (BipartiteGraph) bpg.copy();
2081        cln.updateHyperTable();
2091        return cln;
210     }
211  
212     /**
213      *
214      */
215     private void updateHyperTable() {
216         // creates a HyperEdge and HyperVertex for each Edge and Vertex in the tables
2171        for (Iterator iter = bpg.getAllVertices(VERTEX).iterator();
2184            iter.hasNext();
219             ) {
2203            BipartiteVertex bpv = (BipartiteVertex) iter.next();
2213            hypervertices.put(bpv, new HypervertexBPG(bpv, this));
222         }
2231        for (Iterator iter = bpg.getAllVertices(EDGE).iterator();
2243            iter.hasNext();
225             ) {
2262            BipartiteVertex bpe = (BipartiteVertex) iter.next();
2272            hyperedges.put(bpe, new HyperedgeBPG(bpe, this));
228         }
2291    }
230  
231     /**
232      * @see edu.uci.ics.jung.utils.UserDataContainer#addUserDatum(java.lang.Object, java.lang.Object, edu.uci.ics.jung.utils.UserDataContainer.CopyAction)
233      */
234     public void addUserDatum(Object key, Object datum, CopyAction copyAct) {
2350        bpg.addUserDatum(key, datum, copyAct);
2360    }
237  
238     /**
239      * @see edu.uci.ics.jung.utils.UserDataContainer#importUserData(edu.uci.ics.jung.utils.UserDataContainer)
240      */
241     public void importUserData(UserDataContainer udc) {
2420        bpg.importUserData(udc);
2430    }
244  
245     /**
246      * @see edu.uci.ics.jung.utils.UserDataContainer#getUserDatumKeyIterator()
247      */
248     public Iterator getUserDatumKeyIterator() {
2490        return bpg.getUserDatumKeyIterator();
250     }
251  
252     /**
253      * @see edu.uci.ics.jung.utils.UserDataContainer#getUserDatumCopyAction(java.lang.Object)
254      */
255     public CopyAction getUserDatumCopyAction(Object key) {
2560        return bpg.getUserDatumCopyAction(key);
257     }
258  
259     /**
260      * @see edu.uci.ics.jung.utils.UserDataContainer#getUserDatum(java.lang.Object)
261      */
262     public Object getUserDatum(Object key) {
2630        return bpg.getUserDatum(key);
264     }
265  
266     /**
267      * @see edu.uci.ics.jung.utils.UserDataContainer#setUserDatum(java.lang.Object, java.lang.Object, edu.uci.ics.jung.utils.UserDataContainer.CopyAction)
268      */
269     public void setUserDatum(Object key, Object datum, CopyAction copyAct) {
2700        bpg.setUserDatum(key, datum, copyAct);
271  
2720    }
273  
274     /**
275      * @see edu.uci.ics.jung.utils.UserDataContainer#removeUserDatum(java.lang.Object)
276      */
277     public Object removeUserDatum(Object key) {
2780        return bpg.removeUserDatum(key);
279     }
280  
281     /**
282      * Adds a vertex that is already part of the BPG. Is a part of
283      * the Vertex.copy() system.
284      * @param hv
285      */
286     void addVertex_without_adding(HypervertexBPG hv) {
2870        hypervertices.put( hv.underlying_vertex(), hv );
2880        hv.setGraph( this );
2890    }
290  
291     /**
292      * @param vertex2
293      * @return the vertex in the hypergraph corresponding to the underlying
294      * bipartite vertex <code>vertex2</code>
295      */
296     public ArchetypeVertex getVertexCorrespondingTo(BipartiteVertex vertex2) {
2973        return (HypervertexBPG) hypervertices.get(vertex2);
298     }
299  
300     /**
301      * @param vertex2
302      * @return the edge in the hypergraph corresponding to the underlying
303      * bipartite vertex <code>vertex2</code>
304      */
305     public ArchetypeEdge getEdgeCorrespondingTo(BipartiteVertex vertex2) {
3062        return (HyperedgeBPG) hyperedges.get(vertex2);
307     }
308  
309     /**
310      * @param realNeighbors
311      * @return
312      */
313     Set translateUnderlyingVertices(Set vertices) {
31413        Collection translated = CollectionUtils.collect( vertices, vertexToHyperVertex );
31513        return new HashSet( translated );
316     }
317  
318     /**
319      * @param set
320      * @return
321      */
322     Set translateUnderlyingEdges(Set vertices) {
3236        Collection translated = CollectionUtils.collect( vertices, vertexToHyperEdge);
3246        return new HashSet( translated );
325     }
326  
327  
328     private class XToHyperX implements Transformer {
329         private Map map;
330         XToHyperX( Map m ) {
331             this.map = m;
332         }
333         public Object transform(Object o) {
334             return map.get(o);
335         }
336     }
337  
338     /**
339      * Returns a BipartiteGraph equivalent to this Graph. Each
340      * vertex in the BipartiteGraph will be Equal to a HyperVertex
341      * or a HyperEdge in this graph.
342      * (Note that equals is NOT symmetrical in this case;
343      * <tt>hyperedge.equals( vertex )</tt>
344      * will return true for one of these vertices, but
345      * <tt>vertex.equals( hyperedge )</tt>
346      * will return false.
347      */
348     public BipartiteGraph getBipartiteGraphEquivalent() {
3490        return (BipartiteGraph) bpg.copy();
350     }
351  
352 }

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.