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
use super::{
engine::{ExecuteData, ExecuteResult},
Process,
};
use crate::error::Error;
impl Process {
// If more than 1 start id exist then all id:s run in parallel with rayon. When all threads terminates then the
// next id is returned to continue on. Thread terminates on a Parallel or Inclusive Join and End events.
pub(super) fn maybe_parallelize<'a, T>(
&'a self,
start_ids: Vec<&'a usize>,
data: &ExecuteData<'a, T>,
) -> Result<Option<&usize>, Error>
where
T: Send,
{
let result: ExecuteResult<'_> = {
#[cfg(feature = "parallel")]
{
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
// Fork and collect result
let (oks, mut errors): (Vec<_>, Vec<_>) = start_ids
.par_iter()
.map(|output| self.execute(vec![output], data))
.partition(Result::is_ok);
if let Some(result) = errors.pop() {
result
} else {
Ok(oks
.into_iter()
.filter_map(Result::ok)
.flatten()
.collect::<Vec<_>>())
}
}
#[cfg(not(feature = "parallel"))]
{
self.execute(start_ids, data)
}
};
Ok(match result?.as_slice() {
// Test if diagram is balanced in debug mode.
// Check if all result ids is the same. If not. Match on row below.
#[cfg(debug_assertions)]
arr @ [id, ..] if arr.iter().all(|item| item == id) => Some(id),
_arr @ [id, ..] => {
#[cfg(debug_assertions)]
log::error!("unbalanced BPMN diagram detected! {:?}", _arr);
Some(id)
}
// process finished
_ => None,
})
}
}
macro_rules! parallelize_helper {
($self:expr, $outputs:expr, $data:expr, $ty:expr, $noi:expr) => {
if $outputs.len() <= 1 {
$outputs
.first()
.ok_or_else(|| Error::MissingOutput($ty.to_string(), $noi.to_string()))?
} else {
match $self.maybe_parallelize($outputs.ids(), $data)? {
Some(val) => val,
None => continue,
}
}
};
}
pub(super) use parallelize_helper;