logic_mesh/blocks/control/
priority_array.rs

1// Copyright (c) 2022-2024, Radu Racariu.
2
3use uuid::Uuid;
4
5use crate::base::{
6    block::{Block, BlockDesc, BlockProps, BlockState},
7    input::{input_reader::InputReader, Input, InputProps},
8    output::Output,
9};
10use libhaystack::val::kind::HaystackKind;
11
12use crate::{blocks::InputImpl, blocks::OutputImpl};
13
14/// Outputs the result of a priority array based on the input values.
15#[block]
16#[derive(BlockProps, Debug)]
17#[category = "control"]
18pub struct PriorityArray {
19    #[input(name = "ManualLifeSafety", kind = "Number")]
20    pub manual_life_safety: InputImpl,
21    #[input(name = "AutoLifeSafety", kind = "Number")]
22    pub auto_life_safety: InputImpl,
23    #[input(kind = "Number")]
24    pub priority3: InputImpl,
25    #[input(kind = "Number")]
26    pub priority4: InputImpl,
27    #[input(name = "CriticalEquipmentControl", kind = "Number")]
28    pub critical_equipment_control: InputImpl,
29    #[input(name = "MinOnOf", kind = "Number")]
30    pub min_on_of: InputImpl,
31    #[input(kind = "Number")]
32    pub priority7: InputImpl,
33    #[input(name = "ManualOperator", kind = "Number")]
34    pub manual_operator: InputImpl,
35    #[input(kind = "Number")]
36    pub priority9: InputImpl,
37    #[input(kind = "Number")]
38    pub priority10: InputImpl,
39    #[input(kind = "Number")]
40    pub priority11: InputImpl,
41    #[input(kind = "Number")]
42    pub priority12: InputImpl,
43    #[input(kind = "Number")]
44    pub priority13: InputImpl,
45    #[input(kind = "Number")]
46    pub priority14: InputImpl,
47    #[input(kind = "Number")]
48    pub priority15: InputImpl,
49    #[input(kind = "Number")]
50    pub priority16: InputImpl,
51    #[input(kind = "Number")]
52    pub default: InputImpl,
53
54    #[output(kind = "Number")]
55    pub out: OutputImpl,
56}
57
58impl Block for PriorityArray {
59    async fn execute(&mut self) {
60        self.read_inputs_until_ready().await;
61
62        if let Some(Some(input)) = self
63            .inputs()
64            .iter()
65            .map(|input| input.get_value())
66            .find(|input| input.is_some())
67        {
68            self.out.set(input.clone());
69        } else {
70            self.out
71                .set(self.default.get_value().cloned().unwrap_or_default());
72        }
73    }
74}
75
76#[cfg(test)]
77mod test {
78
79    use crate::{
80        base::block::test_utils::write_block_inputs, base::block::Block,
81        blocks::control::PriorityArray,
82    };
83
84    #[tokio::test]
85    async fn test_priority_array_block() {
86        let mut block = PriorityArray::new();
87
88        write_block_inputs(&mut [(&mut block.manual_life_safety, 100.into())]).await;
89
90        block.execute().await;
91        assert_eq!(block.out.value, 100.into());
92    }
93
94    #[tokio::test]
95    async fn test_priority_array_min() {
96        let mut block = PriorityArray::new();
97
98        write_block_inputs(&mut [
99            (&mut block.manual_life_safety, 100.into()),
100            (&mut block.manual_operator, 200.into()),
101        ])
102        .await;
103
104        block.execute().await;
105        assert_eq!(block.out.value, 100.into());
106    }
107
108    #[tokio::test]
109    async fn test_priority_array_max() {
110        let mut block = PriorityArray::new();
111
112        write_block_inputs(&mut [(&mut block.priority16, 200.into())]).await;
113
114        block.execute().await;
115        assert_eq!(block.out.value, 200.into());
116    }
117}