Coverage details for edu.uci.ics.jung.io.PajekNetWriter

LineHitsSource
1 /*
2  * Created on May 4, 2004
3  *
4  * Copyright (c) 2004, the JUNG Project and the Regents of the University
5  * of California
6  * All rights reserved.
7  *
8  * This software is open-source under the BSD license; see either
9  * "license.txt" or
10  * http://jung.sourceforge.net/license.txt for a description.
11  */
12 package edu.uci.ics.jung.io;
13  
14 import java.awt.geom.Point2D;
15 import java.io.BufferedWriter;
16 import java.io.FileWriter;
17 import java.io.IOException;
18 import java.io.Writer;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.Set;
22  
23 import edu.uci.ics.jung.graph.DirectedEdge;
24 import edu.uci.ics.jung.graph.Graph;
25 import edu.uci.ics.jung.graph.UndirectedEdge;
26 import edu.uci.ics.jung.graph.Vertex;
27 import edu.uci.ics.jung.graph.decorators.ConstantEdgeValue;
28 import edu.uci.ics.jung.graph.decorators.Indexer;
29 import edu.uci.ics.jung.graph.decorators.NumberEdgeValue;
30 import edu.uci.ics.jung.graph.decorators.VertexStringer;
31 import edu.uci.ics.jung.utils.Pair;
32 import edu.uci.ics.jung.utils.PredicateUtils;
33 import edu.uci.ics.jung.visualization.VertexLocationFunction;
34  
35  
36 /**
37  * Writes graphs in the Pajek NET format.
38  *
39  * <p>Labels for vertices may optionally be specified by implementations of
40  * <code>VertexStringer</code>. Edge weights are optionally specified by
41  * implementations of <code>NumberEdgeValue</code>. Vertex locations
42  * are optionally specified by implementations of
43  * <code>VertexLocationFunction</code>. Note that vertex location coordinates
44  * must be normalized to the interval [0, 1] on each axis in order to conform to the
45  * Pajek specification.</p>
46  *
47  * @author Joshua O'Madadhain
48  */
49 public class PajekNetWriter
50 {
51     public PajekNetWriter()
523    {
533    }
54  
55     /**
56      * Saves <code>g</code> to <code>filename</code>. Labels for vertices may
57      * be supplied by <code>vs</code>. Edge weights are specified by <code>nev</code>.
58      *
59      * @see #save(Graph, Writer, VertexStringer, NumberEdgeValue, VertexLocationFunction)
60      * @throws IOException
61      */
62     public void save(Graph g, String filename, VertexStringer vs,
63             NumberEdgeValue nev, VertexLocationFunction vld) throws IOException
64     {
656        save(g, new FileWriter(filename), vs, nev, vld);
666    }
67     
68     public void save(Graph g, String filename, VertexStringer vs,
69             NumberEdgeValue nev) throws IOException
70     {
710        save(g, new FileWriter(filename), vs, nev, null);
720    }
73     
74     /**
75      * Saves <code>g</code> to <code>filename</code>; no vertex labels are written out,
76      * and the edge weights are written as 1.0.
77      *
78      * @throws IOException
79      */
80     public void save(Graph g, String filename) throws IOException
81     {
820        save(g, filename, null, null, null);
830    }
84  
85     /**
86      * Saves <code>g</code> to <code>w</code>; no vertex labels are written out,
87      * and the edge weights are written as 1.0.
88      *
89      * @throws IOException
90      */
91     public void save(Graph g, Writer w) throws IOException
92     {
930        save(g, w, null, null, null);
940    }
95  
96     public void save(Graph g, Writer w, VertexStringer vs,
97             NumberEdgeValue nev) throws IOException
98     {
990        save(g, w, vs, nev, null);
1000    }
101     
102     /**
103      * Writes <code>graph</code> to <code>w</code>. Labels for vertices may
104      * be supplied by <code>vs</code> (defaults to no labels if null),
105      * edge weights may be specified by <code>nev</code>
106      * (defaults to weights of 1.0 if null),
107      * and vertex locations may be specified by <code>vld</code> (defaults
108      * to no locations if null).
109      */
110     public void save(Graph graph, Writer w, VertexStringer vs, NumberEdgeValue nev, VertexLocationFunction vld) throws IOException
111     {
112         /*
113          * TODO: Changes we might want to make:
114          * - optionally writing out in list form
115          */
116         
1176        BufferedWriter writer = new BufferedWriter(w);
1186        if (nev == null)
1194            nev = new ConstantEdgeValue(1);
1206        writer.write("*Vertices " + graph.numVertices());
1216        writer.newLine();
1226        Vertex currentVertex = null;
123  
124 // if (vld != null)
125 // vld = VertexLocationUtils.scale(vld, 1.0, 1.0);
126         
1276        Indexer id = Indexer.getIndexer(graph);
1286        for (Iterator i = graph.getVertices().iterator(); i.hasNext();)
129         {
13030            currentVertex = (Vertex) i.next();
131             // convert from 0-based to 1-based index
13230            Integer v_id = new Integer(id.getIndex(currentVertex) + 1);
13330            writer.write(v_id.toString());
13430            if (vs != null)
135             {
13630                String label = vs.getLabel(currentVertex);
13730                if (label != null)
13830                    writer.write (" \"" + label + "\"");
139             }
14030            if (vld != null)
141             {
14210                Point2D location = vld.getLocation(currentVertex);
14310                if (location != null)
14410                    writer.write (" " + location.getX() + " " + location.getY() + " 0.0");
145             }
14630            writer.newLine();
147         }
148  
1496        Set d_set = new HashSet();
1506        Set u_set = new HashSet();
151  
1526        boolean directed = PredicateUtils.enforcesDirected(graph);
1536        boolean undirected = PredicateUtils.enforcesUndirected(graph);
154         // if it's strictly one or the other, no need to create extra sets
1556        if (directed)
1561            d_set = graph.getEdges();
1576        if (undirected)
1581            u_set = graph.getEdges();
1596        if (!directed && !undirected) // mixed-mode graph
160         {
1614            d_set = PredicateUtils.getEdges(graph, Graph.DIRECTED_EDGE);
1624            u_set = PredicateUtils.getEdges(graph, Graph.UNDIRECTED_EDGE);
163         }
164  
165         // write out directed edges
1666        if (!d_set.isEmpty())
167         {
1684            writer.write("*Arcs");
1694            writer.newLine();
170         }
1716        for (Iterator eIt = d_set.iterator(); eIt.hasNext();)
172         {
17318            DirectedEdge e = (DirectedEdge) eIt.next();
17418            int source_id = id.getIndex(e.getSource()) + 1;
17518            int target_id = id.getIndex(e.getDest()) + 1;
17618            float weight = nev.getNumber(e).floatValue();
17718            writer.write(source_id + " " + target_id + " " + weight);
17818            writer.newLine();
179         }
180  
181         // write out undirected edges
1826        if (!u_set.isEmpty())
183         {
1844            writer.write("*Edges");
1854            writer.newLine();
186         }
1876        for (Iterator eIt = u_set.iterator(); eIt.hasNext();)
188         {
18918            UndirectedEdge e = (UndirectedEdge) eIt.next();
19018            Pair endpoints = e.getEndpoints();
19118            int v1_id = id.getIndex((Vertex) endpoints.getFirst()) + 1;
19218            int v2_id = id.getIndex((Vertex) endpoints.getSecond()) + 1;
19318            float weight = nev.getNumber(e).floatValue();
19418            writer.write(v1_id + " " + v2_id + " " + weight);
19518            writer.newLine();
196         }
1976        writer.close();
1986    }
199 }

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.