1
2
3
4
5
6
7
8
9
10 package edu.uci.ics.jung.io;
11
12 import java.io.BufferedWriter;
13 import java.io.File;
14 import java.io.FileWriter;
15 import java.io.IOException;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19
20 import javax.xml.parsers.ParserConfigurationException;
21
22 import junit.framework.Assert;
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 import org.xml.sax.SAXException;
28
29 import com.google.common.base.Function;
30 import com.google.common.base.Supplier;
31 import com.google.common.collect.BiMap;
32
33 import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
34 import edu.uci.ics.jung.graph.Graph;
35 import edu.uci.ics.jung.graph.Hypergraph;
36 import edu.uci.ics.jung.graph.SetHypergraph;
37 import edu.uci.ics.jung.graph.UndirectedSparseGraph;
38
39
40
41
42
43 public class TestGraphMLReader extends TestCase
44 {
45
46 Supplier<Graph<Number, Number>> graphFactory;
47 Supplier<Number> vertexFactory;
48 Supplier<Number> edgeFactory;
49 GraphMLReader<Graph<Number, Number>, Number, Number> gmlreader;
50
51 public static Test suite()
52 {
53 return new TestSuite(TestGraphMLReader.class);
54 }
55
56 @Override
57 protected void setUp() throws ParserConfigurationException, SAXException
58 {
59 graphFactory = new Supplier<Graph<Number, Number>>()
60 {
61 public Graph<Number, Number> get()
62 {
63 return new DirectedSparseMultigraph<Number, Number>();
64 }
65 };
66 vertexFactory = new Supplier<Number>()
67 {
68 int n = 0;
69
70 public Number get()
71 {
72 return n++;
73 }
74 };
75 edgeFactory = new Supplier<Number>()
76 {
77 int n = 0;
78
79 public Number get()
80 {
81 return n++;
82 }
83 };
84 gmlreader = new GraphMLReader<Graph<Number, Number>, Number, Number>(
85 vertexFactory, edgeFactory);
86 }
87
88 public void testLoad() throws IOException
89 {
90 String testFilename = "toy_graph.ml";
91
92 Graph<Number, Number> graph = loadGraph(testFilename);
93
94 Assert.assertEquals(graph.getVertexCount(), 3);
95 Assert.assertEquals(graph.getEdgeCount(), 3);
96
97 BiMap<Number, String> vertex_ids = gmlreader.getVertexIDs();
98
99 Number joe = vertex_ids.inverse().get("1");
100 Number bob = vertex_ids.inverse().get("2");
101 Number sue = vertex_ids.inverse().get("3");
102
103 Assert.assertNotNull(joe);
104 Assert.assertNotNull(bob);
105 Assert.assertNotNull(sue);
106
107 Map<String, GraphMLMetadata<Number>> vertex_metadata =
108 gmlreader.getVertexMetadata();
109 Function<Number, String> name =
110 vertex_metadata.get("name").transformer;
111 Assert.assertEquals(name.apply(joe), "Joe");
112 Assert.assertEquals(name.apply(bob), "Bob");
113 Assert.assertEquals(name.apply(sue), "Sue");
114
115 Assert.assertTrue(graph.isPredecessor(joe, bob));
116 Assert.assertTrue(graph.isPredecessor(bob, joe));
117 Assert.assertTrue(graph.isPredecessor(sue, joe));
118 Assert.assertFalse(graph.isPredecessor(joe, sue));
119 Assert.assertFalse(graph.isPredecessor(sue, bob));
120 Assert.assertFalse(graph.isPredecessor(bob, sue));
121
122 File testFile = new File(testFilename);
123 testFile.delete();
124 }
125
126 public void testAttributes() throws IOException
127 {
128 Graph<Number, Number> graph = new UndirectedSparseGraph<Number, Number>();
129 gmlreader.load("src/test/resources/edu/uci/ics/jung/io/graphml/attributes.graphml", graph);
130
131 Assert.assertEquals(graph.getVertexCount(), 6);
132 Assert.assertEquals(graph.getEdgeCount(), 7);
133
134
135 BiMap<Number, String> vertex_ids = gmlreader.getVertexIDs();
136 for (Map.Entry<Number, String> entry : vertex_ids.entrySet())
137 {
138 Assert.assertEquals(entry.getValue().charAt(0), 'n');
139 Assert.assertEquals(
140 Integer.parseInt(entry.getValue().substring(1)), entry
141 .getKey().intValue());
142 }
143
144
145 BiMap<Number, String> edge_ids = gmlreader.getEdgeIDs();
146 for (Map.Entry<Number, String> entry : edge_ids.entrySet())
147 {
148 Assert.assertEquals(entry.getValue().charAt(0), 'e');
149 Assert.assertEquals(
150 Integer.parseInt(entry.getValue().substring(1)), entry
151 .getKey().intValue());
152 }
153
154
155
156
157
158
159 Map<String, GraphMLMetadata<Number>> vertex_metadata =
160 gmlreader.getVertexMetadata();
161 Map<String, GraphMLMetadata<Number>> edge_metadata =
162 gmlreader.getEdgeMetadata();
163
164
165
166
167 Function<Number, String> vertex_color =
168 vertex_metadata.get("d0").transformer;
169 Assert.assertEquals(vertex_color.apply(0), "green");
170 Assert.assertEquals(vertex_color.apply(1), "yellow");
171 Assert.assertEquals(vertex_color.apply(2), "blue");
172 Assert.assertEquals(vertex_color.apply(3), "red");
173 Assert.assertEquals(vertex_color.apply(4), "yellow");
174 Assert.assertEquals(vertex_color.apply(5), "turquoise");
175
176
177
178 Function<Number, String> edge_weight =
179 edge_metadata.get("d1").transformer;
180 Assert.assertEquals(edge_weight.apply(0), "1.0");
181 Assert.assertEquals(edge_weight.apply(1), "1.0");
182 Assert.assertEquals(edge_weight.apply(2), "2.0");
183 Assert.assertEquals(edge_weight.apply(3), null);
184 Assert.assertEquals(edge_weight.apply(4), null);
185 Assert.assertEquals(edge_weight.apply(5), null);
186 Assert.assertEquals(edge_weight.apply(6), "1.1");
187
188 }
189
190 public void testLoadHypergraph() throws IOException,
191 ParserConfigurationException, SAXException
192 {
193 Hypergraph<Number, Number> graph = new SetHypergraph<Number, Number>();
194 GraphMLReader<Hypergraph<Number, Number>, Number, Number> hyperreader =
195 new GraphMLReader<Hypergraph<Number, Number>, Number, Number>(
196 vertexFactory, edgeFactory);
197 hyperreader.load("src/test/resources/edu/uci/ics/jung/io/graphml/hyper.graphml", graph);
198
199 Assert.assertEquals(graph.getVertexCount(), 7);
200 Assert.assertEquals(graph.getEdgeCount(), 4);
201
202
203 Set<Number> incident = new HashSet<Number>();
204 incident.add(0);
205 incident.add(3);
206 Assert.assertEquals(graph.getIncidentEdges(0), incident);
207
208
209 incident.clear();
210 incident.add(0);
211 incident.add(2);
212 Assert.assertEquals(graph.getIncidentEdges(1), incident);
213
214
215 incident.clear();
216 incident.add(0);
217 Assert.assertEquals(graph.getIncidentEdges(2), incident);
218
219
220 incident.clear();
221 incident.add(1);
222 incident.add(2);
223 Assert.assertEquals(graph.getIncidentEdges(3), incident);
224
225
226 incident.clear();
227 incident.add(1);
228 incident.add(3);
229 Assert.assertEquals(graph.getIncidentEdges(4), incident);
230
231
232 incident.clear();
233 incident.add(1);
234 Assert.assertEquals(graph.getIncidentEdges(5), incident);
235
236
237 incident.clear();
238 incident.add(1);
239 Assert.assertEquals(graph.getIncidentEdges(6), incident);
240 }
241
242 private Graph<Number, Number> loadGraph(String testFilename)
243 throws IOException
244 {
245 BufferedWriter writer = new BufferedWriter(new FileWriter(
246 testFilename));
247 writer.write("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n");
248 writer.write("<?meta name=\"GENERATOR\" content=\"XML::Smart 1.3.1\" ?>\n");
249 writer.write("<graph edgedefault=\"directed\">\n");
250 writer.write("<node id=\"1\" name=\"Joe\"/>\n");
251 writer.write("<node id=\"2\" name=\"Bob\"/>\n");
252 writer.write("<node id=\"3\" name=\"Sue\"/>\n");
253 writer.write("<edge source=\"1\" target=\"2\"/>\n");
254 writer.write("<edge source=\"2\" target=\"1\"/>\n");
255 writer.write("<edge source=\"1\" target=\"3\"/>\n");
256 writer.write("</graph>\n");
257 writer.close();
258
259 Graph<Number, Number> graph = graphFactory.get();
260 gmlreader.load(testFilename, graph);
261 return graph;
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 }