private Graph BuildGraph(IBSharpContext context) { var result = new Graph { RankDir = RankDirType.RL, Label = "Графическая структура проекта", DefaultNode = new Node { FontName = "Consolas", Shape = NodeShapeType.Box3d, FontSize = 8, Style = NodeStyleType.Filled }, DefaultEdge = new Node { FontName = "Consolas", FontSize = 8 }, }; var visited= new List<IBSharpClass>(); if (null != ((BSharpContext) context).Dictionaries) { foreach (var e in ((BSharpContext) context).Dictionaries) { var node = new Node {Code = "d" + e.Key, Label = e.Key,SubgraphCode = "dicts",Shape = NodeShapeType.Mcircle}; result.AddNode(node); foreach (var i in e.Value) { result.AddEdge(new Edge {From = i.cls.FullName, To = "d" + e.Key, ArrowHead = ArrowType.Curve}); } } } foreach (var c in context.Get(BSharpContextDataType.Working)) { BuildClass(result,context, (BSharpClass)c, visited); } result.AutoTune(); return result; }
public void CalculateEdgeInWeight() { var graph = new Graph(); var nodes = new[] {new Node(), new Node(), new Node(), new Node(), new Node(), new Node() }; nodes[0].Code = "1"; nodes[1].Code = "2"; nodes[2].Code = "3"; nodes[3].Code = "4"; nodes[4].Code = "5"; nodes[5].Code = "6"; foreach (var node in nodes) { graph.AddNode(node); } var edge1to2 = Edge.Create(nodes[0].Code, nodes[1].Code); var edge2to3 = Edge.Create(nodes[1].Code, nodes[2].Code); var edge6to3 = Edge.Create(nodes[5].Code, nodes[2].Code); var edge4to5 = Edge.Create(nodes[3].Code, nodes[4].Code); var edge5to3 = Edge.Create(nodes[4].Code, nodes[2].Code); graph.AddEdge(edge1to2); graph.AddEdge(edge2to3); graph.AddEdge(edge6to3); graph.AddEdge(edge4to5); graph.AddEdge(edge5to3); var ws = graph.CalculateWeight(); var zeros = ws.Count(_ => _.Value == 0); var fives = ws.Count(_ => _.Value == 5); var ones = ws.Count(_ => _.Value == 1); Assert.AreEqual(7, ws.Sum(_ => _.Value)); Assert.AreEqual(3, zeros); Assert.AreEqual(2, ones); Assert.AreEqual(1, fives); foreach (var w in ws) { Debug.Print(w.Key + " = " + w.Value); } }
public void CalculateEdgeInWeightWithRecycles() { var graph = new Graph(); var nodes = new[] { new Node(), new Node(), new Node(), new Node(), new Node(), new Node() }; nodes[0].Code = "1"; nodes[1].Code = "2"; nodes[2].Code = "3"; nodes[3].Code = "4"; nodes[4].Code = "5"; nodes[5].Code = "6"; foreach (var node in nodes) { graph.AddNode(node); } var edge3to1 = Edge.Create(nodes[2].Code, nodes[0].Code); var edge1to2 = Edge.Create(nodes[0].Code, nodes[1].Code); var edge2to3 = Edge.Create(nodes[1].Code, nodes[2].Code); var edge6to3 = Edge.Create(nodes[5].Code, nodes[2].Code); var edge4to5 = Edge.Create(nodes[3].Code, nodes[4].Code); var edge5to3 = Edge.Create(nodes[4].Code, nodes[2].Code); graph.AddEdge(edge3to1); graph.AddEdge(edge1to2); graph.AddEdge(edge2to3); graph.AddEdge(edge6to3); graph.AddEdge(edge4to5); graph.AddEdge(edge5to3); var ws = graph.CalculateWeight(); Assert.AreEqual(3, ws.Count(_ => _.Value == 1)); Assert.AreEqual(1, ws.Count(_ => _.Value == 3)); Assert.AreEqual(6, ws.Sum(_ => _.Value)); foreach (var w in graph.CalculateWeight()) { Debug.Print(w.Key + " = " + w.Value); } }
private void BuildClass(Graph g, IBSharpContext ctx, BSharpClass cls, IList<IBSharpClass> visited) { if (visited.Contains(cls)) return; visited.Add(cls); var label = cls.Name; if (!string.IsNullOrWhiteSpace(cls.Prototype)) { label = "[" + cls.Prototype + "]\r\n" + cls.Name; } var n = new Node {Code = cls.FullName, Label = label}; if (!string.IsNullOrWhiteSpace(cls.Namespace)) { n.SubgraphCode = (cls.Namespace.Replace(".", "__")) + "__"; } CheckoutNamespace(g,cls.Namespace); if (cls.Is(BSharpClassAttributes.Abstract)) { n.Shape = NodeShapeType.Box; n.FillColor = Color.Yellow; } g.AddNode(n); if (null != cls.DefaultImport) { g.AddEdge(new Edge {From = cls.FullName, To = cls.DefaultImport.FullName,Label = "0"}); BuildClass(g,ctx,(BSharpClass)cls.DefaultImport,visited); } int idx = 1; foreach (var i in cls.SelfImports) { g.AddEdge(new Edge {From = cls.FullName, To = i.Target.FullName,Label = (idx++).ToString()}); BuildClass(g, ctx, (BSharpClass)i.Target, visited); } foreach (var i in cls.IncludedClasses) { g.AddEdge(new Edge { From = cls.FullName, To = i.FullName, ArrowHead =ArrowType.Diamond}); BuildClass(g, ctx, (BSharpClass)i, visited); } foreach (var i in cls.ReferencedClasses) { g.AddEdge(new Edge { From = cls.FullName, To = i.FullName, ArrowHead = ArrowType.Vee,Color = Color.Blue}); BuildClass(g, ctx, (BSharpClass)i, visited); } foreach (var i in cls.LateIncludedClasses) { g.AddEdge(new Edge { From = cls.FullName, To = i.FullName, ArrowHead = ArrowType.ODiamond, Color = Color.Blue }); BuildClass(g, ctx, (BSharpClass)i, visited); } foreach (var i in cls.ReferencedDictionaries) { g.AddEdge(new Edge { From = cls.FullName, To = "d"+i, ArrowHead = ArrowType.Inv, Color = Color.Blue }); } }