@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.
package/dist/index.cjs CHANGED
@@ -2674,9 +2674,84 @@ function extractLaneMembership(process) {
2674
2674
  function extractTrigger(el) {
2675
2675
  const defs = asElements(el.eventDefinitions);
2676
2676
  if (defs.length === 0) return void 0;
2677
- if (defs.length > 1) return el.$type.includes("Parallel") ? "parallelMultiple" : "multiple";
2677
+ if (defs.length > 1) {
2678
+ return defs.some((def) => def.$type === "bpmn:ParallelMultipleEventDefinition") ? "parallelMultiple" : "multiple";
2679
+ }
2678
2680
  return EVENT_DEF_TO_TRIGGER[defs[0].$type];
2679
2681
  }
2682
+ function parseVariableType(value) {
2683
+ const text = asString(value) ?? "";
2684
+ if (text.includes("int")) return "integer";
2685
+ if (text.includes("boolean")) return "boolean";
2686
+ if (text.includes("date")) return "date";
2687
+ if (text.includes("array")) return "array";
2688
+ if (text.includes("anyType")) return "object";
2689
+ return "string";
2690
+ }
2691
+ function extractEventDefinition(el) {
2692
+ const defs = asElements(el.eventDefinitions);
2693
+ if (defs.length === 0) return void 0;
2694
+ if (defs.length > 1) {
2695
+ return {
2696
+ type: defs.some((def2) => def2.$type === "bpmn:ParallelMultipleEventDefinition") ? "parallelMultiple" : "multiple"
2697
+ };
2698
+ }
2699
+ const [def] = defs;
2700
+ const type = EVENT_DEF_TO_TRIGGER[def.$type];
2701
+ if (!type) return void 0;
2702
+ const eventDefinition = {
2703
+ type,
2704
+ ...asString(def.messageRef?.id) ? { messageRef: def.messageRef.id } : {},
2705
+ ...asString(def.signalRef?.id) ? { signalRef: def.signalRef.id } : {},
2706
+ ...asString(def.errorRef?.id) ? { errorRef: def.errorRef.id } : {},
2707
+ ...asString(def.escalationRef?.id) ? { escalationRef: def.escalationRef.id } : {},
2708
+ ...asString(def.condition?.body) ? { conditionExpression: def.condition.body } : {}
2709
+ };
2710
+ const timeDate = asString(def.timeDate?.body);
2711
+ const timeDuration = asString(def.timeDuration?.body);
2712
+ const timeCycle = asString(def.timeCycle?.body);
2713
+ if (timeDate) eventDefinition.timer = { kind: "date", value: timeDate };
2714
+ else if (timeCycle) eventDefinition.timer = { kind: "cycle", value: timeCycle };
2715
+ else if (timeDuration) eventDefinition.timer = { kind: "duration", value: timeDuration };
2716
+ if (type === "link") {
2717
+ const linkName = asString(el.name) ?? asString(def.name);
2718
+ if (linkName) eventDefinition.linkName = linkName;
2719
+ }
2720
+ return eventDefinition;
2721
+ }
2722
+ function extractDefinitions(rootElement) {
2723
+ const definitions = {};
2724
+ const rootElements = asElements(rootElement.rootElements);
2725
+ const messages = rootElements.filter((element) => element.$type === "bpmn:Message" && asString(element.id)).map((message) => ({
2726
+ id: message.id,
2727
+ name: asString(message.name) ?? message.id
2728
+ }));
2729
+ if (messages.length > 0) definitions.messages = messages;
2730
+ const signals = rootElements.filter((element) => element.$type === "bpmn:Signal" && asString(element.id)).map((signal) => ({
2731
+ id: signal.id,
2732
+ name: asString(signal.name) ?? signal.id
2733
+ }));
2734
+ if (signals.length > 0) definitions.signals = signals;
2735
+ const errors = rootElements.filter((element) => element.$type === "bpmn:Error" && asString(element.id)).map((error) => ({
2736
+ id: error.id,
2737
+ name: asString(error.name) ?? error.id,
2738
+ ...asString(error.errorCode) ? { errorCode: error.errorCode } : {}
2739
+ }));
2740
+ if (errors.length > 0) definitions.errors = errors;
2741
+ const escalations = rootElements.filter((element) => element.$type === "bpmn:Escalation" && asString(element.id)).map((escalation) => ({
2742
+ id: escalation.id,
2743
+ name: asString(escalation.name) ?? escalation.id,
2744
+ ...asString(escalation.escalationCode) ? { escalationCode: escalation.escalationCode } : {}
2745
+ }));
2746
+ if (escalations.length > 0) definitions.escalations = escalations;
2747
+ const itemDefinitions = asElements(rootElement.itemDefinitions).filter((item) => asString(item.id)).map((item) => ({
2748
+ id: String(item.id).replace(/_item$/, ""),
2749
+ name: String(item.id).replace(/_item$/, ""),
2750
+ type: parseVariableType(item.structureRef)
2751
+ }));
2752
+ if (itemDefinitions.length > 0) definitions.variables = itemDefinitions;
2753
+ return Object.keys(definitions).length > 0 ? definitions : void 0;
2754
+ }
2680
2755
  function extractSubProcessVariant(el) {
2681
2756
  if (el.$type === "bpmn:Transaction") return "transaction";
2682
2757
  if (el.$type === "bpmn:AdHocSubProcess") return "adhoc";
@@ -2717,6 +2792,7 @@ function buildNode(el, elementType, parentId, ctx, nodes) {
2717
2792
  const label = asString(el.name);
2718
2793
  const documentation = extractDocumentation(el);
2719
2794
  const trigger = extractTrigger(el);
2795
+ const eventDefinition = extractEventDefinition(el);
2720
2796
  const isNonInterrupting = el.cancelActivity === false;
2721
2797
  const attachedToRef = el.attachedToRef?.id;
2722
2798
  const data = {
@@ -2724,8 +2800,17 @@ function buildNode(el, elementType, parentId, ctx, nodes) {
2724
2800
  ...label ? { label } : {},
2725
2801
  ...documentation ? { documentation } : {},
2726
2802
  ...trigger ? { trigger } : {},
2803
+ ...eventDefinition ? { eventDefinition } : {},
2727
2804
  ...isNonInterrupting ? { isNonInterrupting: true } : {},
2728
- ...attachedToRef ? { attachedToRef } : {}
2805
+ ...elementType === "BoundaryEvent" ? { cancelActivity: el.cancelActivity !== false } : {},
2806
+ ...attachedToRef ? { attachedToRef } : {},
2807
+ ...eventDefinition?.timer ? { timer: eventDefinition.timer, timerExpression: eventDefinition.timer } : {},
2808
+ ...eventDefinition?.messageRef ? { messageRef: eventDefinition.messageRef } : {},
2809
+ ...eventDefinition?.signalRef ? { signalRef: eventDefinition.signalRef } : {},
2810
+ ...eventDefinition?.errorRef ? { errorRef: eventDefinition.errorRef } : {},
2811
+ ...eventDefinition?.escalationRef ? { escalationRef: eventDefinition.escalationRef } : {},
2812
+ ...eventDefinition?.conditionExpression ? { conditionExpression: eventDefinition.conditionExpression } : {},
2813
+ ...eventDefinition?.linkName ? { linkName: eventDefinition.linkName } : {}
2729
2814
  };
2730
2815
  if (elementType === "SubProcess" || elementType === "Transaction" || elementType === "EventSubProcess" || elementType === "AdHocSubProcess") {
2731
2816
  const variant = extractSubProcessVariant(el);
@@ -2875,10 +2960,23 @@ async function parseBpmnXml(xml) {
2875
2960
  };
2876
2961
  return { ...edge, data };
2877
2962
  });
2963
+ const firstProcess = asElements(rootElement.rootElements).find((rootEl) => rootEl.$type === "bpmn:Process");
2964
+ const importedDefinitions = extractDefinitions(rootElement);
2965
+ const process = firstProcess ? (() => {
2966
+ const importedProcess = {};
2967
+ const processId = asString(firstProcess.id);
2968
+ const documentation = extractDocumentation(firstProcess);
2969
+ if (processId) importedProcess.processId = processId;
2970
+ if (typeof firstProcess.isExecutable === "boolean") importedProcess.executable = firstProcess.isExecutable;
2971
+ if (documentation) importedProcess.documentation = documentation;
2972
+ if (importedDefinitions) importedProcess.definitions = importedDefinitions;
2973
+ return importedProcess;
2974
+ })() : importedDefinitions ? { definitions: importedDefinitions } : void 0;
2878
2975
  return {
2879
2976
  nodes: normalizeChildPositions(nodes, nodeIds),
2880
2977
  edges: normalizedEdges,
2881
- warnings
2978
+ warnings,
2979
+ ...process ? { process } : {}
2882
2980
  };
2883
2981
  }
2884
2982
  function collectDefaultFlows(el, defaultFlowById) {
@@ -2908,6 +3006,99 @@ function uid(prefix, id) {
2908
3006
  function asNodes(nodes, types) {
2909
3007
  return nodes.filter((n) => types.includes(n.data.elementType));
2910
3008
  }
3009
+ function normalizeEventDefinition(data) {
3010
+ const trigger = data.eventDefinition?.type ?? data.trigger;
3011
+ if (!trigger || trigger === "none") return void 0;
3012
+ const normalized = { type: trigger };
3013
+ const timer = data.eventDefinition?.timer ?? data.timer ?? data.timerExpression;
3014
+ const messageRef = data.eventDefinition?.messageRef ?? (typeof data.messageRef === "string" ? data.messageRef : void 0);
3015
+ const signalRef = data.eventDefinition?.signalRef ?? (typeof data.signalRef === "string" ? data.signalRef : void 0);
3016
+ const errorRef = data.eventDefinition?.errorRef ?? (typeof data.errorRef === "string" ? data.errorRef : void 0);
3017
+ const escalationRef = data.eventDefinition?.escalationRef ?? (typeof data.escalationRef === "string" ? data.escalationRef : void 0);
3018
+ const conditionExpression = data.eventDefinition?.conditionExpression ?? (typeof data.conditionExpression === "string" ? data.conditionExpression : void 0);
3019
+ const linkName = data.eventDefinition?.linkName ?? (typeof data.linkName === "string" ? data.linkName : void 0);
3020
+ if (timer) normalized.timer = timer;
3021
+ if (messageRef) normalized.messageRef = messageRef;
3022
+ if (signalRef) normalized.signalRef = signalRef;
3023
+ if (errorRef) normalized.errorRef = errorRef;
3024
+ if (escalationRef) normalized.escalationRef = escalationRef;
3025
+ if (conditionExpression) normalized.conditionExpression = conditionExpression;
3026
+ if (linkName) normalized.linkName = linkName;
3027
+ return normalized;
3028
+ }
3029
+ function parseVariableType2(type) {
3030
+ switch (type) {
3031
+ case "integer":
3032
+ return "xsd:int";
3033
+ case "boolean":
3034
+ return "xsd:boolean";
3035
+ case "date":
3036
+ return "xsd:dateTime";
3037
+ case "object":
3038
+ return "xsd:anyType";
3039
+ case "array":
3040
+ return "xsd:anyType";
3041
+ default:
3042
+ return "xsd:string";
3043
+ }
3044
+ }
3045
+ function buildGlobalDefinitions(moddle, definitionsSet) {
3046
+ if (!definitionsSet) return [];
3047
+ const rootElements = [];
3048
+ for (const message of definitionsSet.messages ?? []) {
3049
+ rootElements.push(moddle.create("bpmn:Message", { id: message.id, name: message.name }));
3050
+ }
3051
+ for (const signal of definitionsSet.signals ?? []) {
3052
+ rootElements.push(moddle.create("bpmn:Signal", { id: signal.id, name: signal.name }));
3053
+ }
3054
+ for (const error of definitionsSet.errors ?? []) {
3055
+ rootElements.push(moddle.create("bpmn:Error", {
3056
+ id: error.id,
3057
+ name: error.name,
3058
+ ...error.errorCode ? { errorCode: error.errorCode } : {}
3059
+ }));
3060
+ }
3061
+ for (const escalation of definitionsSet.escalations ?? []) {
3062
+ rootElements.push(moddle.create("bpmn:Escalation", {
3063
+ id: escalation.id,
3064
+ name: escalation.name,
3065
+ ...escalation.escalationCode ? { escalationCode: escalation.escalationCode } : {}
3066
+ }));
3067
+ }
3068
+ return rootElements;
3069
+ }
3070
+ function buildItemDefinitions(moddle, variables) {
3071
+ return (variables ?? []).map(
3072
+ (variable) => moddle.create("bpmn:ItemDefinition", {
3073
+ id: `${variable.id}_item`,
3074
+ itemKind: "Information",
3075
+ structureRef: parseVariableType2(variable.type)
3076
+ })
3077
+ );
3078
+ }
3079
+ function buildEventDefinitions(moddle, node, eventDefinition) {
3080
+ if (eventDefinition.type === "multiple" || eventDefinition.type === "parallelMultiple") {
3081
+ return [];
3082
+ }
3083
+ const defType = TRIGGER_TO_EVENT_DEF[eventDefinition.type];
3084
+ if (!defType) return [];
3085
+ const attrs = { id: uid("EventDef", node.id) };
3086
+ if (eventDefinition.messageRef) attrs.messageRef = { id: eventDefinition.messageRef };
3087
+ if (eventDefinition.signalRef) attrs.signalRef = { id: eventDefinition.signalRef };
3088
+ if (eventDefinition.errorRef) attrs.errorRef = { id: eventDefinition.errorRef };
3089
+ if (eventDefinition.escalationRef) attrs.escalationRef = { id: eventDefinition.escalationRef };
3090
+ if (eventDefinition.conditionExpression) {
3091
+ attrs.condition = moddle.create("bpmn:FormalExpression", {
3092
+ body: eventDefinition.conditionExpression
3093
+ });
3094
+ }
3095
+ if (eventDefinition.timer?.value) {
3096
+ attrs[eventDefinition.timer.kind === "date" ? "timeDate" : eventDefinition.timer.kind === "cycle" ? "timeCycle" : "timeDuration"] = moddle.create("bpmn:FormalExpression", {
3097
+ body: eventDefinition.timer.value
3098
+ });
3099
+ }
3100
+ return [moddle.create(defType, attrs)];
3101
+ }
2911
3102
  function buildSemanticModel(moddle, nodes, edges, opts) {
2912
3103
  const defId = opts.id ?? "Definitions_1";
2913
3104
  const defName = opts.name;
@@ -2919,13 +3110,19 @@ function buildSemanticModel(moddle, nodes, edges, opts) {
2919
3110
  targetNamespace: "http://bpmn.io/schema/bpmn",
2920
3111
  ...defName ? { name: defName } : {}
2921
3112
  });
2922
- const rootElements = [];
3113
+ const rootElements = [
3114
+ ...buildGlobalDefinitions(moddle, opts.process?.definitions)
3115
+ ];
3116
+ const itemDefinitions = buildItemDefinitions(moddle, opts.process?.definitions?.variables);
3117
+ if (itemDefinitions.length > 0) {
3118
+ definitions.itemDefinitions = itemDefinitions;
3119
+ }
2923
3120
  if (isCollaboration) {
2924
3121
  const participants = [];
2925
3122
  const processes = [];
2926
3123
  for (const pool of poolNodes) {
2927
3124
  const processId = `Process_${pool.id}`;
2928
- const process = buildProcess(moddle, nodes, edges, pool.id, laneNodes, processId);
3125
+ const process = buildProcess(moddle, nodes, edges, pool.id, laneNodes, processId, opts);
2929
3126
  processes.push(process);
2930
3127
  const participant = moddle.create("bpmn:Participant", {
2931
3128
  id: pool.id,
@@ -2959,13 +3156,21 @@ function buildSemanticModel(moddle, nodes, edges, opts) {
2959
3156
  });
2960
3157
  rootElements.push(collaboration, ...processes);
2961
3158
  } else {
2962
- const process = buildProcess(moddle, nodes, edges, void 0, laneNodes, "Process_1");
3159
+ const process = buildProcess(
3160
+ moddle,
3161
+ nodes,
3162
+ edges,
3163
+ void 0,
3164
+ laneNodes,
3165
+ opts.process?.processId ?? "Process_1",
3166
+ opts
3167
+ );
2963
3168
  rootElements.push(process);
2964
3169
  }
2965
3170
  definitions.rootElements = rootElements;
2966
3171
  return definitions;
2967
3172
  }
2968
- function buildProcess(moddle, allNodes, allEdges, poolId, laneNodes, processId) {
3173
+ function buildProcess(moddle, allNodes, allEdges, poolId, laneNodes, processId, opts) {
2969
3174
  const subProcessIds = new Set(
2970
3175
  allNodes.filter(
2971
3176
  (n) => n.data.elementType === "SubProcess" || n.data.elementType === "Transaction" || n.data.elementType === "EventSubProcess" || n.data.elementType === "AdHocSubProcess"
@@ -3015,9 +3220,14 @@ function buildProcess(moddle, allNodes, allEdges, poolId, laneNodes, processId)
3015
3220
  }
3016
3221
  const process = moddle.create("bpmn:Process", {
3017
3222
  id: processId,
3018
- isExecutable: true,
3223
+ isExecutable: opts.process?.executable ?? true,
3019
3224
  flowElements
3020
3225
  });
3226
+ if (opts.process?.documentation) {
3227
+ process.documentation = [
3228
+ moddle.create("bpmn:Documentation", { text: opts.process.documentation })
3229
+ ];
3230
+ }
3021
3231
  if (myLanes.length > 0) {
3022
3232
  const laneElements = myLanes.map(
3023
3233
  (l) => moddle.create("bpmn:Lane", {
@@ -3034,7 +3244,8 @@ function buildProcess(moddle, allNodes, allEdges, poolId, laneNodes, processId)
3034
3244
  return process;
3035
3245
  }
3036
3246
  function buildFlowElement(moddle, node, allNodes, allEdges) {
3037
- const { elementType, label, trigger } = node.data;
3247
+ const { elementType, label } = node.data;
3248
+ const eventDefinition = normalizeEventDefinition(node.data);
3038
3249
  if (elementType === "Pool" || elementType === "Lane") return null;
3039
3250
  const moddleType = ELEMENT_TYPE_TO_MODDLE[elementType];
3040
3251
  if (!moddleType) return null;
@@ -3047,15 +3258,15 @@ function buildFlowElement(moddle, node, allNodes, allEdges) {
3047
3258
  moddle.create("bpmn:Documentation", { text: node.data.documentation })
3048
3259
  ];
3049
3260
  }
3050
- if (trigger && trigger !== "none") {
3051
- const defType = TRIGGER_TO_EVENT_DEF[trigger];
3052
- if (defType) {
3053
- attrs.eventDefinitions = [moddle.create(defType, { id: uid("EventDef", node.id) })];
3054
- }
3261
+ if (eventDefinition) {
3262
+ const eventDefinitions = buildEventDefinitions(moddle, node, eventDefinition);
3263
+ if (eventDefinitions.length > 0) attrs.eventDefinitions = eventDefinitions;
3264
+ if (eventDefinition.linkName) attrs.name = eventDefinition.linkName;
3055
3265
  }
3056
3266
  if (elementType === "BoundaryEvent") {
3057
3267
  attrs.attachedToRef = { id: node.data.attachedToRef ?? node.parentId };
3058
- attrs.cancelActivity = node.data.isNonInterrupting ? false : true;
3268
+ const interrupting = node.data.cancelActivity ?? !node.data.isNonInterrupting;
3269
+ attrs.cancelActivity = interrupting;
3059
3270
  }
3060
3271
  const isSubProcess3 = elementType === "SubProcess" || elementType === "Transaction" || elementType === "EventSubProcess" || elementType === "AdHocSubProcess";
3061
3272
  if (isSubProcess3) {