Skip to content

Commit a7ce1dd

Browse files
committed
refactor: simplify node and edge extraction, enhance degree calculation, and improve error messaging in DependencyGraphGenerator and CoreUtils
1 parent d86df14 commit a7ce1dd

2 files changed

Lines changed: 64 additions & 51 deletions

File tree

CodeLineCounter/Services/DependencyGraphGenerator.cs

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,72 +26,85 @@ public static void UnflattenGraph(DotGraph graph, GraphvizUnflattenOptions optio
2626
int chainSize = 0;
2727

2828
// Extraire les arêtes du graphe
29-
var edges = graph.Elements.OfType<DotEdge>().ToList();
30-
var nodes = graph.Elements.OfType<DotNode>().ToList();
31-
var subgraphes = graph.Elements.OfType<DotSubgraph>().ToList();
32-
foreach (var subgraphe in subgraphes)
33-
{
34-
edges.AddRange(subgraphe.Elements.OfType<DotEdge>());
35-
nodes.AddRange(subgraphe.Elements.OfType<DotNode>());
36-
}
29+
List<DotEdge> edges;
30+
List<DotNode> nodes;
31+
ExtractNodesAndEdges(graph, out edges, out nodes);
3732

3833
// Calculer le degré de chaque nœud
39-
foreach (var node in nodes)
40-
{
41-
int degree = GetNodeDegree(edges, node.Identifier.Value);
42-
nodeDegrees[node.Identifier.Value] = degree;
43-
}
34+
CalculateDegrees(nodeDegrees, edges, nodes);
4435

4536
// Traiter les nœuds du graphe
4637
foreach (var node in nodes)
4738
{
4839
DotIdentifier nodeId = node.Identifier;
4940
int degree = nodeDegrees[nodeId.Value];
5041

51-
if (degree == 0)
42+
if (degree == 0 && options.ChainLimit >= 1)
43+
{
44+
AddEdgeAndStyleToUnflatten(graph, options, ref chainNode, ref chainSize, node, nodeId);
45+
}
46+
else if (degree > 1 && options.MaxMinlen >= 1)
47+
{
48+
SetMinLenAttributeWhenUnflatting(options, edges, nodeId);
49+
}
50+
}
51+
}
52+
53+
private static void AddEdgeAndStyleToUnflatten(DotGraph graph, GraphvizUnflattenOptions options, ref DotNode? chainNode, ref int chainSize, DotNode node, DotIdentifier nodeId)
54+
{
55+
if (chainNode != null)
56+
{
57+
var edge = new DotEdge { From = chainNode.Identifier, To = nodeId, Style = DotEdgeStyle.Invis };
58+
graph.Elements.Add(edge);
59+
chainSize++;
60+
61+
if (chainSize < options.ChainLimit)
62+
chainNode = node;
63+
else
5264
{
53-
if (options.ChainLimit < 1)
54-
continue;
55-
56-
if (chainNode != null)
57-
{
58-
var edge = new DotEdge { From = chainNode.Identifier, To = nodeId, Style = DotEdgeStyle.Invis };
59-
graph.Elements.Add(edge);
60-
chainSize++;
61-
62-
if (chainSize < options.ChainLimit)
63-
chainNode = node;
64-
else
65-
{
66-
chainNode = null;
67-
chainSize = 0;
68-
}
69-
}
70-
else
71-
{
72-
chainNode = node;
73-
}
65+
chainNode = null;
66+
chainSize = 0;
7467
}
75-
else if (degree > 1)
68+
}
69+
else
70+
{
71+
chainNode = node;
72+
}
73+
}
74+
75+
private static void SetMinLenAttributeWhenUnflatting(GraphvizUnflattenOptions options, List<DotEdge> edges, DotIdentifier nodeId)
76+
{
77+
int cnt = 0;
78+
foreach (var edge in edges)
79+
{
80+
if (edge.To == nodeId && IsLeaf(edges, edge.From.Value))
7681
{
77-
if (options.MaxMinlen < 1)
78-
continue;
79-
80-
int cnt = 0;
81-
foreach (var edge in edges)
82-
{
83-
if (edge.To == nodeId && IsLeaf(edges, edge.From.Value))
84-
{
85-
DotAttribute minLen = edge.GetAttribute<DotAttribute>("minlen");
86-
minLen.Value = (cnt % options.MaxMinlen + 1).ToString();
87-
edge.SetAttribute("minLen", minLen);
88-
cnt++;
89-
}
90-
}
82+
DotAttribute minLen = edge.GetAttribute<DotAttribute>("minlen");
83+
minLen.Value = (cnt % options.MaxMinlen + 1).ToString();
84+
edge.SetAttribute("minLen", minLen);
85+
cnt++;
9186
}
9287
}
9388
}
9489

90+
private static void CalculateDegrees(Dictionary<string, int> nodeDegrees, List<DotEdge> edges, List<DotNode> nodes)
91+
{
92+
foreach (var nodeId in nodes.Select(node => node.Identifier.Value))
93+
{
94+
int degree = GetNodeDegree(edges, nodeId);
95+
nodeDegrees[nodeId] = degree;
96+
}
97+
}
98+
99+
private static void ExtractNodesAndEdges(DotGraph graph, out List<DotEdge> edges, out List<DotNode> nodes)
100+
{
101+
edges = graph.Elements.OfType<DotEdge>().ToList();
102+
nodes = graph.Elements.OfType<DotNode>().ToList();
103+
var subgraphes = graph.Elements.OfType<DotSubgraph>().ToList();
104+
edges.AddRange(subgraphes.SelectMany(s => s.Elements.OfType<DotEdge>()));
105+
nodes.AddRange(subgraphes.SelectMany(s => s.Elements.OfType<DotNode>()));
106+
}
107+
95108
private static int GetNodeDegree(List<DotEdge> edges, string nodeId)
96109
{
97110
return edges.Count(e => e.From.ToString() == nodeId || e.To.ToString() == nodeId);

CodeLineCounter/Utils/CoreUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static int CheckInputUserChoice(string? inputFromConsole, int solutionCou
9898

9999
if (string.IsNullOrWhiteSpace(input))
100100
{
101-
throw new ArgumentNullException("No input provided. Please enter a valid number.");
101+
throw new ArgumentNullException("inputFromConsole", "No input provided. Please enter a valid number.");
102102
}
103103

104104
if (!int.TryParse(input, out choice))

0 commit comments

Comments
 (0)