-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdependency_graph_demo.rs
More file actions
410 lines (329 loc) · 13.7 KB
/
dependency_graph_demo.rs
File metadata and controls
410 lines (329 loc) · 13.7 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Demonstration of comprehensive dependency graph construction capabilities
use smart_diff_parser::{Language, TreeSitterParser};
use smart_diff_semantic::{
CallType, ComprehensiveDependencyGraphBuilder, DependencyAnalysisConfig, SymbolResolver,
SymbolResolverConfig, TypeExtractor, TypeExtractorConfig,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Smart Code Diff - Comprehensive Dependency Graph Demo");
println!("====================================================");
// Demo basic dependency graph construction
demo_basic_dependency_graph()?;
// Demo comprehensive dependency analysis
demo_comprehensive_analysis()?;
// Demo dependency hotspot identification
demo_hotspot_identification()?;
// Demo cross-file dependency tracking
demo_cross_file_dependencies()?;
Ok(())
}
fn demo_basic_dependency_graph() -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Basic Dependency Graph Construction ---");
let parser = TreeSitterParser::new()?;
let java_code = r#"
package com.example.service;
import java.util.List;
import java.util.Map;
import com.example.model.User;
import com.example.repository.UserRepository;
public class UserService {
private final UserRepository userRepository;
private final ValidationService validationService;
private Map<String, User> userCache;
public UserService(UserRepository userRepository, ValidationService validationService) {
this.userRepository = userRepository;
this.validationService = validationService;
this.userCache = new HashMap<>();
}
public User createUser(String name, String email) {
// Validate input
if (!validationService.isValidEmail(email)) {
throw new IllegalArgumentException("Invalid email");
}
// Check if user exists
User existingUser = userRepository.findByEmail(email);
if (existingUser != null) {
throw new IllegalStateException("User already exists");
}
// Create new user
User newUser = new User(name, email);
User savedUser = userRepository.save(newUser);
// Cache the user
userCache.put(email, savedUser);
return savedUser;
}
public List<User> getAllUsers() {
return userRepository.findAll();
}
public User getUserByEmail(String email) {
// Check cache first
User cachedUser = userCache.get(email);
if (cachedUser != null) {
return cachedUser;
}
// Fetch from repository
User user = userRepository.findByEmail(email);
if (user != null) {
userCache.put(email, user);
}
return user;
}
public void deleteUser(String email) {
User user = getUserByEmail(email);
if (user != null) {
userRepository.delete(user);
userCache.remove(email);
}
}
}
class ValidationService {
public boolean isValidEmail(String email) {
return email != null && email.contains("@") && email.contains(".");
}
public boolean isValidName(String name) {
return name != null && !name.trim().isEmpty();
}
}
"#;
// Parse the code
let parse_result = parser.parse(java_code, Language::Java)?;
// Create dependency graph builder
let config = DependencyAnalysisConfig::default();
let mut builder = ComprehensiveDependencyGraphBuilder::new(config);
// Build dependency graph
let files = vec![("UserService.java".to_string(), parse_result)];
builder.build_comprehensive_graph(files)?;
// Get analysis results
let analysis = builder.analyze_comprehensive_dependencies();
println!("Dependency Graph Analysis:");
println!(" Total nodes: {}", analysis.total_nodes);
println!(" Total edges: {}", analysis.total_edges);
println!(
" Function call dependencies: {}",
analysis.function_call_dependencies
);
println!(" Type dependencies: {}", analysis.type_dependencies);
println!(
" Variable dependencies: {}",
analysis.variable_dependencies
);
println!(
" Inheritance dependencies: {}",
analysis.inheritance_dependencies
);
if !analysis.circular_dependencies.is_empty() {
println!(
" Circular dependencies found: {}",
analysis.circular_dependencies.len()
);
for cycle in &analysis.circular_dependencies {
println!(" Cycle: {:?}", cycle);
}
} else {
println!(" No circular dependencies found");
}
if !analysis.dependency_layers.is_empty() {
println!(" Dependency layers: {}", analysis.dependency_layers.len());
for (i, layer) in analysis.dependency_layers.iter().enumerate() {
println!(" Layer {}: {} nodes", i + 1, layer.len());
for node in layer.iter().take(3) {
println!(" - {}", node);
}
if layer.len() > 3 {
println!(" ... and {} more", layer.len() - 3);
}
}
}
Ok(())
}
fn demo_comprehensive_analysis() -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Comprehensive Dependency Analysis ---");
let parser = TreeSitterParser::new()?;
// Create a more complex example with multiple classes and relationships
let complex_code = r#"
public abstract class BaseProcessor<T> {
protected Logger logger;
protected MetricsCollector metrics;
public BaseProcessor(Logger logger, MetricsCollector metrics) {
this.logger = logger;
this.metrics = metrics;
}
public abstract ProcessResult<T> process(T input);
protected void logProcessing(String message) {
logger.info("Processing: " + message);
metrics.incrementCounter("processing.events");
}
}
public class DataProcessor extends BaseProcessor<DataRecord> {
private final DataValidator validator;
private final DataTransformer transformer;
private final DataRepository repository;
private final CacheManager cacheManager;
public DataProcessor(Logger logger, MetricsCollector metrics,
DataValidator validator, DataTransformer transformer,
DataRepository repository, CacheManager cacheManager) {
super(logger, metrics);
this.validator = validator;
this.transformer = transformer;
this.repository = repository;
this.cacheManager = cacheManager;
}
@Override
public ProcessResult<DataRecord> process(DataRecord input) {
logProcessing("Starting data processing for: " + input.getId());
try {
// Validate input
ValidationResult validationResult = validator.validate(input);
if (!validationResult.isValid()) {
return ProcessResult.failure("Validation failed: " + validationResult.getErrors());
}
// Check cache
String cacheKey = generateCacheKey(input);
DataRecord cachedResult = cacheManager.get(cacheKey);
if (cachedResult != null) {
logProcessing("Cache hit for: " + input.getId());
return ProcessResult.success(cachedResult);
}
// Transform data
DataRecord transformedData = transformer.transform(input);
// Save to repository
DataRecord savedData = repository.save(transformedData);
// Cache result
cacheManager.put(cacheKey, savedData);
logProcessing("Successfully processed: " + input.getId());
return ProcessResult.success(savedData);
} catch (Exception e) {
logger.error("Processing failed for: " + input.getId(), e);
metrics.incrementCounter("processing.errors");
return ProcessResult.failure("Processing failed: " + e.getMessage());
}
}
private String generateCacheKey(DataRecord input) {
return "data_" + input.getId() + "_" + input.getVersion();
}
public List<DataRecord> processAll(List<DataRecord> inputs) {
List<DataRecord> results = new ArrayList<>();
for (DataRecord input : inputs) {
ProcessResult<DataRecord> result = process(input);
if (result.isSuccess()) {
results.add(result.getData());
}
}
return results;
}
}
interface DataValidator {
ValidationResult validate(DataRecord data);
}
interface DataTransformer {
DataRecord transform(DataRecord input);
}
interface DataRepository {
DataRecord save(DataRecord data);
DataRecord findById(String id);
List<DataRecord> findAll();
}
class ProcessResult<T> {
private final boolean success;
private final T data;
private final String errorMessage;
private ProcessResult(boolean success, T data, String errorMessage) {
this.success = success;
this.data = data;
this.errorMessage = errorMessage;
}
public static <T> ProcessResult<T> success(T data) {
return new ProcessResult<>(true, data, null);
}
public static <T> ProcessResult<T> failure(String errorMessage) {
return new ProcessResult<>(false, null, errorMessage);
}
public boolean isSuccess() { return success; }
public T getData() { return data; }
public String getErrorMessage() { return errorMessage; }
}
"#;
let parse_result = parser.parse(complex_code, Language::Java)?;
// Create comprehensive dependency graph
let config = DependencyAnalysisConfig {
include_function_calls: true,
include_type_dependencies: true,
include_variable_usage: true,
include_import_dependencies: true,
include_inheritance: true,
min_dependency_strength: 0.2,
max_transitive_depth: 8,
};
let mut builder = ComprehensiveDependencyGraphBuilder::new(config);
// Add symbol resolution
let mut symbol_resolver = SymbolResolver::with_defaults();
symbol_resolver.process_file("DataProcessor.java", &parse_result)?;
builder = builder.with_symbol_table(symbol_resolver.get_symbol_table().clone());
// Add type extraction
let mut type_extractor = TypeExtractor::with_defaults(Language::Java);
let type_result = type_extractor.extract_types("DataProcessor.java", &parse_result)?;
builder.add_type_extraction_result("DataProcessor.java".to_string(), type_result);
// Build comprehensive graph
let files = vec![("DataProcessor.java".to_string(), parse_result)];
builder.build_comprehensive_graph(files)?;
let analysis = builder.analyze_comprehensive_dependencies();
println!("Comprehensive Analysis Results:");
println!(" Total nodes: {}", analysis.total_nodes);
println!(" Total edges: {}", analysis.total_edges);
println!(" Function calls: {}", analysis.function_call_dependencies);
println!(" Type dependencies: {}", analysis.type_dependencies);
println!(" Variable usage: {}", analysis.variable_dependencies);
println!(" Inheritance: {}", analysis.inheritance_dependencies);
// Show coupling metrics for top nodes
println!("\nTop 5 Most Coupled Components:");
let mut coupling_pairs: Vec<_> = analysis.coupling_metrics.iter().collect();
coupling_pairs.sort_by(|a, b| {
let score_a = (a.1.afferent_coupling + a.1.efferent_coupling) as f64;
let score_b = (b.1.afferent_coupling + b.1.efferent_coupling) as f64;
score_b.partial_cmp(&score_a).unwrap()
});
for (i, (name, metrics)) in coupling_pairs.iter().take(5).enumerate() {
println!(" {}. {}", i + 1, name);
println!(" Afferent coupling: {}", metrics.afferent_coupling);
println!(" Efferent coupling: {}", metrics.efferent_coupling);
println!(" Instability: {:.3}", metrics.instability);
println!(
" Function call coupling: {}",
metrics.function_call_coupling
);
println!(" Type coupling: {}", metrics.type_coupling);
println!();
}
Ok(())
}
fn demo_hotspot_identification() -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Dependency Hotspot Identification ---");
// This would use the same analysis from the previous demo
// For brevity, we'll create a mock analysis result
println!("Dependency hotspots are components with high coupling that may indicate:");
println!(" - Design issues (violation of single responsibility)");
println!(" - Maintenance difficulties");
println!(" - Testing challenges");
println!(" - Potential refactoring candidates");
println!("\nHotspot identification criteria:");
println!(" - High afferent coupling (many components depend on this)");
println!(" - High efferent coupling (this depends on many components)");
println!(" - High instability (ratio of efferent to total coupling)");
println!(" - Complex interaction patterns");
Ok(())
}
fn demo_cross_file_dependencies() -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Cross-File Dependency Tracking ---");
println!("Cross-file dependency analysis tracks:");
println!(" - Import/export relationships");
println!(" - Module dependencies");
println!(" - Package-level coupling");
println!(" - Circular import detection");
println!(" - Dependency inversion opportunities");
println!("\nBenefits of cross-file analysis:");
println!(" - Architectural insight");
println!(" - Refactoring guidance");
println!(" - Module boundary validation");
println!(" - Build optimization opportunities");
Ok(())
}