void dfs_DiscoverVertex(object sender, VertexEventArgs e) { TypeVertex v = (TypeVertex)e.Vertex; Trace.WriteLine("S: " + v.Name); Trace.Indent(); PendingEdges.Push(new List <TypeVertex>()); if (!destcg.ContainsVertex(v)) { CondensedVertex nv; TypeReference tref = v.TypeRef; string trefNamespace = v.Namespace; if (!NamespaceVertexMap.ContainsKey(trefNamespace)) { nv = (CondensedVertex)destcg.AddVertex(); nv.NameSpaces[trefNamespace] = 0; NamespaceVertexMap[trefNamespace] = new List <CondensedVertex>(); NamespaceVertexMap[trefNamespace].Add(nv); } else { nv = NamespaceVertexMap[trefNamespace][0]; } nv.NameSpaces[trefNamespace]++; nv.ContainedTypes.Add(tref); VertexReassignmentMap[v] = nv; return; } }
public void LoadClassDependencies(DetermineClassDeps dcd, bool treatExternalTypesAsSingleImutable) { foreach (TypeDefinition tdef in dcd.ClassDependencies.Keys) { string className = tdef.FullName; foreach (UsedClass uc in dcd.ClassDependencies[tdef]) { string refclassName = uc.Type.FullName; if (!String.IsNullOrEmpty(_examininationNSN)) { if (! ( (tdef.FullName.StartsWith(_examininationNSP) || tdef.FullName.StartsWith(_examininationNSN)) && (uc.Type.FullName.StartsWith(_examininationNSN) || uc.Type.FullName.StartsWith(_examininationNSP)) )) { continue; } } TypeVertex usingVertex = GetVertex(tdef, className, dcd); TypeVertex refVertex = GetVertex(uc.Type, refclassName, dcd); if (AllowParallel || !this.ContainsEdge(usingVertex, refVertex)) { NamedEdge edge = (NamedEdge)this.AddEdge(usingVertex, refVertex); edge.Name = uc.Use.ToString(); } } } }
void graphviz_FormatVertex(object sender, FormatVertexEventArgs e) { TypeVertex v = (TypeVertex)e.Vertex; e.VertexFormatter.Label = v.Name; e.VertexFormatter.Font = new Font(FontFamily.GenericSerif, 8); }
void dfs_TreeEdge(object sender, EdgeEventArgs e) { TypeVertex srcV = (TypeVertex)e.Edge.Source; TypeVertex targetV = (TypeVertex)e.Edge.Target; Trace.WriteLine("E: " + srcV.Name + " -> " + targetV.Name); PendingEdges.Peek().Add(targetV); }
public string GetAssemblyName(CondensedVertex vassem) { int count = 0; string baseName = NameHint; if (vassem.NameSpaces.Count == 1) { baseName = TypeVertex.GetNamespace(vassem); } nameHintCountMap.TryGetValue(baseName, out count); nameHintCountMap[baseName] = count + 1; return(String.Format("{0}.{1}", baseName, count)); }
public void Condense() { NameSpaces.Clear(); foreach (TypeReference tref in ContainedTypes) { string nmspace = TypeVertex.GetNamespace(tref); if (this.NameSpaces.ContainsKey(nmspace)) { this.NameSpaces[nmspace]++; } else { this.NameSpaces[nmspace] = 1; } } }
void dfs_FinishVertex(object sender, VertexEventArgs e) { TypeVertex v = (TypeVertex)e.Vertex; List <TypeVertex> pes = PendingEdges.Pop(); foreach (TypeVertex target in pes) { IVertex srcv = VertexReassignmentMap[v]; IVertex targetv = VertexReassignmentMap[target]; if (srcv != targetv && !destcg.ContainsEdge(srcv, targetv)) { destcg.AddEdge(srcv, targetv); } } Trace.Unindent(); Trace.WriteLine("F: " + v.Name); }
internal void Condense(IVertexCollection iVertexCollection) { foreach (TypeVertex v in iVertexCollection) { if (!this.ContainedTypes.Contains(v.TypeRef)) { this.ContainedTypes.Add(v.TypeRef); } string nmspace = TypeVertex.GetNamespace(v.TypeRef); if (this.NameSpaces.ContainsKey(nmspace)) { this.NameSpaces[nmspace]++; } else { this.NameSpaces[nmspace] = 1; } } }
void dfs_DiscoverVertex(object sender, VertexEventArgs e) { CondensedVertex v = (CondensedVertex)e.Vertex; Trace.WriteLine("S: " + v.Name); Trace.Indent(); PendingEdges.Push(new List <CondensedVertex>()); Debug.Assert(!destcg.ContainsVertex(v)); CondensedVertex nv; if (v.NameSpaces.Count > 1) // scc with mult NS { destcg.AddVertex(v); VertexReassignmentMap[v] = v; return; } string trefNamespace = TypeVertex.GetNamespace(v); if (!NamespaceVertexMap.ContainsKey(trefNamespace)) { nv = (CondensedVertex)destcg.AddVertex(); nv.NameSpaces[trefNamespace] = 0; NamespaceVertexMap[trefNamespace] = new List <CondensedVertex>(); NamespaceVertexMap[trefNamespace].Add(nv); } else { nv = NamespaceVertexMap[trefNamespace][0]; } nv.NameSpaces[trefNamespace]++; foreach (TypeReference tref in v.ContainedTypes) { nv.ContainedTypes.Add(tref); } VertexReassignmentMap[v] = nv; return; }
public TypeDependencyGraph TypeDepAnalysis(List <TypeReference> typesToExamine) { TypeDependencyGraph tdg = new TypeDependencyGraph(); StrongComponentsAlgorithm scgo = new StrongComponentsAlgorithm(this); scgo.Compute(); Dictionary <int, List <IVertex> > sccMap = new Dictionary <int, List <IVertex> >(); foreach (System.Collections.DictionaryEntry de in scgo.Components) { IVertex v = (IVertex)de.Key; int scc_id = (int)de.Value; if (!sccMap.ContainsKey(scc_id)) { sccMap[scc_id] = new List <IVertex>(); } sccMap[scc_id].Add(v); } Stack <List <TypeVertex> > PendingEdges = new Stack <List <TypeVertex> >(); List <TypeVertex> VertexToRemove = new List <TypeVertex>(); VertexEventHandler discV = delegate(Object s, VertexEventArgs e) { PendingEdges.Push(new List <TypeVertex>()); TypeVertex tv = (TypeVertex)e.Vertex; if (scgo.Components.Contains(tv) && sccMap[scgo.Components[tv]].Count > 1) { tv.SCCNum = scgo.Components[tv]; tdg.AddVertex(tv); } else if (typesToExamine.Contains(tv.TypeRef)) { tdg.AddVertex(tv); } else { VertexToRemove.Add(tv); } }; VertexEventHandler finishV = delegate(Object s, VertexEventArgs e) { TypeVertex tv = (TypeVertex)e.Vertex; List <TypeVertex> pes = PendingEdges.Pop(); if (tdg.ContainsVertex(tv)) { foreach (TypeVertex target in pes) { if (tdg.ContainsVertex(target) && !tdg.ContainsEdge(tv, target)) { tdg.AddEdge(tv, target); } } } }; EdgeEventHandler treeedge = delegate(Object o, EdgeEventArgs e) { PendingEdges.Peek().Add((TypeVertex)e.Edge.Target); }; DepthFirstSearchAlgorithm dfs = new DepthFirstSearchAlgorithm(this); dfs.DiscoverVertex += discV; dfs.FinishVertex += finishV; dfs.TreeEdge += treeedge; dfs.Compute(); foreach (TypeVertex v in VertexToRemove) { this.RemoveVertex(v); } return(tdg); }
public void CompactNS(bool agressive) { List <CondensedVertex> allvertex = new List <CondensedVertex>(); foreach (CondensedVertex v in this.Vertices) { allvertex.Add(v); } foreach (CondensedVertex v in allvertex) { if (v.NameSpaces.Count > 1 || v.ImutableExternalType) { continue; //throw new NotImplementedException(); } int numVertex = this.VerticesCount; Debug.Assert(numVertex == SCCCount); string ns = TypeVertex.GetNamespace(v); List <CondensedVertex> candidateDestVertexs; if (!NamespaceAssignmentMap.TryGetValue(ns, out candidateDestVertexs)) { candidateDestVertexs = new List <CondensedVertex>(); NamespaceAssignmentMap[ns] = candidateDestVertexs; } bool vertexReasigned = false; foreach (CondensedVertex destV in candidateDestVertexs) { vertexReasigned = TryMergeVertex(v, destV, ref numVertex); if (vertexReasigned) { break; } } if (!vertexReasigned && agressive) { foreach (List <CondensedVertex> listC in NamespaceAssignmentMap.Values) { if (listC != candidateDestVertexs && !vertexReasigned) { foreach (CondensedVertex destV in listC) { vertexReasigned = TryMergeVertex(v, destV, ref numVertex); if (vertexReasigned) { break; } } } } } if (!vertexReasigned) { numVertex++; CondensedVertex destV = this.AddVertex() as CondensedVertex; NamespaceAssignmentMap[ns].Add(destV); vertexReasigned = TryMergeVertex(v, destV, ref numVertex); Debug.Assert(vertexReasigned); } } Debug.Assert(this.VerticesCount == SCCCount); Debug.WriteLine(VerticesCount.ToString()); }
private string RenderAnalysisGraph(TypeDependencyGraph tdg, GraphvizImageType imageType, string fileName) { string output; GraphvizAlgorithm renderer; renderer = new GraphvizAlgorithm(tdg); renderer.ImageType = imageType; renderer.GraphFormat.RankDirection = GraphvizRankDirection.LR; Color[] colors = { Color.Beige, Color.Cornsilk, Color.DimGray, Color.Khaki, Color.PeachPuff, Color.Wheat, Color.Olive, Color.Moccasin, Color.LightCoral, Color.LightGoldenrodYellow, Color.LightGray, Color.LightGreen, Color.LightPink, Color.LightSalmon, Color.LightSeaGreen, Color.LightSkyBlue, Color.LightSlateGray, Color.LightSteelBlue, Color.LightYellow, Color.Lime, Color.MediumAquamarine, Color.MediumBlue, Color.MediumOrchid, Color.MediumPurple, Color.MediumSeaGreen, Color.MediumSlateBlue, Color.MediumSpringGreen, Color.MediumTurquoise, Color.MediumVioletRed, Color.MintCream, }; int nextColorInd = 0; Dictionary <int, Color> colormap = new Dictionary <int, Color>(); FormatVertexEventHandler fvertex = delegate(Object s, FormatVertexEventArgs args) { TypeVertex v = (TypeVertex)args.Vertex; args.VertexFormatter.Label = v.Name; args.VertexFormatter.Font = new Font(FontFamily.GenericSerif, 8); if (v.SCCNum >= 0) { Color c; if (!colormap.TryGetValue(v.SCCNum, out c)) { if (nextColorInd > colors.GetUpperBound(0)) { nextColorInd = 0; } c = colors[nextColorInd++]; colormap[v.SCCNum] = c; } args.VertexFormatter.FillColor = c; args.VertexFormatter.Style = GraphvizVertexStyle.Filled; } }; FormatEdgeEventHandler Fedge = delegate(Object s, FormatEdgeEventArgs args) { args.EdgeFormatter.Head = new GraphvizEdgeExtremity(true); args.EdgeFormatter.HeadArrow = new GraphvizArrow(GraphvizArrowShape.Dot); }; renderer.FormatVertex += fvertex; renderer.FormatEdge += Fedge; output = renderer.Write(fileName); return(output); }