autosar_data_abstraction/software_component/
port.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
use crate::{abstraction_element, software_component, AbstractionElement, AutosarAbstractionError};
use autosar_data::{Element, ElementName};
use software_component::{AbstractPortInterface, PortInterface, SwComponentType};

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

/// `RPortPrototype` represents a required port prototype
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RPortPrototype(Element);
abstraction_element!(RPortPrototype, RPortPrototype);

impl RPortPrototype {
    /// Create a new `RPortPrototype`
    pub(crate) fn new<T: AbstractPortInterface>(
        name: &str,
        parent_element: &Element,
        interface: &T,
    ) -> Result<Self, AutosarAbstractionError> {
        let r_port_prototype = parent_element.create_named_sub_element(ElementName::RPortPrototype, name)?;
        r_port_prototype
            .create_sub_element(ElementName::RequiredInterfaceTref)?
            .set_reference_target(interface.element())?;

        Ok(Self(r_port_prototype))
    }

    /// Get the port interface of the port prototype
    pub fn port_interface(&self) -> Result<PortInterface, AutosarAbstractionError> {
        let interface_elem = self
            .element()
            .get_sub_element(ElementName::RequiredInterfaceTref)
            .and_then(|r| r.get_reference_target().ok())
            .ok_or(AutosarAbstractionError::InvalidParameter(
                "RPortPrototype is incomplete: RequiredInterfaceTref is missing".to_string(),
            ))?;
        PortInterface::try_from(interface_elem)
    }

    /// Get the component type containing the port prototype
    pub fn component_type(&self) -> Result<SwComponentType, AutosarAbstractionError> {
        let component_type_elem = self.element().named_parent()?.unwrap();
        SwComponentType::try_from(component_type_elem)
    }
}

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

/// `PPortPrototype` represents a provided port prototype
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PPortPrototype(Element);
abstraction_element!(PPortPrototype, PPortPrototype);

impl PPortPrototype {
    /// Create a new `PPortPrototype`
    pub(crate) fn new<T: AbstractPortInterface>(
        name: &str,
        parent_element: &Element,
        interface: &T,
    ) -> Result<Self, AutosarAbstractionError> {
        let p_port_prototype = parent_element.create_named_sub_element(ElementName::PPortPrototype, name)?;
        p_port_prototype
            .create_sub_element(ElementName::ProvidedInterfaceTref)?
            .set_reference_target(interface.element())?;

        Ok(Self(p_port_prototype))
    }

    /// Get the port interface of the port prototype
    pub fn port_interface(&self) -> Result<PortInterface, AutosarAbstractionError> {
        let interface_elem = self
            .element()
            .get_sub_element(ElementName::ProvidedInterfaceTref)
            .and_then(|r| r.get_reference_target().ok())
            .ok_or(AutosarAbstractionError::InvalidParameter(
                "PPortPrototype is incomplete: ProvidedInterfaceTref is missing".to_string(),
            ))?;
        PortInterface::try_from(interface_elem)
    }

    /// Get the component type containing the port prototype
    pub fn component_type(&self) -> Result<SwComponentType, AutosarAbstractionError> {
        let component_type_elem = self.element().named_parent()?.unwrap();
        SwComponentType::try_from(component_type_elem)
    }
}

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

/// `PRPortPrototype` represents a provided and required port prototype
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PRPortPrototype(Element);
abstraction_element!(PRPortPrototype, PrPortPrototype);

impl PRPortPrototype {
    /// Create a new `PRPortPrototype`
    pub(crate) fn new<T: AbstractPortInterface>(
        name: &str,
        parent_element: &Element,
        interface: &T,
    ) -> Result<Self, AutosarAbstractionError> {
        if interface.element().element_name() == ElementName::ParameterInterface {
            return Err(AutosarAbstractionError::InvalidParameter(
                "ParameterInterface is not allowed for PRPortPrototype".to_string(),
            ));
        }

        let pr_port_prototype = parent_element.create_named_sub_element(ElementName::PrPortPrototype, name)?;
        pr_port_prototype
            .create_sub_element(ElementName::ProvidedRequiredInterfaceTref)?
            .set_reference_target(interface.element())?;

        Ok(Self(pr_port_prototype))
    }

    /// Get the port interface of the port prototype
    pub fn port_interface(&self) -> Result<PortInterface, AutosarAbstractionError> {
        let interface_elem = self
            .element()
            .get_sub_element(ElementName::ProvidedRequiredInterfaceTref)
            .and_then(|r| r.get_reference_target().ok())
            .ok_or(AutosarAbstractionError::InvalidParameter(
                "PRPortPrototype is incomplete: ProvidedRequiredInterfaceTref is missing".to_string(),
            ))?;
        PortInterface::try_from(interface_elem)
    }

    /// Get the component type containing the port prototype
    pub fn component_type(&self) -> Result<SwComponentType, AutosarAbstractionError> {
        let component_type_elem = self.element().named_parent()?.unwrap();
        SwComponentType::try_from(component_type_elem)
    }
}

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

/// The `PortPrototype` enum represents all possible kinds of port prototypes
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PortPrototype {
    /// A required port prototype
    R(RPortPrototype),
    /// A provided port prototype
    P(PPortPrototype),
    /// A provided and required port prototype
    PR(PRPortPrototype),
}

impl AbstractionElement for PortPrototype {
    fn element(&self) -> &Element {
        match self {
            PortPrototype::R(port) => port.element(),
            PortPrototype::P(port) => port.element(),
            PortPrototype::PR(port) => port.element(),
        }
    }
}

impl From<RPortPrototype> for PortPrototype {
    fn from(port: RPortPrototype) -> Self {
        PortPrototype::R(port)
    }
}

impl From<PPortPrototype> for PortPrototype {
    fn from(port: PPortPrototype) -> Self {
        PortPrototype::P(port)
    }
}

impl From<PRPortPrototype> for PortPrototype {
    fn from(port: PRPortPrototype) -> Self {
        PortPrototype::PR(port)
    }
}

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

    fn try_from(element: Element) -> Result<Self, Self::Error> {
        match element.element_name() {
            ElementName::RPortPrototype => Ok(PortPrototype::R(RPortPrototype(element))),
            ElementName::PPortPrototype => Ok(PortPrototype::P(PPortPrototype(element))),
            ElementName::PrPortPrototype => Ok(PortPrototype::PR(PRPortPrototype(element))),
            _ => Err(AutosarAbstractionError::ConversionError {
                element: element.clone(),
                dest: "PortPrototype".to_string(),
            }),
        }
    }
}

impl PortPrototype {
    /// Get the port interface of the port prototype
    pub fn port_interface(&self) -> Result<PortInterface, AutosarAbstractionError> {
        match self {
            PortPrototype::R(port) => port.port_interface(),
            PortPrototype::P(port) => port.port_interface(),
            PortPrototype::PR(port) => port.port_interface(),
        }
    }

    /// Get the component type containing the port prototype
    pub fn component_type(&self) -> Result<SwComponentType, AutosarAbstractionError> {
        let component_type_elem = self.element().named_parent()?.unwrap();
        SwComponentType::try_from(component_type_elem)
    }
}

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

/// `PortGroup` represents a group of ports
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PortGroup(Element);
abstraction_element!(PortGroup, PortGroup);

impl PortGroup {
    /// Create a new `PortGroup`
    pub(crate) fn new(name: &str, parent_element: &Element) -> Result<Self, AutosarAbstractionError> {
        let port_group = parent_element.create_named_sub_element(ElementName::PortGroup, name)?;

        Ok(Self(port_group))
    }
}

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

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

    #[test]
    fn ports() {
        let model = AutosarModel::new();
        let _file = model.create_file("filename", AutosarVersion::LATEST).unwrap();
        let package = ArPackage::get_or_create(&model, "/package").unwrap();

        let comp = package.create_composition_sw_component_type("comp").unwrap();

        let port_interface = package.create_sender_receiver_interface("sr_interface").unwrap();
        let r_port = comp.create_r_port("sr_r_port", &port_interface).unwrap();
        let p_port = comp.create_p_port("sr_p_port", &port_interface).unwrap();
        let pr_port = comp.create_pr_port("sr_pr_port", &port_interface).unwrap();

        assert_eq!(comp.ports().count(), 3);
        let ports: Vec<PortPrototype> = comp.ports().collect();
        assert_eq!(ports[0], r_port.clone().into());
        assert_eq!(ports[1], p_port.clone().into());
        assert_eq!(ports[2], pr_port.clone().into());
        assert_eq!(r_port.component_type().unwrap(), comp.clone().into());
        assert_eq!(p_port.component_type().unwrap(), comp.clone().into());
        assert_eq!(pr_port.component_type().unwrap(), comp.clone().into());
        assert_eq!(ports[0].component_type().unwrap(), comp.clone().into());

        let port_interface = package.create_client_server_interface("cs_interface").unwrap();
        let r_port = comp.create_r_port("cs_r_port", &port_interface).unwrap();
        let p_port = comp.create_p_port("cs_p_port", &port_interface).unwrap();
        let pr_port = comp.create_pr_port("cs_pr_port", &port_interface).unwrap();

        assert_eq!(comp.ports().count(), 6);
        let ports: Vec<PortPrototype> = comp.ports().collect();
        assert_eq!(ports[3], r_port.into());
        assert_eq!(ports[4], p_port.into());
        assert_eq!(ports[5], pr_port.into());

        let port_interface = package.create_mode_switch_interface("ms_interface").unwrap();
        let r_port = comp.create_r_port("ms_r_port", &port_interface).unwrap();
        let p_port = comp.create_p_port("ms_p_port", &port_interface).unwrap();
        let pr_port = comp.create_pr_port("ms_pr_port", &port_interface).unwrap();

        assert_eq!(comp.ports().count(), 9);
        let ports: Vec<PortPrototype> = comp.ports().collect();
        assert_eq!(ports[6], r_port.into());
        assert_eq!(ports[7], p_port.into());
        assert_eq!(ports[8], pr_port.into());

        let port_interface = package.create_nv_data_interface("nv_interface").unwrap();
        let r_port = comp.create_r_port("nv_r_port", &port_interface).unwrap();
        let p_port = comp.create_p_port("nv_p_port", &port_interface).unwrap();
        let pr_port = comp.create_pr_port("nv_pr_port", &port_interface).unwrap();

        assert_eq!(comp.ports().count(), 12);
        let ports: Vec<PortPrototype> = comp.ports().collect();
        assert_eq!(ports[9], r_port.into());
        assert_eq!(ports[10], p_port.into());
        assert_eq!(ports[11], pr_port.into());

        let port_interface = package.create_parameter_interface("param_interface").unwrap();
        let r_port = comp.create_r_port("param_r_port", &port_interface).unwrap();
        let p_port = comp.create_p_port("param_p_port", &port_interface).unwrap();
        let pr_port_result = comp.create_pr_port("param_pr_port", &port_interface);
        assert!(pr_port_result.is_err());

        assert_eq!(comp.ports().count(), 14);
        let ports: Vec<PortPrototype> = comp.ports().collect();
        assert_eq!(ports[12], r_port.into());
        assert_eq!(ports[13], p_port.into());

        let port_interface = package.create_trigger_interface("trigger_interface").unwrap();
        let r_port = comp.create_r_port("trigger_r_port", &port_interface).unwrap();
        let p_port = comp.create_p_port("trigger_p_port", &port_interface).unwrap();
        let pr_port = comp.create_pr_port("trigger_pr_port", &port_interface).unwrap();

        assert_eq!(comp.ports().count(), 17);
        let ports: Vec<PortPrototype> = comp.ports().collect();
        assert_eq!(ports[14], r_port.into());
        assert_eq!(ports[15], p_port.into());
        assert_eq!(ports[16], pr_port.into());
    }
}