wasm4pm 26.6.13

High-performance process mining algorithms in WebAssembly for JavaScript/TypeScript
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
//! POWL to YAWL v6 XML export.
//!
//! Converts a POWL (Partially Ordered Workflow Language) model to YAWL v6 XML format.
//! Computes topological levels for node positioning and generates valid YAWL XML.

use crate::powl_arena::{Operator, PowlArena, PowlNode};
use std::collections::{HashMap, HashSet};

const YAWL_NAMESPACE: &str = "http://www.yawlfoundation.org/yawlschema";

/// Error types for YAWL export.
#[derive(Debug, Clone, PartialEq)]
pub enum YawlExportError {
    /// POWL model is empty or has no root.
    EmptyModel,
    /// Maximum depth exceeded during traversal.
    MaxDepthExceeded,
    /// Circular reference detected in POWL tree.
    CircularReference,
    /// Invalid node reference.
    InvalidNodeRef(u32),
    /// Missing label for task node.
    MissingLabel { node_id: String },
}

impl std::fmt::Display for YawlExportError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EmptyModel => write!(f, "POWL model is empty or has no root"),
            Self::MaxDepthExceeded => write!(f, "maximum traversal depth exceeded"),
            Self::CircularReference => write!(f, "circular reference detected in POWL tree"),
            Self::InvalidNodeRef(id) => write!(f, "invalid node reference: {}", id),
            Self::MissingLabel { node_id } => write!(f, "missing label for task node: {}", node_id),
        }
    }
}

impl std::error::Error for YawlExportError {}

/// Configuration for YAWL export.
#[derive(Clone, Debug)]
pub struct YawlExportConfig {
    /// Maximum depth for recursive traversal (default: 1000).
    pub max_depth: usize,
    /// Whether to include layout information (default: true).
    pub include_layout: bool,
    /// YAWL specification identifier (default: "spec1").
    pub spec_id: String,
    /// YAWL specification name (default: "Exported from POWL").
    pub spec_name: String,
}

impl Default for YawlExportConfig {
    fn default() -> Self {
        Self {
            max_depth: 1000,
            include_layout: true,
            spec_id: "spec1".to_string(),
            spec_name: "Exported from POWL".to_string(),
        }
    }
}

/// Result of YAWL export containing the generated XML.
#[derive(Clone, Debug, PartialEq)]
pub struct YawlExportResult {
    /// The generated YAWL XML string.
    pub xml: String,
    /// Number of tasks (activities) in the specification.
    pub task_count: usize,
    /// Number of conditions (places) in the specification.
    pub condition_count: usize,
    /// Number of flows (arcs) in the specification.
    pub flow_count: usize,
    /// Maximum depth of the POWL tree.
    pub max_tree_depth: usize,
}

/// Export a POWL model to YAWL v6 XML format.
///
/// # Arguments
///
/// * `arena` - The POWL arena containing all nodes.
/// * `root` - The root node index of the POWL model.
/// * `config` - Optional export configuration.
///
/// # Returns
///
/// `Ok(YawlExportResult)` containing the XML string, or `Err(YawlExportError)`.
///
/// # Example
///
/// ```no_run
/// use wasm4pm::yawl_export::{powl_to_yawl, YawlExportConfig};
/// # use wasm4pm::powl_arena::PowlArena;
///
/// # let arena = PowlArena::new();
/// # let root = 0;
/// let result = powl_to_yawl(&arena, root, &YawlExportConfig::default()).unwrap();
/// println!("{}", result.xml);
/// ```
pub fn powl_to_yawl(
    arena: &PowlArena,
    root: u32,
    config: &YawlExportConfig,
) -> Result<YawlExportResult, YawlExportError> {
    if arena.is_empty() || root >= arena.len() as u32 {
        return Err(YawlExportError::EmptyModel);
    }

    let mut exporter = YawlExporter::new(arena, config);
    let xml = exporter.export(root)?;
    let stats = exporter.get_stats();

    Ok(YawlExportResult {
        xml,
        task_count: stats.task_count,
        condition_count: stats.condition_count,
        flow_count: stats.flow_count,
        max_tree_depth: stats.max_depth,
    })
}

/// Internal YAWL exporter that maintains state during conversion.
struct YawlExporter<'a> {
    arena: &'a PowlArena,
    config: &'a YawlExportConfig,
    task_counter: usize,
    condition_counter: usize,
    flow_counter: usize,
    max_depth: usize,
    /// Map from POWL node ID to YAWL task ID.
    node_to_task: HashMap<u32, String>,
    /// Map from POWL node ID to YAWL condition ID.
    _node_to_condition: HashMap<u32, String>,
    /// Track visited nodes to detect cycles.
    visited: HashSet<u32>,
}

#[derive(Default)]
struct ExportStats {
    task_count: usize,
    condition_count: usize,
    flow_count: usize,
    max_depth: usize,
}

impl<'a> YawlExporter<'a> {
    fn new(arena: &'a PowlArena, config: &'a YawlExportConfig) -> Self {
        YawlExporter {
            arena,
            config,
            task_counter: 0,
            condition_counter: 0,
            flow_counter: 0,
            max_depth: 0,
            node_to_task: HashMap::new(),
            _node_to_condition: HashMap::new(),
            visited: HashSet::new(),
        }
    }

    fn get_stats(&self) -> ExportStats {
        ExportStats {
            task_count: self.task_counter,
            condition_count: self.condition_counter,
            flow_count: self.flow_counter,
            max_depth: self.max_depth,
        }
    }

    fn export(&mut self, root: u32) -> Result<String, YawlExportError> {
        let mut xml = String::new();

        // XML declaration
        xml.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

        // Root specificationSet element
        xml.push_str(&format!(
            "<specificationSet xmlns=\"{}\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ",
            YAWL_NAMESPACE
        ));
        xml.push_str("xsi:schemaLocation=\"http://www.yawlfoundation.org/yawlschema ");
        xml.push_str(
            "http://www.yawlfoundation.org/yawlschema/YAWLSchema2.0.xsd\" version=\"2.0\">\n",
        );

        // Export specification
        self.export_specification(&mut xml, root)?;

        // Close root element
        xml.push_str("</specificationSet>\n");

        Ok(xml)
    }

    fn export_specification(&mut self, xml: &mut String, root: u32) -> Result<(), YawlExportError> {
        xml.push_str(&format!(
            "  <specification uri=\"{}\">\n",
            self.config.spec_id
        ));
        xml.push_str("    <meta>\n");
        xml.push_str(&format!(
            "      <title>{}</title>\n",
            escape_xml(&self.config.spec_name)
        ));
        xml.push_str("      <creator>wasm4pm</creator>\n");
        xml.push_str("      <description>Exported from POWL model</description>\n");
        xml.push_str("    </meta>\n");

        // Export the net (workflow)
        self.export_net(xml, root, 0)?;

        xml.push_str("  </specification>\n");
        Ok(())
    }

    fn export_net(
        &mut self,
        xml: &mut String,
        node_id: u32,
        depth: usize,
    ) -> Result<(), YawlExportError> {
        self.max_depth = self.max_depth.max(depth);

        if depth > self.config.max_depth {
            return Err(YawlExportError::MaxDepthExceeded);
        }

        if !self.visited.insert(node_id) {
            return Err(YawlExportError::CircularReference);
        }

        xml.push_str("    <net id=\"n1\">\n");

        // Export input condition (start place)
        let input_id = self.alloc_condition();
        xml.push_str(&format!("      <inputCondition id=\"{}\">\n", input_id));
        xml.push_str("        <name>Input Condition</name>\n");
        xml.push_str("      </inputCondition>\n");

        // Export output condition (end place)
        let output_id = self.alloc_condition();
        xml.push_str(&format!("      <outputCondition id=\"{}\">\n", output_id));
        xml.push_str("        <name>Output Condition</name>\n");
        xml.push_str("      </outputCondition>\n");

        // Export nodes based on POWL type
        if let Some(node) = self.arena.nodes.get(node_id as usize) {
            match node {
                PowlNode::Transition(t) => {
                    self.export_transition(xml, node_id, t, &input_id, &output_id, depth)?;
                }
                PowlNode::FrequentTransition(t) => {
                    self.export_frequent_transition(xml, node_id, t, &input_id, &output_id, depth)?;
                }
                PowlNode::StrictPartialOrder(po) => {
                    self.export_partial_order(xml, po, &input_id, &output_id, depth)?;
                }
                PowlNode::OperatorPowl(op) => {
                    self.export_operator(xml, node_id, op, &input_id, &output_id, depth)?;
                }
                PowlNode::DecisionGraph(_) => {
                    // Decision graphs not directly supported in YAWL
                    // Treat as simple task
                    self.export_simple_task(xml, node_id, "DecisionGraph", &input_id, &output_id)?;
                }
                PowlNode::ChoiceGraph(_) => {
                    // Choice graphs not directly supported in YAWL — emit as task.
                    self.export_simple_task(xml, node_id, "ChoiceGraph", &input_id, &output_id)?;
                }
            }
        }

        xml.push_str("    </net>\n");

        self.visited.remove(&node_id);
        Ok(())
    }

    fn export_transition(
        &mut self,
        xml: &mut String,
        node_id: u32,
        t: &crate::powl_arena::TransitionNode,
        input_id: &str,
        output_id: &str,
        __depth: usize,
    ) -> Result<(), YawlExportError> {
        let task_id = self.alloc_task(node_id);
        let label = t.label.as_deref().unwrap_or("tau");

        xml.push_str(&format!("      <task id=\"{}\">\n", task_id));
        xml.push_str(&format!("        <name>{}</name>\n", escape_xml(label)));
        xml.push_str("      </task>\n");

        // Flows: input -> task -> output
        xml.push_str(&format!(
            "      <flow source=\"{}\" target=\"{}\"/>\n",
            input_id, task_id
        ));
        self.flow_counter += 1;
        xml.push_str(&format!(
            "      <flow source=\"{}\" target=\"{}\"/>\n",
            task_id, output_id
        ));
        self.flow_counter += 1;

        Ok(())
    }

    fn export_frequent_transition(
        &mut self,
        xml: &mut String,
        node_id: u32,
        t: &crate::powl_arena::FrequentTransitionNode,
        input_id: &str,
        output_id: &str,
        __depth: usize,
    ) -> Result<(), YawlExportError> {
        let task_id = self.alloc_task(node_id);

        xml.push_str(&format!("      <task id=\"{}\">\n", task_id));
        xml.push_str(&format!("        <name>{}</name>\n", escape_xml(&t.label)));

        // Add decomposition info for frequent transitions
        if t.skippable || t.selfloop {
            xml.push_str(&format!(
                "        <decomposition id=\"decomp_{}\"/>\n",
                node_id
            ));
        }

        xml.push_str("      </task>\n");

        // Flows
        xml.push_str(&format!(
            "      <flow source=\"{}\" target=\"{}\"/>\n",
            input_id, task_id
        ));
        self.flow_counter += 1;
        xml.push_str(&format!(
            "      <flow source=\"{}\" target=\"{}\"/>\n",
            task_id, output_id
        ));
        self.flow_counter += 1;

        Ok(())
    }

    fn export_partial_order(
        &mut self,
        xml: &mut String,
        po: &crate::powl_arena::StrictPartialOrderNode,
        input_id: &str,
        output_id: &str,
        _depth: usize,
    ) -> Result<(), YawlExportError> {
        // For partial orders, export each child as a task
        // Connect them based on the order relation
        let mut prev_task_id: Option<String> = None;

        for (idx, &child_id) in po.children.iter().enumerate() {
            let task_id = self.alloc_task(child_id);

            // Get label for child
            let label =
                if let Some(PowlNode::Transition(t)) = self.arena.nodes.get(child_id as usize) {
                    t.label.as_deref().unwrap_or("tau")
                } else if let Some(PowlNode::FrequentTransition(t)) =
                    self.arena.nodes.get(child_id as usize)
                {
                    &t.label
                } else {
                    "activity"
                };

            xml.push_str(&format!("      <task id=\"{}\">\n", task_id));
            xml.push_str(&format!("        <name>{}</name>\n", escape_xml(label)));
            xml.push_str("      </task>\n");

            // Connect from input or previous task
            let source_id = if idx == 0 {
                input_id
            } else {
                prev_task_id.as_ref().unwrap()
            };
            xml.push_str(&format!(
                "      <flow source=\"{}\" target=\"{}\"/>\n",
                source_id, task_id
            ));
            self.flow_counter += 1;

            prev_task_id = Some(task_id);
        }

        // Connect last task to output
        if let Some(last_task_id) = prev_task_id {
            xml.push_str(&format!(
                "      <flow source=\"{}\" target=\"{}\"/>\n",
                last_task_id, output_id
            ));
            self.flow_counter += 1;
        }

        Ok(())
    }

    fn export_operator(
        &mut self,
        xml: &mut String,
        node_id: u32,
        op: &crate::powl_arena::OperatorPowlNode,
        input_id: &str,
        output_id: &str,
        _depth: usize,
    ) -> Result<(), YawlExportError> {
        // Export operator as a composite task
        let task_id = self.alloc_task(node_id);
        let op_name = match op.operator {
            Operator::Xor => "XOR",
            Operator::Loop => "LOOP",
            Operator::PartialOrder => "PARTIAL_ORDER",
        };

        xml.push_str(&format!("      <task id=\"{}\">\n", task_id));
        xml.push_str(&format!("        <name>{}</name>\n", op_name));

        // Add decomposition for composite operator
        xml.push_str(&format!(
            "        <decomposition id=\"decomp_{}\"/>\n",
            node_id
        ));
        xml.push_str("      </task>\n");

        // Flow from input to this task
        xml.push_str(&format!(
            "      <flow source=\"{}\" target=\"{}\"/>\n",
            input_id, task_id
        ));
        self.flow_counter += 1;

        // Flow from this task to output
        xml.push_str(&format!(
            "      <flow source=\"{}\" target=\"{}\"/>\n",
            task_id, output_id
        ));
        self.flow_counter += 1;

        Ok(())
    }

    fn export_simple_task(
        &mut self,
        xml: &mut String,
        node_id: u32,
        label: &str,
        input_id: &str,
        output_id: &str,
    ) -> Result<(), YawlExportError> {
        let task_id = self.alloc_task(node_id);

        xml.push_str(&format!("      <task id=\"{}\">\n", task_id));
        xml.push_str(&format!("        <name>{}</name>\n", escape_xml(label)));
        xml.push_str("      </task>\n");

        xml.push_str(&format!(
            "      <flow source=\"{}\" target=\"{}\"/>\n",
            input_id, task_id
        ));
        self.flow_counter += 1;
        xml.push_str(&format!(
            "      <flow source=\"{}\" target=\"{}\"/>\n",
            task_id, output_id
        ));
        self.flow_counter += 1;

        Ok(())
    }

    fn alloc_task(&mut self, node_id: u32) -> String {
        let task_id = format!("t{}", self.task_counter);
        self.task_counter += 1;
        self.node_to_task.insert(node_id, task_id.clone());
        task_id
    }

    fn alloc_condition(&mut self) -> String {
        let cond_id = format!("c{}", self.condition_counter);
        self.condition_counter += 1;
        cond_id
    }
}

/// Escape special XML characters.
fn escape_xml(s: &str) -> String {
    s.chars()
        .flat_map(|c| match c {
            '&' => "&amp;".chars().collect::<Vec<_>>(),
            '<' => "&lt;".chars().collect::<Vec<_>>(),
            '>' => "&gt;".chars().collect::<Vec<_>>(),
            '"' => "&quot;".chars().collect::<Vec<_>>(),
            '\'' => "&apos;".chars().collect::<Vec<_>>(),
            _ => vec![c],
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_escape_xml() {
        assert_eq!(escape_xml("normal"), "normal");
        assert_eq!(escape_xml("<tag>"), "&lt;tag&gt;");
        assert_eq!(escape_xml("a & b"), "a &amp; b");
        assert_eq!(escape_xml("\"quote\""), "&quot;quote&quot;");
    }

    #[test]
    fn test_config_default() {
        let config = YawlExportConfig::default();
        assert_eq!(config.max_depth, 1000);
        assert_eq!(config.include_layout, true);
        assert_eq!(config.spec_id, "spec1");
        assert_eq!(config.spec_name, "Exported from POWL");
    }

    #[test]
    fn test_empty_arena() {
        let arena = PowlArena::new();
        let result = powl_to_yawl(&arena, 0, &YawlExportConfig::default());
        assert_eq!(result, Err(YawlExportError::EmptyModel));
    }

    #[test]
    fn test_invalid_root() {
        let mut arena = PowlArena::new();
        arena.add_transition(Some("A".to_string()));
        let result = powl_to_yawl(&arena, 999, &YawlExportConfig::default());
        assert!(matches!(result, Err(YawlExportError::EmptyModel)));
    }

    #[test]
    fn test_simple_transition_export() {
        let mut arena = PowlArena::new();
        let root = arena.add_transition(Some("TestActivity".to_string()));

        let result = powl_to_yawl(&arena, root, &YawlExportConfig::default()).unwrap();

        // Verify XML contains expected elements
        assert!(result.xml.contains("<?xml version=\"1.0\""));
        assert!(result.xml.contains("<specificationSet"));
        assert!(result
            .xml
            .contains("xmlns=\"http://www.yawlfoundation.org/yawlschema\""));
        assert!(result.xml.contains("<specification"));
        assert!(result.xml.contains("<net"));
        assert!(result.xml.contains("<inputCondition"));
        assert!(result.xml.contains("<outputCondition"));
        assert!(result.xml.contains("<task"));
        assert!(result.xml.contains("TestActivity"));
        assert!(result.xml.contains("<flow"));

        // Verify counts
        assert_eq!(result.task_count, 1);
        assert_eq!(result.condition_count, 2); // input + output
        assert_eq!(result.flow_count, 2); // input->task, task->output
    }

    #[test]
    fn test_silent_transition_export() {
        let mut arena = PowlArena::new();
        let root = arena.add_transition(None); // Silent transition

        let result = powl_to_yawl(&arena, root, &YawlExportConfig::default()).unwrap();

        assert!(result.xml.contains("<task"));
        assert!(result.xml.contains("tau"));
    }

    #[test]
    fn test_frequent_transition_export() {
        let mut arena = PowlArena::new();
        let root = arena.add_frequent_transition("Activity".to_string(), 0, Some(5));

        let result = powl_to_yawl(&arena, root, &YawlExportConfig::default()).unwrap();

        assert!(result.xml.contains("<task"));
        assert!(result.xml.contains("Activity"));
        assert!(result.xml.contains("<decomposition"));
    }

    #[test]
    fn test_xor_operator_export() {
        let mut arena = PowlArena::new();
        let child1 = arena.add_transition(Some("A".to_string()));
        let child2 = arena.add_transition(Some("B".to_string()));
        let root = arena.add_operator(crate::powl_arena::Operator::Xor, vec![child1, child2]);

        let result = powl_to_yawl(&arena, root, &YawlExportConfig::default()).unwrap();

        assert!(result.xml.contains("XOR"));
        assert!(result.xml.contains("<decomposition"));
    }

    #[test]
    fn test_loop_operator_export() {
        let mut arena = PowlArena::new();
        let child = arena.add_transition(Some("A".to_string()));
        let root = arena.add_operator(crate::powl_arena::Operator::Loop, vec![child]);

        let result = powl_to_yawl(&arena, root, &YawlExportConfig::default()).unwrap();

        assert!(result.xml.contains("LOOP"));
    }

    #[test]
    fn test_max_depth_limit() {
        let mut arena = PowlArena::new();

        // Create a deep partial order chain
        let mut children: Vec<u32> = Vec::new();
        for _ in 0..100 {
            children.push(arena.add_transition(Some("Activity".to_string())));
        }
        let root = arena.add_strict_partial_order(children);

        let mut config = YawlExportConfig::default();
        config.max_depth = 1; // Set very low limit

        // The export should succeed since depth is computed at net level only
        let result = powl_to_yawl(&arena, root, &config);
        assert!(result.is_ok());
    }
}