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
use crate::sketchbook::ids::{DatasetId, ObservationId};
use crate::sketchbook::properties::dynamic_props::*;
use crate::sketchbook::utils::assert_name_valid;
use serde::{Deserialize, Serialize};

/// A typesafe representation wrapping various kinds of dynamic properties.
/// Each property has a `name` and field `variant` encompassing inner data.
///
/// The formula that will be internally created (usually, apart from generic variant) depends on
/// particular type of the property - there are multiple `variants` of properties, each carrying
/// its own different metadata that are later used to build the formula.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct DynProperty {
    name: String,
    annotation: String,
    variant: DynPropertyType,
}

/// Creating dynamic properties.
impl DynProperty {
    /// **(internal)** Shorthand to create a property given its already created internal
    /// `DynPropertyType` data, name, and annotation.
    fn new_raw(name: &str, variant: DynPropertyType, annotation: &str) -> DynProperty {
        DynProperty {
            name: name.to_string(),
            annotation: annotation.to_string(),
            variant,
        }
    }

    /// Create "generic" `DynProperty` instance directly from a formula, which must be in a
    /// correct format (which is verified).
    pub fn try_mk_generic(
        name: &str,
        raw_formula: &str,
        annotation: &str,
    ) -> Result<DynProperty, String> {
        let property = GenericDynProp {
            raw_formula: raw_formula.to_string(),
            processed_formula: HctlFormula::try_from_str(raw_formula)?,
        };
        let variant = DynPropertyType::GenericDynProp(property);
        Ok(Self::new_raw(name, variant, annotation))
    }

    /// Create `DynProperty` instance describing existence of a fixed point corresponding to
    /// a given observation.
    pub fn mk_fixed_point(
        name: &str,
        dataset: Option<DatasetId>,
        observation: Option<ObservationId>,
        annotation: &str,
    ) -> DynProperty {
        let property = ExistsFixedPoint {
            dataset,
            observation,
        };
        let variant = DynPropertyType::ExistsFixedPoint(property);
        Self::new_raw(name, variant, annotation)
    }

    /// Create `DynProperty` instance describing existence of a trap space corresponding to
    /// a given observation. Optionally, the trap space might be required to be minimal or
    /// non-percolable.
    pub fn mk_trap_space(
        name: &str,
        dataset: Option<DatasetId>,
        observation: Option<ObservationId>,
        minimal: bool,
        nonpercolable: bool,
        annotation: &str,
    ) -> DynProperty {
        let property = ExistsTrapSpace {
            dataset,
            observation,
            minimal,
            nonpercolable,
        };
        let variant = DynPropertyType::ExistsTrapSpace(property);
        Self::new_raw(name, variant, annotation)
    }

    /// Create `DynProperty` instance describing existence of a trajectory corresponding to
    /// observations from a given observation (in the given order).
    pub fn mk_trajectory(name: &str, dataset: Option<DatasetId>, annotation: &str) -> DynProperty {
        let property = ExistsTrajectory { dataset };
        let variant = DynPropertyType::ExistsTrajectory(property);
        Self::new_raw(name, variant, annotation)
    }

    /// Create `DynProperty` instance describing the number of existing attractors.
    pub fn try_mk_attractor_count(
        name: &str,
        minimal: usize,
        maximal: usize,
        annotation: &str,
    ) -> Result<DynProperty, String> {
        if minimal > maximal {
            return Err("`minimal` attractor count cannot be larger than `maximal`.".to_string());
        }
        if minimal == 0 || maximal == 0 {
            return Err("Attractor count must be larger than 0.".to_string());
        }
        let property = AttractorCount { minimal, maximal };
        let variant = DynPropertyType::AttractorCount(property);
        Ok(Self::new_raw(name, variant, annotation))
    }

    /// Create `DynProperty` instance describing the existence of an attractor corresponding to
    /// a corresponding dataset, or some specific observation in it.
    pub fn mk_has_attractor(
        name: &str,
        dataset: Option<DatasetId>,
        observation: Option<ObservationId>,
        annotation: &str,
    ) -> DynProperty {
        let property = HasAttractor {
            dataset,
            observation,
        };
        let variant = DynPropertyType::HasAttractor(property);
        Self::new_raw(name, variant, annotation)
    }

    /// Create default `DynProperty` instance of specified variant.
    pub fn default(variant: SimpleDynPropertyType) -> DynProperty {
        match variant {
            SimpleDynPropertyType::GenericDynProp => Self::default_generic(),
            SimpleDynPropertyType::ExistsFixedPoint => Self::default_fixed_point(),
            SimpleDynPropertyType::ExistsTrapSpace => Self::default_trap_space(),
            SimpleDynPropertyType::ExistsTrajectory => Self::default_trajectory(),
            SimpleDynPropertyType::AttractorCount => Self::default_attractor_count(),
            SimpleDynPropertyType::HasAttractor => Self::default_has_attractor(),
        }
    }

    /// Create default "generic" `DynProperty` instance, representing "true" formula.
    pub fn default_generic() -> DynProperty {
        Self::try_mk_generic("Generic dynamic property", "true", "").unwrap()
    }

    /// Create default `DynProperty` instance for the existence of a fixed point, with empty
    /// `dataset` and `observation` fields.
    pub fn default_fixed_point() -> DynProperty {
        Self::mk_fixed_point("Fixed point existence", None, None, "")
    }

    /// Create default `DynProperty` instance for the existence of a trap space, with empty
    /// `dataset` and `observation` fields, and all flags set to false.
    pub fn default_trap_space() -> DynProperty {
        Self::mk_trap_space("Trap space existence", None, None, false, false, "")
    }

    /// Create default `DynProperty` instance for the existence of a trajectory, with an empty
    /// `dataset`field.
    pub fn default_trajectory() -> DynProperty {
        Self::mk_trajectory("Trajectory existence", None, "")
    }

    /// Create default `DynProperty` instance for the number of existing attractors, with default
    /// count being 1.
    pub fn default_attractor_count() -> DynProperty {
        Self::try_mk_attractor_count("Attractor count", 1, 1, "").unwrap()
    }

    /// Create default `DynProperty` instance for the existence of an attractor with empty
    /// `dataset` and `observation` fields.
    pub fn default_has_attractor() -> DynProperty {
        Self::mk_has_attractor("Attractor existence", None, None, "")
    }
}

/// Editing dynamic properties.
impl DynProperty {
    /// Update property's name.
    pub fn set_name(&mut self, new_name: &str) -> Result<(), String> {
        assert_name_valid(new_name)?;
        self.name = new_name.to_string();
        Ok(())
    }

    /// Set property's annotation string.
    pub fn set_annotation(&mut self, annotation: &str) {
        self.annotation = annotation.to_string()
    }

    /// Update property's sub-field `dataset` where applicable. If not applicable, return `Err`.
    pub fn set_dataset(&mut self, new_dataset: DatasetId) -> Result<(), String> {
        let new_dataset = Some(new_dataset);
        match &mut self.variant {
            DynPropertyType::ExistsFixedPoint(prop) => prop.dataset = new_dataset,
            DynPropertyType::ExistsTrapSpace(prop) => prop.dataset = new_dataset,
            DynPropertyType::ExistsTrajectory(prop) => prop.dataset = new_dataset,
            DynPropertyType::HasAttractor(prop) => prop.dataset = new_dataset,
            // Other cases do not have a dataset field
            other_variant => {
                return Err(format!(
                    "{other_variant:?} does not have a field `dataset`."
                ));
            }
        }
        Ok(())
    }

    /// Update property's sub-field `observation` where applicable. If not applicable, return `Err`.
    pub fn set_observation(&mut self, new_obs: ObservationId) -> Result<(), String> {
        let new_obs = Some(new_obs);
        match &mut self.variant {
            DynPropertyType::ExistsFixedPoint(prop) => prop.observation = new_obs,
            DynPropertyType::ExistsTrapSpace(prop) => prop.observation = new_obs,
            DynPropertyType::HasAttractor(prop) => prop.observation = new_obs,
            // Other cases do not have a observation field
            other_variant => {
                return Err(format!(
                    "{other_variant:?} does not have a field `observation`."
                ));
            }
        }
        Ok(())
    }

    /// Update generic property's formula. If not applicable (different variant), return `Err`.
    pub fn set_formula(&mut self, new_formula: &str) -> Result<(), String> {
        if let DynPropertyType::GenericDynProp(prop) = &mut self.variant {
            // first check everything is valid, then update fields
            let parsed_formula = HctlFormula::try_from_str(new_formula)?;
            prop.processed_formula = parsed_formula;
            prop.raw_formula = new_formula.to_string();
            Ok(())
        } else {
            Err(format!(
                "{:?} does not have a formula to update.",
                self.variant
            ))
        }
    }

    /// Update property's sub-field `observation` to None where applicable. If not applicable,
    /// return `Err`.
    pub fn remove_observation(&mut self) -> Result<(), String> {
        if let DynPropertyType::HasAttractor(prop) = &mut self.variant {
            prop.observation = None;
            Ok(())
        } else {
            Err(format!(
                "{:?} does not have a observation to remove.",
                self.variant
            ))
        }
    }

    /// Update property's sub-fields, if the property is of `AttractorCount` variant.
    /// If not applicable, return `Err`.
    pub fn set_attr_count(&mut self, minimal: usize, maximal: usize) -> Result<(), String> {
        if let DynPropertyType::AttractorCount(prop) = &mut self.variant {
            prop.minimal = minimal;
            prop.maximal = maximal;
            Ok(())
        } else {
            Err(format!(
                "{:?} does not have a attractor count to update.",
                self.variant
            ))
        }
    }

    /// Update property's sub-fields, if the property is of `ExistsTrapSpace` variant.
    /// If not applicable, return `Err`.
    pub fn set_trap_space_details(
        &mut self,
        is_minimal: bool,
        nonpercolable: bool,
    ) -> Result<(), String> {
        if let DynPropertyType::ExistsTrapSpace(prop) = &mut self.variant {
            prop.minimal = is_minimal;
            prop.nonpercolable = nonpercolable;
            Ok(())
        } else {
            Err(format!(
                "{:?} does not have a trap space fields to update.",
                self.variant
            ))
        }
    }
}

/// Observing dynamic properties.
impl DynProperty {
    /// Get property's name.
    pub fn get_name(&self) -> &str {
        &self.name
    }

    /// Get annotation string.
    pub fn get_annotation(&self) -> &str {
        &self.annotation
    }

    /// Get property's variant with all the underlying data.
    pub fn get_prop_data(&self) -> &DynPropertyType {
        &self.variant
    }

    /// Check that the property has all required fields filled out. That is just the `dataset`
    /// in most cases at the moment.
    /// If some of the required field is set to None, return error.
    pub fn assert_dataset_filled(&self) -> Result<(), String> {
        let missing_field_msg = "One of the required fields is not filled.";

        match &self.variant {
            DynPropertyType::GenericDynProp(_) => {} // no fields that can be None
            DynPropertyType::AttractorCount(_) => {} // no fields that can be None
            DynPropertyType::HasAttractor(p) => {
                // only dataset has to be filled, observation ID is optional
                if p.dataset.is_none() {
                    return Err(missing_field_msg.to_string());
                }
            }
            DynPropertyType::ExistsFixedPoint(p) => {
                // only dataset has to be filled, observation ID is optional
                if p.dataset.is_none() {
                    return Err(missing_field_msg.to_string());
                }
            }
            DynPropertyType::ExistsTrajectory(p) => {
                if p.dataset.is_none() {
                    return Err(missing_field_msg.to_string());
                }
            }
            DynPropertyType::ExistsTrapSpace(p) => {
                // only dataset has to be filled, observation ID is optional
                if p.dataset.is_none() {
                    return Err(missing_field_msg.to_string());
                }
            }
        }
        Ok(())
    }
}