View Javadoc
1   /*
2    * Created on Jun 22, 2008
3    *
4    * Copyright (c) 2008, The JUNG Authors 
5    *
6    * All rights reserved.
7    *
8    * This software is open-source under the BSD license; see either
9    * "license.txt" or
10   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
11   */
12  package edu.uci.ics.jung.io;
13  
14  import java.io.File;
15  import java.io.FileWriter;
16  import java.io.IOException;
17  import java.util.ArrayList;
18  import java.util.Collections;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import javax.xml.parsers.ParserConfigurationException;
25  
26  import junit.framework.Assert;
27  import junit.framework.TestCase;
28  
29  import org.xml.sax.SAXException;
30  
31  import com.google.common.base.Function;
32  import com.google.common.base.Functions;
33  
34  import edu.uci.ics.jung.graph.DirectedSparseGraph;
35  import edu.uci.ics.jung.graph.Graph;
36  import edu.uci.ics.jung.graph.SparseMultigraph;
37  import edu.uci.ics.jung.graph.util.TestGraphs;
38  
39  public class TestGraphMLWriter extends TestCase
40  {
41      public void testBasicWrite() throws IOException, ParserConfigurationException, SAXException
42      {
43          Graph<String, Number> g = TestGraphs.createTestGraph(true);
44          GraphMLWriter<String, Number> gmlw = new GraphMLWriter<String, Number>();
45          Function<Number, String> edge_weight = new Function<Number, String>() 
46  		{ 
47  			public String apply(Number n) 
48  			{ 
49  				return String.valueOf(n.intValue()); 
50  			} 
51  		};
52  
53  		Function<String, String> vertex_name = Functions.identity();
54  			//TransformerUtils.nopTransformer();
55  		
56          gmlw.addEdgeData("weight", "integer value for the edge", 
57          		Integer.toString(-1), edge_weight);
58          gmlw.addVertexData("name", "identifier for the vertex", null, vertex_name);
59          gmlw.setEdgeIDs(edge_weight);
60          gmlw.setVertexIDs(vertex_name);
61          gmlw.save(g, new FileWriter("src/test/resources/testbasicwrite.graphml"));
62          
63          // TODO: now read it back in and compare the graph connectivity 
64          // and other metadata with what's in TestGraphs.pairs[], etc.
65  //        Factory<String> vertex_factory = null;
66  //        Factory<Object> edge_factory = FactoryUtils.instantiateFactory(Object.class);
67  //        GraphMLReader<Graph<String, Object>, String, Object> gmlr = 
68  //        	new GraphMLReader<Graph<String, Object>, String, Object>(
69  //        			vertex_factory, edge_factory);
70          GraphMLReader<Graph<String, Object>, String, Object> gmlr = 
71              new GraphMLReader<Graph<String, Object>, String, Object>();
72          Graph<String, Object> g2 = new DirectedSparseGraph<String, Object>();
73          gmlr.load("src/test/resources/testbasicwrite.graphml", g2);
74          Map<String, GraphMLMetadata<Object>> edge_metadata = 
75          	gmlr.getEdgeMetadata();
76          Function<Object, String> edge_weight2 = 
77          	edge_metadata.get("weight").transformer;
78          validateTopology(g, g2, edge_weight, edge_weight2);
79          
80          // TODO: delete graph file when done
81          File f = new File("src/test/resources/testbasicwrite.graphml");
82          f.delete();
83      }
84      
85      public void testMixedGraph() throws IOException, ParserConfigurationException, SAXException
86      {
87          Graph<String, Number> g = TestGraphs.getSmallGraph();
88          GraphMLWriter<String, Number> gmlw = new GraphMLWriter<String, Number>();
89          Function<Number, String> edge_weight = new Function<Number, String>() 
90          { 
91              public String apply(Number n) 
92              { 
93                  return String.valueOf(n.doubleValue()); 
94              } 
95          };
96  
97          gmlw.addEdgeData("weight", "integer value for the edge", 
98                  Integer.toString(-1), edge_weight);
99          gmlw.setEdgeIDs(edge_weight);
100         gmlw.save(g, new FileWriter("src/test/resources/testmixedgraph.graphml"));
101 
102         // TODO: now read it back in and compare the graph connectivity 
103         // and other metadata with what's in TestGraphs, etc.
104         GraphMLReader<Graph<String,Object>,String,Object> gmlr = 
105             new GraphMLReader<Graph<String,Object>,String,Object>();
106         Graph<String,Object> g2 = new SparseMultigraph<String,Object>();
107         gmlr.load("src/test/resources/testmixedgraph.graphml", g2);
108         Map<String, GraphMLMetadata<Object>> edge_metadata = 
109             gmlr.getEdgeMetadata();
110         Function<Object, String> edge_weight2 = 
111             edge_metadata.get("weight").transformer;
112         validateTopology(g, g2, edge_weight, edge_weight2);
113         
114         // TODO: delete graph file when done
115         File f = new File("src/test/resources/testmixedgraph.graphml");
116         f.delete();
117     }
118 
119     public <T extends Comparable<T>> void validateTopology(Graph<T,Number> g, Graph<T,Object> g2,
120             Function<Number,String> edge_weight, Function<Object,String> edge_weight2)
121     {
122         Assert.assertEquals(g2.getEdgeCount(), g.getEdgeCount());
123         List<T> g_vertices = new ArrayList<T>(g.getVertices());
124         List<T> g2_vertices = new ArrayList<T>(g2.getVertices());
125         Collections.sort(g_vertices); 
126         Collections.sort(g2_vertices);
127         Assert.assertEquals(g_vertices, g2_vertices);
128 
129         Set<String> g_edges = new HashSet<String>();
130         for (Number n : g.getEdges())
131             g_edges.add(String.valueOf(n));
132         Set<Object> g2_edges = new HashSet<Object>(g2.getEdges());
133         Assert.assertEquals(g_edges, g2_edges);
134         
135         for (T v : g2.getVertices())
136         {
137             for (T w : g2.getVertices())
138             {
139                 Assert.assertEquals(g.isNeighbor(v, w), 
140                         g2.isNeighbor(v, w));
141                 Set<String> e = new HashSet<String>();
142                 for (Number n : g.findEdgeSet(v, w))
143                     e.add(String.valueOf(n));
144                 Set<Object> e2 = new HashSet<Object>(g2.findEdgeSet(v, w));
145                 Assert.assertEquals(e.size(), e2.size());
146                 Assert.assertEquals(e, e2);
147             }
148         }
149         
150         for (Object o : g2.getEdges())
151         {
152             String weight = edge_weight.apply(new Double((String)o));
153             String weight2 = edge_weight2.apply(o);
154             Assert.assertEquals(weight2, weight);
155         }        
156 //                Number n = g.findEdge(v, w);
157 //                Object o = g2.findEdge(v, w);
158 //                if (n != null)
159 //                {
160 //                    String weight = edge_weight.apply(n);
161 //                    String weight2 = edge_weight2.apply(o);
162 //                    Assert.assertEquals(weight2, weight);
163 //                }
164 //            }
165 //        }
166         
167     }
168     
169 }