wasm4pm 26.7.1

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
# Examples for wasm4pm

This directory contains working examples demonstrating how to use wasm4pm in different environments.

## Quick Start

### Run Node.js Example

```bash
npm run example:nodejs
# or directly
node examples/nodejs.js
```

### Open Browser Example

```bash
npm run example:browser
```

### React Example

```bash
npm run example:react
```

### Webpack Bundler Example

```bash
npm run example:webpack
```

## Examples Overview

### nodejs.js - Node.js Integration

**Description**: Complete Node.js example with colored output and structured logging.

**Features**:

- Loading XES files
- Loading OCEL JSON files
- Analyzing event logs (statistics, duration, dotted chart)
- Process discovery (DFG, Alpha++, DECLARE)
- State management
- Error handling
- Complete workflow examples

**Run**:

```bash
node examples/nodejs.js
```

**Output**: Colored console output with sections showing:

1. Initialization
2. XES file loading and analysis
3. OCEL file loading and analysis
4. Process discovery algorithms
5. Available algorithms
6. State management
7. Complete workflow

### browser-full.html - Full-Featured Browser Demo

**Description**: Interactive web interface with a modern UI for process mining.

**Features**:

- Tab-based interface (XES, OCEL, File Upload)
- Real-time status indicator
- Multiple analysis and discovery options
- Result viewing with JSON formatting
- Object management
- File upload support
- Sample data loading
- Progress indicators

**Open**: Double-click the file or run:

```bash
npm run example:browser
```

**Sections**:

1. **Input Data** - Load XES, OCEL, or upload files
2. **Analysis & Discovery** - Run various algorithms
3. **Results** - View formatted output

### react-example.tsx - React Component

**Description**: Reusable React component for integrating process mining.

**Features**:

- Custom `useWasm()` hook for WASM initialization
- React state management
- TypeScript support
- File input handling
- Analysis results display
- Error handling
- Loading states

**Usage**:

```tsx
import ProcessMiningDemo from './examples/react-example';

function App() {
  return <ProcessMiningDemo />;
}
```

**Compile**:

```bash
npm run example:react
```

### webpack.config.js - Webpack Configuration

**Description**: Configuration for bundling the WASM module with Webpack.

**Features**:

- WASM module bundling
- TypeScript support
- Development and production modes
- Source maps
- Dev server with hot reload
- CSS and asset handling

**Usage**:

```bash
npm run example:webpack
# Opens dev server at http://localhost:8080
```

## Environment-Specific Setup

### Node.js Environment

Requirements:

- Node.js 14+
- @wasm4pm/cli package built for Node.js

Build for Node.js:

```bash
npm run build:nodejs
```

Usage:

```javascript
const pm = require('@wasm4pm/cli');
pm.init();
const handle = pm.load_eventlog_from_xes(xesContent);
```

### Browser Environment

Requirements:

- Modern browser with WebAssembly support
- WASM module built for web

Build for browser:

```bash
npm run build:web
```

Usage:

```javascript
import init, * as pm from '@wasm4pm/cli';

async function setup() {
  await init();
  pm.init();
  const handle = pm.load_eventlog_from_xes(xesContent);
}
```

### React Environment

Requirements:

- React 16.8+ (hooks)
- TypeScript (optional but recommended)
- Webpack or similar bundler

Basic component:

```tsx
function MyComponent() {
  const { pm, isReady, error } = useWasm();

  const loadLog = () => {
    const handle = pm.load_eventlog_from_xes(content);
    // Use handle...
  };

  return <button onClick={loadLog}>Load</button>;
}
```

## Common Patterns

### Loading Files in Node.js

```javascript
const fs = require('fs');
const xesContent = fs.readFileSync('event_log.xes', 'utf-8');
const handle = pm.load_eventlog_from_xes(xesContent);
```

### Loading Files in Browser

```javascript
const input = document.getElementById('fileInput');
input.addEventListener('change', (e) => {
  const file = e.target.files[0];
  const reader = new FileReader();
  reader.onload = (event) => {
    const content = event.target.result;
    const handle = pm.load_eventlog_from_xes(content);
  };
  reader.readAsText(file);
});
```

### Error Handling

```javascript
try {
  const handle = pm.load_eventlog_from_xes(content);
} catch (error) {
  console.error('Failed to load:', error.message);
  // Handle error...
}
```

### Workflow Pattern

```javascript
// 1. Load
const handle = pm.load_eventlog_from_xes(xesContent);

// 2. Analyze
const stats = JSON.parse(pm.analyze_event_statistics(handle));

// 3. Discover
const dfg = JSON.parse(pm.discover_dfg(handle));

// 4. Export/Display
console.log('Statistics:', stats);
console.log('DFG:', dfg);

// 5. Cleanup
pm.delete_object(handle);
```

## API Quick Reference

### Initialization

```javascript
pm.init(); // Initialize WASM
pm.get_version(); // Get version string
```

### Data Loading

```javascript
pm.load_eventlog_from_xes(content); // Load XES file
pm.load_ocel_from_json(content); // Load OCEL JSON
pm.load_ocel_from_xml(content); // Load OCEL XML
```

### Analysis

```javascript
pm.analyze_event_statistics(handle); // Event frequencies
pm.analyze_case_duration(handle); // Case duration stats
pm.analyze_dotted_chart(handle); // Dotted chart data
pm.analyze_ocel_statistics(handle); // OCEL-specific stats
```

### Discovery

```javascript
pm.discover_dfg(handle); // Directly-Follows Graph
pm.discover_alpha_plus_plus(handle, threshold); // Alpha++ Petri Net
pm.discover_declare(handle); // DECLARE constraints
pm.discover_oc_dfg(handle); // Object-Centric DFG
```

### State Management

```javascript
pm.object_count(); // Count stored objects
pm.delete_object(handle); // Delete specific object
pm.clear_all_objects(); // Clear all objects
```

### Info

```javascript
pm.available_discovery_algorithms(); // List algorithms
pm.available_analysis_functions(); // List functions
```

## Sample Data Format

### XES Format

```xml
<?xml version="1.0" encoding="UTF-8"?>
<log xes.version="1.0">
  <trace>
    <string key="concept:name" value="Case001"/>
    <event>
      <string key="concept:name" value="Activity A"/>
      <date key="time:timestamp" value="2023-01-01T10:00:00"/>
    </event>
  </trace>
</log>
```

### OCEL Format

```json
{
  "ocel:global-event": {
    "ocel:attribute": []
  },
  "ocel:global-object": {
    "ocel:object-type": [{ "ocel:name": "Order" }, { "ocel:name": "Item" }]
  },
  "ocel:events": {
    "ocel:event": [
      {
        "ocel:id": "e1",
        "ocel:type": "Create Order",
        "ocel:timestamp": "2023-01-01T10:00:00",
        "ocel:omap": { "ocel:o": [{ "ocel:id": "o1" }] }
      }
    ]
  },
  "ocel:objects": {
    "ocel:object": [{ "ocel:id": "o1", "ocel:type": "Order" }]
  }
}
```

## Troubleshooting

### Module Not Found

```bash
# Build the module first
npm run build:all
```

### WASM Not Supported

Ensure your browser supports WebAssembly:

- Chrome 57+
- Firefox 52+
- Safari 11+
- Edge 16+

### File Loading Issues

- Check file encoding (UTF-8)
- Verify file format (XES or OCEL)
- Check CORS headers if loading from URL

### Performance Issues

- Reduce log size for testing
- Use memoization in React
- Profile with browser DevTools

## Contributing

To add new examples:

1. Create a new file in the examples directory
2. Follow existing code style
3. Add documentation in this README
4. Include error handling
5. Test with sample data
6. Add npm script if appropriate

## Further Reading

- [wasm4pm README](../README.md)
- [Integration Tests](../__tests__/integration/README.md)
- [Test Fixtures](../__tests__/data/fixtures/README.md)
- [XES Standard](http://xes-standard.org/)
- [OCEL Standard](https://www.ocelpetrisolver.org/)

## Support

For issues or questions:

1. Check existing examples
2. Review test files for usage patterns
3. See integration tests for edge cases
4. Check main repository README