TestLoadGraphFromStream4() { // Nested graph, using sample XML from the GraphML Primer. // GraphMLGraphAdapter should, according to the Primer, "ignore nodes // which are not contained in the top-level graph and to ignore edges // which have do not have both endpoints in the top-level graph." const String XmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + " xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">" + " <graph id=\"G\" edgedefault=\"undirected\">" + " <node id=\"n0\"/>" + " <node id=\"n1\"/>" + " <node id=\"n2\"/>" + " <node id=\"n3\"/>" + " <node id=\"n4\"/>" + " <node id=\"n5\">" + " <graph id=\"n5:\" edgedefault=\"undirected\">" + " <node id=\"n5::n0\"/>" + " <node id=\"n5::n1\"/>" + " <node id=\"n5::n2\"/>" + " <edge id=\"e0\" source=\"n5::n0\" target=\"n5::n2\"/>" + " <edge id=\"e1\" source=\"n5::n1\" target=\"n5::n2\"/>" + " </graph>" + " </node>" + " <node id=\"n6\">" + " <graph id=\"n6:\" edgedefault=\"undirected\">" + " <node id=\"n6::n0\">" + " <graph id=\"n6::n0:\" edgedefault=\"undirected\">" + " <node id=\"n6::n0::n0\"/>" + " </graph>" + " </node>" + " <node id=\"n6::n1\"/>" + " <node id=\"n6::n2\"/>" + " <edge id=\"e10\" source=\"n6::n1\" target=\"n6::n0::n0\"/>" + " <edge id=\"e11\" source=\"n6::n1\" target=\"n6::n2\"/>" + " </graph>" + " </node>" + " <edge id=\"e2\" source=\"n5::n2\" target=\"n0\"/>" + " <edge id=\"e3\" source=\"n0\" target=\"n2\"/>" + " <edge id=\"e4\" source=\"n0\" target=\"n1\"/>" + " <edge id=\"e5\" source=\"n1\" target=\"n3\"/>" + " <edge id=\"e6\" source=\"n3\" target=\"n2\"/>" + " <edge id=\"e7\" source=\"n2\" target=\"n4\"/>" + " <edge id=\"e8\" source=\"n3\" target=\"n6::n1\"/>" + " <edge id=\"e9\" source=\"n6::n1\" target=\"n4\"/>" + " </graph>" + "</graphml>" ; Stream oXmlStream = new StringStream(XmlString); IGraph oGraph = m_oGraphAdapter.LoadGraphFromStream(oXmlStream); Assert.AreEqual(GraphDirectedness.Undirected, oGraph.Directedness); IVertexCollection oVertices = oGraph.Vertices; IEdgeCollection oEdges = oGraph.Edges; Boolean bFound; IVertex oVertex; Assert.AreEqual(7, oVertices.Count); bFound = oVertices.Find("n0", out oVertex); Assert.IsTrue(bFound); bFound = oVertices.Find("n1", out oVertex); Assert.IsTrue(bFound); bFound = oVertices.Find("n2", out oVertex); Assert.IsTrue(bFound); bFound = oVertices.Find("n3", out oVertex); Assert.IsTrue(bFound); bFound = oVertices.Find("n4", out oVertex); Assert.IsTrue(bFound); bFound = oVertices.Find("n5", out oVertex); Assert.IsTrue(bFound); bFound = oVertices.Find("n6", out oVertex); Assert.IsTrue(bFound); IEdge oEdge; bFound = oEdges.Find("e3", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n0", oEdge.Vertices[0].Name); Assert.AreEqual("n2", oEdge.Vertices[1].Name); bFound = oEdges.Find("e4", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n0", oEdge.Vertices[0].Name); Assert.AreEqual("n1", oEdge.Vertices[1].Name); bFound = oEdges.Find("e5", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n1", oEdge.Vertices[0].Name); Assert.AreEqual("n3", oEdge.Vertices[1].Name); bFound = oEdges.Find("e6", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n3", oEdge.Vertices[0].Name); Assert.AreEqual("n2", oEdge.Vertices[1].Name); Assert.IsFalse(oEdge.ContainsKey("weight")); bFound = oEdges.Find("e7", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n2", oEdge.Vertices[0].Name); Assert.AreEqual("n4", oEdge.Vertices[1].Name); }
TestLoadGraphFromStream2() { // Overall test, using sample XML from the GraphML Primer. const String XmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns " + "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">" + "<key id=\"d0\" for=\"node\" attr.name=\"color\" attr.type=\"string\">" + "<default>yellow</default>" + "</key>" + "<key id=\"d1\" for=\"edge\" attr.name=\"weight\" attr.type=\"double\"/>" + "<graph id=\"G\" edgedefault=\"undirected\">" + "<node id=\"n0\">" + "<data key=\"d0\">green</data>" + "</node>" + "<node id=\"n1\"/>" + "<node id=\"n2\">" + "<data key=\"d0\">blue</data>" + "</node>" + "<node id=\"n3\">" + "<data key=\"d0\">red</data>" + "</node>" + "<node id=\"n4\"/>" + "<node id=\"n5\">" + "<data key=\"d0\">turquoise</data>" + "</node>" + "<edge id=\"e0\" source=\"n0\" target=\"n2\">" + "<data key=\"d1\">1.0</data>" + "</edge>" + "<edge id=\"e1\" source=\"n0\" target=\"n1\">" + "<data key=\"d1\">1.0</data>" + "</edge>" + "<edge id=\"e2\" source=\"n1\" target=\"n3\">" + "<data key=\"d1\">2.0</data>" + "</edge>" + "<edge id=\"e3\" source=\"n3\" target=\"n2\"/>" + "<edge id=\"e4\" source=\"n2\" target=\"n4\"/>" + "<edge id=\"e5\" source=\"n3\" target=\"n5\"/>" + "<edge id=\"e6\" source=\"n5\" target=\"n4\">" + "<data key=\"d1\">1.1</data>" + "</edge>" + "</graph>" + "</graphml>" ; Stream oXmlStream = new StringStream(XmlString); IGraph oGraph = m_oGraphAdapter.LoadGraphFromStream(oXmlStream); Assert.AreEqual(GraphDirectedness.Undirected, oGraph.Directedness); IVertexCollection oVertices = oGraph.Vertices; IEdgeCollection oEdges = oGraph.Edges; Boolean bFound; IVertex oVertex; String sColor; Assert.AreEqual(6, oVertices.Count); bFound = oVertices.Find("n0", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("color", typeof(String)); Assert.AreEqual("green", sColor); bFound = oVertices.Find("n1", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("color", typeof(String)); Assert.AreEqual("yellow", sColor); bFound = oVertices.Find("n2", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("color", typeof(String)); Assert.AreEqual("blue", sColor); bFound = oVertices.Find("n3", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("color", typeof(String)); Assert.AreEqual("red", sColor); bFound = oVertices.Find("n4", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("color", typeof(String)); Assert.AreEqual("yellow", sColor); bFound = oVertices.Find("n5", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("color", typeof(String)); Assert.AreEqual("turquoise", sColor); IEdge oEdge; Double dWeight; bFound = oEdges.Find("e0", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n0", oEdge.Vertices[0].Name); Assert.AreEqual("n2", oEdge.Vertices[1].Name); dWeight = (Double)oEdge.GetRequiredValue("weight", typeof(Double)); Assert.AreEqual(1.0, dWeight); bFound = oEdges.Find("e1", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n0", oEdge.Vertices[0].Name); Assert.AreEqual("n1", oEdge.Vertices[1].Name); dWeight = (Double)oEdge.GetRequiredValue("weight", typeof(Double)); Assert.AreEqual(1.0, dWeight); bFound = oEdges.Find("e2", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n1", oEdge.Vertices[0].Name); Assert.AreEqual("n3", oEdge.Vertices[1].Name); dWeight = (Double)oEdge.GetRequiredValue("weight", typeof(Double)); Assert.AreEqual(2.0, dWeight); bFound = oEdges.Find("e3", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n3", oEdge.Vertices[0].Name); Assert.AreEqual("n2", oEdge.Vertices[1].Name); Assert.IsFalse(oEdge.ContainsKey("weight")); bFound = oEdges.Find("e4", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n2", oEdge.Vertices[0].Name); Assert.AreEqual("n4", oEdge.Vertices[1].Name); Assert.IsFalse(oEdge.ContainsKey("weight")); bFound = oEdges.Find("e5", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n3", oEdge.Vertices[0].Name); Assert.AreEqual("n5", oEdge.Vertices[1].Name); Assert.IsFalse(oEdge.ContainsKey("weight")); bFound = oEdges.Find("e6", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n5", oEdge.Vertices[0].Name); Assert.AreEqual("n4", oEdge.Vertices[1].Name); dWeight = (Double)oEdge.GetRequiredValue("weight", typeof(Double)); Assert.AreEqual(1.1, dWeight); }
TestLoadGraphFromStream3() { // Overall test, using sample XML from the GraphML Primer, but with // missing attr.name and attr.type attributes, which are optional. // // Also, include key nodes with "for" attributes not used by NodeXL. // The for="graphml" is actually illegal, but was found in a GraphML // file created by a program called yED. // // Also, include a data node with no inner text, which was also in the // yED file. const String XmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns " + "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">" + "<key id=\"d0\" for=\"node\">" + "<default>yellow</default>" + "</key>" + "<key id=\"d1\" for=\"edge\"/>" + "<key id=\"dSkip1\" for=\"graph\"/>" + "<key id=\"dSkip2\" for=\"all\"/>" + "<key id=\"dIllegal\" for=\"graphml\"/>" + "<graph id=\"G\" edgedefault=\"undirected\">" + "<node id=\"n0\">" + "<data key=\"d0\">green</data>" + "</node>" + "<node id=\"n1\"/>" + "<node id=\"n2\">" + "<data key=\"d0\">blue</data>" + "</node>" + "<node id=\"n3\">" + "<data key=\"d0\">red</data>" + "</node>" + "<node id=\"n4\">" + "<data key=\"d0\" />" + "</node>" + "<node id=\"n5\">" + "<data key=\"d0\">turquoise</data>" + "</node>" + "<edge id=\"e0\" source=\"n0\" target=\"n2\">" + "<data key=\"d1\">1.0</data>" + "</edge>" + "<edge id=\"e1\" source=\"n0\" target=\"n1\">" + "<data key=\"d1\">1.0</data>" + "</edge>" + "<edge id=\"e2\" source=\"n1\" target=\"n3\">" + "<data key=\"d1\">2.0</data>" + "</edge>" + "<edge id=\"e3\" source=\"n3\" target=\"n2\"/>" + "<edge id=\"e4\" source=\"n2\" target=\"n4\"/>" + "<edge id=\"e5\" source=\"n3\" target=\"n5\"/>" + "<edge id=\"e6\" source=\"n5\" target=\"n4\">" + "<data key=\"d1\">1.1</data>" + "</edge>" + "</graph>" + "</graphml>" ; Stream oXmlStream = new StringStream(XmlString); IGraph oGraph = m_oGraphAdapter.LoadGraphFromStream(oXmlStream); Assert.AreEqual(GraphDirectedness.Undirected, oGraph.Directedness); IVertexCollection oVertices = oGraph.Vertices; IEdgeCollection oEdges = oGraph.Edges; Boolean bFound; IVertex oVertex; String sColor; Assert.AreEqual(6, oVertices.Count); bFound = oVertices.Find("n0", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("d0", typeof(String)); Assert.AreEqual("green", sColor); bFound = oVertices.Find("n1", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("d0", typeof(String)); Assert.AreEqual("yellow", sColor); bFound = oVertices.Find("n2", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("d0", typeof(String)); Assert.AreEqual("blue", sColor); bFound = oVertices.Find("n3", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("d0", typeof(String)); Assert.AreEqual("red", sColor); bFound = oVertices.Find("n4", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("d0", typeof(String)); Assert.AreEqual(String.Empty, sColor); bFound = oVertices.Find("n5", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("d0", typeof(String)); Assert.AreEqual("turquoise", sColor); IEdge oEdge; String sWeight; bFound = oEdges.Find("e0", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n0", oEdge.Vertices[0].Name); Assert.AreEqual("n2", oEdge.Vertices[1].Name); sWeight = (String)oEdge.GetRequiredValue("d1", typeof(String)); Assert.AreEqual("1.0", sWeight); bFound = oEdges.Find("e1", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n0", oEdge.Vertices[0].Name); Assert.AreEqual("n1", oEdge.Vertices[1].Name); sWeight = (String)oEdge.GetRequiredValue("d1", typeof(String)); Assert.AreEqual("1.0", sWeight); bFound = oEdges.Find("e2", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n1", oEdge.Vertices[0].Name); Assert.AreEqual("n3", oEdge.Vertices[1].Name); sWeight = (String)oEdge.GetRequiredValue("d1", typeof(String)); Assert.AreEqual("2.0", sWeight); bFound = oEdges.Find("e3", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n3", oEdge.Vertices[0].Name); Assert.AreEqual("n2", oEdge.Vertices[1].Name); Assert.IsFalse(oEdge.ContainsKey("d1")); bFound = oEdges.Find("e4", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n2", oEdge.Vertices[0].Name); Assert.AreEqual("n4", oEdge.Vertices[1].Name); Assert.IsFalse(oEdge.ContainsKey("d1")); bFound = oEdges.Find("e5", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n3", oEdge.Vertices[0].Name); Assert.AreEqual("n5", oEdge.Vertices[1].Name); Assert.IsFalse(oEdge.ContainsKey("d1")); bFound = oEdges.Find("e6", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("n5", oEdge.Vertices[0].Name); Assert.AreEqual("n4", oEdge.Vertices[1].Name); sWeight = (String)oEdge.GetRequiredValue("d1", typeof(String)); Assert.AreEqual("1.1", sWeight); }
TestLoadGraphFromStreamOrFile ( Boolean bFromStream ) { const String XmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + " <graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">" + " " + " <key id=\"VertexColor\" for=\"node\" attr.name=\"Color\" attr.type=\"string\" />" + " <key id=\"VertexLatestPostDate\" for=\"node\" attr.name=\"Latest Post Date\"" + " attr.type=\"string\" />" + " <key id=\"EdgeWidth\" for=\"edge\" attr.name=\"Width\" attr.type=\"double\">" + " <default>1.5</default>" + " </key>" + " <graph edgedefault=\"undirected\">" + " <node id=\"V1\">" + " <data key=\"VertexColor\">red</data>" + " <data key=\"VertexLatestPostDate\">2009/07/05</data>" + " </node>" + " <node id=\"V2\">" + " <data key=\"VertexColor\">orange</data>" + " <data key=\"VertexLatestPostDate\">2009/07/12</data>" + " </node>" + " <node id=\"V3\">" + " <data key=\"VertexColor\">blue</data>" + " </node>" + " <node id=\"V4\">" + " <data key=\"VertexColor\">128,0,128</data>" + " </node>" + " <node id=\"V5\" />" + " <edge id=\"E1\" source=\"V1\" target=\"V2\" />" + " <edge id=\"E2\" source=\"V3\" target=\"V2\">" + " <data key=\"EdgeWidth\">2.5</data>" + " </edge>" + " </graph>" + " </graphml>" ; IGraph oGraph = null; if (bFromStream) { Stream oXmlStream = new StringStream(XmlString); oGraph = m_oGraphAdapter.LoadGraphFromStream(oXmlStream); } else { using (StreamWriter oStreamWriter = new StreamWriter(m_sTempFileName)) { oStreamWriter.Write(XmlString); } oGraph = m_oGraphAdapter.LoadGraphFromFile(m_sTempFileName); } Assert.AreEqual(GraphDirectedness.Undirected, oGraph.Directedness); IVertexCollection oVertices = oGraph.Vertices; IEdgeCollection oEdges = oGraph.Edges; Boolean bFound; IVertex oVertex; String sColor, sLatestPostDate; Assert.AreEqual(5, oVertices.Count); bFound = oVertices.Find("V1", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("Color", typeof(String)); Assert.AreEqual("red", sColor); sLatestPostDate = (String)oVertex.GetRequiredValue( "Latest Post Date", typeof(String)); Assert.AreEqual("2009/07/05", sLatestPostDate); bFound = oVertices.Find("V2", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("Color", typeof(String)); Assert.AreEqual("orange", sColor); sLatestPostDate = (String)oVertex.GetRequiredValue( "Latest Post Date", typeof(String)); Assert.AreEqual("2009/07/12", sLatestPostDate); bFound = oVertices.Find("V3", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("Color", typeof(String)); Assert.AreEqual("blue", sColor); bFound = oVertices.Find("V4", out oVertex); Assert.IsTrue(bFound); sColor = (String)oVertex.GetRequiredValue("Color", typeof(String)); Assert.AreEqual("128,0,128", sColor); bFound = oVertices.Find("V5", out oVertex); Assert.IsTrue(bFound); IEdge oEdge; Double dWidth; bFound = oEdges.Find("E1", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("V1", oEdge.Vertices[0].Name); Assert.AreEqual("V2", oEdge.Vertices[1].Name); dWidth = (Double)oEdge.GetRequiredValue("Width", typeof(Double)); Assert.AreEqual(1.5, dWidth); bFound = oEdges.Find("E2", out oEdge); Assert.IsTrue(bFound); Assert.AreEqual("V3", oEdge.Vertices[0].Name); Assert.AreEqual("V2", oEdge.Vertices[1].Name); dWidth = (Double)oEdge.GetRequiredValue("Width", typeof(Double)); Assert.AreEqual(2.5, dWidth); String [] asGraphMLVertexAttributes = ( String[] ) oGraph.GetRequiredValue(ReservedMetadataKeys.AllVertexMetadataKeys, typeof(String[])); Assert.AreEqual(2, asGraphMLVertexAttributes.Length); Assert.IsTrue(Array.IndexOf <String>(asGraphMLVertexAttributes, "Color") >= 0); Assert.IsTrue(Array.IndexOf <String>(asGraphMLVertexAttributes, "Latest Post Date") >= 0); String [] asGraphMLEdgeAttributes = ( String[] ) oGraph.GetRequiredValue(ReservedMetadataKeys.AllEdgeMetadataKeys, typeof(String[])); Assert.AreEqual(1, asGraphMLEdgeAttributes.Length); Assert.IsTrue(Array.IndexOf <String>(asGraphMLEdgeAttributes, "Width") >= 0); }