autosar_data_abstraction/ecu_configuration/definition/
container.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
use crate::{
    abstraction_element,
    ecu_configuration::{
        EcucAddInfoParamDef, EcucAnyReferenceDef, EcucBooleanParamDef, EcucChoiceReferenceDef, EcucDefinitionElement,
        EcucEnumerationParamDef, EcucFloatParamDef, EcucForeignReferenceDef, EcucFunctionNameDef,
        EcucInstanceReferenceDef, EcucIntegerParamDef, EcucLinkerSymbolDef, EcucMultilineStringParamDef,
        EcucParameterDef, EcucReferenceDef, EcucStringParamDef, EcucUriReferenceDef,
    },
    AbstractionElement, AutosarAbstractionError,
};
use autosar_data::{Element, ElementName};

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

/// Marker trait for container definitions
pub trait AbstractEcucContainerDef: EcucDefinitionElement {}

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

/// The `EcucChoiceContainerDef` is used to define configuration containers
/// that provide a choice between several EcucParamConfContainerDef
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucChoiceContainerDef(Element);
abstraction_element!(EcucChoiceContainerDef, EcucChoiceContainerDef);
impl EcucDefinitionElement for EcucChoiceContainerDef {}
impl AbstractEcucContainerDef for EcucChoiceContainerDef {}

impl EcucChoiceContainerDef {
    pub(crate) fn new(name: &str, containers_elem: &Element) -> Result<Self, AutosarAbstractionError> {
        let choice_container_def_elem =
            containers_elem.create_named_sub_element(ElementName::EcucChoiceContainerDef, name)?;

        Ok(Self(choice_container_def_elem))
    }

    /// create a new `EcucParamConfContainerDef` as one of the choices in this choice container
    pub fn create_param_conf_container_def(
        &self,
        name: &str,
    ) -> Result<EcucParamConfContainerDef, AutosarAbstractionError> {
        let containers_elem = self.element().get_or_create_sub_element(ElementName::Choices)?;
        EcucParamConfContainerDef::new(name, &containers_elem)
    }

    /// iterate over the choices in the container
    pub fn choices(&self) -> impl Iterator<Item = EcucParamConfContainerDef> {
        self.element()
            .get_sub_element(ElementName::Choices)
            .into_iter()
            .flat_map(|elem| elem.sub_elements())
            .filter_map(|elem| EcucParamConfContainerDef::try_from(elem).ok())
    }
}

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

/// The `EcucParamConfContainerDef` is used to define configuration containers
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucParamConfContainerDef(Element);
abstraction_element!(EcucParamConfContainerDef, EcucParamConfContainerDef);
impl EcucDefinitionElement for EcucParamConfContainerDef {}
impl AbstractEcucContainerDef for EcucParamConfContainerDef {}

impl EcucParamConfContainerDef {
    pub(crate) fn new(name: &str, containers_elem: &Element) -> Result<Self, AutosarAbstractionError> {
        let param_conf_container_def_elem =
            containers_elem.create_named_sub_element(ElementName::EcucParamConfContainerDef, name)?;

        Ok(Self(param_conf_container_def_elem))
    }

    /// create a new `EcucChoiceContainerDef` as a sub-container
    pub fn create_choice_container_def(&self, name: &str) -> Result<EcucChoiceContainerDef, AutosarAbstractionError> {
        let containers_elem = self.element().get_or_create_sub_element(ElementName::SubContainers)?;
        EcucChoiceContainerDef::new(name, &containers_elem)
    }

    /// create a new `EcucParamConfContainerDef` as a sub-container
    pub fn create_param_conf_container_def(
        &self,
        name: &str,
    ) -> Result<EcucParamConfContainerDef, AutosarAbstractionError> {
        let containers_elem = self.element().get_or_create_sub_element(ElementName::SubContainers)?;
        EcucParamConfContainerDef::new(name, &containers_elem)
    }

    /// iterate over the sub-containers
    pub fn sub_containers(&self) -> impl Iterator<Item = EcucContainerDef> {
        self.element()
            .get_sub_element(ElementName::SubContainers)
            .into_iter()
            .flat_map(|elem| elem.sub_elements())
            .filter_map(|elem| EcucContainerDef::try_from(elem).ok())
    }

    /// create a new EcucAddInfoParamDef in the container
    pub fn create_add_info_param_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucAddInfoParamDef, AutosarAbstractionError> {
        let parameters_elem = self.element().get_or_create_sub_element(ElementName::Parameters)?;
        EcucAddInfoParamDef::new(name, &parameters_elem, origin)
    }

    /// create a new EcucBooleanParamDef in the container
    pub fn create_boolean_param_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucBooleanParamDef, AutosarAbstractionError> {
        let parameters_elem = self.element().get_or_create_sub_element(ElementName::Parameters)?;
        EcucBooleanParamDef::new(name, &parameters_elem, origin)
    }

    /// create a new EcucEnumerationParamDef in the container
    pub fn create_enumeration_param_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucEnumerationParamDef, AutosarAbstractionError> {
        let parameters_elem = self.element().get_or_create_sub_element(ElementName::Parameters)?;
        EcucEnumerationParamDef::new(name, &parameters_elem, origin)
    }

    /// create a new EcucFloatParamDef in the container
    pub fn create_float_param_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucFloatParamDef, AutosarAbstractionError> {
        let parameters_elem = self.element().get_or_create_sub_element(ElementName::Parameters)?;
        EcucFloatParamDef::new(name, &parameters_elem, origin)
    }

    /// create a new EcucIntegerParamDef in the container
    pub fn create_integer_param_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucIntegerParamDef, AutosarAbstractionError> {
        let parameters_elem = self.element().get_or_create_sub_element(ElementName::Parameters)?;
        EcucIntegerParamDef::new(name, &parameters_elem, origin)
    }

    /// create a new EcucFunctionNameDef in the container
    pub fn create_function_name_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucFunctionNameDef, AutosarAbstractionError> {
        let parameters_elem = self.element().get_or_create_sub_element(ElementName::Parameters)?;
        EcucFunctionNameDef::new(name, &parameters_elem, origin)
    }

    /// create a new EcucLinkerSymbolDef in the container
    pub fn create_linker_symbol_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucLinkerSymbolDef, AutosarAbstractionError> {
        let parameters_elem = self.element().get_or_create_sub_element(ElementName::Parameters)?;
        EcucLinkerSymbolDef::new(name, &parameters_elem, origin)
    }

    /// create a new EcucMultilineStringParamDef in the container
    pub fn create_multiline_string_param_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucMultilineStringParamDef, AutosarAbstractionError> {
        let parameters_elem = self.element().get_or_create_sub_element(ElementName::Parameters)?;
        EcucMultilineStringParamDef::new(name, &parameters_elem, origin)
    }

    /// create a new EcucStringParamDef in the container
    pub fn create_string_param_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucStringParamDef, AutosarAbstractionError> {
        let parameters_elem = self.element().get_or_create_sub_element(ElementName::Parameters)?;
        EcucStringParamDef::new(name, &parameters_elem, origin)
    }

    /// get the parameters in the container
    pub fn parameters(&self) -> impl Iterator<Item = EcucParameterDef> {
        self.element()
            .get_sub_element(ElementName::Parameters)
            .into_iter()
            .flat_map(|elem| elem.sub_elements())
            .filter_map(|elem| EcucParameterDef::try_from(elem).ok())
    }

    /// create a new EcucForeignReferenceDef in the container
    pub fn create_foreign_reference_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucForeignReferenceDef, AutosarAbstractionError> {
        let foreign_references_elem = self.element().get_or_create_sub_element(ElementName::References)?;
        EcucForeignReferenceDef::new(name, &foreign_references_elem, origin)
    }

    /// create a new EcucInstanceReferenceDef in the container
    pub fn create_instance_reference_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucInstanceReferenceDef, AutosarAbstractionError> {
        let instance_references_elem = self.element().get_or_create_sub_element(ElementName::References)?;
        EcucInstanceReferenceDef::new(name, &instance_references_elem, origin)
    }

    /// create a new EcucChoiceReferenceDef in the container
    pub fn create_choice_reference_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucChoiceReferenceDef, AutosarAbstractionError> {
        let choice_references_elem = self.element().get_or_create_sub_element(ElementName::References)?;
        EcucChoiceReferenceDef::new(name, &choice_references_elem, origin)
    }

    /// create a new EcucReferenceDef in the container
    pub fn create_reference_def(&self, name: &str, origin: &str) -> Result<EcucReferenceDef, AutosarAbstractionError> {
        let references_elem = self.element().get_or_create_sub_element(ElementName::References)?;
        EcucReferenceDef::new(name, &references_elem, origin)
    }

    /// create a new EcucUriReferenceDef in the container
    pub fn create_uri_reference_def(
        &self,
        name: &str,
        origin: &str,
    ) -> Result<EcucUriReferenceDef, AutosarAbstractionError> {
        let uri_references_elem = self.element().get_or_create_sub_element(ElementName::References)?;
        EcucUriReferenceDef::new(name, &uri_references_elem, origin)
    }

    /// get the references in the container
    pub fn references(&self) -> impl Iterator<Item = EcucAnyReferenceDef> {
        self.element()
            .get_sub_element(ElementName::References)
            .into_iter()
            .flat_map(|elem| elem.sub_elements())
            .filter_map(|elem| EcucAnyReferenceDef::try_from(elem).ok())
    }
}

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

/// `EcucContainerDef` is an enum of both container definitions, which is used as a return type by iterators
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EcucContainerDef {
    /// the container is a choice container
    Choice(EcucChoiceContainerDef),
    /// the container is a parameter container
    ParamConf(EcucParamConfContainerDef),
}

impl AbstractionElement for EcucContainerDef {
    fn element(&self) -> &Element {
        match self {
            EcucContainerDef::Choice(elem) => elem.element(),
            EcucContainerDef::ParamConf(elem) => elem.element(),
        }
    }
}

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

    fn try_from(element: Element) -> Result<Self, Self::Error> {
        match element.element_name() {
            ElementName::EcucChoiceContainerDef => Ok(EcucContainerDef::Choice(element.try_into()?)),
            ElementName::EcucParamConfContainerDef => Ok(EcucContainerDef::ParamConf(element.try_into()?)),
            _ => Err(AutosarAbstractionError::ConversionError {
                element,
                dest: "EcucContainerDef".to_string(),
            }),
        }
    }
}

impl EcucDefinitionElement for EcucContainerDef {}
impl AbstractEcucContainerDef for EcucContainerDef {}

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

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

    #[test]
    fn container() {
        let model = AutosarModel::new();
        let _file = model.create_file("filename", AutosarVersion::LATEST).unwrap();
        let package = ArPackage::get_or_create(&model, "/pkg1").unwrap();
        let ecuc_module = package.create_ecuc_module_def("ecuc_module").unwrap();
        assert_eq!(ecuc_module.containers().count(), 0);

        let choice_container = ecuc_module.create_choice_container_def("ChoiceContainer").unwrap();
        assert_eq!(choice_container.choices().count(), 0);
        choice_container
            .create_param_conf_container_def("ParamConfContainerChoice1")
            .unwrap();
        choice_container
            .create_param_conf_container_def("ParamConfContainerChoice2")
            .unwrap();
        assert_eq!(choice_container.choices().count(), 2);

        let param_conf_container = ecuc_module
            .create_param_conf_container_def("ParamConfContainer")
            .unwrap();
        assert_eq!(param_conf_container.sub_containers().count(), 0);
        param_conf_container
            .create_choice_container_def("ChoiceContainer")
            .unwrap();
        assert_eq!(param_conf_container.sub_containers().count(), 1);

        assert_eq!(param_conf_container.parameters().count(), 0);
        param_conf_container
            .create_add_info_param_def("AddInfoParam", "origin")
            .unwrap();
        param_conf_container
            .create_boolean_param_def("BooleanParam", "origin")
            .unwrap();
        param_conf_container
            .create_enumeration_param_def("EnumerationParam", "origin")
            .unwrap();
        param_conf_container
            .create_float_param_def("FloatParam", "origin")
            .unwrap();
        param_conf_container
            .create_integer_param_def("IntegerParam", "origin")
            .unwrap();
        param_conf_container
            .create_function_name_def("FunctionName", "origin")
            .unwrap();
        param_conf_container
            .create_linker_symbol_def("LinkerSymbol", "origin")
            .unwrap();
        param_conf_container
            .create_multiline_string_param_def("MultilineStringParam", "origin")
            .unwrap();
        param_conf_container
            .create_string_param_def("StringParam", "origin")
            .unwrap();
        assert_eq!(param_conf_container.parameters().count(), 9);

        assert_eq!(param_conf_container.references().count(), 0);
        param_conf_container
            .create_foreign_reference_def("ForeignReference", "origin")
            .unwrap();
        param_conf_container
            .create_instance_reference_def("InstanceReference", "origin")
            .unwrap();
        param_conf_container
            .create_choice_reference_def("ChoiceReference", "origin")
            .unwrap();
        param_conf_container
            .create_reference_def("Reference", "origin")
            .unwrap();
        param_conf_container
            .create_uri_reference_def("UriReference", "origin")
            .unwrap();
        assert_eq!(param_conf_container.references().count(), 5);

        assert_eq!(ecuc_module.containers().count(), 2);
        let mut containers_iter = ecuc_module.containers();
        assert_eq!(containers_iter.next().unwrap().element(), choice_container.element());
        assert_eq!(
            containers_iter.next().unwrap().element(),
            param_conf_container.element()
        );
    }
}