AddVertices ( IGraph oGraph, Int32 iVerticesToAdd ) { Debug.Assert(oGraph != null); Debug.Assert(iVerticesToAdd >= 0); IVertex[] aoVertices = new IVertex[iVerticesToAdd]; IVertexCollection oVertexCollection = oGraph.Vertices; // Add the vertices. for (Int32 i = 0; i < iVerticesToAdd; i++) { IVertex oVertex = aoVertices[i] = new Vertex(); oVertex.Name = oVertex.ID.ToString(); oVertexCollection.Add(oVertex); } return(aoVertices); }
TestGetVertexIDPair6() { // Undirected graph, useDirectedness = false. IGraph oUndirectedGraph = new Graph(GraphDirectedness.Undirected); IVertexCollection oUndirectedVertices = oUndirectedGraph.Vertices; IVertex oUndirectedVertexA = oUndirectedVertices.Add(); IVertex oUndirectedVertexB = oUndirectedVertices.Add(); IVertex oUndirectedVertexC = oUndirectedVertices.Add(); IVertex oUndirectedVertexD = oUndirectedVertices.Add(); IEdgeCollection oEdges = oUndirectedGraph.Edges; IEdge oEdge1 = oEdges.Add(oUndirectedVertexA, oUndirectedVertexB, false); IEdge oEdge2 = oEdges.Add(oUndirectedVertexA, oUndirectedVertexB, false); IEdge oEdge3 = oEdges.Add(oUndirectedVertexB, oUndirectedVertexA, false); IEdge oEdge4 = oEdges.Add(oUndirectedVertexA, oUndirectedVertexC, false); Int64 i64VertexIDPair1 = EdgeUtil.GetVertexIDPair(oEdge1, false); Int64 i64VertexIDPair2 = EdgeUtil.GetVertexIDPair(oEdge2, false); Int64 i64VertexIDPair3 = EdgeUtil.GetVertexIDPair(oEdge3, false); Int64 i64VertexIDPair4 = EdgeUtil.GetVertexIDPair(oEdge4, false); // Make sure that the left-shift worked. Assert.IsTrue(i64VertexIDPair1 > Int32.MaxValue); Assert.IsTrue(i64VertexIDPair2 > Int32.MaxValue); Assert.IsTrue(i64VertexIDPair3 > Int32.MaxValue); Assert.IsTrue(i64VertexIDPair4 > Int32.MaxValue); Assert.AreEqual(i64VertexIDPair1, i64VertexIDPair2); Assert.AreEqual(i64VertexIDPair1, i64VertexIDPair3); Assert.AreNotEqual(i64VertexIDPair1, i64VertexIDPair4); Assert.AreEqual(i64VertexIDPair2, i64VertexIDPair3); Assert.AreNotEqual(i64VertexIDPair2, i64VertexIDPair4); Assert.AreNotEqual(i64VertexIDPair3, i64VertexIDPair4); }
CreateGraph ( IVertex [] aoVertices, List <PajekEdgeData> oUndirectedEdgeData, List <PajekEdgeData> oDirectedEdgeData ) { Debug.Assert(oUndirectedEdgeData != null); Debug.Assert(oDirectedEdgeData != null); GraphDirectedness eDirectedness = GraphDirectedness.Undirected; Int32 iVertices = 0; Int32 iUndirectedEdges = oUndirectedEdgeData.Count; Int32 iDirectedEdges = oDirectedEdgeData.Count; if (iUndirectedEdges > 0 && iDirectedEdges > 0) { eDirectedness = GraphDirectedness.Mixed; } else if (iDirectedEdges > 0) { eDirectedness = GraphDirectedness.Directed; } IGraph oGraph = new Graph(eDirectedness); IVertexCollection oVertices = oGraph.Vertices; IEdgeCollection oEdges = oGraph.Edges; if (aoVertices != null) { // Populate the vertex collection. foreach (IVertex oVertex in aoVertices) { oVertices.Add(oVertex); } iVertices = aoVertices.Length; } // Populate the edges collection. foreach (PajekEdgeData oEdgeData in oUndirectedEdgeData) { AddEdgeToGraph(oEdgeData, oEdges, aoVertices, false); } foreach (PajekEdgeData oEdgeData in oDirectedEdgeData) { AddEdgeToGraph(oEdgeData, oEdges, aoVertices, true); } return(oGraph); }
TestGetVertexIDPair() { // Directed graph. IGraph oDirectedGraph = new Graph(GraphDirectedness.Directed); IVertexCollection oDirectedVertices = oDirectedGraph.Vertices; IVertex oDirectedVertexA = oDirectedVertices.Add(); IVertex oDirectedVertexB = oDirectedVertices.Add(); IVertex oDirectedVertexC = oDirectedVertices.Add(); IVertex oDirectedVertexD = oDirectedVertices.Add(); IEdgeCollection oEdges = oDirectedGraph.Edges; IEdge oEdge1 = oEdges.Add(oDirectedVertexA, oDirectedVertexB, true); IEdge oEdge2 = oEdges.Add(oDirectedVertexA, oDirectedVertexB, true); IEdge oEdge3 = oEdges.Add(oDirectedVertexB, oDirectedVertexA, true); IEdge oEdge4 = oEdges.Add(oDirectedVertexA, oDirectedVertexC, true); Int64 i64VertexIDPair1 = EdgeUtil.GetVertexIDPair(oEdge1); Int64 i64VertexIDPair2 = EdgeUtil.GetVertexIDPair(oEdge2); Int64 i64VertexIDPair3 = EdgeUtil.GetVertexIDPair(oEdge3); Int64 i64VertexIDPair4 = EdgeUtil.GetVertexIDPair(oEdge4); // Make sure that the left-shift worked. Assert.IsTrue(i64VertexIDPair1 > Int32.MaxValue); Assert.IsTrue(i64VertexIDPair2 > Int32.MaxValue); Assert.IsTrue(i64VertexIDPair3 > Int32.MaxValue); Assert.IsTrue(i64VertexIDPair4 > Int32.MaxValue); Assert.AreEqual(i64VertexIDPair1, i64VertexIDPair2); Assert.AreNotEqual(i64VertexIDPair1, i64VertexIDPair3); Assert.AreNotEqual(i64VertexIDPair1, i64VertexIDPair4); Assert.AreNotEqual(i64VertexIDPair2, i64VertexIDPair3); Assert.AreNotEqual(i64VertexIDPair2, i64VertexIDPair4); Assert.AreNotEqual(i64VertexIDPair3, i64VertexIDPair4); }
SetUp() { m_oDirectedGraph = new Graph(GraphDirectedness.Directed); IVertexCollection oDirectedVertices = m_oDirectedGraph.Vertices; m_oDirectedVertexA = oDirectedVertices.Add(); m_oDirectedVertexA.Name = "A"; m_oDirectedVertexB = oDirectedVertices.Add(); m_oDirectedVertexB.Name = "B"; m_oDirectedVertexC = oDirectedVertices.Add(); m_oDirectedVertexC.Name = "C"; m_oDirectedVertexD = oDirectedVertices.Add(); m_oDirectedVertexD.Name = "D"; m_oDirectedVertexWithNullName = oDirectedVertices.Add(); m_oDirectedVertexWithNullName.Name = null; m_oUndirectedGraph = new Graph(GraphDirectedness.Undirected); IVertexCollection oUndirectedVertices = m_oUndirectedGraph.Vertices; m_oUndirectedVertexA = oUndirectedVertices.Add(); m_oUndirectedVertexA.Name = "A"; m_oUndirectedVertexB = oUndirectedVertices.Add(); m_oUndirectedVertexB.Name = "B"; m_oUndirectedVertexC = oUndirectedVertices.Add(); m_oUndirectedVertexC.Name = "C"; m_oUndirectedVertexD = oUndirectedVertices.Add(); m_oUndirectedVertexD.Name = "D"; m_oUndirectedVertexWithNullName = oUndirectedVertices.Add(); m_oUndirectedVertexWithNullName.Name = null; }
ParseVertices ( IGraph oGraph, XmlNode oGraphXmlNode, XmlNamespaceManager oXmlNamespaceManager, Dictionary <String, GraphMLAttribute> oGraphMLAttributeDictionary ) { Debug.Assert(oGraph != null); Debug.Assert(oGraphXmlNode != null); Debug.Assert(oXmlNamespaceManager != null); Debug.Assert(oGraphMLAttributeDictionary != null); AssertValid(); IVertexCollection oVertices = oGraph.Vertices; // The key is the id attribute of the "node" XML node, and the value is // the corresponding IVertex. Dictionary <String, IVertex> oVertexDictionary = new Dictionary <String, IVertex>(); foreach (XmlNode oNodeXmlNode in oGraphXmlNode.SelectNodes( GraphMLPrefix + ":node", oXmlNamespaceManager)) { String sID = XmlUtil2.SelectRequiredSingleNodeAsString( oNodeXmlNode, "@id", null); IVertex oVertex = oVertices.Add(); oVertex.Name = sID; try { oVertexDictionary.Add(sID, oVertex); } catch (ArgumentException) { throw new XmlException( "The id \"" + sID + "\" exists for two \"node\" XML nodes." + " Node id values must be unique." ); } ParseGraphMLAttributeValues(oNodeXmlNode, oXmlNamespaceManager, oVertex, true, oGraphMLAttributeDictionary); } return(oVertexDictionary); }
AddLoadedVertex ( String sVertexName, IVertexCollection oVertices, Dictionary <Int32, IVertex> oVertexDictionary ) { Debug.Assert(!String.IsNullOrEmpty(sVertexName)); Debug.Assert(oVertices != null); Debug.Assert(oVertexDictionary != null); AssertValid(); IVertex oVertex = new Vertex(); oVertex.Name = sVertexName; oVertices.Add(oVertex); oVertexDictionary.Add(oVertices.Count - 1, oVertex); }
private void add_nodes(DataTable node_table, IVertexCollection oVertices) { DataRowCollection node_rows = node_table.Rows; DataColumnCollection node_col = node_table.Columns; foreach (DataRow row in node_rows) { //Notice: "nodename" and "nodeid" should be edited IVertex oVertexA = oVertices.Add(); oVertexA.Name = row["NodeName"].ToString(); oVertexA.Tag = row["ID"].ToString(); int i = 0; foreach(DataColumn col in node_col){ oVertexA.SetValue(col.ColumnName, row[i++]); } } }
CreateVertex ( String sVertexName, IVertexCollection oVertices, Dictionary <String, IVertex> oVertexNameDictionary ) { Debug.Assert(!String.IsNullOrEmpty(sVertexName)); Debug.Assert(oVertices != null); Debug.Assert(oVertexNameDictionary != null); IVertex oVertex = oVertices.Add(); oVertex.Name = sVertexName; oVertexNameDictionary.Add(sVertexName, oVertex); return(oVertex); }
VertexNameToVertex ( String sVertexName, IVertexCollection oVertices, Dictionary <String, IVertex> oDictionary ) { Debug.Assert(!String.IsNullOrEmpty(sVertexName)); Debug.Assert(oVertices != null); Debug.Assert(oDictionary != null); AssertValid(); IVertex oVertex; if (!oDictionary.TryGetValue(sVertexName, out oVertex)) { oVertex = oVertices.Add(); oVertex.Name = sVertexName; oDictionary.Add(sVertexName, oVertex); } return(oVertex); }
AddLoadedVertex ( String sVertexName, IVertexCollection oVertices, Dictionary<Int32, IVertex> oVertexDictionary ) { Debug.Assert( !String.IsNullOrEmpty(sVertexName) ); Debug.Assert(oVertices != null); Debug.Assert(oVertexDictionary != null); AssertValid(); IVertex oVertex = new Vertex(); oVertex.Name = sVertexName; oVertices.Add(oVertex); oVertexDictionary.Add(oVertices.Count - 1, oVertex); }
Clone ( Boolean copyMetadataValues, Boolean copyTag ) { AssertValid(); const String MethodName = "Clone"; IGraph oNewGraph = new Graph(m_eDirectedness); // Copy the base-class fields to the new edge. this.CopyTo(oNewGraph, copyMetadataValues, copyTag); // The vertices need to be copied to the new graph. Loop through the // vertices in this original graph. IVertexCollection oNewVertices = oNewGraph.Vertices; foreach (IVertex oOriginalVertex in m_oVertexCollection) { IVertex oNewVertex = oOriginalVertex.Clone( copyMetadataValues, copyTag); // To make it easier to copy the edges in this original graph, // temporarily store the ID of the new vertex in the Tag of the // original vertex. Save the Tag so it can be restored later. oOriginalVertex.Tag = new VertexMapper(oOriginalVertex.Tag, oNewVertex); oNewVertices.Add(oNewVertex); } // The edges need to be copied to the new graph. Loop through the // edges in this original graph. IEdgeCollection oNewEdges = oNewGraph.Edges; foreach (IEdge oOriginalEdge in m_oEdgeCollection) { // Get the original edge's vertices. IVertex oOriginalVertex1, oOriginalVertex2; EdgeUtil.EdgeToVertices(oOriginalEdge, this.ClassName, MethodName, out oOriginalVertex1, out oOriginalVertex2); // Retrieve the VertexMapper objects that were temporarily stored // in the vertices' Tags. Debug.Assert(oOriginalVertex1.Tag is VertexMapper); Debug.Assert(oOriginalVertex2.Tag is VertexMapper); VertexMapper oVertexMapper1 = (VertexMapper)oOriginalVertex1.Tag; VertexMapper oVertexMapper2 = (VertexMapper)oOriginalVertex2.Tag; // Get the new vertices that correspond to the original edge's // vertices. IVertex oNewVertex1 = oVertexMapper1.NewVertex; IVertex oNewVertex2 = oVertexMapper2.NewVertex; // Copy the original edge, connecting the new vertices in the // process. IEdge oNewEdge = oOriginalEdge.Clone(copyMetadataValues, copyTag, oNewVertex1, oNewVertex2, oOriginalEdge.IsDirected); oNewEdges.Add(oNewEdge); } // Restore the original vertices' Tags. foreach (IVertex oOriginalVertex in m_oVertexCollection) { Debug.Assert(oOriginalVertex.Tag is VertexMapper); VertexMapper oVertexMapper = (VertexMapper)oOriginalVertex.Tag; oOriginalVertex.Tag = oVertexMapper.OriginalVertexTag; } return(oNewGraph); }
ImportMatrixWorkbook ( Microsoft.Office.Interop.Excel.Application application, String sourceWorkbookName, Boolean sourceWorkbookHasVertexNames, GraphDirectedness sourceWorkbookDirectedness ) { Debug.Assert(application != null); Debug.Assert(!String.IsNullOrEmpty(sourceWorkbookName)); AssertValid(); // Get the active worksheet of the source workbook. Worksheet oSourceWorksheet = GetActiveSourceWorksheet( application, sourceWorkbookName); // Read or create the names of the vertices in the source workbook. String [] asVertexNames; Int32 iFirstEdgeWeightRowOneBased, iFirstEdgeWeightColumnOneBased; if (sourceWorkbookHasVertexNames) { asVertexNames = ReadVertexNames(oSourceWorksheet); iFirstEdgeWeightRowOneBased = iFirstEdgeWeightColumnOneBased = 2; } else { asVertexNames = CreateVertexNames(oSourceWorksheet); iFirstEdgeWeightRowOneBased = iFirstEdgeWeightColumnOneBased = 1; } Int32 iVertices = asVertexNames.Length; switch (sourceWorkbookDirectedness) { case GraphDirectedness.Directed: break; case GraphDirectedness.Undirected: CheckSymmetryOfUndirectedMatrix(oSourceWorksheet, iFirstEdgeWeightRowOneBased, iFirstEdgeWeightColumnOneBased, iVertices); break; default: Debug.Assert(false); break; } // Create a graph and populate it with the vertices. IGraph oGraph = new Graph(sourceWorkbookDirectedness); IVertexCollection oVertices = oGraph.Vertices; IVertex[] aoOrderedVertices = new IVertex[iVertices]; for (Int32 i = 0; i < iVertices; i++) { IVertex oVertex = oVertices.Add(); oVertex.Name = asVertexNames[i]; aoOrderedVertices[i] = oVertex; } // Read the edges and import them into the graph. ReadEdges(oSourceWorksheet, iFirstEdgeWeightRowOneBased, iFirstEdgeWeightColumnOneBased, oGraph, aoOrderedVertices); return(oGraph); }
GetSubgraphAsNewGraph ( ICollection <IVertex> verticesToInclude ) { Debug.Assert(verticesToInclude != null); Debug.Assert(verticesToInclude.Count > 0); IVertex oFirstVertex = verticesToInclude.First(); IGraph oParentGraph = oFirstVertex.ParentGraph; IGraph oNewGraph = new Graph(oParentGraph.Directedness); IEdgeCollection oNewEdges = oNewGraph.Edges; IVertexCollection oNewVertices = oNewGraph.Vertices; // This maps vertex IDs in the original graph to the corresponding new // vertices in the new graph. Dictionary <Int32, IVertex> oVertexIDToNewVertexDictionary = new Dictionary <Int32, IVertex>(verticesToInclude.Count); // Copy the vertices into the new graph. foreach (IVertex oVertex in verticesToInclude) { IVertex oNewVertex = oNewVertices.Add(); oNewVertex.Name = oVertex.Name; oVertexIDToNewVertexDictionary.Add(oVertex.ID, oNewVertex); } // This contains the IDs of the original edges that have been copied // into the new graph. HashSet <Int32> oIDsOfCopiedEdges = new HashSet <Int32>(); // Copy the edges connecting the vertices into the new graph. foreach (IVertex oVertex in verticesToInclude) { foreach (IEdge oIncidentEdge in oVertex.IncidentEdges) { IVertex oAdjacentVertex = oIncidentEdge.GetAdjacentVertex( oVertex); IVertex oNewAdjacentVertex; if ( !oVertexIDToNewVertexDictionary.TryGetValue( oAdjacentVertex.ID, out oNewAdjacentVertex) || oIDsOfCopiedEdges.Contains(oIncidentEdge.ID) ) { // The adjacent vertex is not in the set of vertices to // include, or the edge has already been copied into the // new graph. continue; } IVertex oNewVertex = oVertexIDToNewVertexDictionary[oVertex.ID]; IEdge oNewEdge; Boolean bIncidentEdgeIsDirected = oIncidentEdge.IsDirected; if (oVertex == oIncidentEdge.Vertices[0]) { oNewEdge = oNewEdges.Add(oNewVertex, oNewAdjacentVertex, bIncidentEdgeIsDirected); } else { oNewEdge = oNewEdges.Add(oNewAdjacentVertex, oNewVertex, bIncidentEdgeIsDirected); } oIDsOfCopiedEdges.Add(oIncidentEdge.ID); } } return(oNewGraph); }
public MiRNADataReader(string sourcefile) { // Create a graph with mixed directedness. graph = new Graph(); vertexes = new Dictionary <string, Vertex>(); // Add vertices to the graph. IVertexCollection oVertices = graph.Vertices; IEdgeCollection oEdges = graph.Edges; using (StreamReader sr = new StreamReader(sourcefile)) { while (sr.Peek() != -1) { string line = sr.ReadLine().Trim(); if (line.Equals("")) { continue; } string[] tks = line.Split('\t'); string[] miRNAs = tks[2].Split('|'); string[] diseases = tks[6].Split('|'); for (int i = 0; i < miRNAs.Length; i++) { Vertex s = new Vertex(); s.Name = miRNAs[i].ToLower(); if (!vertexes.ContainsKey(s.Name)) { vertexes.Add(s.Name, s); oVertices.Add(s); } else { s = vertexes[s.Name]; } for (int j = 0; j < diseases.Length; j++) { Vertex t = new Vertex(); t.Name = diseases[j].ToLower(); t.SetValue(MeSH, tks[7]); //MeSHDictionaryTagger mesh = MeSHDictionaryTagger.GetInstance(@"D:\Resources\Models\NCBI\MeSH\desc2014.xml", @"D:\Resources\Models\ThirdParty\TsujiiLab\GENIATagger"); //string name = mesh.GetMeSHTrees(tks[7]).First().ToString(); if (!vertexes.ContainsKey(t.GetValue(MeSH).ToString())) { vertexes.Add(t.GetValue(MeSH).ToString(), t); oVertices.Add(t); } else { t = vertexes[t.GetValue(MeSH).ToString()]; } Smrf.NodeXL.Core.Edge e = new Smrf.NodeXL.Core.Edge(s, t, false); e.Tag = tks[5]; e.SetValue(SENTENCE, tks[1]); e.Name = tks[4]; oEdges.Add(e); } } } } }
TestSaveGraph2() { // Undirected graph. IGraph oGraph = new Graph(GraphDirectedness.Undirected); IVertexCollection oVertices = oGraph.Vertices; IEdgeCollection oEdges = oGraph.Edges; IVertex oVertexA = oVertices.Add(); oVertexA.Name = "a"; IVertex oVertexB = oVertices.Add(); oVertexB.Name = "b"; IVertex oVertexC = oVertices.Add(); oVertexC.Name = "c"; IVertex oVertexD = oVertices.Add(); oVertexD.Name = "d"; IEdge oEdge; oEdge = oEdges.Add(oVertexD, oVertexB, false); oEdge.SetValue(ReservedMetadataKeys.EdgeWeight, 1.0); oEdge = oEdges.Add(oVertexB, oVertexC, false); oEdge.SetValue(ReservedMetadataKeys.EdgeWeight, 3.0); oEdge = oEdges.Add(oVertexC, oVertexC, false); oEdge.SetValue(ReservedMetadataKeys.EdgeWeight, 4.12); m_oGraphAdapter.SaveGraph(oGraph, m_sTempFileName); String sFileContents = FileUtil.ReadTextFile(m_sTempFileName); const String ExpectedFileContents = "DL\r\n" + "N=4\r\n" + "FORMAT = FULLMATRIX DIAGONAL PRESENT\r\n" + "ROW LABELS:\r\n" + "\"a\"\r\n" + "\"b\"\r\n" + "\"c\"\r\n" + "\"d\"\r\n" + "COLUMN LABELS:\r\n" + "\"a\"\r\n" + "\"b\"\r\n" + "\"c\"\r\n" + "\"d\"\r\n" + "DATA:\r\n" + "0 0 0 0 \r\n" + "0 0 3 1 \r\n" + "0 3 4.12 0 \r\n" + "0 1 0 0 \r\n" ; Assert.AreEqual(ExpectedFileContents, sFileContents); }
//************************************************************************* // Method: VertexNameToVertex() // /// <summary> /// Finds or creates a vertex given a vertex name. /// </summary> /// /// <param name="sVertexName"> /// Name of the vertex to find or create. /// </param> /// /// <param name="oVertices"> /// Vertex collection to add a new vertex to if a new vertex is created. /// </param> /// /// <param name="oDictionary"> /// Dictionary of existing vertices. The key is the vertex name and the /// value is the vertex. /// </param> /// /// <returns> /// The found or created vertex. /// </returns> /// /// <remarks> /// If <paramref name="oDictionary" /> contains a vertex named <paramref /// name="sVertexName" />, the vertex is returned. Otherwise, a vertex is /// created, added to <paramref name="oVertices" />, added to <paramref /// name="oDictionary" />, and returned. /// </remarks> //************************************************************************* protected IVertex VertexNameToVertex( String sVertexName, IVertexCollection oVertices, Dictionary<String, IVertex> oDictionary ) { Debug.Assert( !String.IsNullOrEmpty(sVertexName) ); Debug.Assert(oVertices != null); Debug.Assert(oDictionary != null); AssertValid(); IVertex oVertex; if ( !oDictionary.TryGetValue(sVertexName, out oVertex) ) { oVertex = oVertices.Add(); oVertex.Name = sVertexName; oDictionary.Add(sVertexName, oVertex); } return (oVertex); }
//************************************************************************* // Method: CreateVertex() // /// <summary> /// Creates a vertex. /// </summary> /// /// <param name="sVertexName"> /// Name of the vertex to create. /// </param> /// /// <param name="oVertices"> /// Vertex collection to add the new vertex to. /// </param> /// /// <param name="oVertexNameDictionary"> /// Dictionary of existing vertices. The key is the vertex name and the /// value is the vertex. The dictionary can't already contain <paramref /// name="sVertexName" />. /// </param> /// /// <returns> /// The created vertex. /// </returns> /// /// <remarks> /// This method creates a vertex and adds it to both <paramref /// name="oVertices" /> and <paramref name="oVertexNameDictionary" />. /// </remarks> //************************************************************************* protected IVertex CreateVertex( String sVertexName, IVertexCollection oVertices, Dictionary<String, IVertex> oVertexNameDictionary ) { Debug.Assert( !String.IsNullOrEmpty(sVertexName) ); Debug.Assert(oVertices != null); Debug.Assert(oVertexNameDictionary != null); AssertValid(); IVertex oVertex = oVertices.Add(); oVertex.Name = sVertexName; oVertexNameDictionary.Add(sVertexName, oVertex); return (oVertex); }
PopulateGraph() { IGraph oGraph = m_oNodeXLControl.Graph; IVertexCollection oVertices = oGraph.Vertices; IEdgeCollection oEdges = oGraph.Edges; Double dWidth = this.Width; Double dHeight = this.Height; Random oRandom = new Random(); // m_oNodeXLControl.Layout.Margin = 0; { #if false // Two shapes only. IVertex oVertex1 = oVertices.Add(); oVertex1.SetValue(ReservedMetadataKeys.PerVertexShape, VertexShape.Circle); oVertex1.SetValue(ReservedMetadataKeys.PerVertexRadius, 5.0F); oVertex1.SetValue(ReservedMetadataKeys.LockVertexLocation, true); oVertex1.Location = new System.Drawing.PointF(300, 300); oVertex1.SetValue(ReservedMetadataKeys.PerVertexLabel, "This is A: " + oVertex1.Location); IVertex oVertex2 = oVertices.Add(); oVertex2.SetValue(ReservedMetadataKeys.PerVertexShape, VertexShape.Circle); oVertex2.SetValue(ReservedMetadataKeys.PerVertexRadius, 5.0F); oVertex2.SetValue(ReservedMetadataKeys.LockVertexLocation, true); oVertex2.Location = new System.Drawing.PointF(500, 300); oVertex2.SetValue(ReservedMetadataKeys.PerVertexLabel, "This is B: " + oVertex2.Location); IEdge oEdge = oEdges.Add(oVertex1, oVertex2, true); // oEdge.SetValue(ReservedMetadataKeys.PerEdgeWidth, 20F); m_oNodeXLControl.DrawGraph(true); return; #endif } { #if false // Two labels only. IVertex oVertex1 = oVertices.Add(); oVertex1.SetValue(ReservedMetadataKeys.PerVertexShape, VertexShape.Label); oVertex1.SetValue(ReservedMetadataKeys.PerVertexLabel, "This is a label."); oVertex1.SetValue(ReservedMetadataKeys.LockVertexLocation, true); oVertex1.Location = new System.Drawing.PointF(300, 300); IVertex oVertex2 = oVertices.Add(); oVertex2.SetValue(ReservedMetadataKeys.PerVertexShape, VertexShape.Label); oVertex2.SetValue(ReservedMetadataKeys.PerVertexLabel, "This is another label."); oVertex2.SetValue(ReservedMetadataKeys.LockVertexLocation, true); oVertex2.Location = new System.Drawing.PointF(500, 100); oEdges.Add(oVertex1, oVertex2, true); m_oNodeXLControl.DrawGraph(true); return; #endif } Smrf.NodeXL.Visualization.Wpf.VertexShape[] aeShapes = (Smrf.NodeXL.Visualization.Wpf.VertexShape[]) Enum.GetValues(typeof(Smrf.NodeXL.Visualization.Wpf. VertexShape)); Int32 iShapes = aeShapes.Length; Int32 Vertices = 100; IVertex oFirstVertex = oVertices.Add(); oFirstVertex.SetValue(ReservedMetadataKeys.PerVertexRadius, 4.0F); IVertex oPreviousVertex = oFirstVertex; for (Int32 i = 1; i < Vertices; i++) { IVertex oVertex = oVertices.Add(); VertexShape eShape = aeShapes[oRandom.Next(iShapes)]; oVertex.SetValue(ReservedMetadataKeys.PerVertexShape, eShape); #if false // Hard-coded vertex shape. oVertex.SetValue(ReservedMetadataKeys.PerVertexShape, VertexDrawer.VertexShape.Diamond); #endif #if true // Vertex color. oVertex.SetValue(ReservedMetadataKeys.PerColor, System.Windows.Media.Color.FromArgb(255, (Byte)oRandom.Next(256), (Byte)oRandom.Next(256), (Byte)oRandom.Next(256)) ); #endif #if true // Vertex radius. Single fRadius = (Single)( Smrf.NodeXL.Visualization.Wpf.VertexDrawer.MinimumRadius + (0.1 * Smrf.NodeXL.Visualization.Wpf.VertexDrawer.MaximumRadius - Smrf.NodeXL.Visualization.Wpf.VertexDrawer.MinimumRadius) * oRandom.NextDouble()); oVertex.SetValue(ReservedMetadataKeys.PerVertexRadius, fRadius); #endif if (true && oRandom.Next(20) == 0) // Image { oVertex.SetValue(ReservedMetadataKeys.PerVertexShape, VertexShape.Image); oVertex.SetValue(ReservedMetadataKeys.PerVertexImage, new System.Windows.Media.Imaging.BitmapImage( new Uri(oRandom.Next(2) == 1 ? "..\\..\\Images\\TestImage1.gif" : "..\\..\\Images\\TestImage2.jpg", UriKind.Relative))); } if (eShape == VertexShape.Label) { String sLabel = "This is a label"; if (oRandom.Next(2) == 0) { sLabel = LongLabel; } oVertex.SetValue(ReservedMetadataKeys.PerVertexLabel, sLabel); /* * oVertex.SetValue( ReservedMetadataKeys.PerColor, * System.Windows.Media.Color.FromArgb(255, 0, 0, 0) ); * * oVertex.SetValue( * * ReservedMetadataKeys.PerVertexLabelFillColor, * System.Windows.Media.Color.FromArgb(255, 200, 200, * 200) ); * * oVertex.SetValue(ReservedMetadataKeys.PerAlpha, (Single)128); */ } else { String sAnnotation = "This is an annotation."; oVertex.SetValue(ReservedMetadataKeys.PerVertexLabel, sAnnotation); } if (true && oRandom.Next(1) == 1) // Vertex visibility. { oVertex.SetValue(ReservedMetadataKeys.Visibility, VisibilityKeyValue.Filtered); } #if false // Vertex alpha. oVertex.SetValue( ReservedMetadataKeys.PerAlpha, (Single)oRandom.Next(256)); #endif #if false // Vertex IsSelected. oVertex.SetValue(ReservedMetadataKeys.IsSelected, null); #endif oVertex.Location = new System.Drawing.PointF( (Single)(dWidth * oRandom.NextDouble()), (Single)(dHeight * oRandom.NextDouble()) ); IEdge oEdge = oEdges.Add(oFirstVertex, oVertex, true); oEdge.SetValue(ReservedMetadataKeys.PerEdgeLabel, "This is an edge label"); #if false // Edge color. oEdge.SetValue(ReservedMetadataKeys.PerColor, System.Windows.Media.Color.FromArgb(255, (Byte)oRandom.Next(256), (Byte)oRandom.Next(256), (Byte)oRandom.Next(256)) ); #endif #if false // Edge width. Double dEdgeWidth = EdgeDrawer.MinimumWidth + (EdgeDrawer.MaximumWidth - EdgeDrawer.MinimumWidth) * oRandom.NextDouble(); oEdge.SetValue(ReservedMetadataKeys.PerEdgeWidth, dEdgeWidth); #endif #if true // Edge visibility. oEdge.SetValue(ReservedMetadataKeys.Visibility, VisibilityKeyValue.Visible); #endif #if true // Edge alpha. oEdge.SetValue(ReservedMetadataKeys.PerAlpha, (Single)oRandom.Next(256)); #endif #if false // Edge IsSelected. oEdge.SetValue(ReservedMetadataKeys.IsSelected, null); #endif #if true if (oRandom.Next(1) == 0) { IEdge oRandomEdge = oEdges.Add(oPreviousVertex, oVertex, true); #if true // Edge label. oRandomEdge.SetValue(ReservedMetadataKeys.PerEdgeLabel, "This is a random edge label"); #endif } #endif oPreviousVertex = oVertex; } AddToolTipsToVertices(); SetBackgroundImage(); m_oNodeXLControl.DrawGraph(true); }
TestRemoveIsolatesFromGroups() { IGraph oGraph = new Graph(); IEdgeCollection oEdges = oGraph.Edges; IVertexCollection oVertices = oGraph.Vertices; // Non-isolates. IVertex oVertexA = oVertices.Add(); IVertex oVertexB = oVertices.Add(); IVertex oVertexC = oVertices.Add(); IVertex oVertexD = oVertices.Add(); // Isolates. IVertex oVertexE = oVertices.Add(); IVertex oVertexF = oVertices.Add(); IVertex oVertexG = oVertices.Add(); oEdges.Add(oVertexA, oVertexB); oEdges.Add(oVertexB, oVertexC); oEdges.Add(oVertexD, oVertexA); // Group1 contains all non-isolate vertices. GroupInfo oGroup1 = new GroupInfo(); oGroup1.Vertices.AddLast(oVertexA); oGroup1.Vertices.AddLast(oVertexB); oGroup1.Vertices.AddLast(oVertexC); // Group2 contains a mix of isolate and non-isolate vertices. GroupInfo oGroup2 = new GroupInfo(); oGroup2.Vertices.AddLast(oVertexD); oGroup2.Vertices.AddLast(oVertexE); // Group3 contains all isolate vertices. GroupInfo oGroup3 = new GroupInfo(); oGroup3.Vertices.AddLast(oVertexF); oGroup3.Vertices.AddLast(oVertexG); // Group4 is empty. GroupInfo oGroup4 = new GroupInfo(); List <GroupInfo> oGroups = new List <GroupInfo>(); oGroups.Add(oGroup1); oGroups.Add(oGroup2); oGroups.Add(oGroup3); oGroups.Add(oGroup4); GroupUtil.RemoveIsolatesFromGroups(oGroups); Assert.AreEqual(2, oGroups.Count); Assert.AreEqual(3, oGroups[0].Vertices.Count); Assert.IsNotNull(oGroups[0].Vertices.Find(oVertexA)); Assert.IsNotNull(oGroups[0].Vertices.Find(oVertexB)); Assert.IsNotNull(oGroups[0].Vertices.Find(oVertexC)); Assert.AreEqual(1, oGroups[1].Vertices.Count); Assert.IsNotNull(oGroups[1].Vertices.Find(oVertexD)); }
protected void PopulateAndDrawGraph() { // Get the graph's vertex collection. IVertexCollection oVertices = Graph.Graph.Vertices; // Add three vertices. IVertex oVertexA = oVertices.Add(); IVertex oVertexB = oVertices.Add(); IVertex oVertexC = oVertices.Add(); // Change the color, radius, and shape of vertex A. oVertexA.SetValue(ReservedMetadataKeys.PerColor, Color.FromArgb(255, 255, 0, 255)); oVertexA.SetValue(ReservedMetadataKeys.PerVertexRadius, 20F); oVertexA.SetValue(ReservedMetadataKeys.PerVertexShape, VertexShape.Sphere); // Draw vertex B as a Label, which is a rectangle containing text. oVertexB.SetValue(ReservedMetadataKeys.PerVertexShape, VertexShape.Label); oVertexB.SetValue(ReservedMetadataKeys.PerVertexLabel, "Label"); // Set the label's text and fill colors. oVertexB.SetValue(ReservedMetadataKeys.PerColor, Color.FromArgb(255, 220, 220, 220)); oVertexB.SetValue(ReservedMetadataKeys.PerVertexLabelFillColor, Color.FromArgb(255, 0, 0, 0)); // Annotate vertex C with text that is drawn outside the vertex. All // shapes except Label can be annotated. oVertexC.SetValue(ReservedMetadataKeys.PerVertexLabel, "Annotation"); // Get the graph's edge collection. IEdgeCollection oEdges = Graph.Graph.Edges; // Connect the vertices with directed edges. IEdge oEdge1 = oEdges.Add(oVertexA, oVertexB, true); IEdge oEdge2 = oEdges.Add(oVertexB, oVertexC, true); IEdge oEdge3 = oEdges.Add(oVertexC, oVertexA, true); // Customize their appearance. oEdge1.SetValue(ReservedMetadataKeys.PerColor, Color.FromArgb(255, 55, 125, 98)); oEdge1.SetValue(ReservedMetadataKeys.PerEdgeWidth, 3F); oEdge1.SetValue(ReservedMetadataKeys.PerEdgeLabel, "This is edge 1"); oEdge2.SetValue(ReservedMetadataKeys.PerEdgeWidth, 5F); oEdge2.SetValue(ReservedMetadataKeys.PerEdgeLabel, "This is edge 2"); oEdge3.SetValue(ReservedMetadataKeys.PerColor, Color.FromArgb(255, 0, 255, 0)); Graph.DrawGraph(true); }
CloneVertexIntoSubgraph ( IVertex oOriginalVertex, IGraph oSubgraph, Decimal decLevels ) { Debug.Assert(oOriginalVertex != null); Debug.Assert(oSubgraph != null); Debug.Assert(decLevels >= 0); AssertValid(); // Get the original vertices and edges to clone. For the vertex // dictionary, the key is the IVertex and the value is the vertex's // level, which is the distance of the vertex from oOriginalVertex. // For the edge HashSet, the key is the IEdge. Dictionary <IVertex, Int32> oOriginalVerticesToClone; HashSet <IEdge> oOriginalEdgesToClone; SubgraphCalculator.GetSubgraph(oOriginalVertex, decLevels, true, out oOriginalVerticesToClone, out oOriginalEdgesToClone); // Clone the vertices. This dictionary maps the IDs of the original // vertices to their clones. Dictionary <Int32, IVertex> oOriginalToSubgraphVertexMapper = new Dictionary <Int32, IVertex>(); IVertexCollection oSubgraphVertices = oSubgraph.Vertices; foreach (IVertex oOriginalVertexToClone in oOriginalVerticesToClone.Keys) { IVertex oSubgraphVertex = oOriginalVertexToClone.Clone(false, false); oSubgraphVertices.Add(oSubgraphVertex); oOriginalToSubgraphVertexMapper.Add(oOriginalVertexToClone.ID, oSubgraphVertex); } // This dictionary is no longer needed. oOriginalVerticesToClone.Clear(); oOriginalVerticesToClone = null; // Clone the edges. IEdgeCollection oSubgraphEdges = oSubgraph.Edges; foreach (IEdge oOriginalEdgeToClone in oOriginalEdgesToClone) { IVertex [] aoOriginalVertices = oOriginalEdgeToClone.Vertices; IVertex oSubgraphVertex1 = oOriginalToSubgraphVertexMapper[aoOriginalVertices[0].ID]; IVertex oSubgraphVertex2 = oOriginalToSubgraphVertexMapper[aoOriginalVertices[1].ID]; oSubgraphEdges.Add(oSubgraphVertex1, oSubgraphVertex2, oOriginalEdgeToClone.IsDirected); } Debug.Assert(oOriginalToSubgraphVertexMapper.ContainsKey( oOriginalVertex.ID)); return(oOriginalToSubgraphVertexMapper[oOriginalVertex.ID]); }
TestSaveGraph() { // Directed and undirected graphs. foreach (Boolean bDirected in TestGraphUtil.AllBoolean) { IGraph oGraph = new Graph(bDirected ? GraphDirectedness.Directed : GraphDirectedness.Undirected); IVertexCollection oVertices = oGraph.Vertices; IEdgeCollection oEdges = oGraph.Edges; IVertex oVertex1 = oVertices.Add(); oVertex1.Name = "Vertex1"; oVertex1.SetValue("VertexAttribute1", 123); // Int32 IVertex oVertex2 = oVertices.Add(); oVertex2.Name = "Vertex2"; oVertex2.SetValue("VertexAttribute2", "abc"); // String IVertex oVertex3 = oVertices.Add(); oVertex3.Name = "Vertex3"; oVertex3.SetValue("VertexAttribute1", 4.0); // Double oVertex3.SetValue("VertexAttribute2", 23456.0F); // Single IVertex oVertex4 = oVertices.Add(); oVertex4.Name = "Vertex4"; IVertex oVertex5 = oVertices.Add(); oVertex5.Name = "Vertex5"; IEdge oEdge; oEdge = oEdges.Add(oVertex1, oVertex2, bDirected); oEdge.SetValue("EdgeAttribute1", "ea1"); oEdge = oEdges.Add(oVertex3, oVertex4, bDirected); oEdge.SetValue("EdgeAttribute2", "ea2"); oGraph.SetValue(ReservedMetadataKeys.AllVertexMetadataKeys, new String [] { "VertexAttribute1", "VertexAttribute2", }); oGraph.SetValue(ReservedMetadataKeys.AllEdgeMetadataKeys, new String [] { "EdgeAttribute1", "EdgeAttribute2", }); m_oGraphAdapter.SaveGraph(oGraph, m_sTempFileName); String sFileContents; using (StreamReader oStreamReader = new StreamReader(m_sTempFileName)) { sFileContents = oStreamReader.ReadToEnd(); } XmlDocument oXmlDocument = new XmlDocument(); oXmlDocument.LoadXml(sFileContents); XmlNamespaceManager oXmlNamespaceManager = new XmlNamespaceManager( oXmlDocument.NameTable); oXmlNamespaceManager.AddNamespace("g", GraphMLGraphAdapter.GraphMLUri); String [] asRequiredXPaths = new String [] { // Graph node. String.Format("/g:graphml/g:graph[@edgedefault='{0}']", bDirected ? "directed" : "undirected"), "/g:graphml/g:key[@id='V-VertexAttribute1' and @for='node'" + " and @attr.name='VertexAttribute1'" + " and @attr.type='string']", // Vertex nodes. "/g:graphml/g:key[@id='V-VertexAttribute2' and @for='node'" + " and @attr.name='VertexAttribute2'" + " and @attr.type='string']", "/g:graphml/g:key[@id='E-EdgeAttribute1' and @for='edge'" + " and @attr.name='EdgeAttribute1'" + " and @attr.type='string']", "/g:graphml/g:key[@id='E-EdgeAttribute2' and @for='edge'" + " and @attr.name='EdgeAttribute2'" + " and @attr.type='string']", "/g:graphml/g:graph/g:node[@id='Vertex1']/" + "g:data[@key='V-VertexAttribute1' and .='123']", "/g:graphml/g:graph/g:node[@id='Vertex2']/" + "g:data[@key='V-VertexAttribute2' and .='abc']", "/g:graphml/g:graph/g:node[@id='Vertex3']/" + "g:data[@key='V-VertexAttribute1' and .='4']", "/g:graphml/g:graph/g:node[@id='Vertex3']/" + "g:data[@key='V-VertexAttribute2' and .='23456']", "/g:graphml/g:graph/g:node[@id='Vertex4']", "/g:graphml/g:graph/g:node[@id='Vertex5']", // Edge nodes. "/g:graphml/g:graph/g:edge[@source='Vertex1' and" + " @target='Vertex2']/" + "g:data[@key='E-EdgeAttribute1' and .='ea1']", "/g:graphml/g:graph/g:edge[@source='Vertex3' and" + " @target='Vertex4']/" + "g:data[@key='E-EdgeAttribute2' and .='ea2']", }; foreach (String sRequiredXPath in asRequiredXPaths) { XmlNode oXmlNode = oXmlDocument.SelectSingleNode( sRequiredXPath, oXmlNamespaceManager); Assert.IsNotNull(oXmlNode); } } }