-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroundtrip_processor.py
More file actions
894 lines (735 loc) · 35.7 KB
/
roundtrip_processor.py
File metadata and controls
894 lines (735 loc) · 35.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
"""
GNN Processing Functions - Enhanced for Comprehensive Testing
This module provides enhanced processing functions for GNN files with integrated
round-trip testing, cross-format validation, and comprehensive reporting.
Enhanced Features:
- Integration with 100% round-trip testing system
- Multi-level validation (Basic, Standard, Strict, Research, Round-trip)
- Enhanced error reporting and performance metrics
- Cross-format consistency validation
- Production-ready testing infrastructure
"""
from __future__ import annotations
import functools
import json
import logging
import time
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from typing import Any, List, Optional, Tuple, Type
from utils import log_step_error, log_step_start, log_step_success, log_step_warning
logger = logging.getLogger(__name__)
@dataclass
class _TestingClassRefs:
"""Typed container for lazily-imported GNN testing classes.
Fields are None when the corresponding module could not be imported
(circular-dependency guard). Check ``ENHANCED_TESTING_AVAILABLE`` or
individual fields before use.
"""
GNNRoundTripTester: Optional[Type[Any]] = None
ComprehensiveTestReport: Optional[Type[Any]] = None
RoundTripResult: Optional[Type[Any]] = None
GNNParsingSystem: Optional[Type[Any]] = None
GNNFormat: Optional[Type[Any]] = None
GNNValidator: Optional[Type[Any]] = None
ValidationLevel: Optional[Type[Any]] = None
ValidationResult: Optional[Type[Any]] = None
CrossFormatValidator: Optional[Type[Any]] = None
CrossFormatValidationResult: Optional[Type[Any]] = None
def is_empty(self) -> bool:
"""Return True when none of the modules loaded successfully."""
return all(v is None for v in vars(self).values())
@functools.lru_cache(maxsize=None)
def _get_testing_modules() -> _TestingClassRefs:
"""Lazy import of testing modules to avoid circular dependencies."""
try:
# Use lazy imports to break circular dependency
import importlib
test_round_trip = importlib.import_module('gnn.testing.test_round_trip')
parsers = importlib.import_module('gnn.parsers')
schema_validator = importlib.import_module('gnn.schema_validator')
cross_format_validator = importlib.import_module('gnn.cross_format_validator')
return _TestingClassRefs(
GNNRoundTripTester=getattr(test_round_trip, 'GNNRoundTripTester', None),
ComprehensiveTestReport=getattr(test_round_trip, 'ComprehensiveTestReport', None),
RoundTripResult=getattr(test_round_trip, 'RoundTripResult', None),
GNNParsingSystem=getattr(parsers, 'GNNParsingSystem', None),
GNNFormat=getattr(parsers, 'GNNFormat', None),
GNNValidator=getattr(schema_validator, 'GNNValidator', None),
ValidationLevel=getattr(schema_validator, 'ValidationLevel', None),
ValidationResult=getattr(schema_validator, 'ValidationResult', None),
CrossFormatValidator=getattr(cross_format_validator, 'CrossFormatValidator', None),
CrossFormatValidationResult=getattr(cross_format_validator, 'CrossFormatValidationResult', None),
)
except (ImportError, AttributeError, RecursionError):
return _TestingClassRefs()
def is_enhanced_testing_available() -> bool:
"""Return True if enhanced testing modules are importable (dynamic check)."""
return not _get_testing_modules().is_empty()
def validate_gnn_with_round_trip(
target_dir: Path,
validation_level: str = "standard",
enable_round_trip: bool = False,
enable_cross_format: bool = False,
test_subset: Optional[List[str]] = None
) -> Tuple[int, Optional[str]]:
"""
Enhanced validation using GNN processing capabilities.
Returns:
Tuple of (exit_code, error_message)
"""
logger = logging.getLogger(__name__)
# Load testing modules lazily
modules = _get_testing_modules()
if modules.is_empty():
logger.warning("Enhanced testing modules not available, using basic validation")
return 2, "Enhanced testing modules not available"
try:
# Initialize validation level
try:
val_level = modules.ValidationLevel(validation_level.lower())
except ValueError:
logger.warning(f"Invalid validation level '{validation_level}', using STANDARD")
val_level = modules.ValidationLevel.STANDARD
# Initialize enhanced validator
validator = modules.GNNValidator(
validation_level=val_level,
enable_round_trip_testing=enable_round_trip
)
# Process files with enhanced validation
total_files = 0
validation_errors = 0
for file_path in target_dir.rglob("*.md"):
total_files += 1
logger.info(f"Enhanced validation of {file_path}")
try:
result = validator.validate_file(file_path)
if not result.is_valid:
validation_errors += 1
logger.error(f"Validation failed for {file_path}: {result.errors}")
elif result.warnings:
logger.warning(f"Validation warnings for {file_path}: {result.warnings}")
except Exception as e:
validation_errors += 1
logger.error(f"Error during enhanced validation of {file_path}: {e}")
logger.info(f"Enhanced validation complete: {total_files - validation_errors}/{total_files} files valid")
if validation_errors > 0:
return 1, f"Enhanced validation failed for {validation_errors} files"
return 0, None
except Exception as e:
logger.error(f"Enhanced validation error: {e}")
return 1, f"Enhanced validation error: {e}"
def process_gnn_folder(
target_dir: Path,
output_dir: Path,
logger: logging.Logger,
recursive: bool = False,
verbose: bool = False,
validation_level: str = "standard",
enable_round_trip: bool = False,
**kwargs
) -> bool:
"""
Enhanced GNN folder processing with comprehensive validation and testing.
Features:
- Multi-level validation (basic, standard, strict, research, round_trip)
- Format detection and analysis
- Performance metrics and detailed reporting
- Integration with round-trip testing system
Args:
target_dir: Directory containing GNN files to process
output_dir: Output directory for results
logger: Logger instance for this step
recursive: Whether to process files recursively
verbose: Whether to enable verbose logging
validation_level: Validation level (basic, standard, strict, research, round_trip)
enable_round_trip: Whether to enable round-trip testing
**kwargs: Additional processing options
Returns:
True if processing succeeded, False otherwise
"""
log_step_start(logger, f"Enhanced GNN processing: '{target_dir}' (validation: {validation_level})")
if not is_enhanced_testing_available():
log_step_warning(logger, "Enhanced testing not available - using basic processing")
return _basic_gnn_processing(target_dir, output_dir, logger, recursive, verbose, **kwargs)
# Initialize validation level
try:
val_level = _get_testing_modules().ValidationLevel(validation_level.lower())
except ValueError:
logger.warning(f"Invalid validation level '{validation_level}', using STANDARD")
val_level = _get_testing_modules().ValidationLevel.STANDARD
# Initialize enhanced validator
validator = _get_testing_modules().GNNValidator(
validation_level=val_level,
enable_round_trip_testing=enable_round_trip
)
# Create enhanced output directory structure
enhanced_output_dir = output_dir / "enhanced_gnn_processing"
enhanced_output_dir.mkdir(parents=True, exist_ok=True)
# Discovery phase
logger.info(f"Phase 1: Enhanced GNN file discovery (recursive: {recursive})")
start_time = time.time()
gnn_files = []
if recursive:
gnn_files.extend(target_dir.rglob("*.md"))
gnn_files.extend(target_dir.rglob("*.json"))
gnn_files.extend(target_dir.rglob("*.xml"))
gnn_files.extend(target_dir.rglob("*.yaml"))
gnn_files.extend(target_dir.rglob("*.pkl"))
else:
gnn_files.extend(target_dir.glob("*.md"))
gnn_files.extend(target_dir.glob("*.json"))
gnn_files.extend(target_dir.glob("*.xml"))
gnn_files.extend(target_dir.glob("*.yaml"))
gnn_files.extend(target_dir.glob("*.pkl"))
# Filter for actual GNN files
gnn_files = [f for f in gnn_files if f.is_file()]
discovery_time = time.time() - start_time
logger.info(f"Discovered {len(gnn_files)} potential GNN files in {discovery_time:.3f}s")
if not gnn_files:
log_step_warning(logger, "No GNN files found for processing")
return True
# Processing phase
logger.info("Phase 2: Enhanced validation and analysis")
processing_start = time.time()
processing_results = {
'files_processed': 0,
'files_valid': 0,
'files_invalid': 0,
'files_with_warnings': 0,
'format_distribution': {},
'validation_results': [],
'performance_metrics': {},
'round_trip_results': []
}
for gnn_file in gnn_files:
try:
logger.info(f"Processing: {gnn_file.relative_to(target_dir)}")
file_start = time.time()
# Validate file with enhanced validator
validation_result = validator.validate_file(gnn_file, val_level)
# Track results
processing_results['files_processed'] += 1
if validation_result.is_valid:
processing_results['files_valid'] += 1
else:
processing_results['files_invalid'] += 1
if validation_result.warnings:
processing_results['files_with_warnings'] += 1
# Track format distribution
file_format = validation_result.format_tested or 'unknown'
processing_results['format_distribution'][file_format] = \
processing_results['format_distribution'].get(file_format, 0) + 1
# Store detailed results
file_result = {
'file': str(gnn_file.relative_to(target_dir)),
'format': file_format,
'validation_level': validation_result.validation_level.value,
'is_valid': validation_result.is_valid,
'errors': validation_result.errors,
'warnings': validation_result.warnings,
'suggestions': validation_result.suggestions,
'semantic_checksum': validation_result.semantic_checksum,
'performance': validation_result.performance_metrics,
'round_trip_success_rate': validation_result.get_round_trip_success_rate()
}
processing_results['validation_results'].append(file_result)
# Collect round-trip results if available
if validation_result.round_trip_results:
processing_results['round_trip_results'].extend([
{
'file': str(gnn_file.relative_to(target_dir)),
'format': result.target_format.value,
'success': result.success,
'errors': result.errors,
'differences': result.differences
}
for result in validation_result.round_trip_results
])
file_time = time.time() - file_start
# Log results
if validation_result.is_valid:
status = "✅"
if validation_result.warnings:
status += f" ({len(validation_result.warnings)} warnings)"
else:
status = f"❌ ({len(validation_result.errors)} errors)"
logger.info(f" {status} {file_format.upper()} ({file_time:.3f}s)")
if verbose and validation_result.errors:
for error in validation_result.errors:
logger.info(f" Error: {error}")
if verbose and validation_result.round_trip_results:
rt_success_rate = validation_result.get_round_trip_success_rate()
logger.info(f" Round-trip: {rt_success_rate:.1f}% success")
except Exception as e:
logger.error(f"Error processing {gnn_file.name}: {e}")
processing_results['files_processed'] += 1
processing_results['files_invalid'] += 1
processing_time = time.time() - processing_start
total_time = time.time() - start_time
# Generate comprehensive report
processing_results['performance_metrics'] = {
'total_time': total_time,
'discovery_time': discovery_time,
'processing_time': processing_time,
'avg_file_time': processing_time / len(gnn_files) if gnn_files else 0,
'files_per_second': len(gnn_files) / processing_time if processing_time > 0 else 0
}
# Save detailed results
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
report_file = enhanced_output_dir / f"gnn_processing_report_{timestamp}.json"
with open(report_file, 'w', encoding='utf-8') as f:
json.dump(processing_results, f, indent=2, default=str)
# Summary logging
success_rate = (processing_results['files_valid'] / processing_results['files_processed'] * 100) \
if processing_results['files_processed'] > 0 else 0
logger.info("Enhanced GNN processing completed:")
logger.info(f" 📁 Files processed: {processing_results['files_processed']}")
logger.info(f" ✅ Valid files: {processing_results['files_valid']}")
logger.info(f" ❌ Invalid files: {processing_results['files_invalid']}")
logger.info(f" ⚠️ Files with warnings: {processing_results['files_with_warnings']}")
logger.info(f" 📈 Success rate: {success_rate:.1f}%")
logger.info(f" ⏱️ Total time: {total_time:.3f}s")
logger.info(f" 📊 Report saved: {report_file}")
# Format distribution
if processing_results['format_distribution']:
logger.info(" 📋 Format distribution:")
for fmt, count in processing_results['format_distribution'].items():
logger.info(f" {fmt}: {count}")
# Round-trip summary
if processing_results['round_trip_results']:
rt_total = len(processing_results['round_trip_results'])
rt_success = sum(1 for r in processing_results['round_trip_results'] if r['success'])
rt_rate = (rt_success / rt_total * 100) if rt_total > 0 else 0
logger.info(f" 🔄 Round-trip tests: {rt_success}/{rt_total} ({rt_rate:.1f}%)")
if success_rate >= 80.0:
log_step_success(logger, f"Enhanced GNN processing completed successfully ({success_rate:.1f}%)")
return True
else:
log_step_warning(logger, f"Enhanced GNN processing completed with issues ({success_rate:.1f}%)")
return False
def _basic_gnn_processing(
target_dir: Path,
output_dir: Path,
logger: logging.Logger,
recursive: bool = False,
verbose: bool = False,
**kwargs
) -> bool:
"""
Recovery basic GNN processing when enhanced testing is not available.
"""
log_step_warning(logger, "Using basic GNN processing (enhanced features unavailable)")
# Create basic output directory
basic_output_dir = output_dir / "basic_gnn_processing"
basic_output_dir.mkdir(parents=True, exist_ok=True)
# Simple file discovery
pattern = "**/*.md" if recursive else "*.md"
gnn_files = list(target_dir.glob(pattern))
logger.info(f"Found {len(gnn_files)} .md files")
if not gnn_files:
return True
# Basic validation
valid_files = 0
for gnn_file in gnn_files:
try:
with open(gnn_file, 'r', encoding='utf-8') as f:
content = f.read()
# Check for basic GNN sections
if "## ModelName" in content and "## StateSpaceBlock" in content:
valid_files += 1
logger.info(f"✅ {gnn_file.name}: Basic GNN structure found")
else:
logger.warning(f"⚠️ {gnn_file.name}: Missing required GNN sections")
except Exception as e:
logger.error(f"❌ {gnn_file.name}: {e}")
success_rate = (valid_files / len(gnn_files) * 100) if gnn_files else 0
logger.info(f"Basic processing: {valid_files}/{len(gnn_files)} valid ({success_rate:.1f}%)")
return success_rate >= 50.0
def _generate_performance_analysis(report: Any, test_time: float) -> str:
"""Generate performance analysis section for reports."""
lines = [
"## Performance Analysis",
"",
"### Test Execution Metrics",
f"- **Total test time:** {test_time:.3f} seconds",
f"- **Tests per second:** {report.total_tests / test_time:.2f}" if test_time > 0 else "- **Tests per second:** N/A",
f"- **Average test time:** {test_time / report.total_tests:.3f} seconds" if report.total_tests > 0 else "- **Average test time:** N/A",
"",
"### Success Rate Analysis",
f"- **Successful tests:** {report.tests_passed}/{report.total_tests} ({report.tests_passed/report.total_tests*100:.1f}%)" if report.total_tests > 0 else "- **Successful tests:** 0/0",
f"- **Failed tests:** {report.tests_failed}/{report.total_tests} ({report.tests_failed/report.total_tests*100:.1f}%)" if report.total_tests > 0 else "- **Failed tests:** 0/0",
"",
"### Format Coverage",
f"- **Formats tested:** {len(report.format_results)}"
]
if hasattr(report, 'format_results') and report.format_results:
lines.append("- **Formats:**")
for fmt, result in report.format_results.items():
status = "✓" if result.get('success', False) else "✗"
lines.append(f" - {status} {fmt}")
return "\n".join(lines)
def _is_gnn_file(file_path: Path) -> bool:
"""Determine if a file is likely a GNN file based on content analysis."""
try:
# Quick heuristics based on filename
filename_lower = file_path.name.lower()
if any(keyword in filename_lower for keyword in ['gnn', 'actinf', 'pomdp', 'model']):
return True
# Content-based detection for non-obvious files
if file_path.suffix.lower() == '.md':
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read(1000) # Read first 1KB
# Look for GNN section headers
gnn_indicators = [
'## GNNSection', '## ModelName', '## StateSpaceBlock',
'## Connections', '## InitialParameterization'
]
return any(indicator in content for indicator in gnn_indicators)
except Exception:
return False
# For other formats, assume they are GNN files if they're in our target directory
return file_path.suffix.lower() in ['.json', '.xml', '.yaml', '.pkl']
except Exception:
return False
def _validate_binary_cross_format(file_path: Path, cross_validator) -> Any:
"""Validate binary files for cross-format consistency."""
try:
# Binary files need special handling for cross-format validation
if not file_path.exists():
logger.error(f"Binary file not found: {file_path}")
# Return a recovery result
return type('CrossFormatValidationResult', (), {
'is_valid': False,
'errors': [f"File not found: {file_path}"],
'warnings': [],
'binary_format': str(file_path.suffix),
'content_hash': None
})()
# For binary files, we can only do basic format checks
logger.info(f"Validating binary file: {file_path}")
# Check file size and basic properties
file_size = file_path.stat().st_size
if file_size == 0:
return type('CrossFormatValidationResult', (), {
'is_valid': False,
'errors': ["Empty binary file"],
'warnings': [],
'binary_format': str(file_path.suffix),
'content_hash': None
})()
# Create a successful result for valid binary files
return type('CrossFormatValidationResult', (), {
'is_valid': True,
'errors': [],
'warnings': [],
'binary_format': str(file_path.suffix),
'content_hash': str(hash(file_path.read_bytes()))
})()
except Exception as e:
logger.error(f"Error validating binary file {file_path}: {e}")
return type('CrossFormatValidationResult', (), {
'is_valid': False,
'errors': [str(e)],
'warnings': [],
'binary_format': str(file_path.suffix),
'content_hash': None
})()
def run_gnn_round_trip_tests(
target_dir: Path,
output_dir: Path,
logger: logging.Logger,
reference_file: Optional[str] = None,
test_subset: Optional[List[str]] = None,
enable_parallel: bool = False,
**kwargs
) -> bool:
"""
Enhanced round-trip tests with performance optimization and detailed reporting.
Args:
target_dir: Directory containing GNN files
output_dir: Output directory for test results
logger: Logger instance
reference_file: Optional specific reference file
test_subset: Optional list of formats to test
enable_parallel: Whether to enable parallel testing
**kwargs: Additional test options
Returns:
True if all tests passed, False otherwise
"""
if not is_enhanced_testing_available():
log_step_warning(logger, "Enhanced round-trip testing not available")
return False
log_step_start(logger, "Enhanced GNN round-trip testing")
# Create enhanced output directory
round_trip_output_dir = output_dir / "enhanced_round_trip_tests"
round_trip_output_dir.mkdir(parents=True, exist_ok=True)
try:
# Load testing modules lazily
modules = _get_testing_modules()
if modules.is_empty():
log_step_warning(logger, "Enhanced testing modules not available")
return False
# Initialize enhanced round-trip tester
temp_dir = round_trip_output_dir / "temp"
temp_dir.mkdir(exist_ok=True)
tester = modules.GNNRoundTripTester(temp_dir)
# Configure reference file
if reference_file:
ref_path = Path(reference_file)
if not ref_path.is_absolute():
ref_path = target_dir / ref_path
if ref_path.exists():
tester.reference_file = ref_path
logger.info(f"Using custom reference file: {ref_path}")
else:
log_step_warning(logger, f"Custom reference file not found: {ref_path}")
# Verify reference file
if not tester.reference_file.exists():
log_step_error(logger, f"Reference file not found: {tester.reference_file}")
return False
logger.info(f"Testing with reference: {tester.reference_file}")
# Configure test subset if specified
_original_formats = tester.supported_formats.copy()
if test_subset:
# Filter to requested formats
subset_formats = []
for fmt_name in test_subset:
try:
fmt = modules.GNNFormat(fmt_name.lower())
if fmt in tester.supported_formats:
subset_formats.append(fmt)
else:
logger.warning(f"Format {fmt_name} not supported")
except ValueError:
logger.warning(f"Unknown format: {fmt_name}")
if subset_formats:
tester.supported_formats = [modules.GNNFormat.MARKDOWN] + subset_formats
logger.info(f"Testing subset: {[f.value for f in subset_formats]}")
else:
logger.warning("No valid formats in subset, using all supported")
# Run comprehensive tests with enhanced reporting
logger.info("Starting enhanced round-trip testing...")
start_time = time.time()
report = tester.run_comprehensive_tests()
test_time = time.time() - start_time
# Generate enhanced report
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
report_file = round_trip_output_dir / f"enhanced_round_trip_report_{timestamp}.md"
_enhanced_report_content = tester.generate_report(report, report_file)
# Add performance analysis
performance_analysis = _generate_performance_analysis(report, test_time)
with open(report_file, 'a') as f:
f.write(f"\n\n{performance_analysis}")
# Enhanced summary logging
success_rate = report.get_success_rate()
format_summary = report.get_format_summary()
logger.info("Enhanced round-trip testing completed:")
logger.info(f" 📊 Total tests: {report.total_tests}")
logger.info(f" ✅ Successful: {report.successful_tests}")
logger.info(f" ❌ Failed: {report.failed_tests}")
logger.info(f" 📈 Success rate: {success_rate:.1f}%")
logger.info(f" ⏱️ Test time: {test_time:.3f}s")
logger.info(f" 📄 Report: {report_file}")
# Format category performance
categories = [
("Schema", ['json', 'xml', 'yaml', 'xsd', 'asn1', 'pkl', 'protobuf']),
("Language", ['scala', 'lean', 'coq', 'python', 'haskell', 'isabelle']),
("Formal", ['tla_plus', 'agda', 'alloy', 'z_notation', 'bnf', 'ebnf']),
("Other", ['maxima', 'pickle'])
]
for category, formats in categories:
category_success = sum(1 for fmt_name in formats
if any(fmt.value == fmt_name and summary.get('success', 0) > 0
for fmt, summary in format_summary.items()))
category_total = len([fmt for fmt_name in formats
if any(fmt.value == fmt_name for fmt in format_summary.keys())])
if category_total > 0:
category_rate = (category_success / category_total) * 100
status = "✅" if category_rate == 100 else "⚠️" if category_rate >= 50 else "❌"
logger.info(f" {status} {category}: {category_success}/{category_total} ({category_rate:.1f}%)")
# Determine success
if success_rate == 100.0:
log_step_success(logger, "🎉 ALL ROUND-TRIP TESTS PASSED! Perfect format interoperability achieved.")
return True
elif success_rate >= 80.0:
log_step_success(logger, f"🎊 EXCELLENT round-trip performance: {success_rate:.1f}%")
return True
else:
log_step_warning(logger, f"⚠️ Round-trip issues found: {success_rate:.1f}% success rate")
# Log specific failures for debugging
failed_formats = [result.target_format.value for result in report.round_trip_results
if not result.success]
if failed_formats:
logger.warning(f"Failed formats: {', '.join(failed_formats)}")
return False
except Exception as e:
log_step_error(logger, f"Enhanced round-trip testing failed: {e}")
logger.exception("Detailed error:")
return False
def validate_gnn_cross_format_consistency(
target_dir: Path,
output_dir: Path,
logger: logging.Logger,
files_to_test: Optional[List[str]] = None,
include_binary: bool = False,
**kwargs
) -> bool:
"""
Enhanced cross-format consistency validation with comprehensive analysis.
Args:
target_dir: Directory containing GNN files to test
output_dir: Output directory for validation results
logger: Logger instance
files_to_test: Optional list of specific files to test
include_binary: Whether to include binary formats in validation
**kwargs: Additional validation options
Returns:
True if all validations passed, False otherwise
"""
if not is_enhanced_testing_available():
log_step_warning(logger, "Enhanced cross-format validation not available")
return False
log_step_start(logger, "Enhanced cross-format consistency validation")
# Create enhanced output directory
validation_output_dir = output_dir / "enhanced_cross_format_validation"
validation_output_dir.mkdir(parents=True, exist_ok=True)
try:
# Load testing modules lazily
modules = _get_testing_modules()
if modules.is_empty():
log_step_warning(logger, "Enhanced validation modules not available")
return False
# Initialize enhanced validators
modules.GNNValidator(validation_level=modules.ValidationLevel.STRICT)
cross_validator = modules.CrossFormatValidator()
# Discover GNN files to test
if files_to_test:
gnn_files = [target_dir / f for f in files_to_test if (target_dir / f).exists()]
else:
# Enhanced file discovery
extensions = ['*.md', '*.json', '*.xml', '*.yaml']
if include_binary:
extensions.extend(['*.pkl', '*.pickle'])
gnn_files = []
for ext in extensions:
gnn_files.extend(target_dir.glob(ext))
# Filter for GNN-relevant files
gnn_files = [f for f in gnn_files if _is_gnn_file(f)]
if not gnn_files:
log_step_warning(logger, "No GNN files found for cross-format validation")
return True
logger.info(f"Testing cross-format consistency for {len(gnn_files)} files")
# Enhanced validation tracking
validation_results = {
'total_files': len(gnn_files),
'consistent_files': 0,
'inconsistent_files': 0,
'validation_errors': 0,
'files': [],
'format_analysis': {},
'performance_metrics': {}
}
start_time = time.time()
for gnn_file in gnn_files:
logger.info(f"Validating: {gnn_file.relative_to(target_dir)}")
file_start = time.time()
try:
# Read file content
if gnn_file.suffix.lower() in ['.pkl', '.pickle']:
# Handle binary files
result = _validate_binary_cross_format(gnn_file, cross_validator)
else:
with open(gnn_file, 'r', encoding='utf-8') as f:
content = f.read()
# Test cross-format consistency
cross_result = cross_validator.validate_cross_format_consistency(content)
result = cross_result
file_time = time.time() - file_start
# Process results
file_result = {
'file': str(gnn_file.relative_to(target_dir)),
'format': gnn_file.suffix.lower(),
'is_consistent': result.is_consistent,
'formats_tested': getattr(result, 'schema_formats', []),
'inconsistencies': getattr(result, 'inconsistencies', []),
'warnings': getattr(result, 'warnings', []),
'metadata': getattr(result, 'metadata', {}),
'validation_time': file_time
}
validation_results['files'].append(file_result)
# Update counters
if result.is_consistent:
validation_results['consistent_files'] += 1
logger.info(f" ✅ Consistent across {len(getattr(result, 'schema_formats', []))} formats ({file_time:.3f}s)")
else:
validation_results['inconsistent_files'] += 1
logger.warning(f" ❌ Inconsistent - {len(getattr(result, 'inconsistencies', []))} issues ({file_time:.3f}s)")
for issue in getattr(result, 'inconsistencies', [])[:3]: # Show first 3
logger.warning(f" • {issue}")
# Track format analysis
file_format = gnn_file.suffix.lower()
if file_format not in validation_results['format_analysis']:
validation_results['format_analysis'][file_format] = {
'total': 0, 'consistent': 0, 'avg_time': 0
}
format_stats = validation_results['format_analysis'][file_format]
format_stats['total'] += 1
if result.is_consistent:
format_stats['consistent'] += 1
format_stats['avg_time'] = (format_stats['avg_time'] * (format_stats['total'] - 1) + file_time) / format_stats['total']
except Exception as e:
logger.error(f" ❌ Validation error: {e}")
validation_results['validation_errors'] += 1
file_result = {
'file': str(gnn_file.relative_to(target_dir)),
'format': gnn_file.suffix.lower(),
'is_consistent': False,
'error': str(e),
'validation_time': time.time() - file_start
}
validation_results['files'].append(file_result)
total_time = time.time() - start_time
# Enhanced performance metrics
validation_results['performance_metrics'] = {
'total_time': total_time,
'avg_file_time': total_time / len(gnn_files) if gnn_files else 0,
'files_per_second': len(gnn_files) / total_time if total_time > 0 else 0
}
# Generate enhanced report
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
report_file = validation_output_dir / f"enhanced_cross_format_validation_{timestamp}.json"
with open(report_file, 'w', encoding='utf-8') as f:
json.dump(validation_results, f, indent=2, default=str)
# Summary and results
success_rate = (validation_results['consistent_files'] / validation_results['total_files'] * 100) \
if validation_results['total_files'] > 0 else 0
logger.info("Enhanced cross-format validation completed:")
logger.info(f" 📁 Files tested: {validation_results['total_files']}")
logger.info(f" ✅ Consistent: {validation_results['consistent_files']}")
logger.info(f" ❌ Inconsistent: {validation_results['inconsistent_files']}")
logger.info(f" ⚠️ Errors: {validation_results['validation_errors']}")
logger.info(f" 📈 Success rate: {success_rate:.1f}%")
logger.info(f" ⏱️ Total time: {total_time:.3f}s")
logger.info(f" 📊 Report: {report_file}")
# Format analysis summary
if validation_results['format_analysis']:
logger.info(" 📋 Format analysis:")
for fmt, stats in validation_results['format_analysis'].items():
fmt_rate = (stats['consistent'] / stats['total'] * 100) if stats['total'] > 0 else 0
logger.info(f" {fmt}: {stats['consistent']}/{stats['total']} ({fmt_rate:.1f}%, {stats['avg_time']:.3f}s avg)")
if success_rate == 100.0:
log_step_success(logger, "✅ All files passed cross-format consistency validation")
return True
elif success_rate >= 80.0:
log_step_success(logger, f"🎊 Good cross-format consistency: {success_rate:.1f}%")
return True
else:
log_step_warning(logger, f"❌ Cross-format consistency issues: {success_rate:.1f}% success")
return False
except Exception as e:
log_step_error(logger, f"Enhanced cross-format validation failed: {e}")
logger.exception("Detailed error:")
return False