-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDgmlRelationshipExport.cs
More file actions
72 lines (59 loc) · 2.16 KB
/
Copy pathDgmlRelationshipExport.cs
File metadata and controls
72 lines (59 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using CSharpCodeAnalyst.CodeGraph.Colors;
using CSharpCodeAnalyst.CodeGraph.Graph;
namespace CSharpCodeAnalyst.CodeGraph.Export;
/// <summary>
/// Debug class to export the relationship information of a code graph to a dgml file.
/// See <see cref="DgmlExport" /> for hierarchy and relationships.
/// </summary>
public class DgmlRelationshipExport
{
public static void Export(string fileName, Graph.CodeGraph codeGraph)
{
var writer = new DgmlFileBuilder();
var uniqueNodes = new HashSet<CodeElement>(codeGraph.Nodes.Values);
WriteCategories(writer);
WriteNodes(writer, uniqueNodes, codeGraph);
WriteEdges(writer, uniqueNodes);
writer.WriteOutput(fileName);
}
private static void WriteCategories(DgmlFileBuilder writer)
{
var elementTypes = Enum.GetValues(typeof(CodeElementType)).Cast<CodeElementType>();
foreach (var type in elementTypes)
{
writer.AddCategory(type.ToString(), "Background", $"#{ColorDefinitions.GetRbgOf(type):X}");
}
}
private static void WriteEdges(DgmlFileBuilder writer, IEnumerable<CodeElement> nodes)
{
foreach (var node in nodes)
{
foreach (var child in node.Relationships)
{
writer.AddEdgeById(node.Id, child.TargetId, child.Type.ToString());
}
}
}
private static void WriteNodes(DgmlFileBuilder writer, IEnumerable<CodeElement> nodes, Graph.CodeGraph codeGraph)
{
// Find all nodes we need for the graph.
var allNodes = new HashSet<CodeElement>();
foreach (var node in nodes.Where(n => n.Relationships.Count != 0))
{
allNodes.Add(node);
foreach (var relationship in node.Relationships)
{
var targetElement = codeGraph.Nodes[relationship.TargetId];
allNodes.Add(targetElement);
}
}
foreach (var node in allNodes)
{
writer.AddNodeById(node.Id, GetDgmlLabel(node), node.ElementType.ToString());
}
}
private static string GetDgmlLabel(CodeElement node)
{
return node.Name;
}
}