openscenario-rs 0.3.1

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
//! Entity selection types for OpenSCENARIO scenarios
//!
//! This module provides types for selecting and managing entities in scenarios:
//! - EntitySelection: Main entity selection framework with selection criteria
//! - SelectedEntities: Container for selected entities with entity references
//! - EntityDistribution: Entity distribution system for probabilistic entity spawning
//! - EntityDistributionEntry: Individual distribution entry with entity reference and weight
//! - ScenarioObjectTemplate: Template system for scenario object creation
//! - ExternalObjectReference: Reference to external object definitions
//! - ByObjectType: Entity selection by object type (vehicle, pedestrian, etc.)
//! - ByType: Generic type-based selection criteria

use crate::types::basic::{Double, OSString};
use crate::types::enums::ObjectType;
use crate::types::scenario::triggers::EntityRef;
use serde::{Deserialize, Serialize};

/// Main entity selection framework with selection criteria
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EntitySelection {
    /// Selection by object type
    #[serde(rename = "ByType", skip_serializing_if = "Option::is_none")]
    pub by_type: Option<ByObjectType>,

    /// Selection by entity name pattern
    #[serde(rename = "ByName", skip_serializing_if = "Option::is_none")]
    pub by_name: Option<ByName>,
}

/// Container for selected entities with entity references
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SelectedEntities {
    /// List of entity references
    #[serde(rename = "EntityRef")]
    pub entity_refs: Vec<EntityRef>,
}

/// Entity distribution system for probabilistic entity spawning
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EntityDistribution {
    /// List of distribution entries
    #[serde(rename = "EntityDistributionEntry")]
    pub entries: Vec<EntityDistributionEntry>,
}

/// Individual distribution entry with entity reference and weight
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EntityDistributionEntry {
    /// Reference to the entity
    #[serde(rename = "@entityRef")]
    pub entity_ref: OSString,

    /// Probability weight for this entity
    #[serde(rename = "@weight")]
    pub weight: Double,
}

/// Template system for scenario object creation
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ScenarioObjectTemplate {
    /// Name of the template
    #[serde(rename = "@name")]
    pub name: OSString,

    /// Object type for the template
    #[serde(rename = "@objectType")]
    pub object_type: ObjectType,

    /// Template properties (optional)
    #[serde(rename = "Properties", skip_serializing_if = "Option::is_none")]
    pub properties: Option<TemplateProperties>,

    /// External object reference (optional)
    #[serde(
        rename = "ExternalObjectReference",
        skip_serializing_if = "Option::is_none"
    )]
    pub external_object_reference: Option<ExternalObjectReference>,
}

/// Reference to external object definitions
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExternalObjectReference {
    /// Path to the external object file
    #[serde(rename = "@file")]
    pub file: OSString,

    /// Name of the object within the external file
    #[serde(rename = "@name")]
    pub name: OSString,
}

/// Entity selection by object type (vehicle, pedestrian, etc.)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ByObjectType {
    /// Type of object to select
    #[serde(rename = "@objectType")]
    pub object_type: ObjectType,
}

/// Generic type-based selection criteria
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ByType {
    /// Type specification for selection
    #[serde(rename = "@type")]
    pub type_spec: OSString,
}

/// Selection by entity name pattern
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ByName {
    /// Name pattern for entity selection
    #[serde(rename = "@name")]
    pub name: OSString,
}

/// Template properties for scenario object templates
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct TemplateProperties {
    /// Custom properties as key-value pairs
    #[serde(rename = "Property", default)]
    pub properties: Vec<TemplateProperty>,
}

/// Individual template property
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TemplateProperty {
    /// Property name
    #[serde(rename = "@name")]
    pub name: OSString,

    /// Property value
    #[serde(rename = "@value")]
    pub value: OSString,
}

// Default implementations
impl Default for EntitySelection {
    fn default() -> Self {
        Self {
            by_type: Some(ByObjectType::default()),
            by_name: None,
        }
    }
}

impl Default for SelectedEntities {
    fn default() -> Self {
        Self {
            entity_refs: vec![EntityRef::default()],
        }
    }
}

impl Default for EntityDistribution {
    fn default() -> Self {
        Self {
            entries: vec![EntityDistributionEntry::default()],
        }
    }
}

impl Default for EntityDistributionEntry {
    fn default() -> Self {
        Self {
            entity_ref: OSString::literal("DefaultEntity".to_string()),
            weight: Double::literal(1.0),
        }
    }
}

impl Default for ScenarioObjectTemplate {
    fn default() -> Self {
        Self {
            name: OSString::literal("DefaultTemplate".to_string()),
            object_type: ObjectType::Vehicle,
            properties: None,
            external_object_reference: None,
        }
    }
}

impl Default for ExternalObjectReference {
    fn default() -> Self {
        Self {
            file: OSString::literal("objects.xml".to_string()),
            name: OSString::literal("DefaultObject".to_string()),
        }
    }
}

impl Default for ByObjectType {
    fn default() -> Self {
        Self {
            object_type: ObjectType::Vehicle,
        }
    }
}

impl Default for ByType {
    fn default() -> Self {
        Self {
            type_spec: OSString::literal("vehicle".to_string()),
        }
    }
}

impl Default for ByName {
    fn default() -> Self {
        Self {
            name: OSString::literal("*".to_string()),
        }
    }
}

impl Default for TemplateProperty {
    fn default() -> Self {
        Self {
            name: OSString::literal("property".to_string()),
            value: OSString::literal("value".to_string()),
        }
    }
}

// Implementation methods
impl EntitySelection {
    /// Create a new entity selection by object type
    pub fn by_object_type(object_type: ObjectType) -> Self {
        Self {
            by_type: Some(ByObjectType { object_type }),
            by_name: None,
        }
    }

    /// Create a new entity selection by name pattern
    pub fn by_name(name: impl Into<String>) -> Self {
        Self {
            by_type: None,
            by_name: Some(ByName {
                name: OSString::literal(name.into()),
            }),
        }
    }

    /// Create a new entity selection with both type and name criteria
    pub fn by_type_and_name(object_type: ObjectType, name: impl Into<String>) -> Self {
        Self {
            by_type: Some(ByObjectType { object_type }),
            by_name: Some(ByName {
                name: OSString::literal(name.into()),
            }),
        }
    }
}

impl SelectedEntities {
    /// Create a new selected entities container
    pub fn new() -> Self {
        Self {
            entity_refs: Vec::new(),
        }
    }

    /// Create selected entities from a list of entity names
    pub fn from_names(names: Vec<impl Into<String>>) -> Self {
        Self {
            entity_refs: names
                .into_iter()
                .map(|name| EntityRef::new(name.into()))
                .collect(),
        }
    }

    /// Add an entity reference
    pub fn add_entity(&mut self, entity_name: impl Into<String>) {
        self.entity_refs.push(EntityRef::new(entity_name.into()));
    }

    /// Get the number of selected entities
    pub fn count(&self) -> usize {
        self.entity_refs.len()
    }
}

impl EntityDistribution {
    /// Create a new entity distribution
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
        }
    }

    /// Add a distribution entry
    pub fn add_entry(&mut self, entity_ref: impl Into<String>, weight: f64) {
        self.entries.push(EntityDistributionEntry {
            entity_ref: OSString::literal(entity_ref.into()),
            weight: Double::literal(weight),
        });
    }

    /// Create a uniform distribution from entity names
    pub fn uniform(entity_names: Vec<impl Into<String>>) -> Self {
        let weight = 1.0 / entity_names.len() as f64;
        let entries = entity_names
            .into_iter()
            .map(|name| EntityDistributionEntry {
                entity_ref: OSString::literal(name.into()),
                weight: Double::literal(weight),
            })
            .collect();

        Self { entries }
    }

    /// Get the total weight of all entries
    pub fn total_weight(&self) -> f64 {
        self.entries
            .iter()
            .filter_map(|entry| entry.weight.as_literal())
            .sum()
    }
}

impl EntityDistributionEntry {
    /// Create a new distribution entry
    pub fn new(entity_ref: impl Into<String>, weight: f64) -> Self {
        Self {
            entity_ref: OSString::literal(entity_ref.into()),
            weight: Double::literal(weight),
        }
    }
}

impl ScenarioObjectTemplate {
    /// Create a new scenario object template
    pub fn new(name: impl Into<String>, object_type: ObjectType) -> Self {
        Self {
            name: OSString::literal(name.into()),
            object_type,
            properties: None,
            external_object_reference: None,
        }
    }

    /// Create a template with external object reference
    pub fn with_external_reference(
        name: impl Into<String>,
        object_type: ObjectType,
        file: impl Into<String>,
        object_name: impl Into<String>,
    ) -> Self {
        Self {
            name: OSString::literal(name.into()),
            object_type,
            properties: None,
            external_object_reference: Some(ExternalObjectReference {
                file: OSString::literal(file.into()),
                name: OSString::literal(object_name.into()),
            }),
        }
    }

    /// Add a property to the template
    pub fn add_property(&mut self, name: impl Into<String>, value: impl Into<String>) {
        if self.properties.is_none() {
            self.properties = Some(TemplateProperties::default());
        }

        if let Some(ref mut props) = self.properties {
            props.properties.push(TemplateProperty {
                name: OSString::literal(name.into()),
                value: OSString::literal(value.into()),
            });
        }
    }
}

impl ExternalObjectReference {
    /// Create a new external object reference
    pub fn new(file: impl Into<String>, name: impl Into<String>) -> Self {
        Self {
            file: OSString::literal(file.into()),
            name: OSString::literal(name.into()),
        }
    }
}

impl ByObjectType {
    /// Create a new object type selector
    pub fn new(object_type: ObjectType) -> Self {
        Self { object_type }
    }

    /// Create a vehicle selector
    pub fn vehicle() -> Self {
        Self::new(ObjectType::Vehicle)
    }

    /// Create a pedestrian selector
    pub fn pedestrian() -> Self {
        Self::new(ObjectType::Pedestrian)
    }

    /// Create a miscellaneous object selector
    pub fn miscellaneous_object() -> Self {
        Self::new(ObjectType::MiscellaneousObject)
    }
}

impl ByType {
    /// Create a new type selector
    pub fn new(type_spec: impl Into<String>) -> Self {
        Self {
            type_spec: OSString::literal(type_spec.into()),
        }
    }
}

impl ByName {
    /// Create a new name selector
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: OSString::literal(name.into()),
        }
    }

    /// Create a wildcard selector (matches all)
    pub fn wildcard() -> Self {
        Self::new("*")
    }

    /// Create a prefix selector
    pub fn prefix(prefix: impl Into<String>) -> Self {
        Self::new(format!("{}*", prefix.into()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::enums::ObjectType;

    #[test]
    fn test_entity_selection_creation() {
        // Test by object type
        let selection = EntitySelection::by_object_type(ObjectType::Vehicle);
        assert!(selection.by_type.is_some());
        assert_eq!(selection.by_type.unwrap().object_type, ObjectType::Vehicle);
        assert!(selection.by_name.is_none());

        // Test by name
        let selection = EntitySelection::by_name("Ego*");
        assert!(selection.by_type.is_none());
        assert!(selection.by_name.is_some());
        assert_eq!(
            selection.by_name.unwrap().name.as_literal().unwrap(),
            "Ego*"
        );

        // Test by type and name
        let selection = EntitySelection::by_type_and_name(ObjectType::Pedestrian, "Walker*");
        assert!(selection.by_type.is_some());
        assert!(selection.by_name.is_some());
        assert_eq!(
            selection.by_type.unwrap().object_type,
            ObjectType::Pedestrian
        );
        assert_eq!(
            selection.by_name.unwrap().name.as_literal().unwrap(),
            "Walker*"
        );
    }

    #[test]
    fn test_selected_entities() {
        let mut entities = SelectedEntities::new();
        assert_eq!(entities.count(), 0);

        entities.add_entity("Ego");
        entities.add_entity("Target1");
        assert_eq!(entities.count(), 2);

        let entities_from_names = SelectedEntities::from_names(vec!["Car1", "Car2", "Car3"]);
        assert_eq!(entities_from_names.count(), 3);
    }

    #[test]
    fn test_entity_distribution() {
        let mut distribution = EntityDistribution::new();
        distribution.add_entry("Car1", 0.6);
        distribution.add_entry("Car2", 0.4);

        assert_eq!(distribution.entries.len(), 2);
        assert_eq!(distribution.total_weight(), 1.0);

        let uniform_dist = EntityDistribution::uniform(vec!["A", "B", "C", "D"]);
        assert_eq!(uniform_dist.entries.len(), 4);
        assert!((uniform_dist.total_weight() - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_entity_distribution_entry() {
        let entry = EntityDistributionEntry::new("TestEntity", 0.75);
        assert_eq!(entry.entity_ref.as_literal().unwrap(), "TestEntity");
        assert_eq!(entry.weight.as_literal().unwrap(), &0.75);
    }

    #[test]
    fn test_scenario_object_template() {
        let mut template = ScenarioObjectTemplate::new("VehicleTemplate", ObjectType::Vehicle);
        assert_eq!(template.name.as_literal().unwrap(), "VehicleTemplate");
        assert_eq!(template.object_type, ObjectType::Vehicle);
        assert!(template.properties.is_none());

        template.add_property("color", "red");
        template.add_property("mass", "1500");
        assert!(template.properties.is_some());
        assert_eq!(template.properties.as_ref().unwrap().properties.len(), 2);

        let external_template = ScenarioObjectTemplate::with_external_reference(
            "ExternalVehicle",
            ObjectType::Vehicle,
            "vehicles.xml",
            "SportsCar",
        );
        assert!(external_template.external_object_reference.is_some());
        let ext_ref = external_template.external_object_reference.unwrap();
        assert_eq!(ext_ref.file.as_literal().unwrap(), "vehicles.xml");
        assert_eq!(ext_ref.name.as_literal().unwrap(), "SportsCar");
    }

    #[test]
    fn test_external_object_reference() {
        let ext_ref = ExternalObjectReference::new("objects/vehicles.xml", "Sedan");
        assert_eq!(ext_ref.file.as_literal().unwrap(), "objects/vehicles.xml");
        assert_eq!(ext_ref.name.as_literal().unwrap(), "Sedan");
    }

    #[test]
    fn test_by_object_type() {
        let vehicle_selector = ByObjectType::vehicle();
        assert_eq!(vehicle_selector.object_type, ObjectType::Vehicle);

        let pedestrian_selector = ByObjectType::pedestrian();
        assert_eq!(pedestrian_selector.object_type, ObjectType::Pedestrian);

        let misc_selector = ByObjectType::miscellaneous_object();
        assert_eq!(misc_selector.object_type, ObjectType::MiscellaneousObject);
    }

    #[test]
    fn test_by_type() {
        let type_selector = ByType::new("custom_vehicle_type");
        assert_eq!(
            type_selector.type_spec.as_literal().unwrap(),
            "custom_vehicle_type"
        );
    }

    #[test]
    fn test_by_name() {
        let wildcard_selector = ByName::wildcard();
        assert_eq!(wildcard_selector.name.as_literal().unwrap(), "*");

        let prefix_selector = ByName::prefix("Ego");
        assert_eq!(prefix_selector.name.as_literal().unwrap(), "Ego*");

        let exact_selector = ByName::new("SpecificEntity");
        assert_eq!(exact_selector.name.as_literal().unwrap(), "SpecificEntity");
    }

    #[test]
    fn test_serialization() {
        let selection = EntitySelection::by_object_type(ObjectType::Vehicle);
        let xml = quick_xml::se::to_string(&selection).unwrap();
        assert!(xml.contains("ByType"));
        assert!(xml.contains("objectType=\"vehicle\""));

        let entities = SelectedEntities::from_names(vec!["Ego", "Target"]);
        let xml = quick_xml::se::to_string(&entities).unwrap();
        assert!(xml.contains("EntityRef"));
        assert!(xml.contains("entityRef=\"Ego\""));
        assert!(xml.contains("entityRef=\"Target\""));

        let distribution = EntityDistribution::uniform(vec!["Car1", "Car2"]);
        let xml = quick_xml::se::to_string(&distribution).unwrap();
        assert!(xml.contains("EntityDistributionEntry"));
        assert!(xml.contains("entityRef=\"Car1\""));
        assert!(xml.contains("weight=\"0.5\""));
    }
}