@jylups/flowable-bpmn-moddle 0.0.1-next.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
  Flowable moddle extensions for BPMN 2.0.
4
4
 
5
- > Work in progress. This package is being built in Phase 1 of the Flowable BPMN editor ([jylups/bpmn-js-env#7](https://github.com/jylups/bpmn-js-env/issues/7)). It is not published yet.
5
+ Built in Phase 1 of the Flowable BPMN editor ([jylups/bpmn-js-env#7](https://github.com/jylups/bpmn-js-env/issues/7)).
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @jylups/flowable-bpmn-moddle
11
+ ```
6
12
 
7
13
  ## Usage
8
14
 
@@ -20,7 +26,69 @@ const modeler = new BpmnModeler({
20
26
 
21
27
  The descriptor is also available as `@jylups/flowable-bpmn-moddle/resources/flowable.json`.
22
28
 
23
- The extensions it covers are listed in [docs/flowable-bpmn-extensions.md](https://github.com/jylups/bpmn-js-env/blob/main/docs/flowable-bpmn-extensions.md).
29
+ ## Activiti namespace
30
+
31
+ Flowable also reads its extensions in the Activiti namespace (`http://activiti.org/bpmn`). The descriptor registers only the Flowable namespace, so import such models through the migration helpers to get typed content that is written back as `flowable:`:
32
+
33
+ ```js
34
+ import { migrateActivitiXML, useFlowablePrefix } from '@jylups/flowable-bpmn-moddle';
35
+
36
+ const { rootElement } = await moddle.fromXML(migrateActivitiXML(xml), 'bpmn:Definitions');
37
+
38
+ // ... edit ...
39
+
40
+ const { xml: saved } = await moddle.toXML(useFlowablePrefix(rootElement));
41
+ ```
42
+
43
+ `migrateActivitiXML` only rewrites `xmlns` declarations. It also fixes models that bind the `flowable` prefix to the Activiti namespace, which otherwise fail to export.
44
+
45
+ Attributes Flowable reads without a namespace (listener, field, in/out and similar sub-attributes, the sequence flow `skipExpression`) are read whether or not they carry a prefix.
46
+
47
+ ## Unknown Flowable elements
48
+
49
+ Flowable keeps any extension element it doesn't know, and Flowable Work/Design and users add their own. With the descriptor registered, bpmn-moddle would drop `flowable:*` elements it has no type for. Call `registerUnknownFlowableElements` with the moddle instance before importing, and they are kept and written back unchanged:
50
+
51
+ ```js
52
+ import { registerUnknownFlowableElements } from '@jylups/flowable-bpmn-moddle';
53
+
54
+ registerUnknownFlowableElements(moddle, xml); // in bpmn-js: modeler.get('moddle')
55
+
56
+ const { rootElement } = await moddle.fromXML(xml, 'bpmn:Definitions');
57
+ ```
58
+
59
+ The Flowable Work/Design elements seen so far (`work-form-field-validation`, `task-candidates-type`, `static-process-key`, `translation`) are typed.
60
+
61
+ `itemSubjectRef` is kept as text, because Flowable uses values such as `xsd:string` without declaring an `itemDefinition`.
62
+
63
+ ## Covered extensions
64
+
65
+ | Area | Extensions |
66
+ |---|---|
67
+ | Flow nodes | `async`, `exclusive`, `asyncLeave`, `asyncLeaveExclusive` (`flowable:AsyncCapable`) |
68
+ | Extension elements | `failedJobRetryTimeCycle`, `element-name`, `jobCategory`, `includeInHistory`, `localization` with `documentation` |
69
+ | Listeners | `executionListener`, `taskListener` (shared `flowable:Listener`), `eventListener` |
70
+ | Process | `candidateStarterUsers`, `candidateStarterGroups`, `isEagerExecutionFetching`, `potentialStarter`, `historyLevel` |
71
+ | Start event | `initiator`, `formKey`, `formFieldValidation`, `sameDeployment` (`flowable:FormSupported`), `formProperty` with `value` |
72
+ | User task | `assignee`, `owner`, `candidateUsers`, `candidateGroups`, `dueDate`, `businessCalendarName`, `category`, `priority`, `extensionId`, `skipExpression`, `taskIdVariableName`, `taskCompleterVariableName` (`flowable:UserTask`), the form attributes (`flowable:FormSupported`), `customResource` |
73
+ | Service task | `class`, `expression`, `delegateExpression`, `resultVariableName` (and `resultVariable`), `useLocalScopeForResultVariable`, `storeResultVariableAsTransient`, `skipExpression`, `extensionId`, `triggerable`, `type`, http `parallelInSameTransaction`, the case attributes, external worker `topic` and `doNotIncludeVariables` (`flowable:ServiceTask`); `mapException`, `httpRequestHandler` / `httpResponseHandler`, `externalWorkerInParameter` / `externalWorkerOutParameter` |
74
+ | Script task | `resultVariable`, `resultVariableName`, `autoStoreVariables`, `doNotIncludeVariables`, `skipExpression` |
75
+ | Business rule task | `ruleVariablesInput`, `rules`, `resultVariable`, `class`, `exclude` |
76
+ | Send and receive tasks | send task `type` (with `field`), receive task `skipExpression` |
77
+ | Call activity | `calledElementType`, `processInstanceName`, `businessKey`, `inheritBusinessKey`, `inheritVariables`, `sameDeployment`, `calledElementBinding`, `useLocalScopeForOutParameters`, `completeAsync`, `fallbackToDefaultTenant`, `idVariableName` |
78
+ | In/out parameters | `in` and `out` (on `flowable:IOParameter`: `source`, `sourceExpression`, `target`, `targetExpression`, `sourceType`, `targetType`, `transient`; plus `variables`, `local`, `businessKey`) on call activities, start events, script tasks, case tasks and events |
79
+ | Multi-instance | `collection`, `elementVariable`, `elementIndexVariable`, `noWaitStatesAsyncLeave`; the `collection` handler (`class` / `delegateExpression` with `string` / `expression`); `variableAggregation` with `variable` children |
80
+ | Event definitions | timer `businessCalendarName`, `timeCycle` `endDate`, signal `signalExpression` and `async`, message `messageExpression`, error `errorVariableName` / `errorVariableTransient` / `errorVariableLocalScope`, terminate `terminateAll` / `terminateMultiInstance`, `variableListenerEventDefinition` |
81
+ | Event registry | `eventType`, `eventName`, `eventCorrelationParameter`, `eventInParameter`, `eventOutParameter`, `startEventCorrelationConfiguration`, `triggerEventType`, `sendSynchronously`, `triggerEventCorrelationParameter`, `channelKey`, `systemChannel` |
82
+ | Data objects, signals, sequence flows | data object `value` (`flowable:Value`), signal `scope`, sequence flow `skipExpression` |
83
+ | Fields and scripts | `field` with `stringValue`, `expression` (attribute) or `string` / `expression` children (`expressionElement`), `script` |
84
+
85
+ Listener, field and script attributes are written without a prefix, as Flowable's own exporter writes them; prefixed spellings such as `flowable:class` on a listener are read too.
86
+
87
+ With the descriptor registered, a `resourceAssignmentExpression` (in `potentialOwner`, `humanPerformer` and `flowable:potentialStarter`) is written with a `<formalExpression>` child, as Flowable expects, instead of bpmn-moddle's `<expression xsi:type="tFormalExpression">`.
88
+
89
+ The diagram interchange attributes Flowable writes (`sourceDockerX`/`Y` and `targetDockerX`/`Y` on `BPMNEdge`, `rotation` on `BPMNLabel`) are kept as unknown attributes and written back unchanged. They are not typed, because extending `bpmndi:BPMNEdge` or `bpmndi:BPMNLabel` from this package would make moddle write those tags as `bPMNEdge` / `bPMNLabel`.
90
+
91
+ The full list of Flowable extensions, and which ones are covered so far, is in the [coverage matrix](https://github.com/jylups/bpmn-js-env/blob/main/docs/coverage.md). The extensions themselves are listed in [docs/flowable-bpmn-extensions.md](https://github.com/jylups/bpmn-js-env/blob/main/docs/flowable-bpmn-extensions.md).
24
92
 
25
93
  ## Development
26
94
 
package/index.js CHANGED
@@ -7,3 +7,12 @@ import flowableDescriptor from './resources/flowable.json' with { type: 'json' }
7
7
  * Pass it to bpmn-js or bpmn-moddle as `moddleExtensions: { flowable }`.
8
8
  */
9
9
  export default flowableDescriptor;
10
+
11
+ export {
12
+ ACTIVITI_NS,
13
+ FLOWABLE_NS,
14
+ migrateActivitiXML,
15
+ useFlowablePrefix
16
+ } from './lib/migrate.js';
17
+
18
+ export { registerUnknownFlowableElements } from './lib/unknown.js';
package/lib/migrate.js ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Helpers for models written with the Activiti namespace.
3
+ *
4
+ * Flowable reads its extensions in the Flowable namespace
5
+ * (http://flowable.org/bpmn) and, for compatibility, in the Activiti
6
+ * namespace (http://activiti.org/bpmn). The descriptor only registers the
7
+ * Flowable namespace, so Activiti spellings would stay generic, untyped
8
+ * content. These helpers move a model to the Flowable namespace so that
9
+ * everything is typed and written as flowable:.
10
+ */
11
+
12
+ export const FLOWABLE_NS = 'http://flowable.org/bpmn';
13
+
14
+ export const ACTIVITI_NS = 'http://activiti.org/bpmn';
15
+
16
+ const ACTIVITI_DECLARATION = /(\sxmlns(?::[A-Za-z_][\w.-]*)?\s*=\s*)(["'])http:\/\/activiti\.org\/bpmn\2/g;
17
+
18
+ /**
19
+ * Rewrites namespace declarations of the Activiti namespace to the Flowable
20
+ * namespace. Only xmlns declarations change, so element names, attribute
21
+ * names and text stay as they are; prefixes bound to the Activiti namespace
22
+ * then resolve to the Flowable descriptor when the XML is imported.
23
+ *
24
+ * @param {string} xml
25
+ *
26
+ * @return {string}
27
+ */
28
+ export function migrateActivitiXML(xml) {
29
+ return xml.replace(ACTIVITI_DECLARATION, `$1$2${ FLOWABLE_NS }$2`);
30
+ }
31
+
32
+ /**
33
+ * Removes namespace declarations on the definitions that bind another prefix
34
+ * (for example activiti) to the Flowable namespace, so that the writer uses
35
+ * the flowable prefix. Call it after importing XML that went through
36
+ * migrateActivitiXML, before saving.
37
+ *
38
+ * @param {Object} definitions - the imported bpmn:Definitions
39
+ *
40
+ * @return {Object} the definitions
41
+ */
42
+ export function useFlowablePrefix(definitions) {
43
+ const attrs = definitions.$attrs || {};
44
+
45
+ for (const name of Object.keys(attrs)) {
46
+ if (name.startsWith('xmlns:') && name !== 'xmlns:flowable' && attrs[name] === FLOWABLE_NS) {
47
+ delete attrs[name];
48
+ }
49
+ }
50
+
51
+ if (attrs['xmlns:flowable'] && attrs['xmlns:flowable'] !== FLOWABLE_NS) {
52
+ delete attrs['xmlns:flowable'];
53
+ }
54
+
55
+ return definitions;
56
+ }
package/lib/unknown.js ADDED
@@ -0,0 +1,91 @@
1
+ import { FLOWABLE_NS } from './migrate.js';
2
+
3
+ /**
4
+ * Keeps flowable:* elements that the descriptor does not define.
5
+ *
6
+ * Because the Flowable namespace is registered with types, bpmn-moddle drops
7
+ * any flowable:* element it has no type for (an "unknown type" warning).
8
+ * Flowable itself keeps such elements, and Flowable Work/Design and users add
9
+ * their own. Before importing, this function finds the flowable:* element
10
+ * names in the XML that have no type and registers a generic type for each:
11
+ * a text body plus any child elements. Attributes are kept as unknown
12
+ * attributes, as for any element. The element is then read, kept and written
13
+ * back unchanged.
14
+ *
15
+ * Call it with the moddle instance that imports the XML (in bpmn-js:
16
+ * modeler.get('moddle')), before importXML / fromXML. Registered types stay
17
+ * on that instance.
18
+ *
19
+ * @param {Object} moddle - a bpmn-moddle instance with the Flowable descriptor
20
+ * @param {string} xml - the XML that is about to be imported
21
+ *
22
+ * @return {string[]} the names of the types registered, e.g. ['flowable:CustomValue']
23
+ */
24
+ export function registerUnknownFlowableElements(moddle, xml) {
25
+ const pkg = moddle.getPackage('flowable');
26
+
27
+ if (!pkg) {
28
+ throw new Error('the Flowable descriptor is not registered with this moddle instance');
29
+ }
30
+
31
+ const registered = [];
32
+
33
+ for (const prefix of flowablePrefixes(xml)) {
34
+ const tag = new RegExp(`<${ escapeRegExp(prefix) }:([A-Za-z_][\\w.-]*)`, 'g');
35
+
36
+ for (const match of xml.matchAll(tag)) {
37
+ const typeName = match[1].charAt(0).toUpperCase() + match[1].slice(1);
38
+ const qualifiedName = `${ pkg.prefix }:${ typeName }`;
39
+
40
+ if (registered.includes(qualifiedName) || hasType(moddle, qualifiedName)) {
41
+ continue;
42
+ }
43
+
44
+ moddle.registry.registerType({
45
+ name: typeName,
46
+ superClass: [ 'Element' ],
47
+ meta: { generic: true },
48
+ properties: [
49
+ { name: 'body', isBody: true, type: 'String' },
50
+ { name: 'children', type: 'Element', isMany: true }
51
+ ]
52
+ }, pkg);
53
+
54
+ registered.push(qualifiedName);
55
+ }
56
+ }
57
+
58
+ return registered;
59
+ }
60
+
61
+ /**
62
+ * Prefixes the XML binds to the Flowable namespace.
63
+ *
64
+ * @param {string} xml
65
+ *
66
+ * @return {Set<string>}
67
+ */
68
+ function flowablePrefixes(xml) {
69
+ const prefixes = new Set();
70
+ const declaration = /\sxmlns:([A-Za-z_][\w.-]*)\s*=\s*(["'])([^"']*)\2/g;
71
+
72
+ for (const match of xml.matchAll(declaration)) {
73
+ if (match[3] === FLOWABLE_NS) {
74
+ prefixes.add(match[1]);
75
+ }
76
+ }
77
+
78
+ return prefixes;
79
+ }
80
+
81
+ function hasType(moddle, name) {
82
+ try {
83
+ return !!moddle.getType(name);
84
+ } catch {
85
+ return false;
86
+ }
87
+ }
88
+
89
+ function escapeRegExp(value) {
90
+ return value.replace(/[.*+?^${}()|[\]\\-]/g, '\\$&');
91
+ }
package/package.json CHANGED
@@ -1,15 +1,18 @@
1
1
  {
2
2
  "name": "@jylups/flowable-bpmn-moddle",
3
- "version": "0.0.1-next.0",
3
+ "version": "0.1.0",
4
4
  "description": "Flowable moddle extensions for BPMN 2.0",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./index.js",
8
+ "./lib/migrate.js": "./lib/migrate.js",
9
+ "./lib/unknown.js": "./lib/unknown.js",
8
10
  "./resources/flowable.json": "./resources/flowable.json",
9
11
  "./package.json": "./package.json"
10
12
  },
11
13
  "files": [
12
14
  "index.js",
15
+ "lib",
13
16
  "resources"
14
17
  ],
15
18
  "scripts": {
@@ -42,6 +45,7 @@
42
45
  },
43
46
  "devDependencies": {
44
47
  "@eslint/js": "^10.0.1",
48
+ "@xmldom/xmldom": "^0.9.12",
45
49
  "bpmn-moddle": "^10.3.1",
46
50
  "eslint": "^10.15.0",
47
51
  "globals": "^17.14.0",