/// <summary> /// Parses a particular string format to a network. Format is /// "n,e,d|pair;pair;pair" where n = node count, e = edge count, d = true/false for directedness and /// pair is a number pair of node indices indicating an edges. Ex: "5,3,false|0,1;1,2;3,5" *note that the /// number of pairs should match the number of edges /// </summary> public static IBasicAdjList ParseToNetwork(string input, bool enableNodeData, bool enableEdgeData) { // n,e,directed|pair; // 7,23,false|0,3;0,2;0,4;1,4;1,2;1,3;2,5;2,4;2,4;3,5;3,1;3,6;3,0;4,4;4,5;4,3;5,1;5,1;5,4;5,6;5,0;6,3;6,1; IBasicAdjList network = null; var split1 = input.Split(new[] {'|'}); var split2 = split1[0].Split(new[] { ',' }); int n = int.Parse(split2[0]); int m = int.Parse(split2[1]); bool directed = bool.Parse(split2[2]); var split3 = split1[1].Split(new[] { ';' }); // pairs if(split3.Length != m) throw new ArgumentException(string.Format("The number of pairs {0} did not match the number of edges {1} in {2}", split3.Length, m, input)); INode a = null; INode b = null; using (var fac = new BasicAdjListFactory()) { network = new BasicAdjList(Guid.NewGuid(), directed); for(int i=0; i<n; i++) network.CreateNode(); } string[] pair = null; int indexA = -1; int indexB = -1; for (int i = 0; i < split3.Length; i++ ) { pair = split3[i].Split(new[] { ',' }); indexA = int.Parse(pair[0]); indexB = int.Parse(pair[1]); a = network.Nodes[indexA]; b = network.Nodes[indexB]; network.CreateEdge(a, b); } using (var fac = new DataAttributeTableFactory(network)) { if (enableNodeData) fac.CreateNodeDataTable(); if (enableEdgeData) fac.CreateEdgeDataTable(); } return network; }
public void CreateNodeDataTable_With_4_Params_Assigns_Table_To_Network_As_NodeData() { var net = BasicAdjListGenerator.GenerateAdjList(0, 0, false) as BasicAdjList; var fac = new DataAttributeTableFactory(); _table = fac.CreateNodeDataTable(net, new string[] { "a" }, new Type[] { typeof(int) }, new object[]{3}); Assert.AreSame(net.NodeData, _table, "Table and NodeData ref should match"); }
public void CreateNodeDataTable_Throws_Ex_If_Network_Is_Null() { var fac = new DataAttributeTableFactory(); _table = fac.CreateNodeDataTable(null); }
public void CreateNodeDataTable_Creates_NodeData_With_Primary_Key_Of_Node_Index(int nodeCount, string[] colNames, Type[] colTypes, object[] defaultVals) { IBasicAdjList net = BasicAdjListGenerator.GenerateAdjList(nodeCount, 0, false); var fac = new DataAttributeTableFactory(); _table = fac.CreateNodeDataTable(net, colNames, colTypes, defaultVals); Assert.IsNotNull(_table, "Table should not be null"); Assert.IsNotNull(_table.Network, "Network is not null"); Assert.AreSame(_table.Network, net, "Network ref should match expected"); Assert.AreEqual(_table.Columns.Count, colNames.Length + 1, "column count should match expected"); Assert.AreEqual(_table.Columns["Index"].DataType, typeof(int), "Datatype of keys col should match expected"); Assert.AreEqual(_table.Rows.Count, nodeCount, "Rowcount should match expected"); DataColumn col = null; for (int i = 1; i < _table.Columns.Count; i++) { col = _table.Columns[i]; Assert.AreEqual(col.ColumnName, colNames[i - 1], string.Format("Col name {0} should match expected", i)); Assert.AreEqual(col.DataType, colTypes[i - 1], string.Format("Col type {0} should match expected", i)); Assert.AreEqual(col.DefaultValue, defaultVals[i - 1], string.Format("Col defaults {0} should match expected", i)); } }
public void CreateNodeDataTable_Creates_NodeData_With_Primary_Key_Of_Node_Index(int nodeCount) { IBasicAdjList net = BasicAdjListGenerator.GenerateAdjList(nodeCount, 0, false); var fac = new DataAttributeTableFactory(); _table = fac.CreateNodeDataTable(net); Assert.IsNotNull(_table, "Table should not be null"); Assert.IsNotNull(_table.Network, "Network is not null"); Assert.AreSame(_table.Network, net, "Network ref should match expected"); Assert.AreEqual(_table.Columns.Count, 1, "column count should match expected"); Assert.AreEqual(_table.Columns["Index"].DataType, typeof(int), "Datatype of keys col should match expected"); Assert.AreEqual(_table.Rows.Count, nodeCount, "Rowcount should match expected"); }
public void CreateNodeDataTable_Assigns_Table_To_Network_As_NodeData() { var net = BasicAdjListGenerator.GenerateAdjList(0, 0, false) as BasicAdjList; var fac = new DataAttributeTableFactory(); _table = fac.CreateNodeDataTable(net); Assert.AreSame(net.NodeData, _table, "Table and NodeData ref should match"); }
public void CreateNodeDataTable_Creates_NodeData_With_Map_Of_Nodes_To_Rows_Where_Attrib_Table_Is_Empty(int nodeCount) { //Arrange IBasicAdjList net = BasicAdjListGenerator.GenerateAdjList(nodeCount, 0, false); var fac = new DataAttributeTableFactory(net); //Act DataAttributeTable<INode> table = fac.CreateNodeDataTable(); //Assert Assert.NotNull(table); Assert.NotNull(table.Network); Assert.Same(table.Network, net); Assert.Equal(table.Columns.Count, 0); Assert.Equal(table.Rows.Count, nodeCount); Assert.NotNull(table.RowOwnerMap); Assert.Equal(table.RowOwnerMap.Count, nodeCount); }
public void CreateNodeDataTable_With_4_Params_Throws_Ex_If_Network_Is_Null() { var fac = new DataAttributeTableFactory(); _table = fac.CreateNodeDataTable(null, new string[] { "a" }, new Type[] { typeof(int) }, new object[] {-3}); }
public void CreateNodeDataTable_With_4_Params_Throws_Ex_If_Network_Is_Null() { //Arrange var fac = new DataAttributeTableFactory(null); //Act, Assert var ex = Assert.Throws<ArgumentNullException>(() => fac.CreateNodeDataTable(new string[] { "a" }, new Type[] { typeof(int) }, new object[] { -3 })); }
public void CreateNodeDataTable_With_4_Params_Assigns_Table_To_Network_As_NodeData() { //Arrange var net = BasicAdjListGenerator.GenerateAdjList(0, 0, false) as BasicAdjList; var fac = new DataAttributeTableFactory(net); //Act DataAttributeTable<INode> table = fac.CreateNodeDataTable(new string[] { "a" }, new Type[] { typeof(int) }, new object[] { 3 }); //Assert Assert.Same(net.NodeData, table); }
public void CreateNodeDataTable_With_4_Params_Throws_Ex_If_Lengths_Do_Not_Match(string[] colNames, Type[] colTypes, object[] defaultVals) { //Arrange var mockNet = new Mock<IAdjList>(); var fac = new DataAttributeTableFactory(mockNet.Object); //Act, Assert var ex = Assert.Throws<ArgumentException>(() => fac.CreateNodeDataTable(colNames, colTypes, defaultVals)); }
public void CreateNodeDataTable_With_3_Params_Throws_Ex_If_Any_Param_Array_Is_Null(string[] colNames, Type[] colTypes) { //Arrange var mockNet = new Mock<IAdjList>(); var fac = new DataAttributeTableFactory(mockNet.Object); //Act, Assert var ex = Assert.Throws<ArgumentNullException>(() => fac.CreateNodeDataTable(colNames, colTypes)); }
public void CreateNodeDataTable_Throws_Ex_If_Network_Is_Null() { //Arrange var fac = new DataAttributeTableFactory(null); //Act, Assert var ex = Assert.Throws<ArgumentNullException>(() => fac.CreateNodeDataTable()); }
public void CreateNodeDataTable_Creates_NodeData_With_Map_Of_Nodes_To_Rows_With_2_Cols(int nodeCount, string[] colNames, Type[] colTypes, object[] defaultVals) { //Arrange IBasicAdjList net = BasicAdjListGenerator.GenerateAdjList(nodeCount, 0, false); var fac = new DataAttributeTableFactory(net); //Act DataAttributeTable<INode> table = fac.CreateNodeDataTable( colNames, colTypes, defaultVals); //Assert Assert.NotNull(table); Assert.NotNull(table.Network); Assert.Same(table.Network, net); Assert.Equal(table.Columns.Count, colNames.Length); Assert.Equal(table.Rows.Count, nodeCount); DataColumn col = null; for (int i = 0; i < table.Columns.Count; i++) { col = table.Columns[i]; Assert.Equal(col.ColumnName, colNames[i]); Assert.Equal(col.DataType, colTypes[i]); Assert.Equal(col.DefaultValue, defaultVals[i]); } }
public void CreateNodeDataTable_With_4_Params_Throws_Ex_If_Any_Param_Array_Is_Null_But_Not_DefaultVals_Array(string[] colNames, Type[] colTypes, object[] defaultVals) { var mockNet = new Mock<IAdjList>(); var fac = new DataAttributeTableFactory(); _table = fac.CreateNodeDataTable(mockNet.Object, colNames, colTypes, defaultVals); }
/// <summary> /// Creates an IBasicAdjList with n nodes, m edges, and nodeDataAttribs, edgeDataAttribs determine /// if it can hold data attribustes /// </summary> /// <param name="n">number of nodes</param> /// <param name="m">number of edges</param> /// <param name="directed">directed or not</param> /// <param name="enableNodeDataAttribs">if true, add node data attrib capability</param> /// <param name="enableEdgeDataAttribs">if true, add edge data attrib capability</param> /// <returns></returns> public static IBasicAdjList GenerateAdjListWithDataAttribs(int n, int m, bool directed, bool enableNodeDataAttribs, bool enableEdgeDataAttribs) { var rgg = new RandomGeneratorBasicAdjList(); rgg.NodeCount = n; rgg.EdgeCount = m; rgg.AllowCycles = AllowCycles; rgg.AllowSelfLoops = AllowSelfLoops; rgg.AllowMultiEdges = AllowMultiEdges; rgg.IsDirected = directed; BasicAdjList net = (BasicAdjList)rgg.Generate(NextId); using (var fac = new DataAttributeTableFactory(net)) { if (enableNodeDataAttribs) fac.CreateNodeDataTable(); if (enableEdgeDataAttribs) fac.CreateEdgeDataTable(); } GarunteeEdgeCount(net, m); return net; }
public void CreateNodeDataTable_With_4_Params_Throws_Ex_If_Lengths_Do_Not_Match(string[] colNames, Type[] colTypes, object[] defaultVals) { var mockNet = new Mock<IAdjList>(); var fac = new DataAttributeTableFactory(); _table = fac.CreateNodeDataTable(mockNet.Object, colNames, colTypes, defaultVals); }
public static IBasicAdjList GenerateAdjListWithDataAttribs(List<Point> edgeList, bool directed, bool enableNodeDataAttribs, bool enableEdgeDataAttribs) { var q = ((from p in edgeList select p.X) .Union((from p in edgeList select p.Y))).Distinct(); var nodeIndices = q.ToList(); nodeIndices.Sort(); var net = new BasicAdjList(Guid.NewGuid(), directed); for (int i = 0; i < nodeIndices.Count; i++) net.CreateNode(); INode srcNode = null; INode targNode = null; for (int i = 0; i < edgeList.Count; i++) { srcNode = net.Nodes[edgeList[i].X]; targNode = net.Nodes[edgeList[i].Y]; net.CreateEdge(srcNode, targNode); } using (var fac = new DataAttributeTableFactory(net)) { if (enableNodeDataAttribs) fac.CreateNodeDataTable(); if (enableEdgeDataAttribs) fac.CreateEdgeDataTable(); } return net; }
public void Copy_For_Nodes_For_Empty_Network(int numNodes, int numEdges, bool isDirected, bool selfLoops, bool multiEdges, int[] intVals, double[] doubleVals, bool[] boolVals, string[] stringVals) { //Arrange var copier = new DataAttributeTableCopier(); #region Create src and target networks IBasicAdjList src = BasicAdjListGenerator.GenerateAdjListWithDataAttribs(numNodes, numEdges, isDirected, true, false); var builder = new DataAttributesBuilder(); var colIndex = builder.AddColumn((INodeAttributes)src, typeof(int)); builder.PopulateCol<int, INode>(src.NodeData, colIndex, intVals); colIndex = builder.AddColumn((INodeAttributes)src, typeof(double)); builder.PopulateCol<double, INode>(src.NodeData, colIndex, doubleVals); colIndex = builder.AddColumn((INodeAttributes)src, typeof(bool)); builder.PopulateCol<bool, INode>(src.NodeData, colIndex, boolVals); colIndex = builder.AddColumn((INodeAttributes)src, typeof(string)); builder.PopulateCol<string, INode>(src.NodeData, colIndex, stringVals); // create a copy of src network and enable node data attribs IBasicAdjList targ = new BasicAdjList(Guid.NewGuid(), isDirected); foreach (var srcNode in src.Nodes) { targ.CreateNode(); } INode a; INode b; foreach (var srcEdge in src.Edges) { a = targ.Nodes[srcEdge.SourceNode.Index]; b = targ.Nodes[srcEdge.DestinationNode.Index]; targ.CreateEdge(a, b); } using (var fac = new DataAttributeTableFactory(targ)) { fac.CreateNodeDataTable(); } #endregion //Act copier.Copy(src.NodeData, targ.NodeData); //Assert Assert.NotNull(targ.NodeData); Assert.Equal(src.NodeData.ColumnCount, targ.NodeData.ColumnCount); Assert.Equal(numNodes, targ.NodeData.RowCount); for (int i = 0; i < targ.NodeDataAttributeCount; i++) { Assert.Equal(src.NodeData.DataTypes[i], targ.NodeData.DataTypes[i]); Assert.Equal(src.NodeData.ColumnNames[i], targ.NodeData.ColumnNames[i]); Assert.Equal(src.NodeData.ColumnDefaultValues[i], targ.NodeData.ColumnDefaultValues[i]); } }
public void CreateNodeDataTable_Assigns_Table_To_Network_As_NodeData() { //Arrange var net = BasicAdjListGenerator.GenerateAdjList(0, 0, false) as BasicAdjList; var fac = new DataAttributeTableFactory(net); //Act DataAttributeTable<INode> table = fac.CreateNodeDataTable(); Assert.Same(net.NodeData, table); }