ebi 0.3.14

A stochastic process mining utility and library
Documentation
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
use crate::semantics::semantics::Semantics;
use ebi_objects::{
    Activity,
    anyhow::{Result, anyhow},
    ebi_objects::{
        labelled_petri_net::TransitionIndex,
        partially_ordered_workflow_language::{PartiallyOrderedWorkflowLanguage, PowlNode},
        process_tree::{NodeState, TreeMarking},
    },
};
use strum_macros::EnumIs;

/// In the space of transitions, every node has three transitions:
/// - one silent that starts the node,
/// - one that executes the node, and
/// - one silent that finishes it.
impl Semantics for PartiallyOrderedWorkflowLanguage {
    type SemState = TreeMarking;

    fn get_initial_state(&self) -> Option<<Self as Semantics>::SemState> {
        if self.tree.is_empty() {
            None
        } else {
            let mut marking = TreeMarking::new(self.number_of_nodes());
            marking
                .states
                .iter_mut()
                .for_each(|x| *x = NodeState::Enabled);
            Some(marking)
        }
    }

    /// Strategy: do not perform any checking and just update the current node as requested.
    /// However, do not 
    fn execute_transition(
        &self,
        state: &mut <Self as Semantics>::SemState,
        transition: TransitionIndex,
    ) -> Result<()> {
        let (node_index, node_type, transition_type, _, _, _) =
            transition_2_node_type(self, transition)
                .ok_or_else(|| anyhow!("Transition not found."))?;
        match transition_type {
            TransitionType::Start => {
                //if our parent is a choice graph, reset all children
                if let Some((parent_index, _)) = self.get_parent(node_index)
                    && self.tree[parent_index].is_choice_graph()
                {
                    for child in (parent_index + 1)..self.traverse(parent_index) {
                        state[child] = NodeState::Enabled;
                    }
                }
                //start the current child
                state[node_index] = NodeState::Started;
            }
            TransitionType::Execute => {
                state[node_index] = NodeState::Executed;
                //reset all children
                if !node_type.is_activity() {
                    for child in (node_index + 1)..self.traverse(node_index) {
                        state[child] = NodeState::Enabled;
                    }
                }
            }
            TransitionType::End => {
                state[node_index] = NodeState::Closed;
                //close all children
                if !node_type.is_activity() {
                    for child in (node_index + 1)..self.traverse(node_index) {
                        state[child] = NodeState::Closed;
                    }
                }
            }
        }
        Ok(())
    }

    fn is_final_state(&self, state: &<Self as Semantics>::SemState) -> bool {
        if state.states.is_empty() {
            false
        } else {
            state.states[0].is_closed()
        }
    }

    fn is_transition_silent(
        &self,
        transition: TransitionIndex,
        _state: &<Self as Semantics>::SemState,
    ) -> bool {
        if let Some((_, _, transition_type, _, _, activity)) =
            transition_2_node_type(self, transition)
        {
            !(activity.is_some() && transition_type.is_execute())
        } else {
            false
        }
    }

    fn get_transition_activity(
        &self,
        transition: TransitionIndex,
        _state: &<Self as Semantics>::SemState,
    ) -> Option<Activity> {
        let (_, node_type, transition_type, _, _, activity) =
            transition_2_node_type(self, transition)?;
        if transition_type.is_execute() {
            match node_type {
                NodeType::Activity => activity,
                NodeType::PartialOrder | NodeType::ChoiceGraph => None,
            }
        } else {
            None
        }
    }

    fn get_enabled_transitions(
        &self,
        state: &<Self as Semantics>::SemState,
    ) -> Vec<TransitionIndex> {
        enabled_transitions(self, state, 0)
    }
}

#[derive(EnumIs)]
enum NodeType {
    Activity,
    PartialOrder,
    ChoiceGraph,
}

#[derive(EnumIs)]
enum TransitionType {
    Start,
    Execute,
    End,
}

fn transition_2_node_type(
    powl: &PartiallyOrderedWorkflowLanguage,
    transition: usize,
) -> Option<(
    usize,
    NodeType,
    TransitionType,
    bool,
    bool,
    Option<Activity>,
)> {
    let node_index = transition / 3;
    let node = powl.tree.get(node_index)?;
    let transition_type = match transition % 3 {
        0 => TransitionType::Start,
        1 => TransitionType::Execute,
        _ => TransitionType::End,
    };

    Some(match node {
        PowlNode::Activity {
            skippable,
            repeatable,
            activity,
            ..
        } => (
            node_index,
            NodeType::Activity,
            transition_type,
            *skippable,
            *repeatable,
            *activity,
        ),
        PowlNode::PartialOrder {
            skippable,
            repeatable,
            ..
        } => (
            node_index,
            NodeType::PartialOrder,
            transition_type,
            *skippable,
            *repeatable,
            None,
        ),
        PowlNode::ChoiceGraph {
            skippable,
            repeatable,
            ..
        } => (
            node_index,
            NodeType::ChoiceGraph,
            transition_type,
            *skippable,
            *repeatable,
            None,
        ),
    })
}

fn enabled_transitions(
    powl: &PartiallyOrderedWorkflowLanguage,
    state: &TreeMarking,
    node_index: usize,
) -> Vec<TransitionIndex> {
    if node_index >= state.states.len() {
        return vec![];
    }

    let t_start = node_index * 3;
    let t_execute = node_index * 3 + 1;
    let t_close = node_index * 3 + 2;

    match (&powl.tree[node_index], state.states[node_index]) {
        (_, NodeState::Enabled) => {
            //given that we are here in the recursion, we can start the node, which means that we will not recurse further
            vec![t_start]
        }
        (_, NodeState::Closed) => {
            //this node is closed and there's nothing to execute
            vec![]
        }

        //started
        (
            PowlNode::Activity {
                skippable: false, ..
            }
            | PowlNode::PartialOrder {
                skippable: false, ..
            }
            | PowlNode::ChoiceGraph {
                skippable: false, ..
            },
            NodeState::Started,
        ) => {
            //once or one-or-more
            vec![t_execute]
        }
        (
            PowlNode::Activity {
                skippable: true, ..
            }
            | PowlNode::PartialOrder {
                skippable: true, ..
            }
            | PowlNode::ChoiceGraph {
                skippable: true, ..
            },
            NodeState::Started,
        ) => {
            //zero-or-one or zero-or-more
            vec![t_execute, t_close]
        }

        //activity executed
        (
            PowlNode::Activity {
                repeatable: false, ..
            },
            NodeState::Executed,
        ) => {
            //once or zero-or-once activity
            vec![t_close]
        }
        (
            PowlNode::Activity {
                repeatable: true, ..
            },
            NodeState::Executed,
        ) => {
            //zero-or-more or one-or-more activity
            vec![t_execute, t_close]
        }

        //partial order executed
        (
            PowlNode::PartialOrder {
                repeatable,
                number_of_children,
                edges,
                ..
            },
            NodeState::Executed,
        ) => {
            //zero-or-one or zero-or-more
            enabled_transitions_partial_order_execute(
                powl,
                state,
                node_index,
                *repeatable,
                *number_of_children,
                edges,
            )
        }

        //choice graph executed
        (
            PowlNode::ChoiceGraph {
                repeatable,
                edges,
                start_children,
                end_children,
                ..
            },
            NodeState::Executed,
        ) => {
            //zero-or-one or zero-or-more
            enabled_transitions_choice_graph_execute(
                powl,
                state,
                node_index,
                *repeatable,
                edges,
                start_children,
                end_children,
            )
        }
    }
}

//execute a choice graph
fn enabled_transitions_choice_graph_execute(
    powl: &PartiallyOrderedWorkflowLanguage,
    state: &TreeMarking,
    node_index: usize,
    repeatable: bool,
    edges: &[(usize, usize)],
    start_children: &[usize],
    end_children: &[usize],
) -> Vec<usize> {
    let t_execute = node_index * 3 + 1;
    let t_close = node_index * 3 + 2;

    //if any child has started execution, allow it to continue (there can only be one such child in a choice graph)
    for child_index in powl.get_children(node_index) {
        if state.states[child_index] == NodeState::Started
            || state.states[child_index] == NodeState::Executed
        {
            return enabled_transitions(powl, state, child_index);
        }
    }

    //if no child has started yet, we can start the choice graph
    if powl
        .get_children(node_index)
        .all(|child_index| state.states[child_index] == NodeState::Enabled)
    {
        //start the choice graph
        return start_children
            .iter()
            .map(|child_rank| powl.get_child(node_index, *child_rank) * 3)
            .collect();
    }

    let mut result = vec![];
    if end_children
        .iter()
        .map(|child_rank| powl.get_child(node_index, *child_rank))
        .any(|child_index| state.states[child_index] == NodeState::Closed)
    {
        result.push(t_close);
        // one end child is closed; we can close this node
        if repeatable {
            // if it is repeatable, we can execute it again
            result.push(t_execute)
        }
    }

    //a node with an incoming edge from a closed node can start
    for (source_rank, target_rank) in edges {
        let source = powl.get_child(node_index, *source_rank);
        let target = powl.get_child(node_index, *target_rank);

        if state.states[source] == NodeState::Closed && state.states[target] == NodeState::Enabled {
            //source is closed, which means that target can start
            result.push(target * 3);
        }
    }

    result
}

//execute a partial order
fn enabled_transitions_partial_order_execute(
    powl: &PartiallyOrderedWorkflowLanguage,
    state: &TreeMarking,
    node_index: usize,
    repeatable: bool,
    number_of_children: usize,
    edges: &Vec<(usize, usize)>,
) -> Vec<usize> {
    let t_execute = node_index * 3 + 1;
    let t_close = node_index * 3 + 2;

    if powl
        .get_children(node_index)
        .all(|child_index| state[child_index] == NodeState::Closed)
    {
        // all children are closed; we can close (or repeat) this node
        if repeatable {
            return vec![t_execute, t_close];
        } else {
            return vec![t_close];
        }
    }

    let mut result = vec![];

    //gather for each child whether all predecessors are closed
    let mut child_rank_2_all_predecessors_closed = vec![true; number_of_children];
    for (source_rank, target_rank) in edges {
        let source_index = powl.get_child(node_index, *source_rank);
        if state.states[source_index] != NodeState::Closed {
            child_rank_2_all_predecessors_closed[*target_rank] = false;
        }
    }

    for (child_rank, all_predecessors_closed) in
        child_rank_2_all_predecessors_closed.into_iter().enumerate()
    {
        if all_predecessors_closed {
            let child_index = powl.get_child(node_index, child_rank);
            result.extend(enabled_transitions(powl, state, child_index));
        }
    }

    result
}

#[cfg(test)]

mod tests {
    use crate::semantics::semantics::Semantics;
    use ebi_objects::{
        HasActivityKey,
        ebi_objects::partially_ordered_workflow_language::PartiallyOrderedWorkflowLanguage,
    };
    use std::fs;

    #[test]
    fn powl_sem_a() {
        let fin = fs::read_to_string("testfiles/a.powl").unwrap();
        let powl = fin.parse::<PartiallyOrderedWorkflowLanguage>().unwrap();

        let mut state = powl.get_initial_state().unwrap();
        let a = Some(powl.activity_key().process_activity_attempt("a").unwrap());

        assert_eq!(powl.get_enabled_transitions(&state), vec![0]);
        assert!(powl.is_transition_silent(0, &state));

        powl.execute_transition(&mut state, 0).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![1]);
        assert_eq!(powl.get_transition_activity(1, &state), a);
        assert!(!powl.is_final_state(&state));

        powl.execute_transition(&mut state, 1).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![2]);
        assert!(powl.is_transition_silent(2, &state));
        assert!(!powl.is_final_state(&state));

        powl.execute_transition(&mut state, 2).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), Vec::<usize>::new());
        assert!(powl.is_final_state(&state));
    }

    #[test]
    fn powl_sem_a_skippable() {
        let fin = fs::read_to_string("testfiles/a_skippable.powl").unwrap();
        let powl = fin.parse::<PartiallyOrderedWorkflowLanguage>().unwrap();

        let mut state = powl.get_initial_state().unwrap();
        let a = Some(powl.activity_key().process_activity_attempt("a").unwrap());

        assert_eq!(powl.get_enabled_transitions(&state), vec![0]);
        assert!(powl.is_transition_silent(0, &state));

        powl.execute_transition(&mut state, 0).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![1, 2]);
        assert_eq!(powl.get_transition_activity(1, &state), a);
        assert!(!powl.is_final_state(&state));

        powl.execute_transition(&mut state, 1).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![2]);
        assert!(powl.is_transition_silent(2, &state));
        assert!(!powl.is_final_state(&state));

        powl.execute_transition(&mut state, 2).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), Vec::<usize>::new());
        assert!(powl.is_final_state(&state));
    }

    #[test]
    fn powl_sem_or_a_b() {
        let fin = fs::read_to_string("testfiles/or_a_b.powl").unwrap();
        let powl = fin.parse::<PartiallyOrderedWorkflowLanguage>().unwrap();

        let mut state = powl.get_initial_state().unwrap();
        let b = Some(powl.activity_key().process_activity_attempt("b").unwrap());

        assert_eq!(powl.get_enabled_transitions(&state), vec![0]);
        assert!(powl.is_transition_silent(0, &state));

        //start choice
        powl.execute_transition(&mut state, 0).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![1]);
        assert!(powl.is_transition_silent(1, &state));
        assert!(!powl.is_final_state(&state));

        //execute choice
        powl.execute_transition(&mut state, 1).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![3, 6]);
        assert!(powl.is_transition_silent(3, &state));
        assert!(powl.is_transition_silent(6, &state));
        assert!(!powl.is_final_state(&state));

        //start executing b
        powl.execute_transition(&mut state, 6).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![7]);
        assert_eq!(powl.get_transition_activity(7, &state), b);
        assert!(!powl.is_final_state(&state));

        //execute b
        powl.execute_transition(&mut state, 7).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![8]);
        assert!(powl.is_transition_silent(8, &state));
        assert!(!powl.is_final_state(&state));

        //close b
        powl.execute_transition(&mut state, 8).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![2]);
        assert!(powl.is_transition_silent(2, &state));
        assert!(!powl.is_final_state(&state));

        //close or
        powl.execute_transition(&mut state, 2).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), Vec::<usize>::new());
        assert!(powl.is_final_state(&state));
    }

    #[test]
    fn powl_sem_and_a_b() {
        let fin = fs::read_to_string("testfiles/and_a_b.powl").unwrap();
        let powl = fin.parse::<PartiallyOrderedWorkflowLanguage>().unwrap();

        let mut state = powl.get_initial_state().unwrap();
        let a = Some(powl.activity_key().process_activity_attempt("a").unwrap());
        let b = Some(powl.activity_key().process_activity_attempt("b").unwrap());

        assert_eq!(powl.get_enabled_transitions(&state), vec![0]);
        assert!(powl.is_transition_silent(0, &state));

        //start and
        powl.execute_transition(&mut state, 0).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![1]);
        assert!(powl.is_transition_silent(1, &state));
        assert!(!powl.is_final_state(&state));

        //execute and
        powl.execute_transition(&mut state, 1).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![3]);
        assert!(powl.is_transition_silent(3, &state));
        assert!(!powl.is_final_state(&state));

        //start a
        powl.execute_transition(&mut state, 3).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![4]);
        assert_eq!(powl.get_transition_activity(4, &state), a);
        assert!(!powl.is_final_state(&state));

        //execute a
        powl.execute_transition(&mut state, 4).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![5]);
        assert!(powl.is_transition_silent(5, &state));
        assert!(!powl.is_final_state(&state));

        //close a
        powl.execute_transition(&mut state, 5).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![6]);
        assert!(powl.is_transition_silent(6, &state));
        assert!(!powl.is_final_state(&state));

        //start b
        powl.execute_transition(&mut state, 6).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![7]);
        assert_eq!(powl.get_transition_activity(7, &state), b);
        assert!(!powl.is_final_state(&state));

        //execute b
        powl.execute_transition(&mut state, 7).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![8]);
        assert!(powl.is_transition_silent(8, &state));
        assert!(!powl.is_final_state(&state));

        //close b
        powl.execute_transition(&mut state, 8).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![2]);
        assert!(powl.is_transition_silent(2, &state));
        assert!(!powl.is_final_state(&state));

        //close and
        powl.execute_transition(&mut state, 2).unwrap();

        assert!(powl.is_final_state(&state));
    }

    #[test]
    fn powl_sem_loop_a_b() {
        let fin = fs::read_to_string("testfiles/loop(a,b).powl").unwrap();
        let powl = fin.parse::<PartiallyOrderedWorkflowLanguage>().unwrap();

        let mut state = powl.get_initial_state().unwrap();
        let a = Some(powl.activity_key().process_activity_attempt("a").unwrap());
        let b = Some(powl.activity_key().process_activity_attempt("b").unwrap());

        assert_eq!(powl.get_enabled_transitions(&state), vec![0]);
        assert!(powl.is_transition_silent(0, &state));

        //start or
        powl.execute_transition(&mut state, 0).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![1]);
        assert!(powl.is_transition_silent(1, &state));
        assert!(!powl.is_final_state(&state));

        //execute or
        powl.execute_transition(&mut state, 1).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![3]);
        assert!(powl.is_transition_silent(3, &state));
        assert!(!powl.is_final_state(&state));

        //start a
        powl.execute_transition(&mut state, 3).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![4]);
        assert_eq!(powl.get_transition_activity(4, &state), a);
        assert!(!powl.is_final_state(&state));

        //execute a
        powl.execute_transition(&mut state, 4).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![5]);
        assert!(powl.is_transition_silent(5, &state));
        assert!(!powl.is_final_state(&state));

        //close a
        powl.execute_transition(&mut state, 5).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![2, 6]);
        assert!(!powl.is_final_state(&state));
        assert!(powl.is_transition_silent(6, &state));

        //start b
        powl.execute_transition(&mut state, 6).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![7]);
        assert!(!powl.is_final_state(&state));
        assert_eq!(powl.get_transition_activity(7, &state), b);

        //execute b
        powl.execute_transition(&mut state, 7).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![8]);
        assert!(!powl.is_final_state(&state));
        assert!(powl.is_transition_silent(8, &state));

        //close b
        powl.execute_transition(&mut state, 8).unwrap();

        assert_eq!(powl.get_enabled_transitions(&state), vec![3]);
        assert!(!powl.is_final_state(&state));
        assert!(powl.is_transition_silent(3, &state));

        //execute a
        powl.execute_transition(&mut state, 3).unwrap();

    }
}