@aranzatech/diagrams-bpmn 0.2.4 → 0.2.5

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.
@@ -775,9 +775,84 @@ function extractLaneMembership(process) {
775
775
  function extractTrigger(el) {
776
776
  const defs = asElements(el.eventDefinitions);
777
777
  if (defs.length === 0) return void 0;
778
- if (defs.length > 1) return el.$type.includes("Parallel") ? "parallelMultiple" : "multiple";
778
+ if (defs.length > 1) {
779
+ return defs.some((def) => def.$type === "bpmn:ParallelMultipleEventDefinition") ? "parallelMultiple" : "multiple";
780
+ }
779
781
  return EVENT_DEF_TO_TRIGGER[defs[0].$type];
780
782
  }
783
+ function parseVariableType(value) {
784
+ const text = asString(value) ?? "";
785
+ if (text.includes("int")) return "integer";
786
+ if (text.includes("boolean")) return "boolean";
787
+ if (text.includes("date")) return "date";
788
+ if (text.includes("array")) return "array";
789
+ if (text.includes("anyType")) return "object";
790
+ return "string";
791
+ }
792
+ function extractEventDefinition(el) {
793
+ const defs = asElements(el.eventDefinitions);
794
+ if (defs.length === 0) return void 0;
795
+ if (defs.length > 1) {
796
+ return {
797
+ type: defs.some((def2) => def2.$type === "bpmn:ParallelMultipleEventDefinition") ? "parallelMultiple" : "multiple"
798
+ };
799
+ }
800
+ const [def] = defs;
801
+ const type = EVENT_DEF_TO_TRIGGER[def.$type];
802
+ if (!type) return void 0;
803
+ const eventDefinition = {
804
+ type,
805
+ ...asString(def.messageRef?.id) ? { messageRef: def.messageRef.id } : {},
806
+ ...asString(def.signalRef?.id) ? { signalRef: def.signalRef.id } : {},
807
+ ...asString(def.errorRef?.id) ? { errorRef: def.errorRef.id } : {},
808
+ ...asString(def.escalationRef?.id) ? { escalationRef: def.escalationRef.id } : {},
809
+ ...asString(def.condition?.body) ? { conditionExpression: def.condition.body } : {}
810
+ };
811
+ const timeDate = asString(def.timeDate?.body);
812
+ const timeDuration = asString(def.timeDuration?.body);
813
+ const timeCycle = asString(def.timeCycle?.body);
814
+ if (timeDate) eventDefinition.timer = { kind: "date", value: timeDate };
815
+ else if (timeCycle) eventDefinition.timer = { kind: "cycle", value: timeCycle };
816
+ else if (timeDuration) eventDefinition.timer = { kind: "duration", value: timeDuration };
817
+ if (type === "link") {
818
+ const linkName = asString(el.name) ?? asString(def.name);
819
+ if (linkName) eventDefinition.linkName = linkName;
820
+ }
821
+ return eventDefinition;
822
+ }
823
+ function extractDefinitions(rootElement) {
824
+ const definitions = {};
825
+ const rootElements = asElements(rootElement.rootElements);
826
+ const messages = rootElements.filter((element) => element.$type === "bpmn:Message" && asString(element.id)).map((message) => ({
827
+ id: message.id,
828
+ name: asString(message.name) ?? message.id
829
+ }));
830
+ if (messages.length > 0) definitions.messages = messages;
831
+ const signals = rootElements.filter((element) => element.$type === "bpmn:Signal" && asString(element.id)).map((signal) => ({
832
+ id: signal.id,
833
+ name: asString(signal.name) ?? signal.id
834
+ }));
835
+ if (signals.length > 0) definitions.signals = signals;
836
+ const errors = rootElements.filter((element) => element.$type === "bpmn:Error" && asString(element.id)).map((error) => ({
837
+ id: error.id,
838
+ name: asString(error.name) ?? error.id,
839
+ ...asString(error.errorCode) ? { errorCode: error.errorCode } : {}
840
+ }));
841
+ if (errors.length > 0) definitions.errors = errors;
842
+ const escalations = rootElements.filter((element) => element.$type === "bpmn:Escalation" && asString(element.id)).map((escalation) => ({
843
+ id: escalation.id,
844
+ name: asString(escalation.name) ?? escalation.id,
845
+ ...asString(escalation.escalationCode) ? { escalationCode: escalation.escalationCode } : {}
846
+ }));
847
+ if (escalations.length > 0) definitions.escalations = escalations;
848
+ const itemDefinitions = asElements(rootElement.itemDefinitions).filter((item) => asString(item.id)).map((item) => ({
849
+ id: String(item.id).replace(/_item$/, ""),
850
+ name: String(item.id).replace(/_item$/, ""),
851
+ type: parseVariableType(item.structureRef)
852
+ }));
853
+ if (itemDefinitions.length > 0) definitions.variables = itemDefinitions;
854
+ return Object.keys(definitions).length > 0 ? definitions : void 0;
855
+ }
781
856
  function extractSubProcessVariant(el) {
782
857
  if (el.$type === "bpmn:Transaction") return "transaction";
783
858
  if (el.$type === "bpmn:AdHocSubProcess") return "adhoc";
@@ -818,6 +893,7 @@ function buildNode(el, elementType, parentId, ctx, nodes) {
818
893
  const label = asString(el.name);
819
894
  const documentation = extractDocumentation(el);
820
895
  const trigger = extractTrigger(el);
896
+ const eventDefinition = extractEventDefinition(el);
821
897
  const isNonInterrupting = el.cancelActivity === false;
822
898
  const attachedToRef = el.attachedToRef?.id;
823
899
  const data = {
@@ -825,8 +901,17 @@ function buildNode(el, elementType, parentId, ctx, nodes) {
825
901
  ...label ? { label } : {},
826
902
  ...documentation ? { documentation } : {},
827
903
  ...trigger ? { trigger } : {},
904
+ ...eventDefinition ? { eventDefinition } : {},
828
905
  ...isNonInterrupting ? { isNonInterrupting: true } : {},
829
- ...attachedToRef ? { attachedToRef } : {}
906
+ ...elementType === "BoundaryEvent" ? { cancelActivity: el.cancelActivity !== false } : {},
907
+ ...attachedToRef ? { attachedToRef } : {},
908
+ ...eventDefinition?.timer ? { timer: eventDefinition.timer, timerExpression: eventDefinition.timer } : {},
909
+ ...eventDefinition?.messageRef ? { messageRef: eventDefinition.messageRef } : {},
910
+ ...eventDefinition?.signalRef ? { signalRef: eventDefinition.signalRef } : {},
911
+ ...eventDefinition?.errorRef ? { errorRef: eventDefinition.errorRef } : {},
912
+ ...eventDefinition?.escalationRef ? { escalationRef: eventDefinition.escalationRef } : {},
913
+ ...eventDefinition?.conditionExpression ? { conditionExpression: eventDefinition.conditionExpression } : {},
914
+ ...eventDefinition?.linkName ? { linkName: eventDefinition.linkName } : {}
830
915
  };
831
916
  if (elementType === "SubProcess" || elementType === "Transaction" || elementType === "EventSubProcess" || elementType === "AdHocSubProcess") {
832
917
  const variant = extractSubProcessVariant(el);
@@ -976,10 +1061,23 @@ async function parseBpmnXml(xml) {
976
1061
  };
977
1062
  return { ...edge, data };
978
1063
  });
1064
+ const firstProcess = asElements(rootElement.rootElements).find((rootEl) => rootEl.$type === "bpmn:Process");
1065
+ const importedDefinitions = extractDefinitions(rootElement);
1066
+ const process = firstProcess ? (() => {
1067
+ const importedProcess = {};
1068
+ const processId = asString(firstProcess.id);
1069
+ const documentation = extractDocumentation(firstProcess);
1070
+ if (processId) importedProcess.processId = processId;
1071
+ if (typeof firstProcess.isExecutable === "boolean") importedProcess.executable = firstProcess.isExecutable;
1072
+ if (documentation) importedProcess.documentation = documentation;
1073
+ if (importedDefinitions) importedProcess.definitions = importedDefinitions;
1074
+ return importedProcess;
1075
+ })() : importedDefinitions ? { definitions: importedDefinitions } : void 0;
979
1076
  return {
980
1077
  nodes: normalizeChildPositions(nodes, nodeIds),
981
1078
  edges: normalizedEdges,
982
- warnings
1079
+ warnings,
1080
+ ...process ? { process } : {}
983
1081
  };
984
1082
  }
985
1083
  function collectDefaultFlows(el, defaultFlowById) {
@@ -1009,6 +1107,99 @@ function uid(prefix, id) {
1009
1107
  function asNodes(nodes, types) {
1010
1108
  return nodes.filter((n) => types.includes(n.data.elementType));
1011
1109
  }
1110
+ function normalizeEventDefinition(data) {
1111
+ const trigger = data.eventDefinition?.type ?? data.trigger;
1112
+ if (!trigger || trigger === "none") return void 0;
1113
+ const normalized = { type: trigger };
1114
+ const timer = data.eventDefinition?.timer ?? data.timer ?? data.timerExpression;
1115
+ const messageRef = data.eventDefinition?.messageRef ?? (typeof data.messageRef === "string" ? data.messageRef : void 0);
1116
+ const signalRef = data.eventDefinition?.signalRef ?? (typeof data.signalRef === "string" ? data.signalRef : void 0);
1117
+ const errorRef = data.eventDefinition?.errorRef ?? (typeof data.errorRef === "string" ? data.errorRef : void 0);
1118
+ const escalationRef = data.eventDefinition?.escalationRef ?? (typeof data.escalationRef === "string" ? data.escalationRef : void 0);
1119
+ const conditionExpression = data.eventDefinition?.conditionExpression ?? (typeof data.conditionExpression === "string" ? data.conditionExpression : void 0);
1120
+ const linkName = data.eventDefinition?.linkName ?? (typeof data.linkName === "string" ? data.linkName : void 0);
1121
+ if (timer) normalized.timer = timer;
1122
+ if (messageRef) normalized.messageRef = messageRef;
1123
+ if (signalRef) normalized.signalRef = signalRef;
1124
+ if (errorRef) normalized.errorRef = errorRef;
1125
+ if (escalationRef) normalized.escalationRef = escalationRef;
1126
+ if (conditionExpression) normalized.conditionExpression = conditionExpression;
1127
+ if (linkName) normalized.linkName = linkName;
1128
+ return normalized;
1129
+ }
1130
+ function parseVariableType2(type) {
1131
+ switch (type) {
1132
+ case "integer":
1133
+ return "xsd:int";
1134
+ case "boolean":
1135
+ return "xsd:boolean";
1136
+ case "date":
1137
+ return "xsd:dateTime";
1138
+ case "object":
1139
+ return "xsd:anyType";
1140
+ case "array":
1141
+ return "xsd:anyType";
1142
+ default:
1143
+ return "xsd:string";
1144
+ }
1145
+ }
1146
+ function buildGlobalDefinitions(moddle, definitionsSet) {
1147
+ if (!definitionsSet) return [];
1148
+ const rootElements = [];
1149
+ for (const message of definitionsSet.messages ?? []) {
1150
+ rootElements.push(moddle.create("bpmn:Message", { id: message.id, name: message.name }));
1151
+ }
1152
+ for (const signal of definitionsSet.signals ?? []) {
1153
+ rootElements.push(moddle.create("bpmn:Signal", { id: signal.id, name: signal.name }));
1154
+ }
1155
+ for (const error of definitionsSet.errors ?? []) {
1156
+ rootElements.push(moddle.create("bpmn:Error", {
1157
+ id: error.id,
1158
+ name: error.name,
1159
+ ...error.errorCode ? { errorCode: error.errorCode } : {}
1160
+ }));
1161
+ }
1162
+ for (const escalation of definitionsSet.escalations ?? []) {
1163
+ rootElements.push(moddle.create("bpmn:Escalation", {
1164
+ id: escalation.id,
1165
+ name: escalation.name,
1166
+ ...escalation.escalationCode ? { escalationCode: escalation.escalationCode } : {}
1167
+ }));
1168
+ }
1169
+ return rootElements;
1170
+ }
1171
+ function buildItemDefinitions(moddle, variables) {
1172
+ return (variables ?? []).map(
1173
+ (variable) => moddle.create("bpmn:ItemDefinition", {
1174
+ id: `${variable.id}_item`,
1175
+ itemKind: "Information",
1176
+ structureRef: parseVariableType2(variable.type)
1177
+ })
1178
+ );
1179
+ }
1180
+ function buildEventDefinitions(moddle, node, eventDefinition) {
1181
+ if (eventDefinition.type === "multiple" || eventDefinition.type === "parallelMultiple") {
1182
+ return [];
1183
+ }
1184
+ const defType = TRIGGER_TO_EVENT_DEF[eventDefinition.type];
1185
+ if (!defType) return [];
1186
+ const attrs = { id: uid("EventDef", node.id) };
1187
+ if (eventDefinition.messageRef) attrs.messageRef = { id: eventDefinition.messageRef };
1188
+ if (eventDefinition.signalRef) attrs.signalRef = { id: eventDefinition.signalRef };
1189
+ if (eventDefinition.errorRef) attrs.errorRef = { id: eventDefinition.errorRef };
1190
+ if (eventDefinition.escalationRef) attrs.escalationRef = { id: eventDefinition.escalationRef };
1191
+ if (eventDefinition.conditionExpression) {
1192
+ attrs.condition = moddle.create("bpmn:FormalExpression", {
1193
+ body: eventDefinition.conditionExpression
1194
+ });
1195
+ }
1196
+ if (eventDefinition.timer?.value) {
1197
+ attrs[eventDefinition.timer.kind === "date" ? "timeDate" : eventDefinition.timer.kind === "cycle" ? "timeCycle" : "timeDuration"] = moddle.create("bpmn:FormalExpression", {
1198
+ body: eventDefinition.timer.value
1199
+ });
1200
+ }
1201
+ return [moddle.create(defType, attrs)];
1202
+ }
1012
1203
  function buildSemanticModel(moddle, nodes, edges, opts) {
1013
1204
  const defId = opts.id ?? "Definitions_1";
1014
1205
  const defName = opts.name;
@@ -1020,13 +1211,19 @@ function buildSemanticModel(moddle, nodes, edges, opts) {
1020
1211
  targetNamespace: "http://bpmn.io/schema/bpmn",
1021
1212
  ...defName ? { name: defName } : {}
1022
1213
  });
1023
- const rootElements = [];
1214
+ const rootElements = [
1215
+ ...buildGlobalDefinitions(moddle, opts.process?.definitions)
1216
+ ];
1217
+ const itemDefinitions = buildItemDefinitions(moddle, opts.process?.definitions?.variables);
1218
+ if (itemDefinitions.length > 0) {
1219
+ definitions.itemDefinitions = itemDefinitions;
1220
+ }
1024
1221
  if (isCollaboration) {
1025
1222
  const participants = [];
1026
1223
  const processes = [];
1027
1224
  for (const pool of poolNodes) {
1028
1225
  const processId = `Process_${pool.id}`;
1029
- const process = buildProcess(moddle, nodes, edges, pool.id, laneNodes, processId);
1226
+ const process = buildProcess(moddle, nodes, edges, pool.id, laneNodes, processId, opts);
1030
1227
  processes.push(process);
1031
1228
  const participant = moddle.create("bpmn:Participant", {
1032
1229
  id: pool.id,
@@ -1060,13 +1257,21 @@ function buildSemanticModel(moddle, nodes, edges, opts) {
1060
1257
  });
1061
1258
  rootElements.push(collaboration, ...processes);
1062
1259
  } else {
1063
- const process = buildProcess(moddle, nodes, edges, void 0, laneNodes, "Process_1");
1260
+ const process = buildProcess(
1261
+ moddle,
1262
+ nodes,
1263
+ edges,
1264
+ void 0,
1265
+ laneNodes,
1266
+ opts.process?.processId ?? "Process_1",
1267
+ opts
1268
+ );
1064
1269
  rootElements.push(process);
1065
1270
  }
1066
1271
  definitions.rootElements = rootElements;
1067
1272
  return definitions;
1068
1273
  }
1069
- function buildProcess(moddle, allNodes, allEdges, poolId, laneNodes, processId) {
1274
+ function buildProcess(moddle, allNodes, allEdges, poolId, laneNodes, processId, opts) {
1070
1275
  const subProcessIds = new Set(
1071
1276
  allNodes.filter(
1072
1277
  (n) => n.data.elementType === "SubProcess" || n.data.elementType === "Transaction" || n.data.elementType === "EventSubProcess" || n.data.elementType === "AdHocSubProcess"
@@ -1116,9 +1321,14 @@ function buildProcess(moddle, allNodes, allEdges, poolId, laneNodes, processId)
1116
1321
  }
1117
1322
  const process = moddle.create("bpmn:Process", {
1118
1323
  id: processId,
1119
- isExecutable: true,
1324
+ isExecutable: opts.process?.executable ?? true,
1120
1325
  flowElements
1121
1326
  });
1327
+ if (opts.process?.documentation) {
1328
+ process.documentation = [
1329
+ moddle.create("bpmn:Documentation", { text: opts.process.documentation })
1330
+ ];
1331
+ }
1122
1332
  if (myLanes.length > 0) {
1123
1333
  const laneElements = myLanes.map(
1124
1334
  (l) => moddle.create("bpmn:Lane", {
@@ -1135,7 +1345,8 @@ function buildProcess(moddle, allNodes, allEdges, poolId, laneNodes, processId)
1135
1345
  return process;
1136
1346
  }
1137
1347
  function buildFlowElement(moddle, node, allNodes, allEdges) {
1138
- const { elementType, label, trigger } = node.data;
1348
+ const { elementType, label } = node.data;
1349
+ const eventDefinition = normalizeEventDefinition(node.data);
1139
1350
  if (elementType === "Pool" || elementType === "Lane") return null;
1140
1351
  const moddleType = ELEMENT_TYPE_TO_MODDLE[elementType];
1141
1352
  if (!moddleType) return null;
@@ -1148,15 +1359,15 @@ function buildFlowElement(moddle, node, allNodes, allEdges) {
1148
1359
  moddle.create("bpmn:Documentation", { text: node.data.documentation })
1149
1360
  ];
1150
1361
  }
1151
- if (trigger && trigger !== "none") {
1152
- const defType = TRIGGER_TO_EVENT_DEF[trigger];
1153
- if (defType) {
1154
- attrs.eventDefinitions = [moddle.create(defType, { id: uid("EventDef", node.id) })];
1155
- }
1362
+ if (eventDefinition) {
1363
+ const eventDefinitions = buildEventDefinitions(moddle, node, eventDefinition);
1364
+ if (eventDefinitions.length > 0) attrs.eventDefinitions = eventDefinitions;
1365
+ if (eventDefinition.linkName) attrs.name = eventDefinition.linkName;
1156
1366
  }
1157
1367
  if (elementType === "BoundaryEvent") {
1158
1368
  attrs.attachedToRef = { id: node.data.attachedToRef ?? node.parentId };
1159
- attrs.cancelActivity = node.data.isNonInterrupting ? false : true;
1369
+ const interrupting = node.data.cancelActivity ?? !node.data.isNonInterrupting;
1370
+ attrs.cancelActivity = interrupting;
1160
1371
  }
1161
1372
  const isSubProcess = elementType === "SubProcess" || elementType === "Transaction" || elementType === "EventSubProcess" || elementType === "AdHocSubProcess";
1162
1373
  if (isSubProcess) {