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

Line | Hits | Source
--- | --- | ---
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()`
52 | 3 | `{`
53 | 3 | `}`
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 |  | `{`
65 | 6 | `save(g, new FileWriter(filename), vs, nev, vld);`
66 | 6 | `}`
67 |  | ``
68 |  | `public void save(Graph g, String filename, VertexStringer vs,`
69 |  | `NumberEdgeValue nev) throws IOException`
70 |  | `{`
71 | 0 | `save(g, new FileWriter(filename), vs, nev, null);`
72 | 0 | `}`
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 |  | `{`
82 | 0 | `save(g, filename, null, null, null);`
83 | 0 | `}`
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 |  | `{`
93 | 0 | `save(g, w, null, null, null);`
94 | 0 | `}`
95 |  | ``
96 |  | `public void save(Graph g, Writer w, VertexStringer vs,`
97 |  | `NumberEdgeValue nev) throws IOException`
98 |  | `{`
99 | 0 | `save(g, w, vs, nev, null);`
100 | 0 | `}`
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 |  | ``
117 | 6 | `BufferedWriter writer = new BufferedWriter(w);`
118 | 6 | `if (nev == null)`
119 | 4 | `nev = new ConstantEdgeValue(1);`
120 | 6 | `writer.write("*Vertices " + graph.numVertices());`
121 | 6 | `writer.newLine();`
122 | 6 | `Vertex currentVertex = null;`
123 |  | ``
124 |  | `// if (vld != null)`
125 |  | `// vld = VertexLocationUtils.scale(vld, 1.0, 1.0);`
126 |  | ``
127 | 6 | `Indexer id = Indexer.getIndexer(graph);`
128 | 6 | `for (Iterator i = graph.getVertices().iterator(); i.hasNext();)`
129 |  | `{`
130 | 30 | `currentVertex = (Vertex) i.next();`
131 |  | `// convert from 0-based to 1-based index`
132 | 30 | `Integer v_id = new Integer(id.getIndex(currentVertex) + 1);`
133 | 30 | `writer.write(v_id.toString());`
134 | 30 | `if (vs != null)`
135 |  | `{`
136 | 30 | `String label = vs.getLabel(currentVertex);`
137 | 30 | `if (label != null)`
138 | 30 | `writer.write (" \"" + label + "\"");`
139 |  | `}`
140 | 30 | `if (vld != null)`
141 |  | `{`
142 | 10 | `Point2D location = vld.getLocation(currentVertex);`
143 | 10 | `if (location != null)`
144 | 10 | `writer.write (" " + location.getX() + " " + location.getY() + " 0.0");`
145 |  | `}`
146 | 30 | `writer.newLine();`
147 |  | `}`
148 |  | ``
149 | 6 | `Set d_set = new HashSet();`
150 | 6 | `Set u_set = new HashSet();`
151 |  | ``
152 | 6 | `boolean directed = PredicateUtils.enforcesDirected(graph);`
153 | 6 | `boolean undirected = PredicateUtils.enforcesUndirected(graph);`
154 |  | `// if it's strictly one or the other, no need to create extra sets`
155 | 6 | `if (directed)`
156 | 1 | `d_set = graph.getEdges();`
157 | 6 | `if (undirected)`
158 | 1 | `u_set = graph.getEdges();`
159 | 6 | `if (!directed && !undirected) // mixed-mode graph`
160 |  | `{`
161 | 4 | `d_set = PredicateUtils.getEdges(graph, Graph.DIRECTED_EDGE);`
162 | 4 | `u_set = PredicateUtils.getEdges(graph, Graph.UNDIRECTED_EDGE);`
163 |  | `}`
164 |  | ``
165 |  | `// write out directed edges`
166 | 6 | `if (!d_set.isEmpty())`
167 |  | `{`
168 | 4 | `writer.write("*Arcs");`
169 | 4 | `writer.newLine();`
170 |  | `}`
171 | 6 | `for (Iterator eIt = d_set.iterator(); eIt.hasNext();)`
172 |  | `{`
173 | 18 | `DirectedEdge e = (DirectedEdge) eIt.next();`
174 | 18 | `int source_id = id.getIndex(e.getSource()) + 1;`
175 | 18 | `int target_id = id.getIndex(e.getDest()) + 1;`
176 | 18 | `float weight = nev.getNumber(e).floatValue();`
177 | 18 | `writer.write(source_id + " " + target_id + " " + weight);`
178 | 18 | `writer.newLine();`
179 |  | `}`
180 |  | ``
181 |  | `// write out undirected edges`
182 | 6 | `if (!u_set.isEmpty())`
183 |  | `{`
184 | 4 | `writer.write("*Edges");`
185 | 4 | `writer.newLine();`
186 |  | `}`
187 | 6 | `for (Iterator eIt = u_set.iterator(); eIt.hasNext();)`
188 |  | `{`
189 | 18 | `UndirectedEdge e = (UndirectedEdge) eIt.next();`
190 | 18 | `Pair endpoints = e.getEndpoints();`
191 | 18 | `int v1_id = id.getIndex((Vertex) endpoints.getFirst()) + 1;`
192 | 18 | `int v2_id = id.getIndex((Vertex) endpoints.getSecond()) + 1;`
193 | 18 | `float weight = nev.getNumber(e).floatValue();`
194 | 18 | `writer.write(v1_id + " " + v2_id + " " + weight);`
195 | 18 | `writer.newLine();`
196 |  | `}`
197 | 6 | `writer.close();`
198 | 6 | `}`
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.