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
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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
//! Traffic management actions implementation
//!
//! This file contains:
//! - Traffic source and sink actions for dynamic traffic generation
//! - Traffic swarm actions for crowd simulation around entities
//! - Traffic area actions for regional traffic density control
//! - Traffic signal control actions for intersection management
//! - Background traffic definition and distribution specifications
//!
use crate::types::basic::{Boolean, Double, OSString, UnsignedInt};
use crate::types::positions::Position;
use serde::{Deserialize, Serialize};


/// Traffic source action for traffic generation with rate and position
///
/// This action generates traffic vehicles at a specified position with a given rate.
/// Used to create dynamic traffic scenarios where vehicles enter the simulation over time.
///
/// # Fields
///
/// * `rate` - Rate of vehicle generation (vehicles per minute)
/// * `velocity` - Optional velocity for generated vehicles (meters/second)
/// * `position` - Position where vehicles are generated
/// * `traffic_definition` - Definition of traffic properties for generated vehicles
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficSourceAction {
    #[serde(rename = "@rate")]
    pub rate: Double,
    #[serde(rename = "@velocity")]
    pub velocity: Option<Double>,
    #[serde(rename = "Position")]
    pub position: Position,
    #[serde(rename = "TrafficDefinition")]
    pub traffic_definition: TrafficDefinition,
}

/// Traffic sink action for traffic removal with radius control
///
/// This action removes vehicles that enter a specified radius around a position.
/// Used to prevent traffic accumulation and manage vehicle lifecycle in simulations.
///
/// # Fields
///
/// * `rate` - Rate of vehicle removal (vehicles per minute)
/// * `radius` - Radius around position for vehicle removal (meters)
/// * `position` - Center position for removal area
/// * `traffic_definition` - Optional traffic definition to filter which vehicles are removed
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficSinkAction {
    #[serde(rename = "@rate")]
    pub rate: Double,
    #[serde(rename = "@radius")]
    pub radius: Double,
    #[serde(rename = "Position")]
    pub position: Position,
    #[serde(rename = "TrafficDefinition")]
    pub traffic_definition: Option<TrafficDefinition>,
}

/// Traffic swarm action for swarm behavior around central object
///
/// This action creates a swarm of vehicles positioned in an elliptical pattern
/// around a central object. The swarm can be used to simulate traffic
/// density and complex interaction scenarios.
///
/// # Fields
///
/// * `inner_radius` - Inner radius of the elliptical swarm area (meters)
/// * `number_of_vehicles` - Number of vehicles to generate
/// * `offset` - Offset from central object (meters)
/// * `semi_major_axis` - Length of semi-major axis (meters)
/// * `semi_minor_axis` - Length of semi-minor axis (meters)
/// * `velocity` - Optional velocity for swarm vehicles
/// * `central_object` - Reference to central object
/// * `traffic_definition` - Optional traffic definition for generated vehicles
///
/// # Example
///
/// ```rust
/// use openscenario_rs::types::actions::{TrafficSwarmAction, CentralSwarmObject};
/// use openscenario_rs::types::basic::{Double, UnsignedInt};
/// use openscenario_rs::types::positions::Position;
///
/// let swarm = TrafficSwarmAction {
///     inner_radius: Double::literal(5.0),
///     number_of_vehicles: UnsignedInt::literal(10),
///     offset: Double::literal(0.0),
///     semi_major_axis: Double::literal(20.0),
///     semi_minor_axis: Double::literal(15.0),
///     velocity: Some(Double::literal(30.0)),
///     central_object: CentralSwarmObject::new("CentralEntity"),
///     traffic_definition: None,
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficSwarmAction {
    #[serde(rename = "@innerRadius")]
    pub inner_radius: Double,
    #[serde(rename = "@numberOfVehicles")]
    pub number_of_vehicles: UnsignedInt,
    #[serde(rename = "@offset")]
    pub offset: Double,
    #[serde(rename = "@semiMajorAxis")]
    pub semi_major_axis: Double,
    #[serde(rename = "@semiMinorAxis")]
    pub semi_minor_axis: Double,
    #[serde(rename = "@velocity", skip_serializing_if = "Option::is_none")]
    pub velocity: Option<Double>,
    #[serde(rename = "CentralObject")]
    pub central_object: CentralSwarmObject,
    #[serde(rename = "TrafficDefinition", skip_serializing_if = "Option::is_none")]
    pub traffic_definition: Option<TrafficDefinition>,
}

/// Traffic area action for area-based traffic management
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Default)]
pub struct TrafficAreaAction {
    #[serde(rename = "TrafficArea")]
    pub traffic_area: TrafficArea,
    #[serde(rename = "TrafficDefinition")]
    pub traffic_definition: TrafficDefinition,
}

/// Traffic signal action wrapper for all traffic signal operations
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficSignalAction {
    #[serde(flatten)]
    pub signal_action_choice: TrafficSignalActionChoice,
}

/// Traffic signal action choice - controller or state action
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub enum TrafficSignalActionChoice {
    TrafficSignalControllerAction(TrafficSignalControllerAction),
    TrafficSignalStateAction(TrafficSignalStateAction),
}

/// Traffic signal state action for individual signal state control
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficSignalStateAction {
    #[serde(rename = "@name")]
    pub name: OSString,
    #[serde(rename = "@state")]
    pub state: OSString,
}

/// Traffic signal controller action for signal controller management
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficSignalControllerAction {
    #[serde(rename = "@trafficSignalControllerRef")]
    pub traffic_signal_controller_ref: OSString,
    #[serde(rename = "@phase")]
    pub phase_ref: OSString,
}

/// Traffic signal controller definition with phases and timing
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficSignalController {
    #[serde(rename = "@name")]
    pub name: OSString,
    #[serde(rename = "@delay")]
    pub delay: Option<Double>,
    #[serde(rename = "@reference")]
    pub reference: Option<OSString>,
    #[serde(rename = "Phase", default)]
    pub phases: Vec<Phase>,
}

/// Phase definition for traffic signal controller
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Phase {
    #[serde(rename = "@name")]
    pub name: OSString,
    #[serde(rename = "@duration")]
    pub duration: Double,
    #[serde(rename = "TrafficSignalState", default)]
    pub traffic_signal_states: Vec<TrafficSignalState>,
    #[serde(rename = "TrafficSignalGroupState")]
    pub traffic_signal_group_state: Option<TrafficSignalGroupState>,
}

/// Traffic signal state for individual signal control
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficSignalState {
    #[serde(rename = "@trafficSignalId")]
    pub traffic_signal_id: OSString,
    #[serde(rename = "@state")]
    pub state: OSString,
}

/// Traffic signal group state for signal group control
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficSignalGroupState {
    #[serde(rename = "@state")]
    pub state: OSString,
}

/// Traffic stop action for traffic stop enable/disable
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficStopAction {
    #[serde(rename = "@enable")]
    pub enable: Boolean,
}


/// Traffic definition for vehicle category and controller distribution
///
/// Defines the properties of traffic that should be generated, including
/// vehicle types and their probabilities, as well as controller behavior profiles.
///
/// # Fields
///
/// * `vehicle_category_distribution` - Distribution of vehicle categories (cars, trucks, etc.)
/// * `controller_distribution` - Distribution of controller behaviors
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficDefinition {
    #[serde(rename = "VehicleCategoryDistribution")]
    pub vehicle_category_distribution: Option<VehicleCategoryDistribution>,
    #[serde(rename = "ControllerDistribution")]
    pub controller_distribution: Option<ControllerDistribution>,
}

/// Vehicle category distribution for traffic composition
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VehicleCategoryDistribution {
    #[serde(rename = "VehicleCategoryDistributionEntry", default)]
    pub entries: Vec<VehicleCategoryDistributionEntry>,
}

/// Vehicle category distribution entry
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VehicleCategoryDistributionEntry {
    #[serde(rename = "@category")]
    pub category: VehicleCategory,
    #[serde(rename = "@weight")]
    pub weight: Double,
}

/// Vehicle category enumeration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Default)]
pub enum VehicleCategory {
    #[serde(rename = "car")]
    #[default]
    Car,
    #[serde(rename = "van")]
    Van,
    #[serde(rename = "truck")]
    Truck,
    #[serde(rename = "bus")]
    Bus,
    #[serde(rename = "motorbike")]
    Motorbike,
    #[serde(rename = "bicycle")]
    Bicycle,
    #[serde(rename = "trailer")]
    Trailer,
    #[serde(rename = "semitrailer")]
    Semitrailer,
}

/// Controller distribution for traffic behavior
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ControllerDistribution {
    #[serde(rename = "ControllerDistributionEntry", default)]
    pub entries: Vec<ControllerDistributionEntry>,
}

/// Controller distribution entry
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ControllerDistributionEntry {
    #[serde(rename = "@weight")]
    pub weight: Double,
    #[serde(rename = "Controller")]
    pub controller: OSString,
}

/// Central swarm object specification
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CentralSwarmObject {
    #[serde(rename = "@entityRef")]
    pub entity_ref: OSString,
}

/// Traffic area definition with vertices
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficArea {
    #[serde(rename = "Vertex", default)]
    pub vertices: Vec<TrafficAreaVertex>,
}

/// Simple vertex for traffic area definition (x, y, z coordinates)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrafficAreaVertex {
    #[serde(rename = "@x")]
    pub x: Double,
    #[serde(rename = "@y")]
    pub y: Double,
    #[serde(rename = "@z")]
    pub z: Double,
}


impl Default for TrafficSourceAction {
    fn default() -> Self {
        Self {
            rate: Double::literal(10.0),           // 10 vehicles per minute
            velocity: Some(Double::literal(50.0)), // 50 km/h default velocity
            position: Position::default(),
            traffic_definition: TrafficDefinition::default(),
        }
    }
}

impl Default for TrafficSinkAction {
    fn default() -> Self {
        Self {
            rate: Double::literal(10.0),   // 10 vehicles per minute
            radius: Double::literal(50.0), // 50 meter radius
            position: Position::default(),
            traffic_definition: None,
        }
    }
}

impl Default for TrafficSwarmAction {
    fn default() -> Self {
        Self {
            inner_radius: Double::literal(10.0),
            number_of_vehicles: UnsignedInt::literal(20),
            offset: Double::literal(0.0),
            semi_major_axis: Double::literal(100.0),
            semi_minor_axis: Double::literal(50.0),
            velocity: None,
            central_object: CentralSwarmObject::default(),
            traffic_definition: Some(TrafficDefinition::default()),
        }
    }
}


impl Default for TrafficSignalAction {
    fn default() -> Self {
        Self {
            signal_action_choice: TrafficSignalActionChoice::TrafficSignalStateAction(
                TrafficSignalStateAction::default(),
            ),
        }
    }
}

impl Default for TrafficSignalStateAction {
    fn default() -> Self {
        Self {
            name: OSString::literal("DefaultSignal".to_string()),
            state: OSString::literal("green".to_string()),
        }
    }
}

impl Default for TrafficSignalControllerAction {
    fn default() -> Self {
        Self {
            traffic_signal_controller_ref: OSString::literal("DefaultController".to_string()),
            phase_ref: OSString::literal("Phase1".to_string()),
        }
    }
}

impl Default for TrafficSignalController {
    fn default() -> Self {
        Self {
            name: OSString::literal("DefaultController".to_string()),
            delay: None,
            reference: None,
            phases: Vec::new(),
        }
    }
}

impl Default for Phase {
    fn default() -> Self {
        Self {
            name: OSString::literal("DefaultPhase".to_string()),
            duration: Double::literal(30.0),
            traffic_signal_states: Vec::new(),
            traffic_signal_group_state: None,
        }
    }
}

impl Default for TrafficSignalState {
    fn default() -> Self {
        Self {
            traffic_signal_id: OSString::literal("signal_1".to_string()),
            state: OSString::literal("green".to_string()),
        }
    }
}

impl Default for TrafficSignalGroupState {
    fn default() -> Self {
        Self {
            state: OSString::literal("green".to_string()),
        }
    }
}

impl Default for TrafficStopAction {
    fn default() -> Self {
        Self {
            enable: Boolean::literal(true),
        }
    }
}

impl Default for TrafficDefinition {
    fn default() -> Self {
        Self {
            vehicle_category_distribution: Some(VehicleCategoryDistribution::default()),
            controller_distribution: None,
        }
    }
}

impl Default for VehicleCategoryDistribution {
    fn default() -> Self {
        Self {
            entries: vec![
                VehicleCategoryDistributionEntry {
                    category: VehicleCategory::Car,
                    weight: Double::literal(0.7), // 70% cars
                },
                VehicleCategoryDistributionEntry {
                    category: VehicleCategory::Truck,
                    weight: Double::literal(0.2), // 20% trucks
                },
                VehicleCategoryDistributionEntry {
                    category: VehicleCategory::Van,
                    weight: Double::literal(0.1), // 10% vans
                },
            ],
        }
    }
}

impl Default for ControllerDistribution {
    fn default() -> Self {
        Self {
            entries: vec![ControllerDistributionEntry {
                weight: Double::literal(1.0),
                controller: OSString::literal("DefaultTrafficController".to_string()),
            }],
        }
    }
}

impl Default for CentralSwarmObject {
    fn default() -> Self {
        Self {
            entity_ref: OSString::literal("SwarmCenter".to_string()),
        }
    }
}

impl Default for TrafficArea {
    fn default() -> Self {
        Self {
            vertices: vec![
                TrafficAreaVertex::new(0.0, 0.0, 0.0),
                TrafficAreaVertex::new(100.0, 0.0, 0.0),
                TrafficAreaVertex::new(100.0, 100.0, 0.0),
                TrafficAreaVertex::new(0.0, 100.0, 0.0),
            ],
        }
    }
}

impl Default for TrafficAreaVertex {
    fn default() -> Self {
        Self {
            x: Double::literal(0.0),
            y: Double::literal(0.0),
            z: Double::literal(0.0),
        }
    }
}



impl TrafficSourceAction {
    /// Create traffic source with rate and position
    pub fn new(rate: f64, position: Position, traffic_definition: TrafficDefinition) -> Self {
        Self {
            rate: Double::literal(rate),
            velocity: None,
            position,
            traffic_definition,
        }
    }

    /// Create traffic source with velocity
    pub fn with_velocity(
        rate: f64,
        velocity: f64,
        position: Position,
        traffic_definition: TrafficDefinition,
    ) -> Self {
        Self {
            rate: Double::literal(rate),
            velocity: Some(Double::literal(velocity)),
            position,
            traffic_definition,
        }
    }
}

impl TrafficSinkAction {
    /// Create traffic sink with rate, radius and position
    pub fn new(rate: f64, radius: f64, position: Position) -> Self {
        Self {
            rate: Double::literal(rate),
            radius: Double::literal(radius),
            position,
            traffic_definition: None,
        }
    }

    /// Create traffic sink with traffic definition
    pub fn with_traffic_definition(
        rate: f64,
        radius: f64,
        position: Position,
        traffic_definition: TrafficDefinition,
    ) -> Self {
        Self {
            rate: Double::literal(rate),
            radius: Double::literal(radius),
            position,
            traffic_definition: Some(traffic_definition),
        }
    }
}

impl TrafficSwarmAction {
    /// Create traffic swarm around central object
    pub fn new(
        central_object: impl Into<String>,
        semi_major_axis: f64,
        semi_minor_axis: f64,
        number_of_vehicles: u32,
    ) -> Self {
        Self {
            inner_radius: Double::literal(0.0),
            number_of_vehicles: UnsignedInt::literal(number_of_vehicles),
            offset: Double::literal(0.0),
            semi_major_axis: Double::literal(semi_major_axis),
            semi_minor_axis: Double::literal(semi_minor_axis),
            velocity: None,
            central_object: CentralSwarmObject {
                entity_ref: OSString::literal(central_object.into()),
            },
            traffic_definition: None,
        }
    }

    /// Set inner radius for the swarm
    pub fn with_inner_radius(mut self, radius: f64) -> Self {
        self.inner_radius = Double::literal(radius);
        self
    }

    /// Set offset for the swarm
    pub fn with_offset(mut self, offset: f64) -> Self {
        self.offset = Double::literal(offset);
        self
    }

    /// Set velocity for the swarm (deprecated)
    pub fn with_velocity(mut self, velocity: f64) -> Self {
        self.velocity = Some(Double::literal(velocity));
        self
    }

    /// Set traffic definition for the swarm
    pub fn with_traffic_definition(mut self, traffic_definition: TrafficDefinition) -> Self {
        self.traffic_definition = Some(traffic_definition);
        self
    }

    /// Set central swarm object entity reference
    pub fn with_central_swarm_object(mut self, entity_ref: impl Into<String>) -> Self {
        self.central_object = CentralSwarmObject {
            entity_ref: OSString::literal(entity_ref.into()),
        };
        self
    }
}

impl TrafficAreaAction {
    /// Create traffic area with vertices
    pub fn new(vertices: Vec<TrafficAreaVertex>, traffic_definition: TrafficDefinition) -> Self {
        Self {
            traffic_area: TrafficArea { vertices },
            traffic_definition,
        }
    }
}

impl TrafficSignalAction {
    /// Create signal state action
    pub fn state_action(name: String, state: String) -> Self {
        Self {
            signal_action_choice: TrafficSignalActionChoice::TrafficSignalStateAction(
                TrafficSignalStateAction {
                    name: OSString::literal(name),
                    state: OSString::literal(state),
                },
            ),
        }
    }

    /// Create signal controller action
    pub fn controller_action(controller_ref: String, phase_ref: String) -> Self {
        Self {
            signal_action_choice: TrafficSignalActionChoice::TrafficSignalControllerAction(
                TrafficSignalControllerAction {
                    traffic_signal_controller_ref: OSString::literal(controller_ref),
                    phase_ref: OSString::literal(phase_ref),
                },
            ),
        }
    }
}

impl TrafficSignalController {
    /// Create new traffic signal controller
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: OSString::literal(name.into()),
            delay: None,
            reference: None,
            phases: Vec::new(),
        }
    }

    /// Set delay for the controller
    pub fn with_delay(mut self, delay: f64) -> Self {
        self.delay = Some(Double::literal(delay));
        self
    }

    /// Set reference for the controller
    pub fn with_reference(mut self, reference: impl Into<String>) -> Self {
        self.reference = Some(OSString::literal(reference.into()));
        self
    }

    /// Add a phase to the controller
    pub fn add_phase(mut self, phase: Phase) -> Self {
        self.phases.push(phase);
        self
    }

    /// Add multiple phases to the controller
    pub fn with_phases(mut self, phases: Vec<Phase>) -> Self {
        self.phases = phases;
        self
    }
}

impl Phase {
    /// Create new phase
    pub fn new(name: impl Into<String>, duration: f64) -> Self {
        Self {
            name: OSString::literal(name.into()),
            duration: Double::literal(duration),
            traffic_signal_states: Vec::new(),
            traffic_signal_group_state: None,
        }
    }

    /// Add signal state to the phase
    pub fn add_signal_state(
        mut self,
        signal_id: impl Into<String>,
        state: impl Into<String>,
    ) -> Self {
        self.traffic_signal_states.push(TrafficSignalState {
            traffic_signal_id: OSString::literal(signal_id.into()),
            state: OSString::literal(state.into()),
        });
        self
    }

    /// Set traffic signal group state
    pub fn with_group_state(mut self, state: impl Into<String>) -> Self {
        self.traffic_signal_group_state = Some(TrafficSignalGroupState {
            state: OSString::literal(state.into()),
        });
        self
    }

    /// Add multiple signal states
    pub fn with_signal_states(mut self, states: Vec<(String, String)>) -> Self {
        for (signal_id, state) in states {
            self.traffic_signal_states.push(TrafficSignalState {
                traffic_signal_id: OSString::literal(signal_id),
                state: OSString::literal(state),
            });
        }
        self
    }
}

impl TrafficSignalState {
    /// Create new traffic signal state
    pub fn new(signal_id: impl Into<String>, state: impl Into<String>) -> Self {
        Self {
            traffic_signal_id: OSString::literal(signal_id.into()),
            state: OSString::literal(state.into()),
        }
    }
}

impl TrafficSignalGroupState {
    /// Create new traffic signal group state
    pub fn new(state: impl Into<String>) -> Self {
        Self {
            state: OSString::literal(state.into()),
        }
    }
}

impl TrafficSignalStateAction {
    /// Create new traffic signal state action
    pub fn new(name: impl Into<String>, state: impl Into<String>) -> Self {
        Self {
            name: OSString::literal(name.into()),
            state: OSString::literal(state.into()),
        }
    }
}

impl TrafficSignalControllerAction {
    /// Create new traffic signal controller action
    pub fn new(controller_ref: impl Into<String>, phase_ref: impl Into<String>) -> Self {
        Self {
            traffic_signal_controller_ref: OSString::literal(controller_ref.into()),
            phase_ref: OSString::literal(phase_ref.into()),
        }
    }
}

impl TrafficStopAction {
    /// Enable traffic stop
    pub fn enable() -> Self {
        Self {
            enable: Boolean::literal(true),
        }
    }

    /// Disable traffic stop
    pub fn disable() -> Self {
        Self {
            enable: Boolean::literal(false),
        }
    }
}

impl TrafficDefinition {
    /// Create traffic definition with vehicle categories only
    pub fn with_vehicles(distribution: VehicleCategoryDistribution) -> Self {
        Self {
            vehicle_category_distribution: Some(distribution),
            controller_distribution: None,
        }
    }

    /// Create traffic definition with controllers only
    pub fn with_controllers(distribution: ControllerDistribution) -> Self {
        Self {
            vehicle_category_distribution: None,
            controller_distribution: Some(distribution),
        }
    }

    /// Create traffic definition with both vehicles and controllers
    pub fn with_both(
        vehicles: VehicleCategoryDistribution,
        controllers: ControllerDistribution,
    ) -> Self {
        Self {
            vehicle_category_distribution: Some(vehicles),
            controller_distribution: Some(controllers),
        }
    }
}

impl VehicleCategoryDistribution {
    /// Create distribution with single category
    pub fn single_category(category: VehicleCategory, weight: f64) -> Self {
        Self {
            entries: vec![VehicleCategoryDistributionEntry {
                category,
                weight: Double::literal(weight),
            }],
        }
    }

    /// Create distribution for mixed traffic (cars, trucks, vans)
    pub fn mixed_traffic() -> Self {
        Self::default()
    }

    /// Create distribution for urban traffic (mostly cars)
    pub fn urban_traffic() -> Self {
        Self {
            entries: vec![
                VehicleCategoryDistributionEntry {
                    category: VehicleCategory::Car,
                    weight: Double::literal(0.85),
                },
                VehicleCategoryDistributionEntry {
                    category: VehicleCategory::Bus,
                    weight: Double::literal(0.1),
                },
                VehicleCategoryDistributionEntry {
                    category: VehicleCategory::Van,
                    weight: Double::literal(0.05),
                },
            ],
        }
    }
}

impl ControllerDistribution {
    /// Create distribution with single controller
    pub fn single_controller(controller: String, weight: f64) -> Self {
        Self {
            entries: vec![ControllerDistributionEntry {
                weight: Double::literal(weight),
                controller: OSString::literal(controller),
            }],
        }
    }
}

impl CentralSwarmObject {
    /// Create new central swarm object
    pub fn new(entity_ref: impl Into<String>) -> Self {
        Self {
            entity_ref: OSString::literal(entity_ref.into()),
        }
    }
}

impl TrafficAreaVertex {
    /// Create new traffic area vertex
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self {
            x: Double::literal(x),
            y: Double::literal(y),
            z: Double::literal(z),
        }
    }

    /// Get x coordinate value
    pub fn x(&self) -> Option<&f64> {
        self.x.as_literal()
    }

    /// Get y coordinate value
    pub fn y(&self) -> Option<&f64> {
        self.y.as_literal()
    }

    /// Get z coordinate value
    pub fn z(&self) -> Option<&f64> {
        self.z.as_literal()
    }
}

impl TrafficArea {
    /// Create rectangular traffic area
    pub fn rectangle(x: f64, y: f64, width: f64, height: f64) -> Self {
        Self {
            vertices: vec![
                TrafficAreaVertex::new(x, y, 0.0),
                TrafficAreaVertex::new(x + width, y, 0.0),
                TrafficAreaVertex::new(x + width, y + height, 0.0),
                TrafficAreaVertex::new(x, y + height, 0.0),
            ],
        }
    }

    /// Create circular traffic area (approximated with polygon)
    pub fn circle(center_x: f64, center_y: f64, radius: f64, segments: u32) -> Self {
        let mut vertices = Vec::new();
        let segment_angle = 2.0 * std::f64::consts::PI / segments as f64;

        for i in 0..segments {
            let angle = i as f64 * segment_angle;
            let x = center_x + radius * angle.cos();
            let y = center_y + radius * angle.sin();
            vertices.push(TrafficAreaVertex::new(x, y, 0.0));
        }

        Self { vertices }
    }
}

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

    #[test]
    fn test_traffic_source_action_creation() {
        let source =
            TrafficSourceAction::new(15.0, Position::default(), TrafficDefinition::default());

        assert_eq!(source.rate.as_literal(), Some(&15.0));
        assert!(source.velocity.is_none());
    }

    #[test]
    fn test_traffic_source_with_velocity() {
        let source = TrafficSourceAction::with_velocity(
            20.0,
            60.0,
            Position::default(),
            TrafficDefinition::default(),
        );

        assert_eq!(source.rate.as_literal(), Some(&20.0));
        assert_eq!(
            source.velocity.as_ref().and_then(|v| v.as_literal()),
            Some(&60.0)
        );
    }

    #[test]
    fn test_traffic_sink_action_creation() {
        let sink = TrafficSinkAction::new(10.0, 30.0, Position::default());

        assert_eq!(sink.rate.as_literal(), Some(&10.0));
        assert_eq!(sink.radius.as_literal(), Some(&30.0));
        assert!(sink.traffic_definition.is_none());
    }

    #[test]
    fn test_traffic_sink_with_definition() {
        let sink = TrafficSinkAction::with_traffic_definition(
            12.0,
            40.0,
            Position::default(),
            TrafficDefinition::default(),
        );

        assert_eq!(sink.rate.as_literal(), Some(&12.0));
        assert_eq!(sink.radius.as_literal(), Some(&40.0));
        assert!(sink.traffic_definition.is_some());
    }

    #[test]
    fn test_traffic_swarm_action_creation() {
        let swarm = TrafficSwarmAction::new("LeadVehicle", 100.0, 50.0, 15)
            .with_inner_radius(5.0)
            .with_traffic_definition(TrafficDefinition::default());

        assert_eq!(
            swarm.central_object.entity_ref.as_literal(),
            Some(&"LeadVehicle".to_string())
        );
        assert_eq!(swarm.inner_radius.as_literal(), Some(&5.0));
        assert_eq!(swarm.semi_major_axis.as_literal(), Some(&100.0));
        assert_eq!(swarm.semi_minor_axis.as_literal(), Some(&50.0));
        assert_eq!(swarm.number_of_vehicles.as_literal(), Some(&15));
    }

    #[test]
    fn test_traffic_area_action_creation() {
        let vertices = vec![
            TrafficAreaVertex::new(0.0, 0.0, 0.0),
            TrafficAreaVertex::new(50.0, 0.0, 0.0),
            TrafficAreaVertex::new(50.0, 50.0, 0.0),
            TrafficAreaVertex::new(0.0, 50.0, 0.0),
        ];

        let area = TrafficAreaAction::new(vertices.clone(), TrafficDefinition::default());

        assert_eq!(area.traffic_area.vertices.len(), 4);
        assert_eq!(area.traffic_area.vertices[0].x(), Some(&0.0));
        assert_eq!(area.traffic_area.vertices[1].x(), Some(&50.0));
    }

    #[test]
    fn test_traffic_signal_state_action() {
        let signal =
            TrafficSignalAction::state_action("Intersection1".to_string(), "red".to_string());

        if let TrafficSignalActionChoice::TrafficSignalStateAction(state_action) =
            signal.signal_action_choice
        {
            assert_eq!(
                state_action.name.as_literal(),
                Some(&"Intersection1".to_string())
            );
            assert_eq!(state_action.state.as_literal(), Some(&"red".to_string()));
        } else {
            panic!("Expected TrafficSignalStateAction");
        }
    }

    #[test]
    fn test_traffic_signal_controller_action() {
        let signal =
            TrafficSignalAction::controller_action("Controller1".to_string(), "Phase2".to_string());

        if let TrafficSignalActionChoice::TrafficSignalControllerAction(controller_action) =
            signal.signal_action_choice
        {
            assert_eq!(
                controller_action.traffic_signal_controller_ref.as_literal(),
                Some(&"Controller1".to_string())
            );
            assert_eq!(
                controller_action.phase_ref.as_literal(),
                Some(&"Phase2".to_string())
            );
        } else {
            panic!("Expected TrafficSignalControllerAction");
        }
    }

    #[test]
    fn test_traffic_stop_action() {
        let enable = TrafficStopAction::enable();
        assert_eq!(enable.enable.as_literal(), Some(&true));

        let disable = TrafficStopAction::disable();
        assert_eq!(disable.enable.as_literal(), Some(&false));
    }

    #[test]
    fn test_vehicle_category_distribution() {
        let mixed = VehicleCategoryDistribution::mixed_traffic();
        assert_eq!(mixed.entries.len(), 3);
        assert!(mixed
            .entries
            .iter()
            .any(|e| matches!(e.category, VehicleCategory::Car)));

        let urban = VehicleCategoryDistribution::urban_traffic();
        assert_eq!(urban.entries.len(), 3);
        assert_eq!(urban.entries[0].weight.as_literal().unwrap(), &0.85); // Mostly cars
    }

    #[test]
    fn test_traffic_definition_creation() {
        let vehicles = VehicleCategoryDistribution::urban_traffic();
        let controllers = ControllerDistribution::single_controller("AI1".to_string(), 1.0);

        let definition = TrafficDefinition::with_both(vehicles, controllers);

        assert!(definition.vehicle_category_distribution.is_some());
        assert!(definition.controller_distribution.is_some());
    }

    #[test]
    fn test_traffic_area_shapes() {
        let rect = TrafficArea::rectangle(10.0, 20.0, 30.0, 40.0);
        assert_eq!(rect.vertices.len(), 4);
        assert_eq!(rect.vertices[0].x(), Some(&10.0));
        assert_eq!(rect.vertices[0].y(), Some(&20.0));
        assert_eq!(rect.vertices[2].x(), Some(&40.0)); // 10 + 30
        assert_eq!(rect.vertices[2].y(), Some(&60.0)); // 20 + 40

        let circle = TrafficArea::circle(0.0, 0.0, 10.0, 8);
        assert_eq!(circle.vertices.len(), 8);

        // First vertex should be at (10, 0) for angle 0
        assert!((circle.vertices[0].x().unwrap() - 10.0).abs() < 1e-10);
        assert!((circle.vertices[0].y().unwrap() - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_vehicle_categories() {
        let car = VehicleCategory::Car;
        let truck = VehicleCategory::Truck;
        let bike = VehicleCategory::Bicycle;

        // Test that enum variants exist and can be used
        let entry1 = VehicleCategoryDistributionEntry {
            category: car,
            weight: Double::literal(0.6),
        };
        let entry2 = VehicleCategoryDistributionEntry {
            category: truck,
            weight: Double::literal(0.3),
        };
        let entry3 = VehicleCategoryDistributionEntry {
            category: bike,
            weight: Double::literal(0.1),
        };

        assert_eq!(entry1.weight.as_literal().unwrap(), &0.6);
        assert_eq!(entry2.weight.as_literal().unwrap(), &0.3);
        assert_eq!(entry3.weight.as_literal().unwrap(), &0.1);
    }

    #[test]
    fn test_traffic_action_defaults() {
        let source = TrafficSourceAction::default();
        assert_eq!(source.rate.as_literal(), Some(&10.0));
        assert_eq!(
            source.velocity.as_ref().and_then(|v| v.as_literal()),
            Some(&50.0)
        );

        let sink = TrafficSinkAction::default();
        assert_eq!(sink.rate.as_literal(), Some(&10.0));
        assert_eq!(sink.radius.as_literal(), Some(&50.0));

        let swarm = TrafficSwarmAction::default();
        assert_eq!(swarm.number_of_vehicles.as_literal(), Some(&20));
        assert_eq!(swarm.inner_radius.as_literal(), Some(&10.0));
        assert_eq!(swarm.semi_major_axis.as_literal(), Some(&100.0));
        assert_eq!(swarm.semi_minor_axis.as_literal(), Some(&50.0));

        let stop = TrafficStopAction::default();
        assert_eq!(stop.enable.as_literal(), Some(&true));
    }

    // TRAFFIC SIGNAL SYSTEM TESTS

    #[test]
    fn test_traffic_signal_controller_creation() {
        let controller = TrafficSignalController::new("intersection_1")
            .with_delay(1.0)
            .with_reference("ref_1")
            .add_phase(
                Phase::new("green_ns", 30.0)
                    .add_signal_state("signal_1", "green")
                    .add_signal_state("signal_2", "red"),
            );

        assert_eq!(
            controller.name.as_literal(),
            Some(&"intersection_1".to_string())
        );
        assert_eq!(controller.delay.as_ref().unwrap().as_literal(), Some(&1.0));
        assert_eq!(
            controller.reference.as_ref().unwrap().as_literal(),
            Some(&"ref_1".to_string())
        );
        assert_eq!(controller.phases.len(), 1);
        assert_eq!(controller.phases[0].traffic_signal_states.len(), 2);
    }

    #[test]
    fn test_phase_creation_and_manipulation() {
        let phase = Phase::new("green_phase", 45.0)
            .add_signal_state("north_signal", "green")
            .add_signal_state("south_signal", "green")
            .add_signal_state("east_signal", "red")
            .add_signal_state("west_signal", "red")
            .with_group_state("active");

        assert_eq!(phase.name.as_literal(), Some(&"green_phase".to_string()));
        assert_eq!(phase.duration.as_literal(), Some(&45.0));
        assert_eq!(phase.traffic_signal_states.len(), 4);
        assert!(phase.traffic_signal_group_state.is_some());
        assert_eq!(
            phase.traffic_signal_group_state.unwrap().state.as_literal(),
            Some(&"active".to_string())
        );
    }

    #[test]
    fn test_traffic_signal_state_creation() {
        let state = TrafficSignalState::new("signal_123", "yellow");

        assert_eq!(
            state.traffic_signal_id.as_literal(),
            Some(&"signal_123".to_string())
        );
        assert_eq!(state.state.as_literal(), Some(&"yellow".to_string()));
    }

    #[test]
    fn test_traffic_signal_group_state_creation() {
        let group_state = TrafficSignalGroupState::new("flashing");

        assert_eq!(
            group_state.state.as_literal(),
            Some(&"flashing".to_string())
        );
    }

    #[test]
    fn test_traffic_signal_state_action_creation() {
        let action = TrafficSignalStateAction::new("main_signal", "red");

        assert_eq!(action.name.as_literal(), Some(&"main_signal".to_string()));
        assert_eq!(action.state.as_literal(), Some(&"red".to_string()));
    }

    #[test]
    fn test_traffic_signal_controller_action_creation() {
        let action = TrafficSignalControllerAction::new("controller_1", "phase_2");

        assert_eq!(
            action.traffic_signal_controller_ref.as_literal(),
            Some(&"controller_1".to_string())
        );
        assert_eq!(action.phase_ref.as_literal(), Some(&"phase_2".to_string()));
    }

    #[test]
    fn test_complex_traffic_signal_controller() {
        let controller = TrafficSignalController::new("complex_intersection")
            .with_delay(2.5)
            .add_phase(
                Phase::new("north_south_green", 60.0)
                    .add_signal_state("ns_signal", "green")
                    .add_signal_state("ew_signal", "red")
                    .with_group_state("ns_active"),
            )
            .add_phase(
                Phase::new("north_south_yellow", 5.0)
                    .add_signal_state("ns_signal", "yellow")
                    .add_signal_state("ew_signal", "red"),
            )
            .add_phase(
                Phase::new("east_west_green", 45.0)
                    .add_signal_state("ns_signal", "red")
                    .add_signal_state("ew_signal", "green")
                    .with_group_state("ew_active"),
            )
            .add_phase(
                Phase::new("east_west_yellow", 5.0)
                    .add_signal_state("ns_signal", "red")
                    .add_signal_state("ew_signal", "yellow"),
            );

        assert_eq!(controller.phases.len(), 4);
        assert_eq!(controller.phases[0].duration.as_literal(), Some(&60.0));
        assert_eq!(controller.phases[1].duration.as_literal(), Some(&5.0));
        assert_eq!(controller.phases[2].duration.as_literal(), Some(&45.0));
        assert_eq!(controller.phases[3].duration.as_literal(), Some(&5.0));

        // Check first phase details
        let first_phase = &controller.phases[0];
        assert_eq!(first_phase.traffic_signal_states.len(), 2);
        assert!(first_phase.traffic_signal_group_state.is_some());
    }

    #[test]
    fn test_traffic_signal_xml_serialization() {
        let controller = TrafficSignalController::new("test_controller")
            .add_phase(Phase::new("test_phase", 30.0).add_signal_state("signal_1", "green"));

        // Test that serialization works (basic check)
        let serialized = serde_json::to_string(&controller);
        assert!(serialized.is_ok());
    }

    #[test]
    fn test_traffic_signal_defaults() {
        let controller = TrafficSignalController::default();
        assert_eq!(
            controller.name.as_literal(),
            Some(&"DefaultController".to_string())
        );
        assert!(controller.delay.is_none());
        assert!(controller.reference.is_none());
        assert_eq!(controller.phases.len(), 0);

        let phase = Phase::default();
        assert_eq!(phase.name.as_literal(), Some(&"DefaultPhase".to_string()));
        assert_eq!(phase.duration.as_literal(), Some(&30.0));
        assert_eq!(phase.traffic_signal_states.len(), 0);
        assert!(phase.traffic_signal_group_state.is_none());

        let state = TrafficSignalState::default();
        assert_eq!(
            state.traffic_signal_id.as_literal(),
            Some(&"signal_1".to_string())
        );
        assert_eq!(state.state.as_literal(), Some(&"green".to_string()));

        let group_state = TrafficSignalGroupState::default();
        assert_eq!(group_state.state.as_literal(), Some(&"green".to_string()));

        let state_action = TrafficSignalStateAction::default();
        assert_eq!(
            state_action.name.as_literal(),
            Some(&"DefaultSignal".to_string())
        );
        assert_eq!(state_action.state.as_literal(), Some(&"green".to_string()));

        let controller_action = TrafficSignalControllerAction::default();
        assert_eq!(
            controller_action.traffic_signal_controller_ref.as_literal(),
            Some(&"DefaultController".to_string())
        );
        assert_eq!(
            controller_action.phase_ref.as_literal(),
            Some(&"Phase1".to_string())
        );
    }

    #[test]
    fn test_traffic_signal_timing_validation() {
        // Test realistic traffic signal timing
        let controller = TrafficSignalController::new("realistic_intersection")
            .with_delay(1.0)
            .add_phase(Phase::new("green_ns", 45.0))
            .add_phase(Phase::new("yellow_ns", 3.0))
            .add_phase(Phase::new("red_ns_green_ew", 40.0))
            .add_phase(Phase::new("yellow_ew", 3.0));

        // Total cycle time should be reasonable
        let total_time: f64 = controller
            .phases
            .iter()
            .map(|p| p.duration.as_literal().unwrap_or(&0.0))
            .sum();

        assert_eq!(total_time, 91.0); // 45 + 3 + 40 + 3
        assert!(total_time > 60.0 && total_time < 180.0); // Reasonable cycle time
    }

    #[test]
    fn test_traffic_signal_state_transitions() {
        // Test that we can model state transitions properly
        let states = vec![
            TrafficSignalState::new("main", "green"),
            TrafficSignalState::new("main", "yellow"),
            TrafficSignalState::new("main", "red"),
        ];

        assert_eq!(states[0].state.as_literal(), Some(&"green".to_string()));
        assert_eq!(states[1].state.as_literal(), Some(&"yellow".to_string()));
        assert_eq!(states[2].state.as_literal(), Some(&"red".to_string()));

        // All should reference the same signal
        for state in &states {
            assert_eq!(
                state.traffic_signal_id.as_literal(),
                Some(&"main".to_string())
            );
        }
    }

    // COMPREHENSIVE TRAFFIC SWARM ACTION TESTS (Task 2.2 Requirements)

    #[test]
    fn test_traffic_swarm_action_creation_comprehensive() {
        let swarm = TrafficSwarmAction::new("Ego", 100.0, 50.0, 10)
            .with_inner_radius(20.0)
            .with_central_swarm_object("CentralVehicle");

        assert_eq!(swarm.number_of_vehicles.as_literal().unwrap(), &10);
        assert_eq!(swarm.inner_radius.as_literal().unwrap(), &20.0);
        assert_eq!(swarm.semi_major_axis.as_literal().unwrap(), &100.0);
        assert_eq!(swarm.semi_minor_axis.as_literal().unwrap(), &50.0);
        assert_eq!(
            swarm.central_object.entity_ref.as_literal().unwrap(),
            "CentralVehicle"
        );
    }

    #[test]
    fn test_central_swarm_object_creation() {
        let central = CentralSwarmObject::new("LeadVehicle");
        assert_eq!(central.entity_ref.as_literal().unwrap(), "LeadVehicle");
    }

    #[test]
    fn test_xml_serialization_traffic_swarm() {
        let swarm = TrafficSwarmAction::new("Ego", 75.0, 30.0, 5);

        let xml = quick_xml::se::to_string(&swarm).unwrap();
        assert!(xml.contains("TrafficSwarmAction"));
        assert!(xml.contains("semiMajorAxis=\"75\""));
        assert!(xml.contains("semiMinorAxis=\"30\""));
        assert!(xml.contains("numberOfVehicles=\"5\""));
    }

    #[test]
    fn test_xml_round_trip_traffic_swarm() {
        let original = TrafficSwarmAction::new("TestEntity", 120.0, 60.0, 8)
            .with_inner_radius(15.0)
            .with_offset(5.0);

        let xml = quick_xml::se::to_string(&original).unwrap();

        // Try to parse, but handle the error gracefully
        let deserialized = match quick_xml::de::from_str::<TrafficSwarmAction>(&xml) {
            Ok(result) => result,
            Err(e) => {
                println!("Deserialization error: {}", e);
                panic!("Failed to deserialize: {}", e);
            }
        };

        assert_eq!(
            original.central_object.entity_ref.as_literal(),
            deserialized.central_object.entity_ref.as_literal()
        );
        assert_eq!(
            original.semi_major_axis.as_literal(),
            deserialized.semi_major_axis.as_literal()
        );
        assert_eq!(
            original.semi_minor_axis.as_literal(),
            deserialized.semi_minor_axis.as_literal()
        );
        assert_eq!(
            original.number_of_vehicles.as_literal(),
            deserialized.number_of_vehicles.as_literal()
        );
        assert_eq!(
            original.inner_radius.as_literal(),
            deserialized.inner_radius.as_literal()
        );
        assert_eq!(
            original.offset.as_literal(),
            deserialized.offset.as_literal()
        );
    }

    #[test]
    fn test_traffic_swarm_with_central_object() {
        let swarm = TrafficSwarmAction::new("MainVehicle", 80.0, 40.0, 6)
            .with_central_swarm_object("CentralEntity");

        assert_eq!(
            swarm.central_object.entity_ref.as_literal().unwrap(),
            "CentralEntity"
        );
    }

    #[test]
    fn test_traffic_swarm_defaults() {
        let swarm = TrafficSwarmAction::default();
        // Test that defaults are reasonable
        assert!(swarm.central_object.entity_ref.as_literal().is_some());
        assert!(swarm.semi_major_axis.as_literal().is_some());
        assert!(swarm.semi_minor_axis.as_literal().is_some());
        assert!(swarm.number_of_vehicles.as_literal().is_some());
        assert!(swarm.inner_radius.as_literal().is_some());
        assert!(swarm.offset.as_literal().is_some());
    }

    #[test]
    fn test_realistic_swarm_parameters() {
        // Test realistic highway swarm scenario
        let highway_swarm = TrafficSwarmAction::new("EgoVehicle", 200.0, 100.0, 15)
            .with_inner_radius(50.0)
            .with_offset(10.0);

        assert_eq!(highway_swarm.semi_major_axis.as_literal().unwrap(), &200.0);
        assert_eq!(highway_swarm.inner_radius.as_literal().unwrap(), &50.0);
        assert_eq!(highway_swarm.offset.as_literal().unwrap(), &10.0);

        // Test city intersection swarm scenario
        let city_swarm = TrafficSwarmAction::new("CityEgo", 80.0, 60.0, 8)
            .with_central_swarm_object("IntersectionController");

        assert_eq!(
            city_swarm.central_object.entity_ref.as_literal().unwrap(),
            "IntersectionController"
        );
        assert_eq!(city_swarm.number_of_vehicles.as_literal().unwrap(), &8);
    }

    #[test]
    fn test_traffic_swarm_with_velocity() {
        let swarm = TrafficSwarmAction::new("TestVehicle", 90.0, 45.0, 12).with_velocity(60.0);

        assert!(swarm.velocity.is_some());
        assert_eq!(swarm.velocity.unwrap().as_literal().unwrap(), &60.0);
    }

    #[test]
    fn test_traffic_swarm_elliptical_parameters() {
        // Test elliptical swarm with different major/minor axes
        let elliptical_swarm = TrafficSwarmAction::new("EllipseCenter", 150.0, 75.0, 20);

        // Major axis should be larger than minor axis for proper ellipse
        assert!(
            elliptical_swarm.semi_major_axis.as_literal().unwrap()
                > elliptical_swarm.semi_minor_axis.as_literal().unwrap()
        );

        // Test circular swarm (equal axes)
        let circular_swarm = TrafficSwarmAction::new("CircleCenter", 100.0, 100.0, 15);
        assert_eq!(
            circular_swarm.semi_major_axis.as_literal().unwrap(),
            circular_swarm.semi_minor_axis.as_literal().unwrap()
        );
    }

    #[test]
    fn test_traffic_swarm_builder_pattern() {
        let swarm = TrafficSwarmAction::new("BuilderTest", 120.0, 80.0, 10)
            .with_inner_radius(25.0)
            .with_offset(15.0)
            .with_velocity(55.0)
            .with_traffic_definition(TrafficDefinition::default())
            .with_central_swarm_object("NewCentralEntity");

        assert_eq!(swarm.inner_radius.as_literal().unwrap(), &25.0);
        assert_eq!(swarm.offset.as_literal().unwrap(), &15.0);
        assert_eq!(swarm.velocity.unwrap().as_literal().unwrap(), &55.0);
        assert!(swarm.traffic_definition.is_some());
        assert_eq!(
            swarm.central_object.entity_ref.as_literal().unwrap(),
            "NewCentralEntity"
        );
    }
}