bpmn-engine 0.1.0

BPMN 2.0 execution engine for Rust with JSON and XML format support
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! BPMN Model Elements
//!
//! Internal representation of BPMN elements after parsing from JSON.

use crate::model::json::*;
use std::collections::HashMap;

/// Process Definition
///
/// Internal representation of a BPMN process definition.
#[derive(Debug, Clone)]
pub struct ProcessDefinition {
    /// Process ID
    pub id: String,
    /// Process name
    pub name: Option<String>,
    /// Process type
    pub process_type: String,
    /// Is executable
    pub is_executable: bool,
    /// Process elements indexed by ID
    pub elements: HashMap<String, ProcessElement>,
    /// Sequence flows indexed by ID
    pub flows: HashMap<String, SequenceFlow>,
    /// Variables
    pub variables: HashMap<String, Variable>,
}

impl ProcessDefinition {
    /// Create a new process definition from JSON
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        let json_process: BpmnJsonProcess = serde_json::from_str(json)?;
        Self::from_bpmn_json(json_process)
    }

    /// Create a new process definition from XML
    pub fn from_xml(xml: &str) -> Result<Self, crate::model::format::ParseError> {
        crate::model::xml::parse_bpmn_xml(xml)
    }

    /// Create a new process definition with automatic format detection
    pub fn from_auto(input: &str) -> Result<(Self, crate::model::format::BpmnFormat), crate::model::format::ParseError> {
        crate::model::format::AutoParser::parse(input)
    }

    /// Serialize to JSON
    pub fn to_json(&self) -> Result<String, crate::model::format::SerializeError> {
        let mut json_elements = Vec::new();
        
        // Convert elements to JSON
        for element in self.elements.values() {
            json_elements.push(element.to_json_element());
        }
        
        // Convert flows to JSON
        for flow in self.flows.values() {
            json_elements.push(BpmnJsonElement::SequenceFlow(flow.to_json()));
        }
        
        let json_process = BpmnJsonProcess {
            id: self.id.clone(),
            name: self.name.clone(),
            process_type: self.process_type.clone(),
            is_executable: self.is_executable,
            elements: json_elements,
            variables: self.variables.iter().map(|(k, v)| {
                (k.clone(), crate::model::json::BpmnJsonVariable {
                    name: v.name.clone(),
                    variable_type: v.variable_type.clone(),
                    default_value: v.default_value.clone(),
                })
            }).collect(),
        };
        serde_json::to_string_pretty(&json_process).map_err(crate::model::format::SerializeError::Json)
    }

    /// Serialize to XML
    pub fn to_xml(&self) -> Result<String, crate::model::format::SerializeError> {
        crate::model::xml::serialize_bpmn_xml(self)
    }

    /// Create from BPMn JSON structure
    pub fn from_bpmn_json(json_process: BpmnJsonProcess) -> Result<Self, serde_json::Error> {
        let mut elements = HashMap::new();
        let mut flows = HashMap::new();

        for element in json_process.elements {
            match element {
                BpmnJsonElement::SequenceFlow(flow) => {
                    let seq_flow = SequenceFlow::from_json(flow);
                    flows.insert(seq_flow.id.clone(), seq_flow);
                }
                _ => {
                    let process_elem = ProcessElement::from_json_element(element)?;
                    elements.insert(process_elem.id().to_string(), process_elem);
                }
            }
        }

        Ok(Self {
            id: json_process.id,
            name: json_process.name,
            process_type: json_process.process_type,
            is_executable: json_process.is_executable,
            elements,
            flows,
            variables: json_process
                .variables
                .into_iter()
                .map(|(k, v)| (k, Variable::from_json(v)))
                .collect(),
        })
    }

    /// Get element by ID
    pub fn get_element(&self, id: &str) -> Option<&ProcessElement> {
        self.elements.get(id)
    }

    /// Get sequence flow by ID
    pub fn get_flow(&self, id: &str) -> Option<&SequenceFlow> {
        self.flows.get(id)
    }

    /// Get outgoing flows from an element
    pub fn get_outgoing_flows(&self, element_id: &str) -> Vec<&SequenceFlow> {
        self.flows
            .values()
            .filter(|flow| flow.source_ref == element_id)
            .collect()
    }

    /// Get incoming flows to an element
    pub fn get_incoming_flows(&self, element_id: &str) -> Vec<&SequenceFlow> {
        self.flows
            .values()
            .filter(|flow| flow.target_ref == element_id)
            .collect()
    }
}

/// Process Element
///
/// Represents any executable BPMN element (task, gateway, event).
#[derive(Debug, Clone)]
pub enum ProcessElement {
    /// Start Event
    StartEvent(StartEvent),
    /// End Event
    EndEvent(EndEvent),
    /// Intermediate Catch Event
    IntermediateCatchEvent(IntermediateCatchEvent),
    /// Intermediate Throw Event
    IntermediateThrowEvent(IntermediateThrowEvent),
    /// Service Task
    ServiceTask(ServiceTask),
    /// User Task
    UserTask(UserTask),
    /// Script Task
    ScriptTask(ScriptTask),
    /// Manual Task
    ManualTask(ManualTask),
    /// Exclusive Gateway
    ExclusiveGateway(ExclusiveGateway),
    /// Parallel Gateway
    ParallelGateway(ParallelGateway),
    /// Inclusive Gateway
    InclusiveGateway(InclusiveGateway),
}

impl ProcessElement {
    pub fn from_json_element(element: BpmnJsonElement) -> Result<Self, serde_json::Error> {
        match element {
            BpmnJsonElement::StartEvent(e) => Ok(ProcessElement::StartEvent(StartEvent::from_json(e))),
            BpmnJsonElement::EndEvent(e) => Ok(ProcessElement::EndEvent(EndEvent::from_json(e))),
            BpmnJsonElement::IntermediateCatchEvent(e) => {
                Ok(ProcessElement::IntermediateCatchEvent(IntermediateCatchEvent::from_json(e)))
            }
            BpmnJsonElement::IntermediateThrowEvent(e) => {
                Ok(ProcessElement::IntermediateThrowEvent(IntermediateThrowEvent::from_json(e)))
            }
            BpmnJsonElement::ServiceTask(e) => {
                Ok(ProcessElement::ServiceTask(ServiceTask::from_json(e)))
            }
            BpmnJsonElement::UserTask(e) => Ok(ProcessElement::UserTask(UserTask::from_json(e))),
            BpmnJsonElement::ScriptTask(e) => Ok(ProcessElement::ScriptTask(ScriptTask::from_json(e))),
            BpmnJsonElement::ManualTask(e) => Ok(ProcessElement::ManualTask(ManualTask::from_json(e))),
            BpmnJsonElement::ExclusiveGateway(e) => {
                Ok(ProcessElement::ExclusiveGateway(ExclusiveGateway::from_json(e)))
            }
            BpmnJsonElement::ParallelGateway(e) => {
                Ok(ProcessElement::ParallelGateway(ParallelGateway::from_json(e)))
            }
            BpmnJsonElement::InclusiveGateway(e) => {
                Ok(ProcessElement::InclusiveGateway(InclusiveGateway::from_json(e)))
            }
            BpmnJsonElement::SequenceFlow(_) => {
                use serde::de::Error;
                Err(serde_json::Error::custom("SequenceFlow should be handled separately"))
            }
        }
    }

    pub fn id(&self) -> &str {
        match self {
            ProcessElement::StartEvent(e) => &e.base.id,
            ProcessElement::EndEvent(e) => &e.base.id,
            ProcessElement::IntermediateCatchEvent(e) => &e.base.id,
            ProcessElement::IntermediateThrowEvent(e) => &e.base.id,
            ProcessElement::ServiceTask(e) => &e.base.id,
            ProcessElement::UserTask(e) => &e.base.id,
            ProcessElement::ScriptTask(e) => &e.base.id,
            ProcessElement::ManualTask(e) => &e.base.id,
            ProcessElement::ExclusiveGateway(e) => &e.base.id,
            ProcessElement::ParallelGateway(e) => &e.base.id,
            ProcessElement::InclusiveGateway(e) => &e.base.id,
        }
    }

    pub fn to_json_element(&self) -> BpmnJsonElement {
        match self {
            ProcessElement::StartEvent(e) => BpmnJsonElement::StartEvent(e.to_json()),
            ProcessElement::EndEvent(e) => BpmnJsonElement::EndEvent(e.to_json()),
            ProcessElement::IntermediateCatchEvent(e) => BpmnJsonElement::IntermediateCatchEvent(e.to_json()),
            ProcessElement::IntermediateThrowEvent(e) => BpmnJsonElement::IntermediateThrowEvent(e.to_json()),
            ProcessElement::ServiceTask(e) => BpmnJsonElement::ServiceTask(e.to_json()),
            ProcessElement::UserTask(e) => BpmnJsonElement::UserTask(e.to_json()),
            ProcessElement::ScriptTask(e) => BpmnJsonElement::ScriptTask(e.to_json()),
            ProcessElement::ManualTask(e) => BpmnJsonElement::ManualTask(e.to_json()),
            ProcessElement::ExclusiveGateway(e) => BpmnJsonElement::ExclusiveGateway(e.to_json()),
            ProcessElement::ParallelGateway(e) => BpmnJsonElement::ParallelGateway(e.to_json()),
            ProcessElement::InclusiveGateway(e) => BpmnJsonElement::InclusiveGateway(e.to_json()),
        }
    }
}

/// Base element properties
#[derive(Debug, Clone)]
pub struct ElementBase {
    pub id: String,
    pub name: Option<String>,
    pub documentation: Option<String>,
}

impl ElementBase {
    pub fn from_json(base: BpmnJsonElementBase) -> Self {
        Self {
            id: base.id,
            name: base.name,
            documentation: base.documentation,
        }
    }
}

/// Start Event
#[derive(Debug, Clone)]
pub struct StartEvent {
    pub base: ElementBase,
    pub event_definition: Option<EventDefinition>,
}

impl StartEvent {
    pub fn from_json(json: BpmnJsonStartEvent) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
            event_definition: json.event_definition.map(EventDefinition::from_json),
        }
    }

    pub fn to_json(&self) -> BpmnJsonStartEvent {
        BpmnJsonStartEvent {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
            event_definition: self.event_definition.as_ref().map(EventDefinition::to_json),
        }
    }
}

/// End Event
#[derive(Debug, Clone)]
pub struct EndEvent {
    pub base: ElementBase,
    pub event_definition: Option<EventDefinition>,
}

impl EndEvent {
    pub fn from_json(json: BpmnJsonEndEvent) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
            event_definition: json.event_definition.map(EventDefinition::from_json),
        }
    }

    pub fn to_json(&self) -> BpmnJsonEndEvent {
        BpmnJsonEndEvent {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
            event_definition: self.event_definition.as_ref().map(EventDefinition::to_json),
        }
    }
}

/// Intermediate Catch Event
#[derive(Debug, Clone)]
pub struct IntermediateCatchEvent {
    pub base: ElementBase,
    pub event_definition: Option<EventDefinition>,
}

impl IntermediateCatchEvent {
    pub fn from_json(json: BpmnJsonIntermediateCatchEvent) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
            event_definition: json.event_definition.map(EventDefinition::from_json),
        }
    }

    pub fn to_json(&self) -> BpmnJsonIntermediateCatchEvent {
        BpmnJsonIntermediateCatchEvent {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
            event_definition: self.event_definition.as_ref().map(EventDefinition::to_json),
        }
    }
}

/// Intermediate Throw Event
#[derive(Debug, Clone)]
pub struct IntermediateThrowEvent {
    pub base: ElementBase,
    pub event_definition: Option<EventDefinition>,
}

impl IntermediateThrowEvent {
    pub fn from_json(json: BpmnJsonIntermediateThrowEvent) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
            event_definition: json.event_definition.map(EventDefinition::from_json),
        }
    }

    pub fn to_json(&self) -> BpmnJsonIntermediateThrowEvent {
        BpmnJsonIntermediateThrowEvent {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
            event_definition: self.event_definition.as_ref().map(EventDefinition::to_json),
        }
    }
}

/// Service Task
#[derive(Debug, Clone)]
pub struct ServiceTask {
    pub base: ElementBase,
    pub implementation: Option<String>,
    pub operation_ref: Option<String>,
    pub io_mapping: IoMapping,
}

impl ServiceTask {
    pub fn from_json(json: BpmnJsonServiceTask) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
            implementation: json.implementation,
            operation_ref: json.operation_ref,
            io_mapping: IoMapping::from_json(json.io_mapping),
        }
    }

    pub fn to_json(&self) -> BpmnJsonServiceTask {
        BpmnJsonServiceTask {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
            implementation: self.implementation.clone(),
            operation_ref: self.operation_ref.clone(),
            io_mapping: self.io_mapping.to_json(),
        }
    }
}

/// User Task
#[derive(Debug, Clone)]
pub struct UserTask {
    pub base: ElementBase,
    pub assignment: Option<Assignment>,
    pub form_key: Option<String>,
}

impl UserTask {
    pub fn from_json(json: BpmnJsonUserTask) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
            assignment: json.assignment.map(Assignment::from_json),
            form_key: json.form_key,
        }
    }

    pub fn to_json(&self) -> BpmnJsonUserTask {
        BpmnJsonUserTask {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
            assignment: self.assignment.as_ref().map(Assignment::to_json),
            form_key: self.form_key.clone(),
        }
    }
}

/// Script Task
#[derive(Debug, Clone)]
pub struct ScriptTask {
    pub base: ElementBase,
    pub script_format: Option<String>,
    pub script: Option<String>,
}

impl ScriptTask {
    pub fn from_json(json: BpmnJsonScriptTask) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
            script_format: json.script_format,
            script: json.script,
        }
    }

    pub fn to_json(&self) -> BpmnJsonScriptTask {
        BpmnJsonScriptTask {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
            script_format: self.script_format.clone(),
            script: self.script.clone(),
        }
    }
}

/// Manual Task
#[derive(Debug, Clone)]
pub struct ManualTask {
    pub base: ElementBase,
}

impl ManualTask {
    pub fn from_json(json: BpmnJsonManualTask) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
        }
    }

    pub fn to_json(&self) -> BpmnJsonManualTask {
        BpmnJsonManualTask {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
        }
    }
}

/// Exclusive Gateway
#[derive(Debug, Clone)]
pub struct ExclusiveGateway {
    pub base: ElementBase,
    pub default_flow: Option<String>,
}

impl ExclusiveGateway {
    pub fn from_json(json: BpmnJsonExclusiveGateway) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
            default_flow: json.default_flow,
        }
    }

    pub fn to_json(&self) -> BpmnJsonExclusiveGateway {
        BpmnJsonExclusiveGateway {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
            default_flow: self.default_flow.clone(),
        }
    }
}

/// Parallel Gateway
#[derive(Debug, Clone)]
pub struct ParallelGateway {
    pub base: ElementBase,
}

impl ParallelGateway {
    pub fn from_json(json: BpmnJsonParallelGateway) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
        }
    }

    pub fn to_json(&self) -> BpmnJsonParallelGateway {
        BpmnJsonParallelGateway {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
        }
    }
}

/// Inclusive Gateway
#[derive(Debug, Clone)]
pub struct InclusiveGateway {
    pub base: ElementBase,
    pub default_flow: Option<String>,
}

impl InclusiveGateway {
    pub fn from_json(json: BpmnJsonInclusiveGateway) -> Self {
        Self {
            base: ElementBase::from_json(json.base),
            default_flow: json.default_flow,
        }
    }

    pub fn to_json(&self) -> BpmnJsonInclusiveGateway {
        BpmnJsonInclusiveGateway {
            base: BpmnJsonElementBase {
                id: self.base.id.clone(),
                name: self.base.name.clone(),
                documentation: self.base.documentation.clone(),
            },
            default_flow: self.default_flow.clone(),
        }
    }
}

/// Sequence Flow
#[derive(Debug, Clone)]
pub struct SequenceFlow {
    pub id: String,
    pub name: Option<String>,
    pub source_ref: String,
    pub target_ref: String,
    pub condition_expression: Option<ConditionExpression>,
}

impl SequenceFlow {
    pub fn from_json(json: BpmnJsonSequenceFlow) -> Self {
        Self {
            id: json.base.id,
            name: json.base.name,
            source_ref: json.source_ref,
            target_ref: json.target_ref,
            condition_expression: json.condition_expression.map(ConditionExpression::from_json),
        }
    }

    pub fn to_json(&self) -> BpmnJsonSequenceFlow {
        BpmnJsonSequenceFlow {
            base: BpmnJsonElementBase {
                id: self.id.clone(),
                name: self.name.clone(),
                documentation: None,
            },
            source_ref: self.source_ref.clone(),
            target_ref: self.target_ref.clone(),
            condition_expression: self.condition_expression.as_ref().map(ConditionExpression::to_json),
        }
    }
}

/// Event Definition
#[derive(Debug, Clone)]
pub enum EventDefinition {
    Message { message_ref: Option<String> },
    Timer { time_definition: Option<String> },
    Signal { signal_ref: Option<String> },
    Error { error_ref: Option<String> },
    Escalation { escalation_ref: Option<String> },
    Cancel,
    Compensation { activity_ref: Option<String> },
    Conditional { condition: Option<ConditionExpression> },
    Link { name: Option<String> },
    Terminate,
    None,
}

impl EventDefinition {
    pub fn from_json(json: BpmnJsonEventDefinition) -> Self {
        match json {
            BpmnJsonEventDefinition::Message { message_ref } => EventDefinition::Message { message_ref },
            BpmnJsonEventDefinition::Timer { time_definition } => EventDefinition::Timer { time_definition },
            BpmnJsonEventDefinition::Signal { signal_ref } => EventDefinition::Signal { signal_ref },
            BpmnJsonEventDefinition::Error { error_ref } => EventDefinition::Error { error_ref },
            BpmnJsonEventDefinition::Escalation { escalation_ref } => {
                EventDefinition::Escalation { escalation_ref }
            }
            BpmnJsonEventDefinition::Cancel => EventDefinition::Cancel,
            BpmnJsonEventDefinition::Compensation { activity_ref } => {
                EventDefinition::Compensation { activity_ref }
            }
            BpmnJsonEventDefinition::Conditional { condition } => {
                EventDefinition::Conditional {
                    condition: condition.map(ConditionExpression::from_json),
                }
            }
            BpmnJsonEventDefinition::Link { name } => EventDefinition::Link { name },
            BpmnJsonEventDefinition::Terminate => EventDefinition::Terminate,
            BpmnJsonEventDefinition::None => EventDefinition::None,
        }
    }

    pub fn to_json(&self) -> BpmnJsonEventDefinition {
        match self {
            EventDefinition::Message { message_ref } => BpmnJsonEventDefinition::Message { message_ref: message_ref.clone() },
            EventDefinition::Timer { time_definition } => BpmnJsonEventDefinition::Timer { time_definition: time_definition.clone() },
            EventDefinition::Signal { signal_ref } => BpmnJsonEventDefinition::Signal { signal_ref: signal_ref.clone() },
            EventDefinition::Error { error_ref } => BpmnJsonEventDefinition::Error { error_ref: error_ref.clone() },
            EventDefinition::Escalation { escalation_ref } => BpmnJsonEventDefinition::Escalation { escalation_ref: escalation_ref.clone() },
            EventDefinition::Cancel => BpmnJsonEventDefinition::Cancel,
            EventDefinition::Compensation { activity_ref } => BpmnJsonEventDefinition::Compensation { activity_ref: activity_ref.clone() },
            EventDefinition::Conditional { condition } => BpmnJsonEventDefinition::Conditional {
                condition: condition.as_ref().map(ConditionExpression::to_json),
            },
            EventDefinition::Link { name } => BpmnJsonEventDefinition::Link { name: name.clone() },
            EventDefinition::Terminate => BpmnJsonEventDefinition::Terminate,
            EventDefinition::None => BpmnJsonEventDefinition::None,
        }
    }
}

/// Condition Expression
#[derive(Debug, Clone)]
pub struct ConditionExpression {
    pub language: Option<String>,
    pub body: String,
}

impl ConditionExpression {
    pub fn from_json(json: BpmnJsonConditionExpression) -> Self {
        Self {
            language: json.language,
            body: json.body,
        }
    }

    pub fn to_json(&self) -> BpmnJsonConditionExpression {
        BpmnJsonConditionExpression {
            language: self.language.clone(),
            body: self.body.clone(),
        }
    }
}

/// Input/Output Mapping
#[derive(Debug, Clone, Default)]
pub struct IoMapping {
    pub input_parameters: Vec<IoParameter>,
    pub output_parameters: Vec<IoParameter>,
}

impl IoMapping {
    pub fn from_json(json: BpmnJsonIoMapping) -> Self {
        Self {
            input_parameters: json.input_parameters.into_iter().map(IoParameter::from_json).collect(),
            output_parameters: json.output_parameters.into_iter().map(IoParameter::from_json).collect(),
        }
    }

    pub fn to_json(&self) -> BpmnJsonIoMapping {
        BpmnJsonIoMapping {
            input_parameters: self.input_parameters.iter().map(IoParameter::to_json).collect(),
            output_parameters: self.output_parameters.iter().map(IoParameter::to_json).collect(),
        }
    }
}

/// Input/Output Parameter
#[derive(Debug, Clone)]
pub struct IoParameter {
    pub name: String,
    pub source: Option<String>,
    pub target: Option<String>,
    pub value: Option<String>,
}

impl IoParameter {
    pub fn from_json(json: BpmnJsonIoParameter) -> Self {
        Self {
            name: json.name,
            source: json.source,
            target: json.target,
            value: json.value,
        }
    }

    pub fn to_json(&self) -> BpmnJsonIoParameter {
        BpmnJsonIoParameter {
            name: self.name.clone(),
            source: self.source.clone(),
            target: self.target.clone(),
            value: self.value.clone(),
        }
    }
}

/// Assignment
#[derive(Debug, Clone)]
pub struct Assignment {
    pub assignment_type: String,
    pub value: String,
}

impl Assignment {
    pub fn from_json(json: BpmnJsonAssignment) -> Self {
        Self {
            assignment_type: json.assignment_type,
            value: json.value,
        }
    }

    pub fn to_json(&self) -> BpmnJsonAssignment {
        BpmnJsonAssignment {
            assignment_type: self.assignment_type.clone(),
            value: self.value.clone(),
        }
    }
}

/// Variable
#[derive(Debug, Clone)]
pub struct Variable {
    pub name: String,
    pub variable_type: Option<String>,
    pub default_value: Option<serde_json::Value>,
}

impl Variable {
    pub fn from_json(json: BpmnJsonVariable) -> Self {
        Self {
            name: json.name,
            variable_type: json.variable_type,
            default_value: json.default_value,
        }
    }
}