Skip to main content

ebi_bpmn/
stochastic_business_process_model_and_notation.rs

1use crate::{
2    BPMNMarking, BusinessProcessModelAndNotation,
3    element::BPMNElement,
4    parser::{parser::NAMESPACE_SBPMN, parser_state::GlobalIndex},
5    semantics::TransitionIndex,
6    sequence_flow::BPMNSequenceFlow,
7    traits::processable::Processable,
8};
9use anyhow::{Error, Result, anyhow};
10#[cfg(any(test, feature = "testactivities"))]
11use ebi_activity_key::TestActivityKey;
12use ebi_activity_key::{ActivityKey, HasActivityKey, TranslateActivityKey};
13use std::{
14    fmt::{Display, Formatter},
15    io::BufRead,
16    str::FromStr,
17};
18/** A struct with a stochastic Business Process Model and Notation (SBPMN) model.
19 **/
20#[derive(Clone, Debug)]
21pub struct StochasticBusinessProcessModelAndNotation {
22    pub bpmn: BusinessProcessModelAndNotation,
23}
24
25impl StochasticBusinessProcessModelAndNotation {
26    pub fn import_from_reader(reader: &mut dyn BufRead) -> Result<Self>
27    where
28        Self: Sized,
29    {
30        let bpmn = BusinessProcessModelAndNotation::import_from_reader(reader, false)?;
31        if !bpmn.stochastic_namespace {
32            return Err(anyhow!(
33                "The SBPMN namespace of `{}` must be declared on the definitions tag.",
34                String::from_utf8_lossy(NAMESPACE_SBPMN)
35            ));
36        }
37        let sbpmn = Self { bpmn };
38        sbpmn.is_structurally_correct()?;
39        Ok(sbpmn)
40    }
41
42    pub fn number_of_elements(&self) -> usize {
43        self.bpmn.number_of_elements()
44    }
45
46    pub fn number_of_message_flows(&self) -> usize {
47        self.bpmn.number_of_message_flows()
48    }
49
50    /// returns all elements in the model (recursively)
51    pub fn elements(&self) -> Vec<&BPMNElement> {
52        self.bpmn.elements()
53    }
54
55    pub fn parent_of(&self, global_index: GlobalIndex) -> Option<&dyn Processable> {
56        self.bpmn.parent_of(global_index)
57    }
58
59    /// Returns all sequence flows (recursive)
60    pub fn sequence_flows(&self) -> Vec<&BPMNSequenceFlow> {
61        self.bpmn.sequence_flows()
62    }
63
64    /// find an element with the given index
65    pub fn global_index_2_element(&self, index: GlobalIndex) -> Option<&BPMNElement> {
66        self.bpmn.global_index_2_element(index)
67    }
68
69    /// find an element with the given index
70    pub fn global_index_2_element_mut(&mut self, index: GlobalIndex) -> Option<&mut BPMNElement> {
71        self.bpmn.global_index_2_element_mut(index)
72    }
73
74    /// return the element that is the source of the given message flow
75    pub fn message_flow_index_2_source(&self, message_flow_index: usize) -> Result<&BPMNElement> {
76        self.bpmn.message_flow_index_2_source(message_flow_index)
77    }
78
79    /// return the element that is the target of the given message flow
80    pub fn message_flow_index_2_target(&self, message_flow_index: usize) -> Result<&BPMNElement> {
81        self.bpmn.message_flow_index_2_target(message_flow_index)
82    }
83
84    /// return the sequence flow with the given global index
85    pub fn global_index_2_sequence_flow_and_parent(
86        &self,
87        sequence_flow_global_index: GlobalIndex,
88    ) -> Option<(&BPMNSequenceFlow, &dyn Processable)> {
89        self.bpmn
90            .global_index_2_sequence_flow_and_parent(sequence_flow_global_index)
91    }
92
93    pub fn transition_debug(
94        &self,
95        transition_index: TransitionIndex,
96        marking: &BPMNMarking,
97    ) -> Option<String> {
98        self.bpmn.transition_debug(transition_index, marking)
99    }
100
101    /// return the sequence flow with this index, if it exists (recurses)
102    pub fn global_index_2_sequence_flow_mut(
103        &mut self,
104        sequence_flow_global_index: GlobalIndex,
105    ) -> Option<&mut BPMNSequenceFlow> {
106        self.bpmn
107            .global_index_2_sequence_flow_mut(sequence_flow_global_index)
108    }
109}
110
111impl HasActivityKey for StochasticBusinessProcessModelAndNotation {
112    fn activity_key(&self) -> &ActivityKey {
113        &self.bpmn.activity_key
114    }
115
116    fn activity_key_mut(&mut self) -> &mut ActivityKey {
117        &mut self.bpmn.activity_key
118    }
119}
120
121impl FromStr for StochasticBusinessProcessModelAndNotation {
122    type Err = Error;
123
124    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
125        let mut reader = std::io::Cursor::new(s);
126        Self::import_from_reader(&mut reader)
127    }
128}
129
130impl Display for StochasticBusinessProcessModelAndNotation {
131    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
132        writeln!(f, "SBPMN model with {} elements", self.number_of_elements())
133    }
134}
135
136impl TranslateActivityKey for StochasticBusinessProcessModelAndNotation {
137    fn translate_using_activity_key(&mut self, to_activity_key: &mut ActivityKey) {
138        self.bpmn.translate_using_activity_key(to_activity_key);
139    }
140}
141
142#[cfg(any(test, feature = "testactivities"))]
143impl TestActivityKey for StochasticBusinessProcessModelAndNotation {
144    fn test_activity_key(&self) {
145        self.bpmn.test_activity_key()
146    }
147}