autosar_data_abstraction/ecu_configuration/definition/
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
use crate::{
    abstraction_element,
    ecu_configuration::{EcucCommonAttributes, EcucDefinitionElement},
    AbstractionElement, AutosarAbstractionError,
};
use autosar_data::{Element, ElementName};

use super::{AbstractEcucContainerDef, EcucContainerDef, EcucDestinationUriDef};

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

/// marker trait for all reference definitions
pub trait AbstractEcucReferenceDef: AbstractionElement + EcucCommonAttributes + EcucDefinitionElement {}

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

/// The `EcucForeignReferenceDef` specifies a reference to an XML description of an entity
/// described in another AUTOSAR template.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucForeignReferenceDef(Element);
abstraction_element!(EcucForeignReferenceDef, EcucForeignReferenceDef);
impl EcucCommonAttributes for EcucForeignReferenceDef {}
impl EcucDefinitionElement for EcucForeignReferenceDef {}
impl AbstractEcucReferenceDef for EcucForeignReferenceDef {}

impl EcucForeignReferenceDef {
    pub(crate) fn new(name: &str, references_elem: &Element, origin: &str) -> Result<Self, AutosarAbstractionError> {
        let ecuc_foreign_reference_def_elem =
            references_elem.create_named_sub_element(ElementName::EcucForeignReferenceDef, name)?;
        let ecuc_foreign_reference_def = Self(ecuc_foreign_reference_def_elem);
        ecuc_foreign_reference_def.set_origin(origin)?;

        Ok(ecuc_foreign_reference_def)
    }

    /// set the destination type of the reference definition
    pub fn set_destination_type(&self, destination_type: Option<&str>) -> Result<(), AutosarAbstractionError> {
        if let Some(destination_type) = destination_type {
            self.element()
                .get_or_create_sub_element(ElementName::DestinationType)?
                .set_character_data(destination_type)?;
        } else {
            let _ = self.element().remove_sub_element_kind(ElementName::DestinationType);
        }

        Ok(())
    }

    /// get the destination type of the reference definition
    pub fn destination_type(&self) -> Option<String> {
        self.element()
            .get_sub_element(ElementName::DestinationType)?
            .character_data()?
            .string_value()
    }
}

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

/// The `EcucInstanceReferenceDef` specifies a reference to an XML description of an entity
/// described in another AUTOSAR template using INSTANCE REFERENCE semantics.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucInstanceReferenceDef(Element);
abstraction_element!(EcucInstanceReferenceDef, EcucInstanceReferenceDef);
impl EcucCommonAttributes for EcucInstanceReferenceDef {}
impl EcucDefinitionElement for EcucInstanceReferenceDef {}
impl AbstractEcucReferenceDef for EcucInstanceReferenceDef {}

impl EcucInstanceReferenceDef {
    pub(crate) fn new(name: &str, references_elem: &Element, origin: &str) -> Result<Self, AutosarAbstractionError> {
        let ecuc_instance_reference_def_elem =
            references_elem.create_named_sub_element(ElementName::EcucInstanceReferenceDef, name)?;
        let ecuc_instance_reference_def = Self(ecuc_instance_reference_def_elem);
        ecuc_instance_reference_def.set_origin(origin)?;

        Ok(ecuc_instance_reference_def)
    }

    /// set the destination type of the reference definition
    pub fn set_destination_type(&self, destination_type: Option<&str>) -> Result<(), AutosarAbstractionError> {
        if let Some(destination_type) = destination_type {
            self.element()
                .get_or_create_sub_element(ElementName::DestinationType)?
                .set_character_data(destination_type)?;
        } else {
            let _ = self.element().remove_sub_element_kind(ElementName::DestinationType);
        }

        Ok(())
    }

    /// get the destination type of the reference definition
    pub fn destination_type(&self) -> Option<String> {
        self.element()
            .get_sub_element(ElementName::DestinationType)?
            .character_data()?
            .string_value()
    }

    /// set the destination context of the reference definition
    ///
    /// The destination context is a string of autosar element names separated by spaces.
    /// Additionally, the '*' character can be used to indicate multiple occurrences of the previous element.
    /// E.g. "SW-COMPONENT-PROTOTYPE* R-PORT-PROTOTYPE"
    pub fn set_destination_context(&self, destination_context: Option<&str>) -> Result<(), AutosarAbstractionError> {
        if let Some(destination_context) = destination_context {
            self.element()
                .get_or_create_sub_element(ElementName::DestinationContext)?
                .set_character_data(destination_context)?;
        } else {
            let _ = self.element().remove_sub_element_kind(ElementName::DestinationContext);
        }

        Ok(())
    }

    /// get the destination context of the reference definition
    ///
    /// The destination context is a string of autosar element names separated by spaces.
    pub fn destination_context(&self) -> Option<String> {
        self.element()
            .get_sub_element(ElementName::DestinationContext)?
            .character_data()?
            .string_value()
    }
}

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

/// The `EcucChoiceReferenceDef` specifies alternative references where only one of the specified
/// references will be used in the ECU configuration.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucChoiceReferenceDef(Element);
abstraction_element!(EcucChoiceReferenceDef, EcucChoiceReferenceDef);
impl EcucCommonAttributes for EcucChoiceReferenceDef {}
impl EcucDefinitionElement for EcucChoiceReferenceDef {}
impl AbstractEcucReferenceDef for EcucChoiceReferenceDef {}

impl EcucChoiceReferenceDef {
    pub(crate) fn new(name: &str, references_elem: &Element, origin: &str) -> Result<Self, AutosarAbstractionError> {
        let ecu_choice_reference_def_elem =
            references_elem.create_named_sub_element(ElementName::EcucChoiceReferenceDef, name)?;
        let ecu_choice_reference_def = Self(ecu_choice_reference_def_elem);
        ecu_choice_reference_def.set_origin(origin)?;

        Ok(ecu_choice_reference_def)
    }

    /// add a reference to a destination container
    pub fn add_destination<T: AbstractEcucContainerDef>(&self, destination: &T) -> Result<(), AutosarAbstractionError> {
        let dest_refs = self.element().get_or_create_sub_element(ElementName::DestinationRefs)?;
        dest_refs
            .create_sub_element(ElementName::DestinationRef)?
            .set_reference_target(destination.element())?;

        Ok(())
    }

    /// get the references to the destination containers
    pub fn destination_refs(&self) -> impl Iterator<Item = EcucContainerDef> {
        self.element()
            .get_sub_element(ElementName::DestinationRefs)
            .into_iter()
            .flat_map(|dest_refs| dest_refs.sub_elements())
            .filter_map(|dest_ref| {
                dest_ref
                    .get_reference_target()
                    .ok()
                    .and_then(|elem| elem.try_into().ok())
            })
    }
}

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

/// The `EcuReferenceDef` specifies references between parameters in the ECU configuration.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucReferenceDef(Element);
abstraction_element!(EcucReferenceDef, EcucReferenceDef);
impl EcucCommonAttributes for EcucReferenceDef {}
impl EcucDefinitionElement for EcucReferenceDef {}
impl AbstractEcucReferenceDef for EcucReferenceDef {}

impl EcucReferenceDef {
    pub(crate) fn new(name: &str, references_elem: &Element, origin: &str) -> Result<Self, AutosarAbstractionError> {
        let ecu_reference_def_elem = references_elem.create_named_sub_element(ElementName::EcucReferenceDef, name)?;
        let ecu_reference_def = Self(ecu_reference_def_elem);
        ecu_reference_def.set_origin(origin)?;

        Ok(ecu_reference_def)
    }

    /// set the destination container of the reference
    pub fn set_destination<T: AbstractEcucContainerDef>(
        &self,
        destination: Option<&T>,
    ) -> Result<(), AutosarAbstractionError> {
        if let Some(destination) = destination {
            self.element()
                .get_or_create_sub_element(ElementName::DestinationRef)?
                .set_reference_target(destination.element())?;
        } else {
            let _ = self.element().remove_sub_element_kind(ElementName::DestinationRef);
        }

        Ok(())
    }

    /// get the destination container of the reference
    pub fn destination(&self) -> Option<EcucContainerDef> {
        self.element()
            .get_sub_element(ElementName::DestinationRef)
            .and_then(|dest_ref| dest_ref.get_reference_target().ok())
            .and_then(|elem| elem.try_into().ok())
    }
}

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

/// The `EcucUriReferenceDef` defines a reference with a destination that is specified via a destinationUri
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucUriReferenceDef(Element);
abstraction_element!(EcucUriReferenceDef, EcucUriReferenceDef);
impl EcucCommonAttributes for EcucUriReferenceDef {}
impl EcucDefinitionElement for EcucUriReferenceDef {}
impl AbstractEcucReferenceDef for EcucUriReferenceDef {}

impl EcucUriReferenceDef {
    pub(crate) fn new(name: &str, references_elem: &Element, origin: &str) -> Result<Self, AutosarAbstractionError> {
        let ecu_uri_reference_def_elem =
            references_elem.create_named_sub_element(ElementName::EcucUriReferenceDef, name)?;
        let ecu_uri_reference_def = Self(ecu_uri_reference_def_elem);
        ecu_uri_reference_def.set_origin(origin)?;

        Ok(ecu_uri_reference_def)
    }

    /// set the destination uri of the reference definition
    pub fn set_destination_uri(
        &self,
        destination_uri: Option<&EcucDestinationUriDef>,
    ) -> Result<(), AutosarAbstractionError> {
        if let Some(destination_uri) = destination_uri {
            self.element()
                .get_or_create_sub_element(ElementName::DestinationUriRef)?
                .set_reference_target(destination_uri.element())?;
        } else {
            let _ = self.element().remove_sub_element_kind(ElementName::DestinationUriRef);
        }

        Ok(())
    }

    /// get the destination uri of the reference definition
    pub fn destination_uri(&self) -> Option<EcucDestinationUriDef> {
        self.element()
            .get_sub_element(ElementName::DestinationUriRef)?
            .get_reference_target()
            .ok()?
            .try_into()
            .ok()
    }
}

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

/// `EcucAnyReferenceDef` is an an enum of all possible reference definitions
/// It is used as a return type by the iterator over all references in a container
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EcucAnyReferenceDef {
    /// the reference is a foreign reference (external reference)
    Foreign(EcucForeignReferenceDef),
    /// the reference is an instance reference (external reference)
    Instance(EcucInstanceReferenceDef),
    /// the reference is a choice reference (internal reference)
    Choice(EcucChoiceReferenceDef),
    /// the reference is a normal reference (internal reference)
    Normal(EcucReferenceDef),
    /// the reference is a uri reference (internal reference)
    Uri(EcucUriReferenceDef),
}

impl AbstractionElement for EcucAnyReferenceDef {
    fn element(&self) -> &Element {
        match self {
            EcucAnyReferenceDef::Foreign(elem) => elem.element(),
            EcucAnyReferenceDef::Instance(elem) => elem.element(),
            EcucAnyReferenceDef::Choice(elem) => elem.element(),
            EcucAnyReferenceDef::Normal(elem) => elem.element(),
            EcucAnyReferenceDef::Uri(elem) => elem.element(),
        }
    }
}

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

    fn try_from(element: Element) -> Result<Self, Self::Error> {
        match element.element_name() {
            ElementName::EcucForeignReferenceDef => Ok(EcucAnyReferenceDef::Foreign(element.try_into()?)),
            ElementName::EcucInstanceReferenceDef => Ok(EcucAnyReferenceDef::Instance(element.try_into()?)),
            ElementName::EcucChoiceReferenceDef => Ok(EcucAnyReferenceDef::Choice(element.try_into()?)),
            ElementName::EcucReferenceDef => Ok(EcucAnyReferenceDef::Normal(element.try_into()?)),
            ElementName::EcucUriReferenceDef => Ok(EcucAnyReferenceDef::Uri(element.try_into()?)),
            _ => Err(AutosarAbstractionError::ConversionError {
                element,
                dest: "EcucAnyReferenceDef".to_string(),
            }),
        }
    }
}

impl EcucDefinitionElement for EcucAnyReferenceDef {}
impl EcucCommonAttributes for EcucAnyReferenceDef {}
impl AbstractEcucReferenceDef for EcucAnyReferenceDef {}

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

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

    #[test]
    fn test_foreign_reference_def() {
        let model = AutosarModel::new();
        let _file = model.create_file("file.arxml", AutosarVersion::LATEST).unwrap();
        let pkg = ArPackage::get_or_create(&model, "/pkg").unwrap();

        let ecuc_module_def = pkg.create_ecuc_module_def("module").unwrap();
        let container = ecuc_module_def.create_param_conf_container_def("container").unwrap();
        let foreign_ref = container.create_foreign_reference_def("foreign_ref", "origin").unwrap();
        assert_eq!(container.references().count(), 1);

        assert_eq!(foreign_ref.destination_type(), None);
        foreign_ref.set_destination_type(Some("type")).unwrap();
        assert_eq!(foreign_ref.destination_type(), Some("type".to_string()));
        assert_eq!(container.references().next().unwrap().element(), foreign_ref.element());
    }

    #[test]
    fn test_instance_reference_def() {
        let model = AutosarModel::new();
        let _file = model.create_file("file.arxml", AutosarVersion::LATEST).unwrap();
        let pkg = ArPackage::get_or_create(&model, "/pkg").unwrap();

        let ecuc_module_def = pkg.create_ecuc_module_def("module").unwrap();
        let container = ecuc_module_def.create_param_conf_container_def("container").unwrap();
        let instance_ref = container
            .create_instance_reference_def("instance_ref", "origin")
            .unwrap();
        assert_eq!(container.references().count(), 1);

        assert_eq!(instance_ref.destination_type(), None);
        instance_ref.set_destination_type(Some("type")).unwrap();
        assert_eq!(instance_ref.destination_type(), Some("type".to_string()));

        assert_eq!(instance_ref.destination_context(), None);
        instance_ref.set_destination_context(Some("context")).unwrap();
        assert_eq!(instance_ref.destination_context(), Some("context".to_string()));
        assert_eq!(container.references().next().unwrap().element(), instance_ref.element());
    }

    #[test]
    fn test_choice_reference_def() {
        let model = AutosarModel::new();
        let _file = model.create_file("file.arxml", AutosarVersion::LATEST).unwrap();
        let pkg = ArPackage::get_or_create(&model, "/pkg").unwrap();

        let ecuc_module_def = pkg.create_ecuc_module_def("module").unwrap();
        let container = ecuc_module_def.create_param_conf_container_def("container").unwrap();
        let choice_ref = container.create_choice_reference_def("choice_ref", "origin").unwrap();
        assert_eq!(container.references().count(), 1);

        assert_eq!(choice_ref.destination_refs().count(), 0);
        let dest = container.create_param_conf_container_def("dest").unwrap();
        choice_ref.add_destination(&dest).unwrap();
        assert_eq!(choice_ref.destination_refs().count(), 1);
        assert_eq!(container.references().next().unwrap().element(), choice_ref.element());
    }

    #[test]
    fn test_reference_def() {
        let model = AutosarModel::new();
        let _file = model.create_file("file.arxml", AutosarVersion::LATEST).unwrap();
        let pkg = ArPackage::get_or_create(&model, "/pkg").unwrap();

        let ecuc_module_def = pkg.create_ecuc_module_def("module").unwrap();
        let container = ecuc_module_def.create_param_conf_container_def("container").unwrap();
        let reference = container.create_reference_def("reference", "origin").unwrap();
        assert_eq!(container.references().count(), 1);

        assert_eq!(reference.destination(), None);
        let dest = container.create_param_conf_container_def("dest").unwrap();
        reference.set_destination(Some(&dest)).unwrap();
        assert_eq!(reference.destination().unwrap(), EcucContainerDef::ParamConf(dest));
        assert_eq!(container.references().next().unwrap().element(), reference.element());
    }

    #[test]
    fn test_uri_reference_def() {
        let model = AutosarModel::new();
        let _file = model.create_file("file.arxml", AutosarVersion::LATEST).unwrap();
        let pkg = ArPackage::get_or_create(&model, "/pkg").unwrap();

        let ecuc_module_def = pkg.create_ecuc_module_def("module").unwrap();
        let container = ecuc_module_def.create_param_conf_container_def("container").unwrap();
        let uri_ref = container.create_uri_reference_def("uri_ref", "origin").unwrap();
        assert_eq!(container.references().count(), 1);

        let uri_def_set = pkg.create_ecuc_destination_uri_def_set("uri_def").unwrap();
        let uri_def = uri_def_set
            .create_destination_uri_def("uri", EcucDestinationUriNestingContract::VertexOfTargetContainer)
            .unwrap();

        assert_eq!(uri_ref.destination_uri(), None);
        uri_ref.set_destination_uri(Some(&uri_def)).unwrap();
        assert_eq!(uri_ref.destination_uri(), Some(uri_def));
        assert_eq!(container.references().next().unwrap().element(), uri_ref.element());
    }
}