cu29-runtime 1.0.0-rc1

Copper Runtime Runtime crate. Copper is an engine for robotics.
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
#[cfg(all(test, feature = "std"))]
mod tests {
    use cu29_runtime::config::{NodeId, read_configuration};
    use std::fs::{create_dir_all, write};
    use tempfile::tempdir;

    #[test]
    fn test_basic_include() {
        let temp_dir = tempdir().unwrap();
        let base_path = temp_dir.path();

        let config_dir = base_path.join("config");
        create_dir_all(&config_dir).unwrap();

        let included_config = r#"(
        tasks: [
            (
                id: "included_task",
                type: "tasks::IncludedTask",
            ),
        ],
        cnx: [],
    )"#;

        let included_path = config_dir.join("included.ron");
        write(&included_path, included_config).unwrap();

        let main_config = r#"(
            tasks: [
                (
                    id: "main_task",
                    type: "tasks::MainTask",
                ),
            ],
            cnx: [],
            includes: [
                (
                    path: "included.ron",
                    params: {},
                ),
            ],
        )"#
        .to_string();

        let main_path = config_dir.join("main.ron");
        write(&main_path, main_config).unwrap();

        let config = read_configuration(main_path.to_str().unwrap()).unwrap();
        let graph = config.get_graph(None).unwrap();

        let all_nodes = graph.get_all_nodes();
        assert_eq!(all_nodes.len(), 2);

        let task_ids: Vec<_> = all_nodes
            .iter()
            .map(|(_, node)| node.get_id().to_string())
            .collect();
        assert!(task_ids.contains(&"main_task".to_string()));
        assert!(task_ids.contains(&"included_task".to_string()));
    }

    #[test]
    fn test_parameter_substitution() {
        let temp_dir = tempdir().unwrap();
        let base_path = temp_dir.path();

        let config_dir = base_path.join("config");
        create_dir_all(&config_dir).unwrap();

        let included_config = r#"(
        tasks: [
            (
                id: "task_{{instance_id}}",
                type: "tasks::Task{{instance_id}}",
                config: {
                    "param_value": {{param_value}},
                },
            ),
        ],
        cnx: [],
    )"#;

        let included_path = config_dir.join("included.ron");
        write(&included_path, included_config).unwrap();

        let main_config = r#"(
            tasks: [],
            cnx: [],
            includes: [
                (
                    path: "included.ron",
                    params: {
                        "instance_id": "42",
                        "param_value": 100,
                    },
                ),
            ],
        )"#
        .to_string();

        let main_path = config_dir.join("main.ron");
        write(&main_path, main_config).unwrap();

        let config = read_configuration(main_path.to_str().unwrap()).unwrap();
        let graph = config.get_graph(None).unwrap();

        let all_nodes = graph.get_all_nodes();
        assert_eq!(all_nodes.len(), 1);

        let (_, node) = all_nodes[0];
        assert_eq!(node.get_id(), "task_42");
        assert_eq!(node.get_type(), "tasks::Task42");
        assert_eq!(
            node.get_param::<i32>("param_value")
                .expect("param_value lookup failed"),
            Some(100)
        );
    }

    #[test]
    fn test_nested_includes() {
        let temp_dir = tempdir().unwrap();
        let base_path = temp_dir.path();

        let config_dir = base_path.join("config");
        create_dir_all(&config_dir).unwrap();

        let nested_config = r#"(
        tasks: [
            (
                id: "nested_task",
                type: "tasks::NestedTask",
            ),
        ],
        cnx: [],
    )"#;

        let nested_path = config_dir.join("nested.ron");
        write(&nested_path, nested_config).unwrap();

        let middle_config = r#"(
            tasks: [
                (
                    id: "middle_task",
                    type: "tasks::MiddleTask",
                ),
            ],
            cnx: [],
            includes: [
                (
                    path: "nested.ron",
                    params: {},
                ),
            ],
        )"#
        .to_string();

        let middle_path = config_dir.join("middle.ron");
        write(&middle_path, middle_config).unwrap();

        let main_config = r#"(
            tasks: [
                (
                    id: "main_task",
                    type: "tasks::MainTask",
                ),
            ],
            cnx: [],
            includes: [
                (
                    path: "middle.ron",
                    params: {},
                ),
            ],
        )"#
        .to_string();

        let main_path = config_dir.join("main.ron");
        write(&main_path, main_config).unwrap();

        let config = read_configuration(main_path.to_str().unwrap()).unwrap();
        let graph = config.get_graph(None).unwrap();

        let all_nodes = graph.get_all_nodes();
        assert_eq!(all_nodes.len(), 3);

        let task_ids: Vec<_> = all_nodes
            .iter()
            .map(|(_, node)| node.get_id().to_string())
            .collect();
        assert!(task_ids.contains(&"main_task".to_string()));
        assert!(task_ids.contains(&"middle_task".to_string()));
        assert!(task_ids.contains(&"nested_task".to_string()));
    }

    #[test]
    fn test_override_behavior() {
        let temp_dir = tempdir().unwrap();
        let base_path = temp_dir.path();

        let config_dir = base_path.join("config");
        create_dir_all(&config_dir).unwrap();

        let included_config = r#"(
        tasks: [
            (
                id: "common_task",
                type: "tasks::IncludedTask",
                config: {
                    "param": 10,
                },
            ),
            (
                id: "only_included_task",
                type: "tasks::OnlyIncludedTask",
            ),
        ],
        cnx: [],
        monitors: [(
            type: "tasks::IncludedMonitor",
        )],
    )"#;

        let included_path = config_dir.join("included.ron");
        write(&included_path, included_config).unwrap();

        let main_config = r#"(
            tasks: [
                (
                    id: "common_task",
                    type: "tasks::MainTask",
                    config: {
                        "param": 20,
                    },
                ),
                (
                    id: "only_main_task",
                    type: "tasks::OnlyMainTask",
                ),
            ],
            cnx: [],
            monitors: [(
                type: "tasks::MainMonitor",
            )],
            includes: [
                (
                    path: "included.ron",
                    params: {},
                ),
            ],
        )"#
        .to_string();

        let main_path = config_dir.join("main.ron");
        write(&main_path, main_config).unwrap();

        let config = read_configuration(main_path.to_str().unwrap()).unwrap();
        let graph = config.get_graph(None).unwrap();

        let all_nodes = graph.get_all_nodes();
        assert_eq!(all_nodes.len(), 3);

        let task_ids: Vec<_> = all_nodes
            .iter()
            .map(|(_, node)| node.get_id().to_string())
            .collect();
        assert!(task_ids.contains(&"common_task".to_string()));
        assert!(task_ids.contains(&"only_included_task".to_string()));
        assert!(task_ids.contains(&"only_main_task".to_string()));

        let common_task = all_nodes
            .iter()
            .find(|(_, node)| node.get_id() == "common_task")
            .unwrap()
            .1;

        assert_eq!(common_task.get_type(), "tasks::MainTask");
        assert_eq!(
            common_task
                .get_param::<i32>("param")
                .expect("param lookup failed"),
            Some(20)
        );

        assert_eq!(
            config.get_monitor_config().unwrap().get_type(),
            "tasks::MainMonitor"
        );
    }

    #[test]
    fn test_error_handling() {
        let temp_dir = tempdir().unwrap();
        let base_path = temp_dir.path();

        let config_dir = base_path.join("config");
        create_dir_all(&config_dir).unwrap();

        let main_config = r#"(
        tasks: [],
        cnx: [],
        includes: [
            (
                path: "non_existent.ron",
                params: {},
            ),
        ],
    )"#;

        let main_path = config_dir.join("main.ron");
        write(&main_path, main_config).unwrap();

        let result = read_configuration(main_path.to_str().unwrap());
        assert!(result.is_err());

        let invalid_config = r#"(
        tasks: (
            (
                id: "invalid_task",
                type: "tasks::InvalidTask",
            ),
        ),
        cnx: [],
    )"#;

        let invalid_path = config_dir.join("invalid.ron");
        write(&invalid_path, invalid_config).unwrap();

        let main_config = r#"(
            tasks: [],
            cnx: [],
            includes: [
                (
                    path: "invalid.ron",
                    params: {},
                ),
            ],
        )"#
        .to_string();

        let main_path = config_dir.join("main.ron");
        write(&main_path, main_config).unwrap();

        let result = read_configuration(main_path.to_str().unwrap());
        assert!(result.is_err());
    }

    #[test]
    fn test_include_preserves_parallel_connections_with_different_messages() {
        let temp_dir = tempdir().unwrap();
        let base_path = temp_dir.path();

        let config_dir = base_path.join("config");
        create_dir_all(&config_dir).unwrap();

        let included_config = r#"(
        tasks: [
            (
                id: "multi_src",
                type: "tasks::MultiSource",
            ),
            (
                id: "multi_input",
                type: "tasks::MultiInput",
            ),
        ],
        cnx: [
            (src: "multi_src", dst: "multi_input", msg: "i32"),
            (src: "multi_src", dst: "multi_input", msg: "u32"),
        ],
    )"#;

        let included_path = config_dir.join("included.ron");
        write(&included_path, included_config).unwrap();

        let main_config = r#"(
            tasks: [],
            cnx: [],
            includes: [
                (
                    path: "included.ron",
                    params: {},
                ),
            ],
        )"#;

        let main_path = config_dir.join("main.ron");
        write(&main_path, main_config).unwrap();

        let config = read_configuration(main_path.to_str().unwrap()).unwrap();
        let graph = config.get_graph(None).unwrap();
        let mut messages: Vec<_> = graph
            .edges()
            .filter(|edge| edge.src == "multi_src" && edge.dst == "multi_input")
            .map(|edge| edge.msg.as_str())
            .collect();
        messages.sort();

        assert_eq!(messages, ["i32", "u32"]);
    }

    #[test]
    fn test_multiple_parameterized_includes() {
        let temp_dir = tempdir().unwrap();
        let base_path = temp_dir.path();

        let config_dir = base_path.join("config");
        create_dir_all(&config_dir).unwrap();

        // Create sensor-detect.ron template file
        let sensor_detect_config = r#"(
        tasks: [
            (
                id: "camera{{id}}",
                type: "cu_camera::Camera",
                config: {
                    "port": "{{port}}",
                },
            ),
            (
                id: "detect{{id}}",
                type: "cu_detector::Detector",
                config: {
                    "threshold": {{threshold}},
                },
            ),
        ],
        cnx: [
            (src: "camera{{id}}", dst: "detect{{id}}", msg: "cu_camera::CameraPayload"),
        ],
    )"#;

        let sensor_detect_path = config_dir.join("sensor-detect.ron");
        write(&sensor_detect_path, sensor_detect_config).unwrap();

        // Create main.ron file with multiple includes
        let main_config = r#"(
        tasks: [
            (
                id: "octopus",
                type: "cu_octopus::octopus0",
            ),
        ],
        includes: [
            ( path: "sensor-detect.ron", params: { "id": 0, "port": "/dev/video0", "threshold": 0.5, },),
            ( path: "sensor-detect.ron", params: { "id": 1, "port": "/dev/video1", "threshold": 0.1, },),
            ( path: "sensor-detect.ron", params: { "id": 2, "port": "/dev/video2", "threshold": 0.7, },),
        ],
        cnx: [
            (src: "detect0", dst: "octopus", msg: "cu_detect::DetectionPayload"),
            (src: "detect1", dst: "octopus", msg: "cu_detect::DetectionPayload"),
            (src: "detect2", dst: "octopus", msg: "cu_detect::DetectionPayload"),
        ],
        monitors: [(
            type: "cu_consolemon::CuConsoleMon",
        )]
    )"#;

        let main_path = config_dir.join("main.ron");
        write(&main_path, main_config).unwrap();

        // Parse the configuration and verify
        let config = read_configuration(main_path.to_str().unwrap()).unwrap();
        let graph = config.get_graph(None).unwrap();

        // Verify tasks
        let all_nodes = graph.get_all_nodes();
        assert_eq!(all_nodes.len(), 7); // 1 octopus + 3 cameras + 3 detectors

        // Verify octopus task exists
        let octopus_task = all_nodes
            .iter()
            .find(|(_, node)| node.get_id() == "octopus")
            .map(|(_, node)| node);
        assert!(octopus_task.is_some());
        assert_eq!(octopus_task.unwrap().get_type(), "cu_octopus::octopus0");

        // Verify camera tasks with correct ports
        for id in 0..3 {
            let camera_id = format!("camera{id}");
            let camera_task = all_nodes
                .iter()
                .find(|(_, node)| node.get_id() == camera_id)
                .map(|(_, node)| node);

            assert!(camera_task.is_some());
            let camera = camera_task.unwrap();
            assert_eq!(camera.get_type(), "cu_camera::Camera");

            let expected_port = format!("/dev/video{id}");
            let port = camera
                .get_param::<String>("port")
                .expect("port lookup failed")
                .expect("missing port");
            assert_eq!(port, expected_port);
        }

        // Verify detector tasks with correct thresholds
        let expected_thresholds = [0.5, 0.1, 0.7];
        #[allow(clippy::needless_range_loop)]
        for id in 0..3 {
            let detect_id = format!("detect{id}");
            let detect_task = all_nodes
                .iter()
                .find(|(_, node)| node.get_id() == detect_id)
                .map(|(_, node)| node);

            assert!(detect_task.is_some());
            let detector = detect_task.unwrap();
            assert_eq!(detector.get_type(), "cu_detector::Detector");

            let threshold = detector
                .get_param::<f64>("threshold")
                .expect("threshold lookup failed")
                .expect("missing threshold");
            assert_eq!(threshold, expected_thresholds[id]);
        }

        // Get the graph and verify connections
        let graph = config.graphs.get_graph(None).unwrap();

        // Find the node IDs for verification
        let mut camera_node_ids = Vec::new();
        let mut detector_node_ids = Vec::new();
        let mut octopus_node_id = None;

        for idx in graph.node_indices() {
            let node_index = idx.index() as u32;
            let node = graph.get_node_weight(node_index).unwrap();
            let id = node.get_id();

            if id == "octopus" {
                octopus_node_id = Some(idx);
            } else if id.starts_with("camera") {
                camera_node_ids.push((id.clone(), idx));
            } else if id.starts_with("detect") {
                detector_node_ids.push((id.clone(), idx));
            }
        }

        // Verify camera-to-detector connections (internal to sensor-detect)
        for id in 0..3 {
            let camera_id = format!("camera{id}");
            let detect_id = format!("detect{id}");
            let camera_idx = graph.get_node_id_by_name(camera_id.as_str()).unwrap();
            let detector_idx = graph.get_node_id_by_name(detect_id.as_str()).unwrap();

            // Check if there's an edge from camera to detector
            let has_connection = graph
                .get_connection_msg_type(camera_idx, detector_idx)
                .unwrap();
            assert_eq!(has_connection, "cu_camera::CameraPayload");
        }

        // Verify detector-to-octopus connections (defined in main.ron)
        let octopus_idx = octopus_node_id.unwrap();

        for id in 0..3 {
            let detect_id = format!("detect{id}");
            let detector_idx = detector_node_ids
                .iter()
                .find(|(id, _)| *id == detect_id)
                .unwrap()
                .1;

            // Check if there's an edge from detector to octopus
            let has_connection = graph
                .get_connection_msg_type(
                    detector_idx.index() as NodeId,
                    octopus_idx.index() as NodeId,
                )
                .unwrap();
            assert_eq!(has_connection, "cu_detect::DetectionPayload");
        }

        // Verify monitor
        assert!(config.get_monitor_config().is_some());
        assert_eq!(
            config.get_monitor_config().unwrap().get_type(),
            "cu_consolemon::CuConsoleMon"
        );
    }
}