public UserGraph BuildGraph(string rootUser) { var graph = new UserGraph(); // HACK! using (var conn = ConnectionFactory.Create()) { conn.Open(); // force things to only 2 levels... // trasa has 193 friends (1st degree) // those friends have 248,634 friends (2nd degree contacts) // those friends have well over 12 million friends (3rd degree, and the query times out before it can finish) #region sql = "Select.."; const string sql = @"select UserName, IsFriendOf, 1 as Degree from userfriends where UserName = @user UNION select UserName, IsFriendOf, 2 as Degree from userfriends where UserName in (select IsFriendOf from userfriends where UserName = @user)"; #endregion using (var cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@user", rootUser); using (var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { while (dr.Read()) { graph.Add(dr.GetString(0), dr.GetString(1)); } } } return graph; } }
public void Build_Simplest_Graph() { var graph = new UserGraph(); graph.Add("root", "root"); string result = ServiceUnderTest.BuildGraphML(graph); Console.WriteLine(result); }
public void Simple_Graph() { var graph = new UserGraph(); graph.Add("test", "test"); Assert.That(graph.Users, Has.Count(1)); Assert.That(graph.Edges.Count(), Is.EqualTo(1)); }
public void Build_Simplest_Graph_With_Edges() { const string root = "test"; const string friend = "friend"; var graph = new UserGraph(); graph.Add(root, friend); string result = ServiceUnderTest.BuildGraphML(graph); Console.WriteLine(result); }
public void Two_Levels() { var root = "test"; var friend = "friend"; var otherFriend = "otherfriend"; var graph = new UserGraph(); graph.Add(root, friend); graph.Add(friend, otherFriend); Assert.That(graph.Users, Has.Count(3)); Assert.That(graph.Edges.Count(), Is.EqualTo(2)); }
public void One_Edge() { var root = "test"; var friend = "friend"; var graph = new UserGraph(); graph.Add(root, friend); Assert.That(graph.Users, Has.Count(2)); Assert.That(graph.Users.Contains(root)); Assert.That(graph.Users.Contains(friend)); Assert.That(graph.Edges.Count(), Is.EqualTo(1)); Assert.That(graph.Edges.First().Source, Is.EqualTo(root)); Assert.That(graph.Edges.First().Target, Is.EqualTo(friend)); }
public string BuildGraphML(UserGraph graphData) { var doc = new XmlDocument(); var graphml = doc.CreateElement("graphml", "http://graphml.graphdrawing.org/xmlnls"); doc.AppendChild(graphml); var graph = doc.CreateElement("graph"); graphml.AppendChild(graph); var graphAttr = doc.CreateAttribute("edgedefault"); graphAttr.Value = "unidirected"; graph.Attributes.Append(graphAttr); // keys var key = doc.CreateElement("key"); var attrId = doc.CreateAttribute("id"); attrId.Value = "name"; key.Attributes.Append(attrId); var attrFor = doc.CreateAttribute("for"); attrFor.Value = "node"; key.Attributes.Append(attrFor); var attrName = doc.CreateAttribute("attr.name"); attrName.Value = "name"; key.Attributes.Append(attrName); var attrType = doc.CreateAttribute("attr.type"); attrType.Value = "string"; key.Attributes.Append(attrType); graph.AppendChild(key); foreach(string userName in graphData.Users) { var node = doc.CreateElement("node"); var id = doc.CreateAttribute("id"); id.Value = userName; node.Attributes.Append(id); var data = doc.CreateElement("data"); var k = doc.CreateAttribute("key"); k.Value = "name"; data.Attributes.Append(k); data.InnerText= userName; node.AppendChild(data); graph.AppendChild(node); } foreach(Edge e in graphData.Edges) { var edge = doc.CreateElement("edge"); var source = doc.CreateAttribute("source"); source.Value = e.Source; var target = doc.CreateAttribute("target"); target.Value = e.Target; edge.Attributes.Append(source); edge.Attributes.Append(target); graph.AppendChild(edge); } return doc.OuterXml; }