autosar_data_abstraction/ecu_configuration/values/
reference.rs

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
use crate::ecu_configuration::{AbstractEcucReferenceDef, EcucAnyReferenceDef, EcucInstanceReferenceDef};
use crate::{abstraction_element, AbstractionElement, AutosarAbstractionError};
use autosar_data::{Element, ElementName};

//#########################################################

/// An `EcucInstanceReferenceValue` provides the mechanism to reference an instance of a prototype
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucInstanceReferenceValue(Element);
abstraction_element!(EcucInstanceReferenceValue, EcucInstanceReferenceValue);

impl EcucInstanceReferenceValue {
    pub(crate) fn new(
        parent: &Element,
        definition: &EcucInstanceReferenceDef,
        target_context: &[&Element],
        target: &Element,
    ) -> Result<Self, AutosarAbstractionError> {
        let instance_ref_elem = parent.create_sub_element(ElementName::EcucInstanceReferenceValue)?;
        let instance_ref = Self(instance_ref_elem);

        instance_ref.set_definition(definition)?;
        instance_ref.set_target(target_context, target)?;

        Ok(instance_ref)
    }

    /// set the parameter definition reference
    pub fn set_definition(&self, definition: &EcucInstanceReferenceDef) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::DefinitionRef)?
            .set_reference_target(definition.element())?;

        Ok(())
    }

    /// get the parameter definition
    ///
    /// This function returns the definition as an `EcucParameterDef` enum, which
    /// could contain either an `EcucFloatParamDef` or an `EcucIntegerParamDef`.
    /// If the definition is not loaded, use `definition_ref()` instead.
    pub fn definition(&self) -> Option<EcucInstanceReferenceDef> {
        let definition_elem = self
            .element()
            .get_sub_element(ElementName::DefinitionRef)?
            .get_reference_target()
            .ok()?;
        EcucInstanceReferenceDef::try_from(definition_elem).ok()
    }

    /// get the parameter definition reference as a string
    ///
    /// This function is an alternative to `definition()`; it is useful when the
    /// referenced definition is not loaded and can't be resolved.
    pub fn definition_ref(&self) -> Option<String> {
        self.element()
            .get_sub_element(ElementName::DefinitionRef)?
            .character_data()?
            .string_value()
    }

    /// Set the target of the reference
    ///
    /// An instance reference targets a specific instance of a prototype. In order to uniquely identify the target,
    /// the target context is required. The target context is a list of elements that are the parent elements of the
    /// target element. The instance reference definition specifies which context elements are required.
    pub fn set_target(&self, taget_context: &[&Element], target: &Element) -> Result<(), AutosarAbstractionError> {
        // remove existing target elements
        let _ = self.element().remove_sub_element_kind(ElementName::ValueIref);
        // create the target context elements

        let value_iref_elem = self.element().create_sub_element(ElementName::ValueIref)?;
        for context_elem in taget_context {
            value_iref_elem
                .create_sub_element(ElementName::ContextElementRef)?
                .set_reference_target(context_elem)?;
        }
        // create the target element
        value_iref_elem
            .create_sub_element(ElementName::TargetRef)?
            .set_reference_target(target)?;

        Ok(())
    }

    /// Get the target of the reference
    ///
    /// Returns the targt element of the instance reference, as well as the context elements that are needed to uniquely
    /// identify the target.
    pub fn target(&self) -> Option<(Vec<Element>, Element)> {
        let value_iref_elem = self.element().get_sub_element(ElementName::ValueIref)?;
        let target = value_iref_elem
            .get_sub_element(ElementName::TargetRef)?
            .get_reference_target()
            .ok()?;

        let context_elements: Vec<_> = value_iref_elem
            .sub_elements()
            .filter(|elem| elem.element_name() == ElementName::ContextElementRef)
            .filter_map(|context_ref| context_ref.get_reference_target().ok())
            .collect();

        Some((context_elements, target))
    }

    /// set the index of the reference
    ///
    /// If the reference definition has `requiresIndex` set to `true`, then the reference
    /// must have an index. Otherwise the index is meaningless.
    pub fn set_index(&self, index: Option<u64>) -> Result<(), AutosarAbstractionError> {
        if let Some(index) = index {
            self.element()
                .get_or_create_sub_element(ElementName::Index)?
                .set_character_data(index)?;
        } else {
            let _ = self.element().remove_sub_element_kind(ElementName::Index);
        }

        Ok(())
    }

    /// get the index of the reference
    ///
    /// If the reference definition has `requiresIndex` set to `true`, then the reference
    /// must have an index. Otherwise the index is meaningless.
    pub fn index(&self) -> Option<u64> {
        self.element()
            .get_sub_element(ElementName::Index)?
            .character_data()?
            .parse_integer()
    }

    /// set the isAutoValue flag
    ///
    /// If the reference definition has `withAuto` set to `true`, then the reference is allowed to have an auto value.
    pub fn set_is_auto_value(&self, is_auto_value: Option<bool>) -> Result<(), AutosarAbstractionError> {
        if let Some(is_auto_value) = is_auto_value {
            self.element()
                .get_or_create_sub_element(ElementName::IsAutoValue)?
                .set_character_data(is_auto_value)?;
        } else {
            let _ = self.element().remove_sub_element_kind(ElementName::IsAutoValue);
        }

        Ok(())
    }

    /// get the isAutoValue flag
    pub fn is_auto_value(&self) -> Option<bool> {
        self.element()
            .get_sub_element(ElementName::IsAutoValue)?
            .character_data()?
            .parse_bool()
    }
}

//#########################################################

/// An `EcucReferenceValue` allows the ecu tonfiguration to refer to any identifiable element in the Autosar model
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucReferenceValue(Element);
abstraction_element!(EcucReferenceValue, EcucReferenceValue);

impl EcucReferenceValue {
    pub(crate) fn new<T: AbstractEcucReferenceDef>(
        parent: &Element,
        definition: &T,
        target: &Element,
    ) -> Result<Self, AutosarAbstractionError> {
        let reference_elem = parent.create_sub_element(ElementName::EcucReferenceValue)?;
        let reference = Self(reference_elem);

        reference.set_definition(definition)?;
        reference.set_target(target)?;

        Ok(reference)
    }

    /// set the parameter definition reference
    pub fn set_definition<T: AbstractEcucReferenceDef>(&self, definition: &T) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::DefinitionRef)?
            .set_reference_target(definition.element())?;

        Ok(())
    }

    /// get the reference definition
    ///
    /// This function returns the definition as an `EcucParameterDef` enum, which
    /// could contain either an `EcucFloatParamDef` or an `EcucIntegerParamDef`.
    /// If the definition is not loaded, use `definition_ref()` instead.
    pub fn definition(&self) -> Option<EcucAnyReferenceDef> {
        let definition_elem = self
            .element()
            .get_sub_element(ElementName::DefinitionRef)?
            .get_reference_target()
            .ok()?;
        EcucAnyReferenceDef::try_from(definition_elem).ok()
    }

    /// get the referenced definition ref as a string
    ///
    /// This function is an alternative to `definition()`; it is useful when the
    /// referenced definition is not loaded and can't be resolved.
    pub fn definition_ref(&self) -> Option<String> {
        self.element()
            .get_sub_element(ElementName::DefinitionRef)?
            .character_data()?
            .string_value()
    }

    /// Set the target of the reference
    pub fn set_target(&self, target: &Element) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::ValueRef)?
            .set_reference_target(target)?;

        Ok(())
    }

    /// Get the target of the reference
    pub fn target(&self) -> Option<Element> {
        self.element()
            .get_sub_element(ElementName::ValueRef)?
            .get_reference_target()
            .ok()
    }

    /// set the index of the reference
    ///
    /// If the reference definition has `requiresIndex` set to `true`, then the reference
    /// must have an index. Otherwise the index is meaningless.
    pub fn set_index(&self, index: Option<u64>) -> Result<(), AutosarAbstractionError> {
        if let Some(index) = index {
            self.element()
                .get_or_create_sub_element(ElementName::Index)?
                .set_character_data(index)?;
        } else {
            let _ = self.element().remove_sub_element_kind(ElementName::Index);
        }

        Ok(())
    }

    /// get the index of the reference
    ///
    /// If the reference definition has `requiresIndex` set to `true`, then the reference
    /// must have an index. Otherwise the index is meaningless.
    pub fn index(&self) -> Option<u64> {
        self.element()
            .get_sub_element(ElementName::Index)?
            .character_data()?
            .parse_integer()
    }

    /// set the isAutoValue flag
    ///
    /// If the reference definition has `withAuto` set to `true`, then the reference is allowed to have an auto value.
    pub fn set_is_auto_value(&self, is_auto_value: Option<bool>) -> Result<(), AutosarAbstractionError> {
        if let Some(is_auto_value) = is_auto_value {
            self.element()
                .get_or_create_sub_element(ElementName::IsAutoValue)?
                .set_character_data(is_auto_value)?;
        } else {
            let _ = self.element().remove_sub_element_kind(ElementName::IsAutoValue);
        }

        Ok(())
    }

    /// get the isAutoValue flag
    pub fn is_auto_value(&self) -> Option<bool> {
        self.element()
            .get_sub_element(ElementName::IsAutoValue)?
            .character_data()?
            .parse_bool()
    }
}

//#########################################################

/// The `EcucAnyReferenceValue` is an enum that can hold either of the reference value types
/// It is used as a return type for the iterator of reference values
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EcucAnyReferenceValue {
    /// An instance reference value
    Instance(EcucInstanceReferenceValue),
    /// A normal reference value
    Reference(EcucReferenceValue),
}

impl TryFrom<Element> for EcucAnyReferenceValue {
    type Error = AutosarAbstractionError;

    fn try_from(element: Element) -> Result<Self, Self::Error> {
        match element.element_name() {
            ElementName::EcucInstanceReferenceValue => {
                Ok(EcucAnyReferenceValue::Instance(EcucInstanceReferenceValue(element)))
            }
            ElementName::EcucReferenceValue => Ok(EcucAnyReferenceValue::Reference(EcucReferenceValue(element))),
            _ => Err(AutosarAbstractionError::ConversionError {
                element,
                dest: "EcucAnyReferenceValue".to_string(),
            }),
        }
    }
}

impl AbstractionElement for EcucAnyReferenceValue {
    fn element(&self) -> &Element {
        match self {
            EcucAnyReferenceValue::Instance(instance) => instance.element(),
            EcucAnyReferenceValue::Reference(reference) => reference.element(),
        }
    }
}

//#########################################################

#[cfg(test)]
mod test {
    use crate::{
        ecu_configuration::EcucAnyReferenceValue, software_component::AbstractSwComponentType, AbstractionElement,
        ArPackage,
    };
    use autosar_data::{AutosarModel, AutosarVersion};

    #[test]
    fn test_ecu_configuration_values() {
        let definition_model = AutosarModel::new();
        let _file = definition_model
            .create_file("definition.arxml", AutosarVersion::LATEST)
            .unwrap();
        let def_package = ArPackage::get_or_create(&definition_model, "/def_package").unwrap();

        let values_model = AutosarModel::new();
        let _file = values_model
            .create_file("values.arxml", AutosarVersion::LATEST)
            .unwrap();
        let val_package = ArPackage::get_or_create(&values_model, "/val_package").unwrap();

        // create a definition for the ECU configuration
        let module_def = def_package.create_ecuc_module_def("ModuleDef").unwrap();
        let container_def = module_def.create_param_conf_container_def("ContainerDef").unwrap();
        let instance_ref_def = container_def
            .create_instance_reference_def("InstanceRefDef", "origin")
            .unwrap();
        let foreign_reference_def = container_def
            .create_foreign_reference_def("ForeignRefDef", "origin")
            .unwrap();
        let choice_reference_def = container_def
            .create_choice_reference_def("ChoiceRefDef", "origin")
            .unwrap();
        let reference_def = container_def.create_reference_def("RefDef", "origin").unwrap();
        let uri_reference_def = container_def.create_uri_reference_def("UriRefDef", "origin").unwrap();

        // create an ecu configuration based on the definition model
        let ecuc_config_values = val_package
            .create_ecuc_module_configuration_values("Module", &module_def)
            .unwrap();
        let container_values = ecuc_config_values
            .create_container_value("Container", &container_def)
            .unwrap();

        // create an instance reference value
        // an InstanceReferenceValue can only refer to certain elements; in oder to be able to set up a valid
        // instance reference we need to create the "infrastructure" in the model
        let comp = val_package.create_composition_sw_component_type("comp").unwrap();
        let port_interface = val_package.create_sender_receiver_interface("sr_interface").unwrap();
        let r_port_prototype = comp.create_r_port("sr_r_port", &port_interface).unwrap();
        // create the instance reference value
        let instance_ref = container_values
            .create_instance_reference(
                &instance_ref_def,
                &[r_port_prototype.element()],
                r_port_prototype.element(),
            )
            .unwrap();
        // the definition is in a different model, so it can't be resolved
        assert!(instance_ref.definition().is_none());
        assert_eq!(instance_ref.definition_ref(), instance_ref_def.element().path().ok());
        let (target_context, target) = instance_ref.target().unwrap();
        assert_eq!(&target, r_port_prototype.element());
        assert_eq!(target_context, &[r_port_prototype.element().clone()]);
        assert_eq!(instance_ref.index(), None);
        instance_ref.set_index(Some(42)).unwrap();
        assert_eq!(instance_ref.index(), Some(42));
        assert_eq!(instance_ref.is_auto_value(), None);
        instance_ref.set_is_auto_value(Some(true)).unwrap();
        assert_eq!(instance_ref.is_auto_value(), Some(true));

        let foreign_ref = container_values
            .create_reference_value(&foreign_reference_def, val_package.element())
            .unwrap();
        assert!(foreign_ref.definition().is_none());
        assert_eq!(
            foreign_ref.definition_ref(),
            foreign_reference_def.element().path().ok()
        );
        assert_eq!(&foreign_ref.target().unwrap(), val_package.element());
        assert_eq!(foreign_ref.index(), None);
        foreign_ref.set_index(Some(42)).unwrap();
        assert_eq!(foreign_ref.index(), Some(42));
        assert_eq!(foreign_ref.is_auto_value(), None);
        foreign_ref.set_is_auto_value(Some(true)).unwrap();
        assert_eq!(foreign_ref.is_auto_value(), Some(true));

        let choice_ref = container_values
            .create_reference_value(&choice_reference_def, val_package.element())
            .unwrap();
        assert!(choice_ref.definition().is_none());
        assert_eq!(choice_ref.definition_ref(), choice_reference_def.element().path().ok());
        assert_eq!(&choice_ref.target().unwrap(), val_package.element());
        assert_eq!(choice_ref.index(), None);
        choice_ref.set_index(Some(42)).unwrap();
        assert_eq!(choice_ref.index(), Some(42));
        assert_eq!(choice_ref.is_auto_value(), None);
        choice_ref.set_is_auto_value(Some(true)).unwrap();
        assert_eq!(choice_ref.is_auto_value(), Some(true));

        let ref_ref = container_values
            .create_reference_value(&reference_def, val_package.element())
            .unwrap();
        assert!(ref_ref.definition().is_none());
        assert_eq!(ref_ref.definition_ref(), reference_def.element().path().ok());
        assert_eq!(&ref_ref.target().unwrap(), val_package.element());
        assert_eq!(ref_ref.index(), None);
        ref_ref.set_index(Some(42)).unwrap();
        assert_eq!(ref_ref.index(), Some(42));
        assert_eq!(ref_ref.is_auto_value(), None);
        ref_ref.set_is_auto_value(Some(true)).unwrap();
        assert_eq!(ref_ref.is_auto_value(), Some(true));

        let uri_ref = container_values
            .create_reference_value(&uri_reference_def, val_package.element())
            .unwrap();
        assert!(uri_ref.definition().is_none());
        assert_eq!(uri_ref.definition_ref(), uri_reference_def.element().path().ok());
        assert_eq!(&uri_ref.target().unwrap(), val_package.element());
        assert_eq!(uri_ref.index(), None);
        uri_ref.set_index(Some(42)).unwrap();
        assert_eq!(uri_ref.index(), Some(42));
        assert_eq!(uri_ref.is_auto_value(), None);
        uri_ref.set_is_auto_value(Some(true)).unwrap();
        assert_eq!(uri_ref.is_auto_value(), Some(true));

        assert_eq!(container_values.reference_values().count(), 5);

        let any_ref = EcucAnyReferenceValue::try_from(instance_ref.element().clone()).unwrap();
        assert!(matches!(any_ref, EcucAnyReferenceValue::Instance(_)));
        assert_eq!(any_ref.element(), instance_ref.element());
        let any_ref = EcucAnyReferenceValue::try_from(foreign_ref.element().clone()).unwrap();
        assert!(matches!(any_ref, EcucAnyReferenceValue::Reference(_)));
        assert_eq!(any_ref.element(), foreign_ref.element());
        let err = EcucAnyReferenceValue::try_from(val_package.element().clone());
        assert!(err.is_err());
    }
}