data-modelling-core 2.4.0

Core SDK library for model operations across platforms
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
//! BPMN importer
//!
//! Provides functionality to import BPMN 2.0 XML files with validation.

use anyhow::{Context, Result};
use std::collections::HashMap;
use uuid::Uuid;

use crate::models::bpmn::BPMNModel;

/// BPMN namespace URIs
const BPMN_NAMESPACE: &str = "http://www.omg.org/spec/BPMN/20100524/MODEL";
const BPMNDI_NAMESPACE: &str = "http://www.omg.org/spec/BPMN/20100524/DI";

/// BPMN Importer
///
/// Imports BPMN 2.0 XML content into a BPMNModel struct.
#[derive(Debug, Default)]
pub struct BPMNImporter {
    /// List of errors encountered during parsing
    pub errors: Vec<String>,
}

impl BPMNImporter {
    /// Create a new BPMNImporter
    pub fn new() -> Self {
        Self { errors: Vec::new() }
    }

    /// Validate BPMN XML against XSD schema
    ///
    /// Performs structural validation of BPMN 2.0 XML including:
    /// - XML well-formedness checking
    /// - Required BPMN elements validation
    /// - Namespace verification
    ///
    /// # Arguments
    ///
    /// * `xml_content` - The BPMN XML content as a string.
    ///
    /// # Returns
    ///
    /// A `Result` indicating whether validation succeeded.
    #[cfg(feature = "bpmn")]
    pub fn validate(&self, xml_content: &str) -> Result<()> {
        use quick_xml::Reader;
        use quick_xml::events::Event;

        let mut reader = Reader::from_str(xml_content);
        reader.config_mut().trim_text(true);

        let mut found_definitions = false;
        let mut found_process = false;
        let mut has_bpmn_namespace = false;

        loop {
            match reader.read_event() {
                Ok(Event::Start(ref e)) => {
                    let local_name_bytes = e.local_name();
                    let local_name = String::from_utf8_lossy(local_name_bytes.as_ref()).to_string();

                    // Check for definitions element (root element)
                    if local_name == "definitions" {
                        found_definitions = true;

                        // Check for BPMN namespace in attributes
                        for attr in e.attributes().flatten() {
                            let value = String::from_utf8_lossy(&attr.value);
                            if value.contains("omg.org/spec/BPMN") || value == BPMN_NAMESPACE {
                                has_bpmn_namespace = true;
                            }
                        }
                    }

                    // Check for process element
                    if local_name == "process" {
                        found_process = true;
                    }
                }
                Ok(Event::Empty(ref e)) => {
                    let local_name_bytes = e.local_name();
                    let local_name = String::from_utf8_lossy(local_name_bytes.as_ref()).to_string();
                    if local_name == "process" {
                        found_process = true;
                    }
                }
                Ok(Event::Eof) => break,
                Err(e) => {
                    return Err(anyhow::anyhow!(
                        "BPMN XML parsing error at position {}: {}",
                        reader.error_position(),
                        e
                    ))
                    .context("BPMN XML validation failed");
                }
                _ => {}
            }
        }

        // Validate required elements
        if !found_definitions {
            return Err(anyhow::anyhow!(
                "Invalid BPMN: missing root 'definitions' element"
            ))
            .context("BPMN XML validation failed");
        }

        if !has_bpmn_namespace {
            return Err(anyhow::anyhow!(
                "Invalid BPMN: missing BPMN namespace declaration (expected {})",
                BPMN_NAMESPACE
            ))
            .context("BPMN XML validation failed");
        }

        if !found_process {
            // Note: Some BPMN files may only contain collaboration elements
            // This is a warning, not an error
            tracing::warn!("BPMN file does not contain a 'process' element");
        }

        Ok(())
    }

    #[cfg(not(feature = "bpmn"))]
    pub fn validate(&self, _xml_content: &str) -> Result<()> {
        // BPMN feature not enabled - skip validation
        Ok(())
    }

    /// Extract metadata from BPMN XML
    ///
    /// Extracts information including:
    /// - Process ID and name
    /// - Target namespace
    /// - Exporter and exporter version
    /// - Process elements count (tasks, gateways, events)
    ///
    /// # Arguments
    ///
    /// * `xml_content` - The BPMN XML content as a string.
    ///
    /// # Returns
    ///
    /// A `HashMap` containing extracted metadata.
    #[cfg(feature = "bpmn")]
    pub fn extract_metadata(&self, xml_content: &str) -> HashMap<String, serde_json::Value> {
        use quick_xml::Reader;
        use quick_xml::events::Event;
        use serde_json::json;

        let mut metadata = HashMap::new();
        let mut reader = Reader::from_str(xml_content);
        reader.config_mut().trim_text(true);

        let mut process_count = 0;
        let mut task_count = 0;
        let mut gateway_count = 0;
        let mut event_count = 0;
        let mut subprocess_count = 0;
        let mut processes: Vec<serde_json::Value> = Vec::new();

        loop {
            match reader.read_event() {
                Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e)) => {
                    let local_name_bytes = e.local_name();
                    let local_name_str =
                        String::from_utf8_lossy(local_name_bytes.as_ref()).to_string();

                    // Extract definitions attributes
                    if local_name_str == "definitions" {
                        for attr in e.attributes().flatten() {
                            let key = String::from_utf8_lossy(attr.key.as_ref()).to_string();
                            let value = String::from_utf8_lossy(&attr.value).to_string();

                            match key.as_str() {
                                "id" => {
                                    metadata.insert("definitionsId".to_string(), json!(value));
                                }
                                "name" => {
                                    metadata.insert("definitionsName".to_string(), json!(value));
                                }
                                "targetNamespace" => {
                                    metadata.insert("targetNamespace".to_string(), json!(value));
                                }
                                "exporter" => {
                                    metadata.insert("exporter".to_string(), json!(value));
                                }
                                "exporterVersion" => {
                                    metadata.insert("exporterVersion".to_string(), json!(value));
                                }
                                _ => {}
                            }
                        }
                    }

                    // Extract process information
                    if local_name_str == "process" {
                        process_count += 1;
                        let mut process_info = serde_json::Map::new();

                        for attr in e.attributes().flatten() {
                            let key = String::from_utf8_lossy(attr.key.as_ref()).to_string();
                            let value = String::from_utf8_lossy(&attr.value).to_string();

                            match key.as_str() {
                                "id" => {
                                    process_info.insert("id".to_string(), json!(value));
                                }
                                "name" => {
                                    process_info.insert("name".to_string(), json!(value));
                                }
                                "isExecutable" => {
                                    process_info
                                        .insert("isExecutable".to_string(), json!(value == "true"));
                                }
                                _ => {}
                            }
                        }

                        if !process_info.is_empty() {
                            processes.push(serde_json::Value::Object(process_info));
                        }
                    }

                    // Count element types
                    if local_name_str.ends_with("Task") || local_name_str == "task" {
                        task_count += 1;
                    } else if local_name_str.ends_with("Gateway") {
                        gateway_count += 1;
                    } else if local_name_str.ends_with("Event") {
                        event_count += 1;
                    } else if local_name_str == "subProcess" {
                        subprocess_count += 1;
                    }
                }
                Ok(Event::Eof) => break,
                Err(_) => break,
                _ => {}
            }
        }

        // Add counts to metadata
        metadata.insert("processCount".to_string(), json!(process_count));
        metadata.insert("taskCount".to_string(), json!(task_count));
        metadata.insert("gatewayCount".to_string(), json!(gateway_count));
        metadata.insert("eventCount".to_string(), json!(event_count));
        metadata.insert("subProcessCount".to_string(), json!(subprocess_count));

        if !processes.is_empty() {
            metadata.insert("processes".to_string(), json!(processes));
        }

        // Add BPMN version info
        metadata.insert("bpmnVersion".to_string(), json!("2.0"));
        metadata.insert("bpmnNamespace".to_string(), json!(BPMN_NAMESPACE));
        metadata.insert("bpmndiNamespace".to_string(), json!(BPMNDI_NAMESPACE));

        metadata
    }

    #[cfg(not(feature = "bpmn"))]
    pub fn extract_metadata(&self, _xml_content: &str) -> HashMap<String, serde_json::Value> {
        // BPMN feature not enabled - return empty metadata
        HashMap::new()
    }

    /// Import BPMN XML content into a BPMNModel struct.
    ///
    /// # Arguments
    ///
    /// * `xml_content` - The BPMN XML content as a string.
    /// * `domain_id` - The domain ID this model belongs to.
    /// * `model_name` - The name for the model (extracted from XML if not provided).
    ///
    /// # Returns
    ///
    /// A `Result` containing the `BPMNModel` if successful, or an error if parsing fails.
    pub fn import(
        &mut self,
        xml_content: &str,
        domain_id: Uuid,
        model_name: Option<&str>,
    ) -> Result<BPMNModel> {
        // Validate XML
        self.validate(xml_content)
            .context("BPMN XML validation failed")?;

        // Extract metadata
        let metadata = self.extract_metadata(xml_content);

        // Determine model name from metadata or parameter
        let name = model_name
            .map(|s| s.to_string())
            .or_else(|| {
                metadata
                    .get("definitionsName")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string())
            })
            .or_else(|| {
                metadata
                    .get("processes")
                    .and_then(|v| v.as_array())
                    .and_then(|arr| arr.first())
                    .and_then(|p| p.get("name"))
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string())
            })
            .unwrap_or_else(|| "bpmn_model".to_string());

        // Create file path
        let file_path = format!("{}/{}.bpmn.xml", domain_id, name);

        // Calculate file size
        let file_size = xml_content.len() as u64;

        Ok(BPMNModel::new(domain_id, name, file_path, file_size))
    }
}

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

    #[test]
    #[cfg(feature = "bpmn")]
    fn test_validate_valid_bpmn() {
        let bpmn_xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
             xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
             id="definitions_1"
             targetNamespace="http://example.com/bpmn">
  <process id="process_1" name="Test Process" isExecutable="true">
    <startEvent id="start_1"/>
    <task id="task_1" name="Do Something"/>
    <endEvent id="end_1"/>
  </process>
</definitions>"#;

        let importer = BPMNImporter::new();
        assert!(importer.validate(bpmn_xml).is_ok());
    }

    #[test]
    #[cfg(feature = "bpmn")]
    fn test_validate_missing_definitions() {
        let bpmn_xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<process id="process_1" name="Test Process">
  <startEvent id="start_1"/>
</process>"#;

        let importer = BPMNImporter::new();
        let result = importer.validate(bpmn_xml);
        assert!(
            result.is_err(),
            "Expected error for missing definitions, got Ok"
        );
        let err = result.unwrap_err();
        // Check the full error chain for the expected message
        let err_chain = format!("{:?}", err);
        assert!(
            err_chain.contains("missing root 'definitions' element")
                || err.to_string().contains("BPMN XML validation failed"),
            "Expected error about missing definitions, got: {}",
            err_chain
        );
    }

    #[test]
    #[cfg(feature = "bpmn")]
    fn test_extract_metadata() {
        let bpmn_xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
             id="definitions_1"
             name="My BPMN Model"
             targetNamespace="http://example.com/bpmn"
             exporter="Test Exporter"
             exporterVersion="1.0.0">
  <process id="process_1" name="Main Process" isExecutable="true">
    <startEvent id="start_1"/>
    <userTask id="task_1" name="User Task"/>
    <serviceTask id="task_2" name="Service Task"/>
    <exclusiveGateway id="gateway_1"/>
    <endEvent id="end_1"/>
  </process>
</definitions>"#;

        let importer = BPMNImporter::new();
        let metadata = importer.extract_metadata(bpmn_xml);

        assert_eq!(
            metadata.get("definitionsName").and_then(|v| v.as_str()),
            Some("My BPMN Model")
        );
        assert_eq!(
            metadata.get("exporter").and_then(|v| v.as_str()),
            Some("Test Exporter")
        );
        assert_eq!(
            metadata.get("exporterVersion").and_then(|v| v.as_str()),
            Some("1.0.0")
        );
        assert_eq!(
            metadata.get("processCount").and_then(|v| v.as_i64()),
            Some(1)
        );
        assert_eq!(metadata.get("taskCount").and_then(|v| v.as_i64()), Some(2));
        assert_eq!(
            metadata.get("gatewayCount").and_then(|v| v.as_i64()),
            Some(1)
        );
        assert_eq!(metadata.get("eventCount").and_then(|v| v.as_i64()), Some(2));
    }
}