@@ -5,15 +5,15 @@ namespace CSharpCodeAnalyst.Exploration;
5
5
6
6
public class CodeGraphExplorer : ICodeGraphExplorer
7
7
{
8
- private List < Dependency > _allDependencies = [ ] ;
8
+ private List < Relationship > _allRelationships = [ ] ;
9
9
private CodeGraph ? _codeGraph ;
10
10
11
11
public void LoadCodeGraph ( CodeGraph graph )
12
12
{
13
13
_codeGraph = graph ;
14
14
15
15
// Clear all cached data
16
- _allDependencies = [ ] ;
16
+ _allRelationships = [ ] ;
17
17
}
18
18
19
19
public List < CodeElement > GetElements ( List < string > ids )
@@ -30,7 +30,7 @@ public List<CodeElement> GetElements(List<string> ids)
30
30
{
31
31
if ( _codeGraph . Nodes . TryGetValue ( id , out var element ) )
32
32
{
33
- // The element is cloned internally and the dependencies discarded.
33
+ // The element is cloned internally and the relationships discarded.
34
34
elements . Add ( element ) ;
35
35
}
36
36
}
@@ -93,21 +93,21 @@ public SearchResult FindParents(List<string> ids)
93
93
}
94
94
95
95
/// <summary>
96
- /// Returns all dependencies that link the given nodes (ids).
96
+ /// Returns all relationships that link the given nodes (ids).
97
97
/// </summary>
98
- public IEnumerable < Dependency > FindAllDependencies ( HashSet < string > ids )
98
+ public IEnumerable < Relationship > FindAllRelationships ( HashSet < string > ids )
99
99
{
100
100
if ( _codeGraph is null )
101
101
{
102
102
return [ ] ;
103
103
}
104
104
105
- var dependencies = _codeGraph . Nodes . Values
106
- . SelectMany ( n => n . Dependencies )
105
+ var relationships = _codeGraph . Nodes . Values
106
+ . SelectMany ( n => n . Relationships )
107
107
. Where ( d => ids . Contains ( d . SourceId ) && ids . Contains ( d . TargetId ) )
108
108
. ToList ( ) ;
109
109
110
- return dependencies ;
110
+ return relationships ;
111
111
}
112
112
113
113
public Invocation FindIncomingCalls ( string id )
@@ -121,7 +121,7 @@ public Invocation FindIncomingCalls(string id)
121
121
122
122
var method = _codeGraph . Nodes [ id ] ;
123
123
124
- var allCalls = GetDependencies ( d => d . Type == DependencyType . Calls ) ;
124
+ var allCalls = GetRelationships ( d => d . Type == RelationshipType . Calls ) ;
125
125
var calls = allCalls . Where ( call => call . TargetId == method . Id ) . ToArray ( ) ;
126
126
var methods = calls . Select ( d => _codeGraph . Nodes [ d . SourceId ] ) ;
127
127
@@ -143,10 +143,10 @@ public Invocation FindIncomingCallsRecursive(string id)
143
143
var processingQueue = new Queue < CodeElement > ( ) ;
144
144
processingQueue . Enqueue ( method ) ;
145
145
146
- var foundCalls = new HashSet < Dependency > ( ) ;
146
+ var foundCalls = new HashSet < Relationship > ( ) ;
147
147
var foundMethods = new HashSet < CodeElement > ( ) ;
148
148
149
- var allCalls = GetDependencies ( d => d . Type == DependencyType . Calls ) ;
149
+ var allCalls = GetRelationships ( d => d . Type == RelationshipType . Calls ) ;
150
150
151
151
var processed = new HashSet < string > ( ) ;
152
152
while ( processingQueue . Any ( ) )
@@ -183,18 +183,18 @@ public SearchResult FollowIncomingCallsRecursive(string id)
183
183
}
184
184
185
185
var allImplementsAndOverrides =
186
- GetDependencies ( d => d . Type is DependencyType . Implements or DependencyType . Overrides ) ;
187
- var allCalls = GetDependencies ( d => d . Type == DependencyType . Calls ) ;
186
+ GetRelationships ( d => d . Type is RelationshipType . Implements or RelationshipType . Overrides ) ;
187
+ var allCalls = GetRelationships ( d => d . Type == RelationshipType . Calls ) ;
188
188
189
- var allHandles = GetDependencies ( d => d . Type == DependencyType . Handles ) ;
190
- var allInvokes = GetDependencies ( d => d . Type == DependencyType . Invokes ) ;
189
+ var allHandles = GetRelationships ( d => d . Type == RelationshipType . Handles ) ;
190
+ var allInvokes = GetRelationships ( d => d . Type == RelationshipType . Invokes ) ;
191
191
192
192
var method = _codeGraph . Nodes [ id ] ;
193
193
194
194
var processingQueue = new Queue < CodeElement > ( ) ;
195
195
processingQueue . Enqueue ( method ) ;
196
196
197
- var foundDependencies = new HashSet < Dependency > ( ) ;
197
+ var foundRelationships = new HashSet < Relationship > ( ) ;
198
198
var foundElements = new HashSet < CodeElement > ( ) ;
199
199
200
200
@@ -209,31 +209,31 @@ public SearchResult FollowIncomingCallsRecursive(string id)
209
209
210
210
// An event is raised by the specialization
211
211
var specializations = allImplementsAndOverrides . Where ( d => d . TargetId == element . Id ) . ToArray ( ) ;
212
- foundDependencies . UnionWith ( specializations ) ;
212
+ foundRelationships . UnionWith ( specializations ) ;
213
213
var specializedSources = specializations . Select ( d => _codeGraph . Nodes [ d . SourceId ] ) . ToHashSet ( ) ;
214
214
foundElements . UnionWith ( specializedSources ) ;
215
215
216
216
// Add all methods that invoke the event
217
217
var invokes = allInvokes . Where ( call => call . TargetId == element . Id ) . ToArray ( ) ;
218
- foundDependencies . UnionWith ( invokes ) ;
218
+ foundRelationships . UnionWith ( invokes ) ;
219
219
var invokeSources = invokes . Select ( d => _codeGraph . Nodes [ d . SourceId ] ) . ToHashSet ( ) ;
220
220
foundElements . UnionWith ( invokeSources ) ;
221
221
222
222
// Add Events that are handled by this method.
223
223
var handles = allHandles . Where ( h => h . SourceId == element . Id ) . ToArray ( ) ;
224
- foundDependencies . UnionWith ( handles ) ;
224
+ foundRelationships . UnionWith ( handles ) ;
225
225
var events = handles . Select ( h => _codeGraph . Nodes [ h . TargetId ] ) . ToHashSet ( ) ;
226
226
foundElements . UnionWith ( events ) ;
227
227
228
228
// Calls
229
229
var calls = allCalls . Where ( call => call . TargetId == element . Id ) . ToArray ( ) ;
230
- foundDependencies . UnionWith ( calls ) ;
230
+ foundRelationships . UnionWith ( calls ) ;
231
231
var callSources = calls . Select ( d => _codeGraph . Nodes [ d . SourceId ] ) . ToHashSet ( ) ;
232
232
foundElements . UnionWith ( callSources ) ;
233
233
234
234
// Abstractions. Sometimes the abstractions is called.
235
235
var abstractions = allImplementsAndOverrides . Where ( d => d . SourceId == element . Id ) . ToArray ( ) ;
236
- foundDependencies . UnionWith ( abstractions ) ;
236
+ foundRelationships . UnionWith ( abstractions ) ;
237
237
var abstractionTargets = abstractions . Select ( d => _codeGraph . Nodes [ d . TargetId ] ) . ToHashSet ( ) ;
238
238
foundElements . UnionWith ( abstractionTargets ) ;
239
239
@@ -249,7 +249,7 @@ public SearchResult FollowIncomingCallsRecursive(string id)
249
249
}
250
250
}
251
251
252
- return new SearchResult ( foundElements , foundDependencies ) ;
252
+ return new SearchResult ( foundElements , foundRelationships ) ;
253
253
}
254
254
255
255
/// <summary>
@@ -267,7 +267,7 @@ public SearchResult FindFullInheritanceTree(string id)
267
267
var type = _codeGraph . Nodes [ id ] ;
268
268
269
269
var types = new HashSet < CodeElement > ( ) ;
270
- var relationships = new HashSet < Dependency > ( ) ;
270
+ var relationships = new HashSet < Relationship > ( ) ;
271
271
var processingQueue = new Queue < CodeElement > ( ) ;
272
272
273
273
var processed = new HashSet < string > ( ) ;
@@ -287,7 +287,8 @@ public SearchResult FindFullInheritanceTree(string id)
287
287
288
288
// Case typeToAnalyze is subclass: typeToAnalyze implements X or inherits from Y
289
289
var abstractionsOfAnalyzedType =
290
- typeToAnalyze . Dependencies . Where ( d => d . Type is DependencyType . Implements or DependencyType . Inherits ) ;
290
+ typeToAnalyze . Relationships . Where ( d =>
291
+ d . Type is RelationshipType . Implements or RelationshipType . Inherits ) ;
291
292
foreach ( var abstraction in abstractionsOfAnalyzedType )
292
293
{
293
294
var baseType = _codeGraph . Nodes [ abstraction . TargetId ] ;
@@ -339,12 +340,12 @@ public SearchResult FindSpecializations(string id)
339
340
340
341
var element = _codeGraph . Nodes [ id ] ;
341
342
342
- var dependencies = _codeGraph . GetAllDependencies ( )
343
- . Where ( d => ( d . Type == DependencyType . Overrides ||
344
- d . Type == DependencyType . Implements ) &&
343
+ var relationships = _codeGraph . GetAllRelationships ( )
344
+ . Where ( d => ( d . Type == RelationshipType . Overrides ||
345
+ d . Type == RelationshipType . Implements ) &&
345
346
d . TargetId == element . Id ) . ToList ( ) ;
346
- var methods = dependencies . Select ( m => _codeGraph . Nodes [ m . SourceId ] ) . ToList ( ) ;
347
- return new SearchResult ( methods , dependencies ) ;
347
+ var methods = relationships . Select ( m => _codeGraph . Nodes [ m . SourceId ] ) . ToList ( ) ;
348
+ return new SearchResult ( methods , relationships ) ;
348
349
}
349
350
350
351
/// <summary>
@@ -361,12 +362,12 @@ public SearchResult FindAbstractions(string id)
361
362
362
363
var element = _codeGraph . Nodes [ id ] ;
363
364
364
- var dependencies = element . Dependencies
365
- . Where ( d => ( d . Type == DependencyType . Overrides ||
366
- d . Type == DependencyType . Implements ) &&
365
+ var relationships = element . Relationships
366
+ . Where ( d => ( d . Type == RelationshipType . Overrides ||
367
+ d . Type == RelationshipType . Implements ) &&
367
368
d . SourceId == element . Id ) . ToList ( ) ;
368
- var methods = dependencies . Select ( m => _codeGraph . Nodes [ m . TargetId ] ) . ToList ( ) ;
369
- return new SearchResult ( methods , dependencies ) ;
369
+ var methods = relationships . Select ( m => _codeGraph . Nodes [ m . TargetId ] ) . ToList ( ) ;
370
+ return new SearchResult ( methods , relationships ) ;
370
371
}
371
372
372
373
@@ -381,13 +382,13 @@ public Invocation FindOutgoingCalls(string id)
381
382
382
383
var method = _codeGraph . Nodes [ id ] ;
383
384
384
- var calls = method . Dependencies
385
- . Where ( d => d . Type == DependencyType . Calls ) . ToList ( ) ;
385
+ var calls = method . Relationships
386
+ . Where ( d => d . Type == RelationshipType . Calls ) . ToList ( ) ;
386
387
var methods = calls . Select ( m => _codeGraph . Nodes [ m . TargetId ] ) . ToList ( ) ;
387
388
return new Invocation ( methods , calls ) ;
388
389
}
389
390
390
- public SearchResult FindOutgoingDependencies ( string id )
391
+ public SearchResult FindOutgoingRelationships ( string id )
391
392
{
392
393
ArgumentNullException . ThrowIfNull ( id ) ;
393
394
@@ -397,12 +398,12 @@ public SearchResult FindOutgoingDependencies(string id)
397
398
}
398
399
399
400
var element = _codeGraph . Nodes [ id ] ;
400
- var dependencies = element . Dependencies ;
401
- var targets = dependencies . Select ( m => _codeGraph . Nodes [ m . TargetId ] ) . ToList ( ) ;
402
- return new SearchResult ( targets , dependencies ) ;
401
+ var relationships = element . Relationships ;
402
+ var targets = relationships . Select ( m => _codeGraph . Nodes [ m . TargetId ] ) . ToList ( ) ;
403
+ return new SearchResult ( targets , relationships ) ;
403
404
}
404
405
405
- public SearchResult FindIncomingDependencies ( string id )
406
+ public SearchResult FindIncomingRelationships ( string id )
406
407
{
407
408
ArgumentNullException . ThrowIfNull ( id ) ;
408
409
if ( _codeGraph is null )
@@ -411,43 +412,43 @@ public SearchResult FindIncomingDependencies(string id)
411
412
}
412
413
413
414
var element = _codeGraph . Nodes [ id ] ;
414
- var dependencies = _codeGraph . Nodes . Values
415
- . SelectMany ( node => node . Dependencies )
415
+ var relationships = _codeGraph . Nodes . Values
416
+ . SelectMany ( node => node . Relationships )
416
417
. Where ( d => d . TargetId == element . Id ) . ToList ( ) ;
417
418
418
- var elements = dependencies . Select ( d => _codeGraph . Nodes [ d . SourceId ] ) ;
419
+ var elements = relationships . Select ( d => _codeGraph . Nodes [ d . SourceId ] ) ;
419
420
420
- return new SearchResult ( elements , dependencies ) ;
421
+ return new SearchResult ( elements , relationships ) ;
421
422
}
422
423
423
- private List < Dependency > GetCachedDependencies ( )
424
+ private List < Relationship > GetCachedRelationships ( )
424
425
{
425
426
if ( _codeGraph is null )
426
427
{
427
428
return [ ] ;
428
429
}
429
430
430
- if ( _allDependencies . Count == 0 )
431
+ if ( _allRelationships . Count == 0 )
431
432
{
432
- _allDependencies = _codeGraph . GetAllDependencies ( ) . ToList ( ) ;
433
+ _allRelationships = _codeGraph . GetAllRelationships ( ) . ToList ( ) ;
433
434
}
434
435
435
- return _allDependencies ;
436
+ return _allRelationships ;
436
437
}
437
438
438
- private List < Dependency > GetDependencies ( Func < Dependency , bool > filter )
439
+ private List < Relationship > GetRelationships ( Func < Relationship , bool > filter )
439
440
{
440
- return GetCachedDependencies ( ) . Where ( filter ) . ToList ( ) ;
441
+ return GetCachedRelationships ( ) . Where ( filter ) . ToList ( ) ;
441
442
}
442
443
443
- private HashSet < Dependency > FindInheritsAndImplementsRelationships ( )
444
+ private HashSet < Relationship > FindInheritsAndImplementsRelationships ( )
444
445
{
445
446
if ( _codeGraph is null )
446
447
{
447
448
return [ ] ;
448
449
}
449
450
450
- var inheritsAndImplements = new HashSet < Dependency > ( ) ;
451
+ var inheritsAndImplements = new HashSet < Relationship > ( ) ;
451
452
_codeGraph . DfsHierarchy ( Collect ) ;
452
453
return inheritsAndImplements ;
453
454
@@ -458,17 +459,17 @@ void Collect(CodeElement c)
458
459
return ;
459
460
}
460
461
461
- foreach ( var dependency in c . Dependencies )
462
+ foreach ( var relationship in c . Relationships )
462
463
{
463
- if ( dependency . Type is DependencyType . Inherits or DependencyType . Implements )
464
+ if ( relationship . Type is RelationshipType . Inherits or RelationshipType . Implements )
464
465
{
465
- inheritsAndImplements . Add ( dependency ) ;
466
+ inheritsAndImplements . Add ( relationship ) ;
466
467
}
467
468
}
468
469
}
469
470
}
470
471
}
471
472
472
- public record struct SearchResult ( IEnumerable < CodeElement > Elements , IEnumerable < Dependency > Dependencies ) ;
473
+ public record struct SearchResult ( IEnumerable < CodeElement > Elements , IEnumerable < Relationship > Relationships ) ;
473
474
474
- public record struct Invocation ( IEnumerable < CodeElement > Methods , IEnumerable < Dependency > Calls ) ;
475
+ public record struct Invocation ( IEnumerable < CodeElement > Methods , IEnumerable < Relationship > Calls ) ;
0 commit comments