Skip to main content

cimstructs/
RegularTimePoint.rs

1/// Time point for a schedule where the time between the consecutive points is constant.
2#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
3pub struct RegularTimePoint {
4    pub id: String,
5    /// Regular interval schedule containing this time point.
6    #[serde(skip_serializing_if = "Option::is_none")]
7    pub interval_schedule: Option<super::base::MridRef>,
8    /// The position of the regular time point in the sequence. Note that time points don't have to be sequential, i.e. time points may be omitted. The actual time for a RegularTimePoint is computed by multiplying the associated regular interval schedule's time step with the regular time point sequence number and adding the associated schedules start time. To specify values for the start time, use sequence number 0. The sequence number cannot be negative.
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub sequence_number: Option<i64>,
11    /// The first value at the time. The meaning of the value is defined by the derived type of the associated schedule.
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub value1: Option<f64>,
14    /// The second value at the time. The meaning of the value is defined by the derived type of the associated schedule.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub value2: Option<f64>,
17}
18impl crate::base::CimElement for RegularTimePoint {
19    fn mrid(&self) -> &str { &self.id }
20    fn type_name(&self) -> &'static str { "RegularTimePoint" }
21    fn as_any(&self) -> &dyn std::any::Any { self }
22    fn to_json_value(&self) -> serde_json::Value {
23        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
24    }
25    fn to_block(&self) -> crate::base::RdfBlock {
26        let mut block = crate::base::RdfBlock {
27            type_name: "RegularTimePoint".to_string(),
28            mrid: self.id.clone(),
29            fields: std::collections::HashMap::new(),
30            duplicate_fields: std::collections::HashSet::new(),
31        };
32        if let Some(ref v) = self.interval_schedule {
33            block.fields.insert("RegularTimePoint.IntervalSchedule".into(), crate::base::FieldValue::Resource(v.mrid.clone()));
34        }
35        if let Some(v) = self.sequence_number {
36            block.fields.insert("RegularTimePoint.sequenceNumber".into(), crate::base::FieldValue::Text(v.to_string()));
37        }
38        if let Some(v) = self.value1 {
39            block.fields.insert("RegularTimePoint.value1".into(), crate::base::FieldValue::Text(v.to_string()));
40        }
41        if let Some(v) = self.value2 {
42            block.fields.insert("RegularTimePoint.value2".into(), crate::base::FieldValue::Text(v.to_string()));
43        }
44        block
45    }
46}
47
48impl RegularTimePoint {
49    pub fn from_block(b: &crate::base::RdfBlock) -> Self {
50        let mut obj = Self::default();
51        obj.id.clone_from(&b.mrid);
52        for (key, val) in &b.fields {
53            match key.as_str() {
54                "RegularTimePoint.IntervalSchedule" => {
55                    if let crate::base::FieldValue::Resource(sv) = val {
56                        obj.interval_schedule = Some(crate::base::MridRef { mrid: sv.clone() });
57                    }
58                }
59                "RegularTimePoint.sequenceNumber" => {
60                    match val {
61                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.sequence_number = Some(v); } }
62                        crate::base::FieldValue::TextList(svs) => {
63                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.sequence_number = Some(v); } }
64                        }
65                        _ => {}
66                    }
67                }
68                "RegularTimePoint.value1" => {
69                    match val {
70                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.value1 = Some(v); } }
71                        crate::base::FieldValue::TextList(svs) => {
72                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.value1 = Some(v); } }
73                        }
74                        _ => {}
75                    }
76                }
77                "RegularTimePoint.value2" => {
78                    match val {
79                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.value2 = Some(v); } }
80                        crate::base::FieldValue::TextList(svs) => {
81                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.value2 = Some(v); } }
82                        }
83                        _ => {}
84                    }
85                }
86                _ => {}
87            }
88        }
89        obj
90    }
91}