public void ListPropertiesOfAdjList(IAdjList network, StringBuilder sb) { var naNetwork = network as INodeAttributes; if (naNetwork != null) { sb.AppendLine(string.Format("Node attributes: {0}", naNetwork.NodeDataAttributeCount)); if (naNetwork.NodeDataAttributeCount > 0) { for (int i = 0; i < naNetwork.NodeDataAttributeCount; i++) { sb.AppendLine(string.Format(" [{0}]: \"{1}\", type: {2}", i, naNetwork.NodeDataAttributeNames[i] ?? "null", naNetwork.NodeDataAttributeTypes[i].Name)); } } } else { sb.AppendLine(string.Format("Node attributes: {0}", 0)); } var eaNetwork = network as IEdgeAttributes; if (eaNetwork != null) { sb.AppendLine(string.Format("Edge attributes: {0}", eaNetwork.EdgeDataAttributeCount)); for (int i = 0; i < eaNetwork.EdgeDataAttributeCount; i++) { sb.AppendLine(string.Format(" [{0}]: \"{1}\", type: {2}", i, eaNetwork.EdgeDataAttributeNames[i] ?? "null", eaNetwork.EdgeDataAttributeTypes[i].Name)); } } else { sb.AppendLine(string.Format("Edge attributes: {0}", 0)); } }
public DataAttributeTable CreateEdgeDataTable(IAdjList network, string[] columnNames, Type[] columnTypes, object[] defaultValues) { if (network == null) throw new ArgumentNullException("network", "network must not be null"); if (columnNames == null) throw new ArgumentNullException("columnNames", "columnNames array must not be null"); if (columnTypes == null) throw new ArgumentNullException("columnTypes", "columnTypes array must not be null"); if (columnNames.Length != columnTypes.Length) throw new ArgumentException(string.Format("columnNames has length = {0} while columnTypes has length = {1}, the lengths must match", columnNames.Length, columnTypes.Length)); if (defaultValues != null && defaultValues.Length != columnNames.Length) throw new ArgumentException(string.Format("defaulValues has length = {0} while columnNames has length = {1}, the lengths must match", defaultValues.Length, columnNames.Length)); var table = new DataAttributeTable(network); table.AutoAcceptChanges = false; var primaryKeyCol = table.Columns.Add("Index", typeof(int)); table.PrimaryKey = new DataColumn[] { primaryKeyCol }; DataColumn col = null; for (int i = 0; i < columnNames.Length; i++) { col = table.Columns.Add(columnNames[i], columnTypes[i]); if (defaultValues != null) col.DefaultValue = defaultValues[i]; } foreach (IEdge edge in network.Edges) { table.Rows.Add(new object[] { edge.Index }); } table.AcceptChanges(); table.AutoAcceptChanges = true; HookEdgeDataToNetwork(network, table); return table; }
/// <summary> /// Creates and attatches to <paramref name="network"/> a DataAttributeTable /// for storing edge data attributes. /// </summary> /// <param name="network">The parent network.</param> /// <returns>DataAttributeTable</returns> public DataAttributeTable CreateEdgeDataTable(IAdjList network) { if (network == null) throw new ArgumentNullException("network", "network must not be null"); var table = new DataAttributeTable(network); table.AutoAcceptChanges = false; var primaryKeyCol = table.Columns.Add("Index", typeof(int)); table.PrimaryKey = new DataColumn[] { primaryKeyCol }; foreach (IEdge edge in network.Edges) { table.Rows.Add(new object[] { edge.Index }); } table.AcceptChanges(); table.AutoAcceptChanges = true; HookEdgeDataToNetwork(network, table); return table; }
/// <summary> /// Reads the graph element attributes and creates the network, then does 1 pass to read the nodes. /// </summary> /// <param name="network"></param> /// <param name="reader"></param> /// <remarks>Assumes reader.Name == "graph" and reader.IsStartElement() == true</remarks> internal void ReadNetwork(out IAdjList network, XmlReader reader) { network = null; reader.MoveToAttribute("id"); string idStr = reader["id"]; Guid id = ToGuid(idStr); reader.MoveToAttribute("edgedefault"); string directedStr = reader["edgedefault"]; bool isDirected = (string.Compare(directedStr, BlueSpider.GraphML.XMLConstants._Directed) == 0); string name = null; if (reader.MoveToAttribute("name")) { name = reader["name"]; } else name = Path.GetFileNameWithoutExtension(File); int n = 0; if (reader.MoveToAttribute("parse.nodes")) { string numNodesStr = reader["parse.nodes"]; if (!int.TryParse(numNodesStr, out n)) n = 0; } reader.MoveToElement(); using (var provider = new NetworkFactoryProvider()) { using (var fac = provider.Create<IBasicAdjList>()) { fac.EnableNodeDataAttributes = true; fac.EnableEdgeDataAttributes = true; network = fac.CreateNetwork(id, n) as IBasicAdjList; network.Name = name; network.IsDirected = isDirected; } } ReadGraphElements(network, reader); }
//P3 /// <summary> /// Reads the graphml element and populates the node and edge maps of the network from the data elements. /// </summary> /// <param name="network"></param> /// <param name="reader"></param> /// <remarks>Assumes reader.Name == "graphml" and reader.IsStartElement() == true</remarks> internal void ReadGraphML2(IAdjList network, XmlReader reader) { if (reader.Name == "graphml" && reader.IsStartElement() && !reader.IsEmptyElement) { if (reader.ReadToDescendant("graph")) { int edgeCtr = 0; int nodeMapCount = network.NodeAttributeCount; int edgeMapCount = network.EdgeAttributeCount; int startDepth = reader.Depth; reader.Read(); // read down from the "graph" element do { if (reader.Name == "data") { reader.Skip(); // graph data has already been read on pass 1 } else if (reader.Name == "node") { if (nodeMapCount > 0) { // only call this if there are data elements ReadNode2(network, reader); if (reader.Name == "node") // read if we are at the end element reader.Read(); } else reader.Skip(); } else if (reader.Name == "edge") { if (edgeMapCount > 0) { ReadEdge2(network, reader, edgeCtr++); // if (reader.Name == "edge") // read if we are at the end element reader.Read(); } else reader.Skip(); } else if (reader.Name == "hyperedge") throw new InvalidOperationException(string.Format("hyperedge elements are not supported")); } while (reader.Depth > startDepth && !IsCancellationPending); } } }
//P1 /// <summary> /// Reads the graphml element and reads keys, nodes and edge markers /// </summary> /// <param name="network">The network created from this method</param> /// <param name="reader"></param> /// <remarks>Assumes reader.Name == "graphml" and reader.IsStartElement() == true</remarks> internal void ReadGraphML1(out IAdjList network, XmlReader reader) { // Create the keys and read the nodes network = null; if (reader.Name == "graphml" && reader.IsStartElement() && !reader.IsEmptyElement) { KeyReader kReader = new KeyReader(); while (reader.Read() && reader.IsStartElement() && !IsCancellationPending) { if (reader.Name == "key" && reader.IsStartElement()) { ReadKey(kReader, reader); } else if (reader.Name == "graph" && reader.IsStartElement()) { ReadNetwork(out network, reader); } else throw new InvalidOperationException(string.Format("{0} elements are not supported yet.", reader.Name)); } } }
/// <summary> /// Creates the edges based on the contents of <see cref="EdgeMarkers"/> dictionary. /// </summary> /// <param name="network"></param> /// <param name="edgeMarkers">A colleciton of xml edge id to xml src node id and xml dest node ids</param> internal void BuildEdges(IAdjList network, IDictionary<int, XmlNodeIdPair> edgeMarkers) { IEdge edge = null; foreach (KeyValuePair<int, XmlNodeIdPair> kvp in edgeMarkers) { if(!IsCancellationPending) { CreateEdge(out edge, kvp.Value.SourceNodeId, kvp.Value.TargetNodeId, network); // keep a record of which edge the edge xml id corresponds to, for later population of edge maps EdgeCtrToEdgeMap.Add(kvp.Key, edge); } } }
internal void HookEdgeDataToNetwork(IAdjList network, DataAttributeTable edgeData) { if (network is BasicAdjList) { var net = network as BasicAdjList; // net.EdgeData = edgeData; } }
/// <summary> /// Ctor: must be constructed with ParentNetwork to which data objects will be associated. /// </summary> /// <param name="network">The parent ParentNetwork for the data objects</param> public DataAttributeTableFactory(IAdjList network) { ParentNetwork = network; }
//P2 internal void ApplyGraphDataElement(string keyId, string itemStr, IAdjList network, IDictionary<string, Key> graphKeys) { Key key = null; if (graphKeys.TryGetValue(keyId, out key)) { if (key.Name==XMLConstants._KeyId_ForNetworkName && !string.IsNullOrEmpty(itemStr)) { network.Name = itemStr; } else network.Name = Path.GetFileNameWithoutExtension(File); } }
private void HookNodeDataToNetwork(IAdjList network, DataAttributeTable<INode> nodeData) { if(network is BasicAdjList) { var net = network as BasicAdjList; net.NodeData = nodeData; } }
private void HookEdgeDataToNetwork(IAdjList network, DataAttributeTable<IEdge> edgeData) { if (network is BasicAdjList) { var net = network as BasicAdjList; net.EdgeData = edgeData; } }
protected override void Dispose(bool disposing) { if (!_disposed) { try { if (disposing) { // Dispose managed resources here ParentNetwork = null; } // Release unmanaged resources here. } finally { // Call Dispose on your base class. base.Dispose(disposing); } } }
/// <summary> /// Reads and creates a INode, but does not read the node data. After this method is called, <see cref="NodeIdToIndexMap"/> /// will contain a mapping of the XML id of the node as specified in the file to the index of the INode in the network. /// </summary> /// <remarks>Assumes reader.Name == "node" and reader.IsStartElement() == true</remarks> internal void ReadNode1(out INode node, IAdjList network, XmlReader reader) { node = null; reader.MoveToAttribute("id"); string idStr = reader["id"]; reader.MoveToElement(); node = network.CreateNode(); NodeIdToIndexMap.Add(idStr, node.Index); CheckNodeChildElements(reader); //if (reader.ReadToDescendant("data")) // SkipData(reader); }
/// <summary> /// Creates an <see cref="IEdge"/> between the node with id <paramref name="srcNodeId"/> and the node with /// id <paramref name="destNodeId"/>. /// </summary> /// <param name="srcNodeId">The XML id of the src node</param> /// <param name="destNodeId">The XML id of the dest node</param> /// <param name="network"></param> /// <param name="reader"></param> internal void CreateEdge(out IEdge edge, string srcNodeId, string destNodeId, IAdjList network) { edge = null; // get the src and dest node indices from the ids int srcNodeIndex = NodeIdToIndexMap[srcNodeId]; int destNodeIndex = NodeIdToIndexMap[destNodeId]; INode srcNode = network.Nodes[srcNodeIndex]; INode destNode = network.Nodes[destNodeIndex]; edge = network.CreateEdge(srcNode, destNode); }
/// <summary> /// Reads the "node" element and using the XML id to node index mapping, retrieves the INode from <paramref name="network"/> /// and then reads any "data" elements below the "node" element and populates the appropriate node maps. /// </summary> /// <remarks>Assumes reader.Name == "node" and reader.IsStartElement() == true</remarks> internal void ReadNode2(IAdjList network, XmlReader reader) { reader.MoveToAttribute("id"); string idStr = reader["id"]; reader.MoveToElement(); INode node = network.Nodes[NodeIdToIndexMap[idStr]]; if (reader.ReadToDescendant("data")) { string mapKey; string itemStr; var nodeAttribsNet = (INodeAttributes)network; ReadDataElement(out mapKey, out itemStr, reader); MapNodeAttrib(mapKey, itemStr, node, nodeAttribsNet); while (reader.ReadToNextSibling("data")) { ReadDataElement(out mapKey, out itemStr, reader); MapNodeAttrib(mapKey, itemStr, node, nodeAttribsNet); } } }
/// <summary> /// Reads the "edge" element and using the XML id to edge index mapping, retrieves the IEdge from <paramref name="network"/> /// and then reads any "data" elements below the "edge" element and populates the appropriate edge maps. /// </summary> /// <remarks>Assumes reader.Name == "edge" and reader.IsStartElement() == true</remarks> internal void ReadEdge2(IAdjList network, XmlReader reader, int edgeCtr) { IEdge edge = EdgeCtrToEdgeMap[edgeCtr]; if (reader.ReadToDescendant("data")) { string mapKey; string itemStr; var edgeAttribsNet = (IEdgeAttributes)network; ReadDataElement(out mapKey, out itemStr, reader); MapEdgeAttrib(mapKey, itemStr, edge, edgeAttribsNet); while (reader.ReadToNextSibling("data")) { ReadDataElement(out mapKey, out itemStr, reader); MapEdgeAttrib(mapKey, itemStr, edge, edgeAttribsNet); } } }
internal void HookNodeDataToNetwork(IAdjList network, DataAttributeTable nodeData) { if(network is BasicAdjList) { var net = network as BasicAdjList; // net.NodeData = nodeData; } }
/// <summary> /// Reads the node and edge entries below the "graph" element. Nodes are created but edges are not, though "edge markers" are added /// to <see cref="EdgeMarkers"/> collection. /// </summary> /// <param name="network"></param> /// <param name="reader"></param> /// <remarks>Assumes reader.Name == "graph" and reader.IsStartElement() == true</remarks> internal void ReadGraphElements(IAdjList network, XmlReader reader) { if (reader.Name == "graph" && reader.IsStartElement() && !reader.IsEmptyElement) { INode node = null; int startDepth = reader.Depth; reader.Read(); // read down from the "graph" element do { if (reader.Name == "data") { string mapKey; string itemStr; ReadDataElement(out mapKey, out itemStr, reader); if (reader.Name == "data") // read if we are at the end data element reader.Read(); ApplyGraphDataElement(mapKey, itemStr, network, GraphKeys); } else if (reader.Name == "node") { ReadNode1(out node, network, reader); reader.Read(); } else if (reader.Name == "edge") { ReadEdgeMarker(reader); if (reader.Name == "edge") // read if we are at the end element reader.Read(); } else if (reader.Name == "hyperedge") throw new InvalidOperationException(string.Format("Hyperedges are currently not supported")); } while (reader.Depth > startDepth); } }
/// <summary> /// Creates and attatches to <paramref name="network"/> a DataAttributeTable /// for storing edge data attributes, with data columns initialized to <paramref name="columnNames"/> /// and <paramref name="columnTypes"/> /// </summary> /// <param name="network">The parent network.</param> /// <param name="columnNames">The unique names of columns to create.</param> /// <param name="columnTypes">The data type of each column in <paramref name="columnNames"/>.</param> /// <returns>DataAttributeTable</returns> public DataAttributeTable CreateEdgeDataTable(IAdjList network, string[] columnNames, Type[] columnTypes) { return CreateEdgeDataTable(network, columnNames, columnTypes, null); }
/// <summary> /// Reads the node and edge entries below the "graph" element /// to <see cref="EdgeMarkers"/> collection. /// </summary> /// <param name="network"></param> /// <param name="reader"></param> /// <remarks>Assumes reader.Name == "graph" and reader.IsStartElement() == true</remarks> internal void ReadGraphElements2(IAdjList network, XmlReader reader) { //if (reader.Name == "graph" && reader.IsStartElement() && !reader.IsEmptyElement) //{ // INode node = null; // int startDepth = reader.Depth; // reader.Read(); // read down from the "graph" element // do // { // if (reader.Name == "node") // { // ReadNode1(out node, network, reader); // reader.Read(); // } // else if (reader.Name == "edge") // { // ReadEdgeMarker(reader); // if (reader.Name == "edge") // read if we are at the end element // reader.Read(); // } // else if (reader.Name == "hyperedge") // throw new InvalidOperationException(string.Format("hyperedge elements are not supported")); // } while (reader.Depth > startDepth); //} }
internal int ExtractKthCoreFromAdjList(IAdjList network, int kValue) { int totalRemoved = 0; int removed = 0; do { removed = 0; INode node = null; for (int i = (network.NodeCount - 1); i >= 0; i--) { node = network.Nodes[i]; if (node.Degree < kValue) { network.RemoveNode(node); removed++; } } totalRemoved += removed; } while (removed > 0); return totalRemoved; }
internal void SetNodeLabels(IAdjList network, IList<string> labels) { var nodeAttribNet = (INodeAttributes)network; int colIndex = nodeAttribNet.NodeData.AddColumn("Node labels", typeof(string), ""); int ctr = 0; foreach (INode node in network.Nodes) { nodeAttribNet.NodeData.SetValue(node, colIndex, labels[ctr++]); } }