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
//! Validation integration for builder system
//!
//! This module provides validation support for the builder system,
//! integrating with the existing validation framework to ensure
//! built scenarios comply with OpenSCENARIO schema requirements.

use crate::builder::{BuilderError, BuilderResult};
use crate::types::scenario::storyboard::OpenScenario;
use crate::types::ValidationContext;
use std::collections::HashMap;

/// Builder validation context that extends the existing validation framework
#[derive(Default)]
pub struct BuilderValidationContext {
    /// Base validation context
    base_context: ValidationContext,
    /// Builder-specific validation rules
    builder_rules: Vec<Box<dyn BuilderValidationRule>>,
    /// Entity reference tracking
    entity_refs: HashMap<String, String>,
    /// Parameter tracking
    parameters: HashMap<String, String>,
}

impl std::fmt::Debug for BuilderValidationContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BuilderValidationContext")
            .field("base_context", &self.base_context)
            .field(
                "builder_rules",
                &format!("{} rules", self.builder_rules.len()),
            )
            .field("entity_refs", &self.entity_refs)
            .field("parameters", &self.parameters)
            .finish()
    }
}

impl BuilderValidationContext {
    /// Create a new builder validation context
    pub fn new() -> Self {
        Self {
            base_context: ValidationContext::new(),
            builder_rules: Vec::new(),
            entity_refs: HashMap::new(),
            parameters: HashMap::new(),
        }
    }

    /// Add a builder-specific validation rule
    pub fn add_rule(mut self, rule: Box<dyn BuilderValidationRule>) -> Self {
        self.builder_rules.push(rule);
        self
    }

    /// Register an entity reference
    pub fn register_entity(&mut self, name: &str, entity_type: &str) {
        self.entity_refs
            .insert(name.to_string(), entity_type.to_string());
    }

    /// Register a parameter
    pub fn register_parameter(&mut self, name: &str, value: &str) {
        self.parameters.insert(name.to_string(), value.to_string());
    }

    /// Validate an entity reference exists
    pub fn validate_entity_ref(&self, entity_ref: &str) -> BuilderResult<()> {
        if !self.entity_refs.contains_key(entity_ref) {
            return Err(BuilderError::invalid_entity_ref(
                entity_ref,
                &self.entity_refs.keys().cloned().collect::<Vec<_>>(),
            ));
        }
        Ok(())
    }

    /// Validate a parameter reference exists
    pub fn validate_parameter_ref(&self, param_ref: &str) -> BuilderResult<()> {
        if !self.parameters.contains_key(param_ref) {
            return Err(BuilderError::validation_error(&format!(
                "Parameter '{}' not found. Available parameters: {}",
                param_ref,
                self.parameters
                    .keys()
                    .cloned()
                    .collect::<Vec<_>>()
                    .join(", ")
            )));
        }
        Ok(())
    }

    /// Validate a complete scenario
    pub fn validate_scenario(&self, scenario: &OpenScenario) -> BuilderResult<()> {
        // Run base validation first - ValidationContext doesn't have validate_scenario method
        // For now, we'll skip base validation and only run builder-specific rules

        // Run builder-specific validation rules
        for rule in &self.builder_rules {
            rule.validate(scenario, self)?;
        }

        Ok(())
    }

    /// Get all registered entities
    pub fn entities(&self) -> &HashMap<String, String> {
        &self.entity_refs
    }

    /// Get all registered parameters
    pub fn parameters(&self) -> &HashMap<String, String> {
        &self.parameters
    }
}

/// Trait for builder-specific validation rules
pub trait BuilderValidationRule {
    /// Validate a scenario using this rule
    fn validate(
        &self,
        scenario: &OpenScenario,
        context: &BuilderValidationContext,
    ) -> BuilderResult<()>;

    /// Get the name of this validation rule
    fn name(&self) -> &str;

    /// Get the description of this validation rule
    fn description(&self) -> &str;
}

/// Validation rule for entity references
#[derive(Debug)]
pub struct EntityReferenceValidationRule;

impl BuilderValidationRule for EntityReferenceValidationRule {
    fn validate(
        &self,
        scenario: &OpenScenario,
        context: &BuilderValidationContext,
    ) -> BuilderResult<()> {
        // Validate that all entity references in maneuver groups exist
        if let Some(storyboard) = &scenario.storyboard {
            for story in &storyboard.stories {
                for act in &story.acts {
                    for maneuver_group in &act.maneuver_groups {
                        // Validate entity references in actors
                        for entity_ref in &maneuver_group.actors.entity_refs {
                            let entity_name = entity_ref.entity_ref.to_string();
                            context.validate_entity_ref(&entity_name)?;
                        }
                    }
                }
            }
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "EntityReferenceValidation"
    }

    fn description(&self) -> &str {
        "Validates that all entity references in actions and conditions exist"
    }
}

/// Validation rule for parameter references
#[derive(Debug)]
pub struct ParameterReferenceValidationRule;

impl BuilderValidationRule for ParameterReferenceValidationRule {
    fn validate(
        &self,
        scenario: &OpenScenario,
        context: &BuilderValidationContext,
    ) -> BuilderResult<()> {
        // This would validate that all parameter references are declared
        // For now, we'll do a basic check
        if let Some(param_decls) = &scenario.parameter_declarations {
            for param in &param_decls.parameter_declarations {
                let param_name = param.name.to_string();
                if !context.parameters.contains_key(&param_name) {
                    // This is actually OK - parameters can be declared without being used
                    // But we could warn about unused parameters
                }
            }
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "ParameterReferenceValidation"
    }

    fn description(&self) -> &str {
        "Validates that all parameter references are properly declared"
    }
}

/// Validation rule for catalog references
#[derive(Debug)]
pub struct CatalogReferenceValidationRule;

impl BuilderValidationRule for CatalogReferenceValidationRule {
    fn validate(
        &self,
        scenario: &OpenScenario,
        _context: &BuilderValidationContext,
    ) -> BuilderResult<()> {
        // Validate that catalog locations are specified if catalog references are used
        if let Some(entities) = &scenario.entities {
            let has_catalog_refs = entities
                .scenario_objects
                .iter()
                .any(|obj| obj.entity_catalog_reference.is_some());

            if has_catalog_refs && scenario.catalog_locations.is_none() {
                return Err(BuilderError::validation_error(
                    "Catalog references found but no catalog locations specified",
                ));
            }
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "CatalogReferenceValidation"
    }

    fn description(&self) -> &str {
        "Validates that catalog locations are specified when catalog references are used"
    }
}

/// Validation rule for storyboard structure
#[derive(Debug)]
pub struct StoryboardStructureValidationRule;

impl BuilderValidationRule for StoryboardStructureValidationRule {
    fn validate(
        &self,
        scenario: &OpenScenario,
        _context: &BuilderValidationContext,
    ) -> BuilderResult<()> {
        if let Some(storyboard) = &scenario.storyboard {
            // Validate that stories have at least one act
            for story in &storyboard.stories {
                if story.acts.is_empty() {
                    return Err(BuilderError::validation_error(&format!(
                        "Story '{}' has no acts",
                        story.name.to_string()
                    )));
                }

                // Validate that acts have at least one maneuver group
                for act in &story.acts {
                    if act.maneuver_groups.is_empty() {
                        return Err(BuilderError::validation_error(&format!(
                            "Act '{}' has no maneuver groups",
                            act.name.to_string()
                        )));
                    }

                    // Validate that maneuver groups have at least one maneuver
                    for maneuver_group in &act.maneuver_groups {
                        if maneuver_group.maneuvers.is_empty() {
                            return Err(BuilderError::validation_error(&format!(
                                "Maneuver group '{}' has no maneuvers",
                                maneuver_group.name.to_string()
                            )));
                        }
                    }
                }
            }
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "StoryboardStructureValidation"
    }

    fn description(&self) -> &str {
        "Validates the hierarchical structure of the storyboard"
    }
}

/// Builder for validation contexts with common rules
#[derive(Default)]
pub struct ValidationContextBuilder {
    rules: Vec<Box<dyn BuilderValidationRule>>,
    entities: HashMap<String, String>,
    parameters: HashMap<String, String>,
}

impl std::fmt::Debug for ValidationContextBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ValidationContextBuilder")
            .field("rules", &format!("{} rules", self.rules.len()))
            .field("entities", &self.entities)
            .field("parameters", &self.parameters)
            .finish()
    }
}

impl ValidationContextBuilder {
    /// Create a new validation context builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Add standard validation rules
    pub fn with_standard_rules(mut self) -> Self {
        self.rules.push(Box::new(EntityReferenceValidationRule));
        self.rules.push(Box::new(ParameterReferenceValidationRule));
        self.rules.push(Box::new(CatalogReferenceValidationRule));
        self.rules.push(Box::new(StoryboardStructureValidationRule));
        self
    }

    /// Add a custom validation rule
    pub fn with_rule(mut self, rule: Box<dyn BuilderValidationRule>) -> Self {
        self.rules.push(rule);
        self
    }

    /// Add entity information
    pub fn with_entity(mut self, name: &str, entity_type: &str) -> Self {
        self.entities
            .insert(name.to_string(), entity_type.to_string());
        self
    }

    /// Add parameter information
    pub fn with_parameter(mut self, name: &str, value: &str) -> Self {
        self.parameters.insert(name.to_string(), value.to_string());
        self
    }

    /// Build the validation context
    pub fn build(self) -> BuilderValidationContext {
        let mut context = BuilderValidationContext::new();

        // Add rules (note: we can't move Box<dyn Trait> easily, so we'll recreate standard rules)
        context = context
            .add_rule(Box::new(EntityReferenceValidationRule))
            .add_rule(Box::new(ParameterReferenceValidationRule))
            .add_rule(Box::new(CatalogReferenceValidationRule))
            .add_rule(Box::new(StoryboardStructureValidationRule));

        // Add entities and parameters
        for (name, entity_type) in self.entities {
            context.register_entity(&name, &entity_type);
        }

        for (name, value) in self.parameters {
            context.register_parameter(&name, &value);
        }

        context
    }
}

/// Trait for types that can be validated by the builder system
pub trait BuilderValidatable {
    /// Validate this object using the builder validation context
    fn validate_with_context(&self, context: &BuilderValidationContext) -> BuilderResult<()>;
}

impl BuilderValidatable for OpenScenario {
    fn validate_with_context(&self, context: &BuilderValidationContext) -> BuilderResult<()> {
        context.validate_scenario(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_builder_validation_context() {
        let mut context = BuilderValidationContext::new();

        // Register entities and parameters
        context.register_entity("ego", "vehicle");
        context.register_entity("target", "vehicle");
        context.register_parameter("speed", "30.0");
        context.register_parameter("lane", "1");

        // Test entity validation
        assert!(context.validate_entity_ref("ego").is_ok());
        assert!(context.validate_entity_ref("target").is_ok());
        assert!(context.validate_entity_ref("unknown").is_err());

        // Test parameter validation
        assert!(context.validate_parameter_ref("speed").is_ok());
        assert!(context.validate_parameter_ref("lane").is_ok());
        assert!(context.validate_parameter_ref("unknown").is_err());
    }

    #[test]
    fn test_validation_context_builder() {
        let context = ValidationContextBuilder::new()
            .with_standard_rules()
            .with_entity("ego", "vehicle")
            .with_entity("target", "pedestrian")
            .with_parameter("speed", "25.0")
            .with_parameter("distance", "100.0")
            .build();

        assert_eq!(context.entities().len(), 2);
        assert_eq!(context.parameters().len(), 2);
        assert!(context.validate_entity_ref("ego").is_ok());
        assert!(context.validate_parameter_ref("speed").is_ok());
    }

    #[test]
    fn test_validation_rules() {
        let rule = EntityReferenceValidationRule;
        assert_eq!(rule.name(), "EntityReferenceValidation");
        assert!(!rule.description().is_empty());

        let rule = ParameterReferenceValidationRule;
        assert_eq!(rule.name(), "ParameterReferenceValidation");
        assert!(!rule.description().is_empty());

        let rule = CatalogReferenceValidationRule;
        assert_eq!(rule.name(), "CatalogReferenceValidation");
        assert!(!rule.description().is_empty());

        let rule = StoryboardStructureValidationRule;
        assert_eq!(rule.name(), "StoryboardStructureValidation");
        assert!(!rule.description().is_empty());
    }
}