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
//! Value-based condition builders (time, speed, etc.)
//!
//! This module provides builders for creating value-based conditions that trigger
//! based on simulation time, parameters, variables, and other non-entity values.
//!
//! # Supported Conditions
//!
//! - **SimulationTimeCondition**: Triggers at specific simulation times
//! - **SpeedCondition**: Triggers when entity speed meets criteria
//! - **ParameterCondition**: Triggers based on parameter values
//! - **VariableCondition**: Triggers based on variable state changes
//!
//! # Usage
//!
//! ```rust
//! use openscenario_rs::builder::conditions::TimeConditionBuilder;
//!
//! let condition = TimeConditionBuilder::new()
//!     .at_time(5.0)
//!     .build()
//!     .unwrap();
//! ```

use crate::builder::{BuilderError, BuilderResult};
use crate::types::{
    basic::{Double, OSString},
    conditions::entity::{
        ByEntityCondition, EntityCondition, SpeedCondition as EntitySpeedCondition,
    },
    conditions::value::{
        ByValueCondition, ParameterCondition, SimulationTimeCondition,
        StoryboardElementStateCondition, VariableCondition,
    },
    enums::{
        ConditionEdge, Rule, StoryboardElementState, StoryboardElementType, TriggeringEntitiesRule,
    },
    scenario::triggers::{Condition, EntityRef, TriggeringEntities},
};

/// Builder for simulation time conditions
///
/// Creates conditions that trigger at specific simulation times or when
/// simulation time meets certain criteria (greater than, less than, etc.).
#[derive(Debug)]
pub struct TimeConditionBuilder {
    time: Option<f64>,
    rule: Rule,
}

impl TimeConditionBuilder {
    /// Create a new time condition builder
    pub fn new() -> Self {
        Self {
            time: None,
            rule: Rule::GreaterThan,
        }
    }

    /// Set target time (triggers when simulation time > target)
    pub fn at_time(mut self, time: f64) -> Self {
        self.time = Some(time);
        self.rule = Rule::GreaterThan;
        self
    }

    /// Set time with custom rule
    pub fn time_rule(mut self, time: f64, rule: Rule) -> Self {
        self.time = Some(time);
        self.rule = rule;
        self
    }

    /// Build the condition
    pub fn build(self) -> BuilderResult<Condition> {
        if self.time.is_none() {
            return Err(BuilderError::validation_error("Time value is required"));
        }

        Ok(Condition {
            name: OSString::literal("TimeCondition".to_string()),
            condition_edge: ConditionEdge::Rising,
            delay: Some(Double::literal(0.0)),
            by_value_condition: Some(ByValueCondition {
                parameter_condition: None,
                time_of_day_condition: None,
                simulation_time_condition: Some(SimulationTimeCondition {
                    value: Double::literal(self.time.unwrap()),
                    rule: self.rule,
                }),
                storyboard_element_state_condition: None,
                user_defined_value_condition: None,
                traffic_signal_condition: None,
                traffic_signal_controller_condition: None,
                variable_condition: None,
            }),
            by_entity_condition: None,
        })
    }
}

/// Builder for speed conditions
///
/// Creates conditions that trigger when an entity's speed meets certain criteria.
/// This is technically an entity condition but is commonly used, so it's included
/// in the value conditions module for convenience.
#[derive(Debug)]
pub struct SpeedConditionBuilder {
    entity_ref: Option<String>,
    speed: Option<f64>,
    rule: Rule,
}

impl Default for SpeedConditionBuilder {
    fn default() -> Self {
        Self {
            entity_ref: None,
            speed: None,
            rule: Rule::GreaterThan,
        }
    }
}

impl SpeedConditionBuilder {
    /// Create a new speed condition builder
    pub fn new() -> Self {
        Self {
            entity_ref: None,
            speed: None,
            rule: Rule::GreaterThan,
        }
    }

    /// Set target entity
    pub fn for_entity(mut self, entity_ref: &str) -> Self {
        self.entity_ref = Some(entity_ref.to_string());
        self
    }

    /// Set speed threshold (triggers when speed > threshold)
    pub fn speed_above(mut self, speed: f64) -> Self {
        self.speed = Some(speed);
        self.rule = Rule::GreaterThan;
        self
    }

    /// Set speed threshold (triggers when speed < threshold)
    pub fn speed_below(mut self, speed: f64) -> Self {
        self.speed = Some(speed);
        self.rule = Rule::LessThan;
        self
    }

    /// Set speed with custom rule
    pub fn speed_rule(mut self, speed: f64, rule: Rule) -> Self {
        self.speed = Some(speed);
        self.rule = rule;
        self
    }

    /// Build the condition
    pub fn build(self) -> BuilderResult<Condition> {
        if self.entity_ref.is_none() {
            return Err(BuilderError::validation_error(
                "Entity reference is required",
            ));
        }
        if self.speed.is_none() {
            return Err(BuilderError::validation_error("Speed value is required"));
        }

        let entity_ref = self.entity_ref.unwrap();

        Ok(Condition {
            name: OSString::literal("SpeedCondition".to_string()),
            condition_edge: ConditionEdge::Rising,
            delay: Some(Double::literal(0.0)),
            by_value_condition: None,
            by_entity_condition: Some(ByEntityCondition {
                triggering_entities: TriggeringEntities {
                    triggering_entities_rule: TriggeringEntitiesRule::Any,
                    entity_refs: vec![EntityRef {
                        entity_ref: OSString::literal(entity_ref.clone()),
                    }],
                },
                entity_condition: EntityCondition::Speed(EntitySpeedCondition {
                    value: Double::literal(self.speed.unwrap()),
                    rule: self.rule,
                    entity_ref: OSString::literal(entity_ref.clone()),
                    direction: None,
                }),
            }),
        })
    }
}

/// Builder for parameter conditions
#[derive(Debug)]
pub struct ParameterConditionBuilder {
    parameter_ref: Option<String>,
    value: Option<f64>,
    rule: Rule,
}

impl Default for ParameterConditionBuilder {
    fn default() -> Self {
        Self {
            parameter_ref: None,
            value: None,
            rule: Rule::EqualTo,
        }
    }
}

impl ParameterConditionBuilder {
    /// Create new parameter condition builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set parameter reference
    pub fn parameter(mut self, parameter_ref: &str) -> Self {
        self.parameter_ref = Some(parameter_ref.to_string());
        self
    }

    /// Set parameter value threshold (above)
    pub fn value_above(mut self, value: f64) -> Self {
        self.value = Some(value);
        self.rule = Rule::GreaterThan;
        self
    }

    /// Set parameter value threshold (below)
    pub fn value_below(mut self, value: f64) -> Self {
        self.value = Some(value);
        self.rule = Rule::LessThan;
        self
    }

    /// Set exact parameter value
    pub fn value_equals(mut self, value: f64) -> Self {
        self.value = Some(value);
        self.rule = Rule::EqualTo;
        self
    }

    /// Build the condition
    pub fn build(self) -> BuilderResult<Condition> {
        if self.parameter_ref.is_none() {
            return Err(BuilderError::validation_error(
                "Parameter reference is required",
            ));
        }
        if self.value.is_none() {
            return Err(BuilderError::validation_error(
                "Parameter value is required",
            ));
        }

        Ok(Condition {
            name: OSString::literal("ParameterCondition".to_string()),
            condition_edge: ConditionEdge::Rising,
            delay: Some(Double::literal(0.0)),
            by_value_condition: Some(ByValueCondition {
                parameter_condition: Some(ParameterCondition {
                    parameter_ref: OSString::literal(self.parameter_ref.unwrap()),
                    value: OSString::literal(self.value.unwrap().to_string()),
                    rule: self.rule,
                }),
                time_of_day_condition: None,
                simulation_time_condition: None,
                storyboard_element_state_condition: None,
                user_defined_value_condition: None,
                traffic_signal_condition: None,
                traffic_signal_controller_condition: None,
                variable_condition: None,
            }),
            by_entity_condition: None,
        })
    }
}

/// Builder for variable conditions
#[derive(Debug)]
pub struct VariableConditionBuilder {
    variable_ref: Option<String>,
    value: Option<f64>,
    rule: Rule,
}

impl Default for VariableConditionBuilder {
    fn default() -> Self {
        Self {
            variable_ref: None,
            value: None,
            rule: Rule::EqualTo,
        }
    }
}

impl VariableConditionBuilder {
    /// Create new variable condition builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set variable reference
    pub fn variable(mut self, variable_ref: &str) -> Self {
        self.variable_ref = Some(variable_ref.to_string());
        self
    }

    /// Set variable value threshold (above)
    pub fn value_above(mut self, value: f64) -> Self {
        self.value = Some(value);
        self.rule = Rule::GreaterThan;
        self
    }

    /// Set variable value threshold (below)
    pub fn value_below(mut self, value: f64) -> Self {
        self.value = Some(value);
        self.rule = Rule::LessThan;
        self
    }

    /// Set exact variable value
    pub fn value_equals(mut self, value: f64) -> Self {
        self.value = Some(value);
        self.rule = Rule::EqualTo;
        self
    }

    /// Build the condition
    pub fn build(self) -> BuilderResult<Condition> {
        if self.variable_ref.is_none() {
            return Err(BuilderError::validation_error(
                "Variable reference is required",
            ));
        }
        if self.value.is_none() {
            return Err(BuilderError::validation_error("Variable value is required"));
        }

        Ok(Condition {
            name: OSString::literal("VariableCondition".to_string()),
            condition_edge: ConditionEdge::Rising,
            delay: Some(Double::literal(0.0)),
            by_value_condition: Some(ByValueCondition {
                parameter_condition: None,
                time_of_day_condition: None,
                simulation_time_condition: None,
                storyboard_element_state_condition: None,
                user_defined_value_condition: None,
                traffic_signal_condition: None,
                traffic_signal_controller_condition: None,
                variable_condition: Some(VariableCondition {
                    variable_ref: OSString::literal(self.variable_ref.unwrap()),
                    value: OSString::literal(self.value.unwrap().to_string()),
                    rule: self.rule,
                }),
            }),
            by_entity_condition: None,
        })
    }
}

/// Builder for storyboard element state conditions
#[derive(Debug, Default)]
pub struct StoryboardElementStateConditionBuilder {
    storyboard_element_type: Option<StoryboardElementType>,
    storyboard_element_ref: Option<String>,
    state: Option<StoryboardElementState>,
}

impl StoryboardElementStateConditionBuilder {
    /// Create new storyboard element state condition builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set story element
    pub fn story(mut self, story_ref: &str) -> Self {
        self.storyboard_element_type = Some(StoryboardElementType::Story);
        self.storyboard_element_ref = Some(story_ref.to_string());
        self
    }

    /// Set act element
    pub fn act(mut self, act_ref: &str) -> Self {
        self.storyboard_element_type = Some(StoryboardElementType::Act);
        self.storyboard_element_ref = Some(act_ref.to_string());
        self
    }

    /// Set event element
    pub fn event(mut self, event_ref: &str) -> Self {
        self.storyboard_element_type = Some(StoryboardElementType::Event);
        self.storyboard_element_ref = Some(event_ref.to_string());
        self
    }

    /// Set state to complete
    pub fn complete(mut self) -> Self {
        self.state = Some(StoryboardElementState::CompleteState);
        self
    }

    /// Set state to running
    pub fn running(mut self) -> Self {
        self.state = Some(StoryboardElementState::RunningState);
        self
    }

    /// Set state to standby
    pub fn standby(mut self) -> Self {
        self.state = Some(StoryboardElementState::StandbyState);
        self
    }

    /// Build the condition
    pub fn build(self) -> BuilderResult<Condition> {
        if self.storyboard_element_type.is_none() {
            return Err(BuilderError::validation_error(
                "Storyboard element type is required",
            ));
        }
        if self.storyboard_element_ref.is_none() {
            return Err(BuilderError::validation_error(
                "Storyboard element reference is required",
            ));
        }
        if self.state.is_none() {
            return Err(BuilderError::validation_error("Element state is required"));
        }

        Ok(Condition {
            name: OSString::literal("StoryboardElementStateCondition".to_string()),
            condition_edge: ConditionEdge::Rising,
            delay: Some(Double::literal(0.0)),
            by_value_condition: Some(ByValueCondition {
                parameter_condition: None,
                time_of_day_condition: None,
                simulation_time_condition: None,
                storyboard_element_state_condition: Some(StoryboardElementStateCondition {
                    storyboard_element_type: self.storyboard_element_type.unwrap(),
                    storyboard_element_ref: OSString::literal(self.storyboard_element_ref.unwrap()),
                    state: self.state.unwrap(),
                }),
                user_defined_value_condition: None,
                traffic_signal_condition: None,
                traffic_signal_controller_condition: None,
                variable_condition: None,
            }),
            by_entity_condition: None,
        })
    }
}

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

    #[test]
    fn test_time_condition_builder() {
        let condition = TimeConditionBuilder::new().at_time(5.0).build().unwrap();

        assert!(condition.by_value_condition.is_some());
        let by_value = condition.by_value_condition.unwrap();
        assert!(by_value.simulation_time_condition.is_some());

        let time_condition = by_value.simulation_time_condition.unwrap();
        assert_eq!(time_condition.value.as_literal().unwrap(), &5.0);
        assert_eq!(time_condition.rule, Rule::GreaterThan);
    }

    #[test]
    fn test_time_condition_with_rule() {
        let condition = TimeConditionBuilder::new()
            .time_rule(10.0, Rule::LessThan)
            .build()
            .unwrap();

        let by_value = condition.by_value_condition.unwrap();
        let time_condition = by_value.simulation_time_condition.unwrap();
        assert_eq!(time_condition.value.as_literal().unwrap(), &10.0);
        assert_eq!(time_condition.rule, Rule::LessThan);
    }

    #[test]
    fn test_speed_condition_builder() {
        let condition = SpeedConditionBuilder::new()
            .for_entity("ego")
            .speed_above(30.0)
            .build()
            .unwrap();

        assert!(condition.by_entity_condition.is_some());
        let by_entity = condition.by_entity_condition.unwrap();

        match by_entity.entity_condition {
            EntityCondition::Speed(speed_condition) => {
                assert_eq!(speed_condition.value.as_literal().unwrap(), &30.0);
                assert_eq!(speed_condition.rule, Rule::GreaterThan);
                assert_eq!(speed_condition.entity_ref.as_literal().unwrap(), "ego");
            }
            _ => panic!("Expected Speed condition"),
        }
    }

    #[test]
    fn test_speed_condition_below() {
        let condition = SpeedConditionBuilder::new()
            .for_entity("target")
            .speed_below(15.0)
            .build()
            .unwrap();

        let by_entity = condition.by_entity_condition.unwrap();
        match by_entity.entity_condition {
            EntityCondition::Speed(speed_condition) => {
                assert_eq!(speed_condition.value.as_literal().unwrap(), &15.0);
                assert_eq!(speed_condition.rule, Rule::LessThan);
            }
            _ => panic!("Expected Speed condition"),
        }
    }

    #[test]
    fn test_time_condition_validation() {
        let result = TimeConditionBuilder::new().build();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Time value is required"));
    }

    #[test]
    fn test_speed_condition_validation() {
        // Missing entity reference
        let result = SpeedConditionBuilder::new().speed_above(30.0).build();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Entity reference is required"));

        // Missing speed value
        let result = SpeedConditionBuilder::new().for_entity("ego").build();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Speed value is required"));
    }
}