private bool TryMergeVertex(CondensedVertex v, CondensedVertex destV, ref int numVertex) { List <KeyValuePair <IVertex, IVertex> > oldEdges = SaveExistingEdges(v); this.RemoveVertex(v); numVertex--; List <IEdge> pendingEdges = MergeOldEdges(v, destV, oldEdges); if (numVertex > SCCCount) { UndoMerge(v, destV, oldEdges, pendingEdges); numVertex++; Debug.Assert(numVertex == this.VerticesCount); Debug.Assert(numVertex == SCCCount); return(false); } else { foreach (TypeReference tref in v.ContainedTypes) { destV.ContainedTypes.Add(tref); } destV.Condense(); return(true); } }
private List <KeyValuePair <IVertex, IVertex> > SaveExistingEdges(CondensedVertex v) { List <KeyValuePair <IVertex, IVertex> > edgeTbl = new List <KeyValuePair <IVertex, IVertex> >(); // Dictionary<IVertex, IVertex> edgeTbl = new Dictionary<IVertex,IVertex>(); EdgeCollection outboundEdges = this.OutEdges(v); foreach (IEdge e in outboundEdges) { edgeTbl.Add(new KeyValuePair <IVertex, IVertex>(e.Source, e.Target)); //CondensedVertex srcV = (CondensedVertex)e.Source; //CondensedVertex targetV = (CondensedVertex)e.Target; //Trace.WriteLine("E: " + srcV.Name + " -> " + targetV.Name); } FilteredEdgeEnumerable inboundEdges = this.SelectEdges(new IsInEdgePredicate(v)); //Debug.WriteLine("inboundEdges"); foreach (IEdge e in inboundEdges) { edgeTbl.Add(new KeyValuePair <IVertex, IVertex>(e.Source, e.Target)); //CondensedVertex srcV = (CondensedVertex)e.Source; //CondensedVertex targetV = (CondensedVertex)e.Target; //Trace.WriteLine("E: " + srcV.Name + " -> " + targetV.Name); } return(edgeTbl); }
private void WriteCompileItemGroup(XmlTextWriter xproj, CondensedVertex va) { Dictionary <string, int> ListedFiles = new Dictionary <string, int>(); xproj.WriteComment("Source files"); xproj.WriteStartElement("ItemGroup"); foreach (TypeReference tref in va.ContainedTypes) { foreach (string fullPath in GetFilesForType(tref.FullName)) { if (ListedFiles.ContainsKey(fullPath)) { continue; } CondensedVertex belongsToVetex = va; if (fileProjectMap.TryGetValue(fullPath, out belongsToVetex)) { //TODO: replace with exception or Log.Error Debug.WriteLine("Warning, file belong to another project"); } else { fileProjectMap[fullPath] = va; } ListedFiles[fullPath] = 1; CheckNameMatchesFile(fullPath, tref); xproj.WriteStartElement("Compile"); xproj.WriteAttributeString("Include", fullPath); xproj.WriteEndElement(); } } xproj.WriteEndElement(); }
public static string GetNamespace(CondensedVertex v) { if (v.NameSpaces.Count > 1) { throw new ArgumentOutOfRangeException("Too many namespaces"); } return(GetNamespace(v.ContainedTypes[0])); }
void dfs_TreeEdge(object sender, EdgeEventArgs e) { CondensedVertex srcV = (CondensedVertex)e.Edge.Source; CondensedVertex targetV = (CondensedVertex)e.Edge.Target; Trace.WriteLine("E: " + srcV.Name + " -> " + targetV.Name); PendingEdges.Peek().Add(targetV); }
public static string GetNamespace(CondensedVertex v) { if (v.NameSpaces.Count > 1) { throw new ArgumentOutOfRangeException("Too many namespaces"); } return GetNamespace(v.ContainedTypes[0]); }
private void UndoMerge(CondensedVertex v, CondensedVertex destV, List <KeyValuePair <IVertex, IVertex> > oldEdges, List <IEdge> pendingEdges) { foreach (IEdge e in pendingEdges) { this.RemoveEdge(e); } this.AddVertex(v); foreach (KeyValuePair <IVertex, IVertex> e in oldEdges) { this.AddEdge(e.Key, e.Value); } }
private void WriteAssemRefsItemGroup(XmlTextWriter xproj, CondensedVertex va) { xproj.WriteComment("Assembly Refs"); xproj.WriteStartElement("ItemGroup"); foreach (CondensedVertex projRef in Graph.AdjacentVertices(va)) { if (projRef.ImutableExternalType) { xproj.WriteStartElement("Reference"); string aRefShortName = ""; string aRefFQN = DetermineClassDeps.AssemblyNameofType(projRef.ContainedTypes[0]); if (aRefFQN.StartsWith("mscorlib")) { aRefFQN = "System,"; } int idx = aRefFQN.IndexOf(","); if (idx > 0) { aRefShortName = aRefFQN.Substring(0, idx); } if (aRefFQN.StartsWith("System")) { xproj.WriteAttributeString("Include", aRefShortName); } else { xproj.WriteAttributeString("Include", aRefFQN); string[] exts = new string[2] { ".dll", ".exe" }; foreach (string ext in exts) { string hintPath = Path.Combine(Path.GetDirectoryName(AssemblyFileName), aRefShortName) + ext; if (File.Exists(hintPath)) { xproj.WriteElementString("SpecificVersion", false.ToString()); //todo - manage directories better xproj.WriteElementString("HintPath", Path.Combine("..", hintPath)); break; } } } xproj.WriteEndElement(); } } xproj.WriteEndElement(); }
private void CreateProjectFile(CondensedVertex va) { if (va.ImutableExternalType || va.AssemblyName.StartsWith("<")) { return; } XmlTextWriter xproj = new XmlTextWriter(Path.Combine(OutputDirectory, va.ProjectFile), null); xproj.Formatting = Formatting.Indented; xproj.WriteStartDocument(); xproj.WriteStartElement("Project"); xproj.WriteAttributeString("DefaultTargets", "Build"); xproj.WriteAttributeString("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003"); xproj.WriteStartElement("PropertyGroup"); /* <Configuration Condition=" ">Debug</Configuration> * <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> */ xproj.WriteStartElement("Configuration"); xproj.WriteAttributeString("Condition", "'$(Configuration)'==''"); xproj.WriteString("Debug"); xproj.WriteEndElement(); xproj.WriteStartElement("Platform"); xproj.WriteAttributeString("Condition", "'$(Platform)'==''"); xproj.WriteString("AnyCPU"); xproj.WriteEndElement(); xproj.WriteElementString("OutputType", "Library"); xproj.WriteElementString("OutputPath", @"bin\Debug"); xproj.WriteElementString("AssemblyName", va.AssemblyName); // xproj.WriteElementString("NoWarn", "1570,1571,1572,1573,1574,1584,1587,1591"); xproj.WriteEndElement(); //PropGroup WriteProjectImport(xproj, @"$(MSBuildBinPath)\Microsoft.CSharp.targets"); WriteAssemRefsItemGroup(xproj, va); WriteProjectRefsItemGroup(xproj, va); WriteCompileItemGroup(xproj, va); WriteEmbeddedResourceItemGroup(xproj, va); xproj.WriteEndElement(); //Project xproj.WriteEndDocument(); xproj.Close(); }
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 CGVertexHandler(object sender, CondensationGraphVertexEventArgs arg) { CondensedVertex cgv = arg.CondensationGraphVertex as CondensedVertex; cgv.IsSCC = true; foreach (TypeVertex tv in arg.StronglyConnectedVertices) { if (tv.ImutableExternalType) { cgv.ImutableExternalType = true; } } cgv.Condense(arg.StronglyConnectedVertices); }
private void WriteProjectRefsItemGroup(XmlTextWriter xproj, CondensedVertex va) { xproj.WriteComment("Project Refs"); xproj.WriteStartElement("ItemGroup"); QuickGraph.Concepts.Collections.IVertexEnumerable projectRefs = Graph.AdjacentVertices(va); foreach (CondensedVertex projRef in projectRefs) { if (!projRef.ImutableExternalType) { xproj.WriteStartElement("ProjectReference"); xproj.WriteAttributeString("Include", Path.GetFileName(projRef.ProjectFile)); xproj.WriteElementString("Project", projRef.AssemblyGUID.ToString("B")); xproj.WriteElementString("Name", projRef.AssemblyName); xproj.WriteEndElement(); } } xproj.WriteEndElement(); }
void dfs_FinishVertex(object sender, VertexEventArgs e) { CondensedVertex v = (CondensedVertex)e.Vertex; List <CondensedVertex> pes = PendingEdges.Pop(); foreach (CondensedVertex 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); }
public void FormatCCCVertex(object sender, FormatVertexEventArgs args) { CondensedVertex o = (CondensedVertex)args.Vertex; args.VertexFormatter.Label = o.Name; if (o.IsSCC) { args.VertexFormatter.Group = "sdfsdf"; args.VertexFormatter.FillColor = Color.AliceBlue; args.VertexFormatter.Style = GraphvizVertexStyle.Filled; args.VertexFormatter.Shape = NGraphviz.Helpers.GraphvizVertexShape.Box; } if (o.ImutableExternalType) { args.VertexFormatter.FillColor = Color.LightSalmon; args.VertexFormatter.Style = GraphvizVertexStyle.Filled; args.VertexFormatter.Shape = NGraphviz.Helpers.GraphvizVertexShape.MSquare; } }
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; }
private List <IEdge> MergeOldEdges(CondensedVertex v, CondensedVertex destV, List <KeyValuePair <IVertex, IVertex> > oldEdges) { List <IEdge> pendingEdges = new List <IEdge>(); foreach (KeyValuePair <IVertex, IVertex> kvp in oldEdges) { IVertex srcV, targetV, nsrcV, ntargetV; nsrcV = srcV = kvp.Key; ntargetV = targetV = kvp.Value; if (srcV == v) { nsrcV = destV; } if (targetV == v) { ntargetV = destV; } if (srcV == destV) { nsrcV = destV; } if (targetV == destV) { ntargetV = destV; } if (!this.ContainsEdge(nsrcV, ntargetV) && (nsrcV != ntargetV)) { pendingEdges.Add(this.AddEdge(nsrcV, ntargetV)); } } return(pendingEdges); }
private void WriteEmbeddedResourceItemGroup(XmlTextWriter xproj, CondensedVertex va) { //throw new Exception("The method or operation is not implemented."); }
private List<KeyValuePair<IVertex, IVertex>> SaveExistingEdges(CondensedVertex v) { List<KeyValuePair<IVertex, IVertex>> edgeTbl = new List<KeyValuePair<IVertex, IVertex>>(); // Dictionary<IVertex, IVertex> edgeTbl = new Dictionary<IVertex,IVertex>(); EdgeCollection outboundEdges = this.OutEdges(v); foreach (IEdge e in outboundEdges) { edgeTbl.Add(new KeyValuePair<IVertex, IVertex>(e.Source, e.Target)); //CondensedVertex srcV = (CondensedVertex)e.Source; //CondensedVertex targetV = (CondensedVertex)e.Target; //Trace.WriteLine("E: " + srcV.Name + " -> " + targetV.Name); } FilteredEdgeEnumerable inboundEdges = this.SelectEdges(new IsInEdgePredicate(v)); //Debug.WriteLine("inboundEdges"); foreach (IEdge e in inboundEdges) { edgeTbl.Add(new KeyValuePair<IVertex, IVertex>(e.Source, e.Target)); //CondensedVertex srcV = (CondensedVertex)e.Source; //CondensedVertex targetV = (CondensedVertex)e.Target; //Trace.WriteLine("E: " + srcV.Name + " -> " + targetV.Name); } return edgeTbl; }
private void WriteCompileItemGroup(XmlTextWriter xproj, CondensedVertex va) { Dictionary<string, int> ListedFiles = new Dictionary<string, int>(); xproj.WriteComment("Source files"); xproj.WriteStartElement("ItemGroup"); foreach (TypeReference tref in va.ContainedTypes) { foreach (string fullPath in GetFilesForType(tref.FullName)) { if (ListedFiles.ContainsKey(fullPath)) continue; CondensedVertex belongsToVetex = va; if (fileProjectMap.TryGetValue(fullPath,out belongsToVetex)) { //TODO: replace with exception or Log.Error Debug.WriteLine("Warning, file belong to another project"); } else { fileProjectMap[fullPath] = va; } ListedFiles[fullPath] = 1; CheckNameMatchesFile(fullPath, tref); xproj.WriteStartElement("Compile"); xproj.WriteAttributeString("Include", fullPath); xproj.WriteEndElement(); } } xproj.WriteEndElement(); }
private void WriteAssemRefsItemGroup(XmlTextWriter xproj, CondensedVertex va) { xproj.WriteComment("Assembly Refs"); xproj.WriteStartElement("ItemGroup"); foreach (CondensedVertex projRef in Graph.AdjacentVertices(va)) { if (projRef.ImutableExternalType) { xproj.WriteStartElement("Reference"); string aRefShortName = ""; string aRefFQN = DetermineClassDeps.AssemblyNameofType(projRef.ContainedTypes[0]); if (aRefFQN.StartsWith("mscorlib")) aRefFQN = "System,"; int idx = aRefFQN.IndexOf(","); if (idx > 0) aRefShortName = aRefFQN.Substring(0, idx); if (aRefFQN.StartsWith("System")) { xproj.WriteAttributeString("Include", aRefShortName); } else { xproj.WriteAttributeString("Include", aRefFQN); string[] exts = new string[2] { ".dll", ".exe" }; foreach (string ext in exts) { string hintPath = Path.Combine(Path.GetDirectoryName(AssemblyFileName), aRefShortName) + ext; if (File.Exists(hintPath)) { xproj.WriteElementString("SpecificVersion", false.ToString()); //todo - manage directories better xproj.WriteElementString("HintPath", Path.Combine("..",hintPath)); break; } } } xproj.WriteEndElement(); } } xproj.WriteEndElement(); }
private void CreateProjectFile(CondensedVertex va) { if (va.ImutableExternalType || va.AssemblyName.StartsWith("<")) return; XmlTextWriter xproj = new XmlTextWriter(Path.Combine(OutputDirectory, va.ProjectFile), null); xproj.Formatting = Formatting.Indented; xproj.WriteStartDocument(); xproj.WriteStartElement("Project"); xproj.WriteAttributeString("DefaultTargets", "Build"); xproj.WriteAttributeString("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003"); xproj.WriteStartElement("PropertyGroup"); /* <Configuration Condition=" ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> */ xproj.WriteStartElement("Configuration"); xproj.WriteAttributeString("Condition", "'$(Configuration)'==''"); xproj.WriteString("Debug"); xproj.WriteEndElement(); xproj.WriteStartElement("Platform"); xproj.WriteAttributeString("Condition", "'$(Platform)'==''"); xproj.WriteString("AnyCPU"); xproj.WriteEndElement(); xproj.WriteElementString("OutputType", "Library"); xproj.WriteElementString("OutputPath", @"bin\Debug"); xproj.WriteElementString("AssemblyName", va.AssemblyName); // xproj.WriteElementString("NoWarn", "1570,1571,1572,1573,1574,1584,1587,1591"); xproj.WriteEndElement();//PropGroup WriteProjectImport(xproj, @"$(MSBuildBinPath)\Microsoft.CSharp.targets"); WriteAssemRefsItemGroup(xproj,va); WriteProjectRefsItemGroup(xproj,va); WriteCompileItemGroup(xproj,va); WriteEmbeddedResourceItemGroup(xproj,va); xproj.WriteEndElement();//Project xproj.WriteEndDocument(); xproj.Close(); }
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 void UndoMerge(CondensedVertex v, CondensedVertex destV, List<KeyValuePair<IVertex, IVertex>> oldEdges, List<IEdge> pendingEdges) { foreach (IEdge e in pendingEdges) this.RemoveEdge(e); this.AddVertex(v); foreach (KeyValuePair<IVertex, IVertex> e in oldEdges) { this.AddEdge(e.Key, e.Value); } }
private bool TryMergeVertex(CondensedVertex v, CondensedVertex destV, ref int numVertex) { List<KeyValuePair<IVertex, IVertex>> oldEdges = SaveExistingEdges(v); this.RemoveVertex(v); numVertex--; List<IEdge> pendingEdges = MergeOldEdges(v, destV, oldEdges); if (numVertex > SCCCount) { UndoMerge(v, destV, oldEdges, pendingEdges); numVertex++; Debug.Assert(numVertex == this.VerticesCount); Debug.Assert(numVertex == SCCCount); return false; } else { foreach (TypeReference tref in v.ContainedTypes) { destV.ContainedTypes.Add(tref); } destV.Condense(); return true; } }
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); }
private List<IEdge> MergeOldEdges(CondensedVertex v, CondensedVertex destV, List<KeyValuePair<IVertex, IVertex>> oldEdges) { List<IEdge> pendingEdges = new List<IEdge>(); foreach (KeyValuePair<IVertex, IVertex> kvp in oldEdges) { IVertex srcV, targetV, nsrcV, ntargetV; nsrcV = srcV = kvp.Key; ntargetV = targetV = kvp.Value; if (srcV == v) nsrcV = destV; if (targetV == v) ntargetV = destV; if (srcV == destV) nsrcV = destV; if (targetV == destV) ntargetV = destV; if (!this.ContainsEdge(nsrcV, ntargetV) && (nsrcV != ntargetV)) { pendingEdges.Add(this.AddEdge(nsrcV, ntargetV)); } } return pendingEdges; }