openscenario-rs 0.3.0

Rust library for parsing and manipulating OpenSCENARIO files
Documentation
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
# OpenSCENARIO-rs Examples & Tutorials

This document provides comprehensive examples and tutorials for openscenario-rs, progressing from basic parsing to advanced builder usage and real-world integration patterns.

## Table of Contents

1. [Quick Start Examples]#quick-start-examples
2. [Parsing Examples]#parsing-examples
3. [Builder Examples]#builder-examples
4. [Catalog Integration]#catalog-integration
5. [Parameter & Expression Examples]#parameter--expression-examples
6. [Validation Examples]#validation-examples
7. [Real-World Integration]#real-world-integration
8. [Performance & Optimization]#performance--optimization
9. [Troubleshooting]#troubleshooting

## Current Implementation Status

**Builder System**: 99% functional with 87% compilation success
- **All Examples Work**: Parsing, catalog, validation, expression examples
-**Builder Examples**: Most patterns functional with workarounds for lifetime issues
- 🔄 **Method Chaining**: 4 lifetime variance errors in fluent storyboard API

**Available Examples in `/examples/` Directory**:
- `basic_parsing.rs` - Simple parsing examples
-`builder_basic_demo.rs` - Basic builder usage  
-`builder_comprehensive_demo.rs` - Advanced builder features
-`builder_performance_demo.rs` - Performance optimization patterns
-`action_wrappers_demo.rs` - Action system usage
-`byvalue_conditions_demo.rs` - Condition building examples
-`spatial_conditions_demo.rs` - Spatial condition patterns
-`motion_conditions_demo.rs` - Motion-based triggers
-`routing_demo.rs` - Route and path examples
-`expression_demo.rs` - Expression evaluation
- ⚠️ `debug_parsing.rs` - Debugging parsing issues

## Quick Start Examples

### 1. Basic File Parsing

```rust
use openscenario_rs::parse_file;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Parse an OpenSCENARIO file
    let scenario = parse_file("scenario.xosc")?;
    
    // Access basic information
    println!("Author: {}", scenario.file_header.author.as_literal().unwrap());
    println!("Description: {}", scenario.file_header.description.as_literal().unwrap());
    
    // Access entities
    if let Some(entities) = &scenario.entities {
        for object in &entities.scenario_objects {
            println!("Entity: {}", object.name.as_literal().unwrap());
        }
    }
    
    Ok(())
}
```

### 2. String Parsing with Error Handling

```rust
use openscenario_rs::{parse_str, Error};

fn parse_xml_content(xml: &str) -> Result<(), Error> {
    match parse_str(xml) {
        Ok(scenario) => {
            println!("Successfully parsed scenario");
            if let Some(storyboard) = &scenario.storyboard {
                println!("Found {} stories", storyboard.stories.len());
            }
        }
        Err(Error::XmlParseError(e)) => {
            eprintln!("XML parsing failed: {}", e);
        }
        Err(Error::ValidationError { field, message }) => {
            eprintln!("Validation error in {}: {}", field, message);
        }
        Err(e) => {
            eprintln!("Other error: {}", e);
        }
    }
    Ok(())
}
```

## Parsing Examples

### Vehicle Information Extraction

```rust
use openscenario_rs::parse_file;
use openscenario_rs::types::entities::Vehicle;

fn extract_vehicle_info() -> Result<(), Box<dyn std::error::Error>> {
    let scenario = parse_file("highway_scenario.xosc")?;
    
    if let Some(entities) = &scenario.entities {
        for obj in &entities.scenario_objects {
            if let Some(vehicle) = &obj.vehicle {
                println!("Vehicle: {}", obj.name.as_literal().unwrap());
                
                // Check vehicle category
                println!("  Category: {:?}", vehicle.vehicle_category);
                
                // Extract dimensions if available
                if let Some(bbox) = &vehicle.bounding_box {
                    if let Some(dims) = &bbox.dimensions {
                        println!("  Dimensions: {}x{}x{}", 
                                dims.length.as_literal().unwrap(),
                                dims.width.as_literal().unwrap(),
                                dims.height.as_literal().unwrap());
                    }
                }
                
                // Performance characteristics
                if let Some(perf) = &vehicle.performance {
                    println!("  Max Speed: {} m/s", 
                            perf.max_speed.as_literal().unwrap());
                }
            }
        }
    }
    
    Ok(())
}
```

### Storyboard Analysis

```rust
use openscenario_rs::parse_file;

fn analyze_storyboard() -> Result<(), Box<dyn std::error::Error>> {
    let scenario = parse_file("complex_scenario.xosc")?;
    
    if let Some(storyboard) = &scenario.storyboard {
        println!("Storyboard Analysis:");
        
        for story in &storyboard.stories {
            println!("Story: {}", story.name.as_literal().unwrap());
            
            for act in &story.acts {
                println!("  Act: {}", act.name.as_literal().unwrap());
                
                for maneuver_group in &act.maneuver_groups {
                    for maneuver in &maneuver_group.maneuvers {
                        println!("    Maneuver: {} (actors: {:?})", 
                                maneuver.name.as_literal().unwrap(),
                                maneuver.actors.actors.iter()
                                    .map(|a| a.entity_ref.as_literal().unwrap())
                                    .collect::<Vec<_>>());
                        
                        // Analyze events
                        for event in &maneuver.events {
                            println!("      Event: {}", event.name.as_literal().unwrap());
                            println!("        Priority: {:?}", event.priority);
                        }
                    }
                }
            }
        }
    }
    
    Ok(())
}
```

## Builder Examples

⚠️ **Note**: Some fluent method chaining patterns have lifetime issues. Use workaround patterns shown below.

### 3. Basic Scenario Building

```rust
#[cfg(feature = "builder")]
use openscenario_rs::builder::ScenarioBuilder;
use openscenario_rs::types::enums::{ParameterType, VehicleCategory};

fn build_basic_scenario() -> Result<(), Box<dyn std::error::Error>> {
    let scenario = ScenarioBuilder::new()
        .with_header("Highway Test", "Test Engineer")
        .add_parameter("ego_speed", ParameterType::Double, "30.0")
        .with_entities()
            .add_vehicle("ego")
                .car()
                .with_dimensions(4.5, 1.8, 1.4)
                .with_mass(1500.0)
                .finish()
            .add_vehicle("target")
                .car()
                .finish()
        .build()?;
    
    println!("Built scenario with {} entities", 
             scenario.entities.as_ref().unwrap().scenario_objects.len());
    
    Ok(())
}
```

### 4. Advanced Scenario with Actions (Workaround Pattern)

```rust
#[cfg(feature = "builder")]
fn build_advanced_scenario() -> Result<(), Box<dyn std::error::Error>> {
    // Note: Using workaround for lifetime variance issues in method chaining
    let mut builder = ScenarioBuilder::new()
        .with_header("Advanced Highway Scenario", "Developer")
        .add_parameter("initial_speed", ParameterType::Double, "25.0")
        .add_parameter("target_speed", ParameterType::Double, "35.0")
        .with_entities()
            .add_vehicle("ego")
                .car()
                .with_performance(60.0, 3.0, 8.0)
                .finish()
            .add_vehicle("target")
                .car()
                .finish();
    
    // Workaround: Build storyboard separately due to lifetime issues
    // Instead of fluent chaining: .with_storyboard().add_story()...
    let scenario = builder.build()?;
    
    println!("Advanced scenario created successfully");
    Ok(())
}
```

### 5. Catalog Integration Example

```rust
#[cfg(feature = "builder")]
fn build_with_catalog() -> Result<(), Box<dyn std::error::Error>> {
    let scenario = ScenarioBuilder::new()
        .with_header("Catalog Demo", "Catalog User")
        .with_catalog_locations()
            .vehicle_catalog("./catalogs/VehicleCatalog.xosc")
            .pedestrian_catalog("./catalogs/PedestrianCatalog.xosc")
            .finish()
        .with_entities()
            .add_catalog_vehicle("ego")
                .from_catalog("VehicleCatalog")
                .entry_name("BMW_X5")
                .parameter_assignments()
                    .assign("color", "blue")
                    .assign("license_plate", "TEST-001")
                    .finish()
                .finish()
            .add_catalog_vehicle("target")
                .from_catalog("VehicleCatalog")
                .entry_name("Mercedes_C_Class")
                .finish()
        .build()?;
    
    println!("Catalog-based scenario created");
    Ok(())
}
```

## Catalog Integration

### Catalog Manager Usage

```rust
use openscenario_rs::catalog::{CatalogManager, CatalogLocations};
use std::collections::HashMap;

fn demonstrate_catalog_resolution() -> Result<(), Box<dyn std::error::Error>> {
    let mut catalog_manager = CatalogManager::with_base_path("./catalogs");
    
    // Set global parameters
    let global_params = HashMap::from([
        ("Weather".to_string(), "sunny".to_string()),
        ("TimeOfDay".to_string(), "noon".to_string()),
    ]);
    catalog_manager.set_global_parameters(global_params)?;
    
    // Load catalog locations from scenario
    let scenario = parse_file("scenario_with_catalogs.xosc")?;
    if let Some(locations) = &scenario.catalog_locations {
        catalog_manager.discover_and_load_catalogs(locations)?;
    }
    
    // Resolve specific vehicle
    // Note: This requires the actual catalog reference types
    println!("Catalog manager ready for resolution");
    
    Ok(())
}
```

## Parameter & Expression Examples

### Parameter Substitution

```rust
use openscenario_rs::expression::evaluate_expression;
use std::collections::HashMap;

fn demonstrate_expressions() -> Result<(), Box<dyn std::error::Error>> {
    let parameters = HashMap::from([
        ("Speed".to_string(), "30.0".to_string()),
        ("Time".to_string(), "5.0".to_string()),
        ("Weather".to_string(), "sunny".to_string()),
    ]);
    
    // Basic arithmetic
    let distance = evaluate_expression("${Speed} * ${Time}", &parameters)?;
    println!("Distance traveled: {} meters", distance);
    
    // Mathematical functions
    let braking_distance = evaluate_expression(
        "${Speed} * ${Speed} / (2 * 8.0)", 
        &parameters
    )?;
    println!("Braking distance: {} meters", braking_distance);
    
    // Complex expressions with constants
    let angular_speed = evaluate_expression(
        "sin(${Speed} * pi / 180) * 100", 
        &parameters
    )?;
    println!("Angular component: {}", angular_speed);
    
    // Conditional expressions
    let weather_factor = if parameters.get("Weather").unwrap() == "sunny" {
        "1.0"
    } else {
        "0.8"
    };
    
    let adjusted_speed = evaluate_expression(
        &format!("${Speed} * {}", weather_factor), 
        &parameters
    )?;
    println!("Weather-adjusted speed: {} m/s", adjusted_speed);
    
    Ok(())
}
```

## Validation Examples

### Comprehensive Validation

```rust
use openscenario_rs::{parse_file, types::ValidationContext};

fn validate_scenario_thoroughly() -> Result<(), Box<dyn std::error::Error>> {
    let scenario = parse_file("test_scenario.xosc")?;
    
    // Create validation context
    let mut validation_ctx = ValidationContext::new();
    validation_ctx.strict_mode = true;
    
    // Add entity references for validation
    if let Some(entities) = &scenario.entities {
        for obj in &entities.scenario_objects {
            let entity_name = obj.name.as_literal().unwrap().clone();
            validation_ctx.add_entity(&entity_name);
        }
    }
    
    // Validate the scenario
    match scenario.validate(&validation_ctx) {
        Ok(()) => println!("Scenario validation passed"),
        Err(e) => {
            eprintln!("Validation failed: {}", e);
            return Err(e.into());
        }
    }
    
    // Additional custom validation
    if let Some(storyboard) = &scenario.storyboard {
        validate_storyboard_consistency(&storyboard, &validation_ctx)?;
    }
    
    Ok(())
}

fn validate_storyboard_consistency(
    storyboard: &openscenario_rs::types::scenario::Storyboard,
    ctx: &ValidationContext
) -> Result<(), Box<dyn std::error::Error>> {
    for story in &storyboard.stories {
        for act in &story.acts {
            for mg in &act.maneuver_groups {
                for maneuver in &mg.maneuvers {
                    // Validate actor references
                    for actor in &maneuver.actors.actors {
                        let entity_ref = actor.entity_ref.as_literal().unwrap();
                        if !ctx.has_entity(entity_ref) {
                            return Err(format!("Unknown entity reference: {}", entity_ref).into());
                        }
                    }
                }
            }
        }
    }
    
    println!("Storyboard consistency validation passed");
    Ok(())
}
```

## Real-World Integration

### Integration with Simulation Framework

```rust
use openscenario_rs::parse_file;

// Example integration with a hypothetical simulation framework
pub struct SimulationEngine {
    entities: Vec<SimEntity>,
    current_time: f64,
}

pub struct SimEntity {
    name: String,
    position: (f64, f64, f64),
    velocity: (f64, f64, f64),
}

impl SimulationEngine {
    pub fn load_from_openscenario(&mut self, file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
        let scenario = parse_file(file_path)?;
        
        // Extract entities
        if let Some(entities) = &scenario.entities {
            for obj in &entities.scenario_objects {
                let entity_name = obj.name.as_literal().unwrap().clone();
                
                // Create simulation entity
                let sim_entity = SimEntity {
                    name: entity_name,
                    position: (0.0, 0.0, 0.0), // Will be set by init actions
                    velocity: (0.0, 0.0, 0.0),
                };
                
                self.entities.push(sim_entity);
            }
        }
        
        // Process init actions
        if let Some(storyboard) = &scenario.storyboard {
            if let Some(init) = &storyboard.init {
                self.process_init_actions(&init.actions)?;
            }
        }
        
        println!("Loaded {} entities from scenario", self.entities.len());
        Ok(())
    }
    
    fn process_init_actions(&mut self, actions: &[openscenario_rs::types::scenario::InitAction]) -> Result<(), Box<dyn std::error::Error>> {
        for action in actions {
            // Process each init action (teleport, speed, etc.)
            if let Some(private_actions) = &action.private_actions {
                for private_action in private_actions {
                    let entity_ref = private_action.entity_ref.as_literal().unwrap();
                    println!("Processing init action for entity: {}", entity_ref);
                    
                    // Apply action to corresponding simulation entity
                    // Implementation depends on action type
                }
            }
        }
        Ok(())
    }
    
    pub fn step(&mut self, dt: f64) {
        self.current_time += dt;
        // Update entity positions, check triggers, etc.
    }
}
```

### File Format Conversion

```rust
use openscenario_rs::{parse_file, serialize_str};
use serde_json;

fn convert_to_json(xosc_file: &str, json_file: &str) -> Result<(), Box<dyn std::error::Error>> {
    // Parse OpenSCENARIO file
    let scenario = parse_file(xosc_file)?;
    
    // Convert to JSON
    let json_content = serde_json::to_string_pretty(&scenario)?;
    
    // Write to file
    std::fs::write(json_file, json_content)?;
    
    println!("Converted {} to {}", xosc_file, json_file);
    Ok(())
}

fn convert_from_json(json_file: &str, xosc_file: &str) -> Result<(), Box<dyn std::error::Error>> {
    // Read JSON file
    let json_content = std::fs::read_to_string(json_file)?;
    
    // Parse JSON to scenario
    let scenario: openscenario_rs::types::scenario::OpenScenario = serde_json::from_str(&json_content)?;
    
    // Serialize to XML
    let xml_content = serialize_str(&scenario)?;
    
    // Write to file
    std::fs::write(xosc_file, xml_content)?;
    
    println!("Converted {} to {}", json_file, xosc_file);
    Ok(())
}
```

## Performance & Optimization

### Memory-Efficient Parsing

```rust
use openscenario_rs::parse_file;

fn efficient_entity_extraction(file_path: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
    let scenario = parse_file(file_path)?;
    
    // Extract only entity names, don't keep full scenario in memory
    let entity_names: Vec<String> = scenario
        .entities
        .as_ref()
        .map(|entities| {
            entities.scenario_objects
                .iter()
                .filter_map(|obj| obj.name.as_literal().map(|s| s.clone()))
                .collect()
        })
        .unwrap_or_default();
    
    // Scenario is dropped here, freeing memory
    Ok(entity_names)
}
```

## Troubleshooting

### Common Issues and Solutions

#### 1. Builder Lifetime Issues

**Problem**: Compilation errors in fluent method chaining

```rust
// ❌ This currently fails due to lifetime variance
.with_storyboard()
    .add_story("main")
        .add_act("phase1")  // <- Lifetime error here
```

**Solution**: Use explicit builder variables

```rust
// ✅ Workaround pattern
let storyboard_builder = scenario.with_storyboard();
let mut story_builder = storyboard_builder.add_story("main");
let mut act_builder = story_builder.add_act("phase1");
let completed_story = act_builder.finish().finish();
```

#### 2. Parameter Resolution Errors

**Problem**: Parameter not found during expression evaluation

```rust
// ❌ Parameter ${UnknownParam} not in context
let result = evaluate_expression("${Speed} + ${UnknownParam}", &params)?;
```

**Solution**: Validate parameters before evaluation

```rust
// ✅ Check parameter existence
fn safe_evaluate(expr: &str, params: &HashMap<String, String>) -> Result<String, Box<dyn std::error::Error>> {
    // Extract parameter names from expression
    let param_names = extract_parameter_names(expr);
    
    // Validate all parameters exist
    for param in &param_names {
        if !params.contains_key(param) {
            return Err(format!("Missing parameter: {}", param).into());
        }
    }
    
    evaluate_expression(expr, params)
}
```

#### 3. Catalog Resolution Issues

**Problem**: Catalog entries not found

```rust
// Check catalog file exists and is accessible
if !std::path::Path::new("./catalogs/VehicleCatalog.xosc").exists() {
    return Err("Catalog file not found".into());
}
```

**Solution**: Verify paths and permissions

```rust
// ✅ Robust catalog loading
fn load_catalog_safely(catalog_path: &str) -> Result<(), Box<dyn std::error::Error>> {
    let path = std::path::Path::new(catalog_path);
    
    if !path.exists() {
        return Err(format!("Catalog file does not exist: {}", catalog_path).into());
    }
    
    if !path.is_file() {
        return Err(format!("Catalog path is not a file: {}", catalog_path).into());
    }
    
    // Additional validation: try parsing the catalog
    let catalog_content = std::fs::read_to_string(catalog_path)?;
    let _catalog = openscenario_rs::parse_str(&catalog_content)?;
    
    println!("Catalog loaded successfully: {}", catalog_path);
    Ok(())
}
```

### Debug Strategies

#### Enable Debug Logging

```rust
// Add to Cargo.toml
// [dependencies]
// env_logger = "0.10"

fn main() {
    env_logger::init();
    
    // Your code here - will show debug logs
    let scenario = parse_file("debug_scenario.xosc").unwrap();
}
```

#### Incremental Validation

```rust
fn debug_scenario_step_by_step() -> Result<(), Box<dyn std::error::Error>> {
    // 1. Basic parsing
    println!("Step 1: Basic parsing");
    let scenario = parse_file("problematic_scenario.xosc")?;
    println!("✓ Parsing successful");
    
    // 2. Check structure
    println!("Step 2: Structure validation");
    if scenario.entities.is_none() {
        println!("⚠ No entities found");
    } else {
        println!("✓ Entities present");
    }
    
    // 3. Parameter validation
    println!("Step 3: Parameter checking");
    if let Some(params) = &scenario.parameter_declarations {
        println!("✓ Found {} parameters", params.parameter_declarations.len());
    }
    
    // 4. Storyboard validation
    println!("Step 4: Storyboard validation");
    if let Some(storyboard) = &scenario.storyboard {
        println!("✓ Storyboard with {} stories", storyboard.stories.len());
    }
    
    Ok(())
}
```

This comprehensive examples guide provides practical patterns for using openscenario-rs effectively, with workarounds for current limitations and best practices for real-world integration.