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
use crate::{
BusinessProcessModelAndNotation,
element::{BPMNElement, BPMNElementTrait},
elements::{
event_based_gateway::BPMNEventBasedGateway, exclusive_gateway::BPMNExclusiveGateway,
inclusive_gateway::BPMNInclusiveGateway,
},
stochastic_business_process_model_and_notation::StochasticBusinessProcessModelAndNotation,
traits::{objectable::BPMNObject, startable::Startable},
};
use anyhow::{Context, Result, anyhow};
use ebi_arithmetic::Signed;
impl BusinessProcessModelAndNotation {
/// Verify whether the model is structurally correct using several, though not exhaustive, checks.
/// If the BPMN model is imported by [import_from_reader] or created using a [BPMNCreator], there is no need to call this method.
///
/// [import_from_reader]: BusinessProcessModelAndNotation::import_from_reader
/// [BPMNCreator]: crate::BPMNCreator
pub fn is_structurally_correct(&self) -> Result<()> {
//check elements
for element in &self.elements {
element
.verify_structural_correctness(self, self)
.with_context(|| {
anyhow!(
"Found a structural correctness issue with element `{}`.",
element.id()
)
})?;
}
//check messages
for message_flow in &self.message_flows {
//each message must connect different pools
if message_flow.source_pool_index == message_flow.target_pool_index {
return Err(anyhow!(
"message flow with id `{}` is intra-pool",
message_flow.id
));
}
}
Ok(())
}
}
impl StochasticBusinessProcessModelAndNotation {
pub fn is_structurally_correct(&self) -> Result<()> {
//check the bpmn itself
self.bpmn
.is_structurally_correct()
.with_context(|| anyhow!("Checking structural correctness of control flow."))?;
//we cannot handle models with multiple start events
{
//gather the start elements
let mut start_elements = vec![];
for element in &self.bpmn.elements {
if let BPMNElement::Process(process) = element {
start_elements
.extend(process.unconstrained_start_events_without_recursing(&self.bpmn)?);
}
}
if start_elements.len() > 1 {
println!("start elements {:?}", start_elements);
return Err(anyhow!("An SBPMN model can have at most one start event."));
}
}
//choice-based sequence flows must have weights
{
for element in self.bpmn.elements() {
if element.is_event_based_gateway()
|| element.is_exclusive_gateway()
|| element.is_inclusive_gateway()
{
if element.outgoing_sequence_flows().len() > 1 {
for sequence_flow_index in element.outgoing_sequence_flows() {
let parent = self
.bpmn
.parent_of(element.global_index())
.ok_or_else(|| anyhow!("parent not found"))?;
let sequence_flow = parent
.sequence_flows_non_recursive()
.get(*sequence_flow_index)
.ok_or_else(|| anyhow!("sequence flow not found"))?;
if sequence_flow.weight.is_none() {
return Err(anyhow!(
"Sequence flow `{}` originates from a choice-making gateway and therefore must have a weight.",
sequence_flow.id
));
}
}
}
}
}
}
//we cannot handle models with steered event-based gateways
{
for element in self.bpmn.elements() {
if element.is_event_based_gateway() {
let parent = self
.bpmn
.parent_of(element.global_index())
.ok_or_else(|| anyhow!("parent not found"))?;
for sequence_flow_index in element.outgoing_sequence_flows() {
let target = parent.sequence_flow_index_2_target(*sequence_flow_index)?;
if let Some(message_flow_index) = element.incoming_message_flows().get(0) {
let message_source =
self.bpmn.message_flow_index_2_source(*message_flow_index)?;
if message_source.outgoing_message_flows_always_have_tokens() {
//a message from a collapsed pool is always there
//no problem
} else {
//otherwise, the message must be there
return Err(anyhow!(
"Event-based gateway `{}` has an outgoing sequence flow to element `{}`, which depends on an uncertain message. This is not supported.",
element.id(),
target.id()
));
}
} else {
//there is no constraining message, so this message start event can start a process instance
//no problem
}
}
}
}
}
//check that outgoing sequence flows have weights
for element in self.bpmn.elements() {
match element {
BPMNElement::CollapsedPool(_)
| BPMNElement::CollapsedSubProcess(_)
| BPMNElement::EndEvent(_)
| BPMNElement::ExpandedSubProcess(_)
| BPMNElement::IntermediateCatchEvent(_)
| BPMNElement::IntermediateThrowEvent(_)
| BPMNElement::ManualTask(_)
| BPMNElement::MessageEndEvent(_)
| BPMNElement::MessageIntermediateCatchEvent(_)
| BPMNElement::MessageIntermediateThrowEvent(_)
| BPMNElement::MessageStartEvent(_)
| BPMNElement::ParallelGateway(_)
| BPMNElement::Process(_)
| BPMNElement::ReceiveTask(_)
| BPMNElement::StartEvent(_)
| BPMNElement::Task(_)
| BPMNElement::TimerIntermediateCatchEvent(_)
| BPMNElement::TimerStartEvent(_)
| BPMNElement::UserTask(_) => {}
BPMNElement::EventBasedGateway(BPMNEventBasedGateway {
outgoing_sequence_flows,
..
})
| BPMNElement::ExclusiveGateway(BPMNExclusiveGateway {
outgoing_sequence_flows,
..
})
| BPMNElement::InclusiveGateway(BPMNInclusiveGateway {
outgoing_sequence_flows,
..
}) => {
if outgoing_sequence_flows.len() > 1 {
//only splits matter
let parent = self
.bpmn
.parent_of(element.global_index())
.ok_or_else(|| anyhow!("parent not found"))?;
for sequence_flow_local_index in outgoing_sequence_flows {
let sequence_flow = parent
.sequence_flows_non_recursive()
.get(*sequence_flow_local_index)
.ok_or_else(|| anyhow!("sequence flow not found"))?;
if let Some(weight) = &sequence_flow.weight {
if weight.is_negative() {
return Err(anyhow!(
"Sequence flow `{}` has a negative weight.",
sequence_flow.id
));
}
} else {
return Err(anyhow!(
"Sequence flow `{}` does not have a weight. It should have a weight as it is an outgoing sequence flow of a gateway that makes a choice.",
sequence_flow.id
));
}
}
}
}
}
}
Ok(())
}
}
macro_rules! verify_structural_correctness_initiation_mode {
($process:ident, $bpmn:ident) => {
//verify initiation and termination
if $process
.initiation_mode($bpmn)?
.is_choice_between_start_events()
{
//there must be end events
if $process.end_events_without_recursing().is_empty() {
return Err(anyhow!(
"Process `{}` has start events but no end events.",
$process.id
));
}
//all elements must have incoming and outgoing arcs
for element in &$process.elements {
if element.can_have_incoming_sequence_flows() {
if element.incoming_sequence_flows().is_empty() {
return Err(anyhow!(
"Given that there are start events in process `{}`, element `{}` should have an incoming sequence flow.",
$process.id,
element.id()
));
}
}
if element.can_have_outgoing_sequence_flows() {
if element.outgoing_sequence_flows().is_empty() {
return Err(anyhow!(
"Given that there are start events in process `{}`, element `{}` should have an outgoing sequence flow.",
$process.id,
element.id()
));
}
}
}
}
};
}
pub(crate) use verify_structural_correctness_initiation_mode;