All files / src bpmn-builder.ts

100% Statements 143/143
100% Branches 58/58
100% Functions 28/28
100% Lines 142/142

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                          51x 51x     51x 51x       51x       51x 51x 51x       2x       1x       1x       2x         2x 2x 2x 2x 2x       46x 42x   4x 4x         4x 4x 4x       49x 47x   2x 2x         2x 2x 2x       1x 1x 1x 1x       98x       32x       7x       1x       2x       9x 9x         9x 9x 9x               7x       7x 7x 7x   7x 5x 5x 5x 5x     7x               4x         4x 4x 4x   4x 3x 3x 3x 3x     4x       8x 8x 2x   8x 8x 8x 8x       4x 4x 4x 4x       6x 4x   6x 6x 6x 6x       14x 2x   14x 14x 14x         14x 14x 14x               196x 196x 196x 196x 196x   196x 196x 196x         196x 1x   196x 196x 196x 196x       14x 7x       7x   56x 14x         14x 14x       24x 8x         8x   24x       13x 13x 13x         13x 13x 13x               11x 11x 11x 11x 11x 11x   11x 11x 11x         11x 1x   11x 11x 11x 11x       50x 50x       229x 229x 229x 229x      
import { BpmnModdle, type BPMNModdle } from 'bpmn-moddle';
 
export interface FlowConfig {
  id: string;
  sourceRef: string;
  targetRef: string;
  name?: string;
}
 
export class BpmnBuilder {
  private moddle: BPMNModdle;
  private definitions: any;
  private process: any;
  private collaboration: any | null = null;
  private elementMap: Map<string, any> = new Map();
 
  constructor(processId = 'Process_1', definitionsId = 'Definitions_1') {
    this.moddle = new BpmnModdle();
    this.definitions = this.moddle.create('bpmn:Definitions', {
      id: definitionsId,
      targetNamespace: 'http://bpmn.io/schema/bpmn',
    });
    this.process = this.moddle.create('bpmn:Process', {
      id: processId,
      isExecutable: false,
    });
    this.process.flowElements = [];
    this.definitions.rootElements = [this.process];
    this.elementMap.set(processId, this.process);
  }
 
  public getModdle(): BPMNModdle {
    return this.moddle;
  }
 
  public getDefinitions(): any {
    return this.definitions;
  }
 
  public getProcess(): any {
    return this.process;
  }
 
  public addProcess(id: string, name?: string): this {
    const proc = this.moddle.create('bpmn:Process', {
      id,
      name,
      isExecutable: false,
    });
    proc.flowElements = [];
    this.definitions.rootElements.push(proc);
    this.elementMap.set(id, proc);
    this.process = proc;
    return this;
  }
 
  public addStartEvent(id: string, name?: string, eventDefinitionType?: string): this {
    if (!eventDefinitionType) {
      return this.addFlowNode('bpmn:StartEvent', id, name);
    }
    const def = this.moddle.create(eventDefinitionType);
    const element = this.moddle.create('bpmn:StartEvent', {
      id,
      name,
      eventDefinitions: [def],
    });
    this.process.flowElements.push(element);
    this.elementMap.set(id, element);
    return this;
  }
 
  public addEndEvent(id: string, name?: string, eventDefinitionType?: string): this {
    if (!eventDefinitionType) {
      return this.addFlowNode('bpmn:EndEvent', id, name);
    }
    const def = this.moddle.create(eventDefinitionType);
    const element = this.moddle.create('bpmn:EndEvent', {
      id,
      name,
      eventDefinitions: [def],
    });
    this.process.flowElements.push(element);
    this.elementMap.set(id, element);
    return this;
  }
 
  public addDataObject(id: string, name?: string): this {
    const element = this.moddle.create('bpmn:DataObject', { id, name });
    this.process.flowElements.push(element);
    this.elementMap.set(id, element);
    return this;
  }
 
  public addTask(id: string, name?: string, type = 'bpmn:Task'): this {
    return this.addFlowNode(type, id, name);
  }
 
  public addExclusiveGateway(id: string, name?: string): this {
    return this.addFlowNode('bpmn:ExclusiveGateway', id, name);
  }
 
  public addParallelGateway(id: string, name?: string): this {
    return this.addFlowNode('bpmn:ParallelGateway', id, name);
  }
 
  public addInclusiveGateway(id: string, name?: string): this {
    return this.addFlowNode('bpmn:InclusiveGateway', id, name);
  }
 
  public addIntermediateCatchEvent(id: string, name?: string): this {
    return this.addFlowNode('bpmn:IntermediateCatchEvent', id, name);
  }
 
  public addBoundaryEvent(id: string, attachedToRef: string, name?: string): this {
    const host = this.elementMap.get(attachedToRef);
    const event = this.moddle.create('bpmn:BoundaryEvent', {
      id,
      name,
      attachedToRef: host || attachedToRef,
    });
    this.process.flowElements.push(event);
    this.elementMap.set(id, event);
    return this;
  }
 
  public addSubProcess(
    id: string,
    name?: string,
    configure?: (builder: BpmnBuilder) => void
  ): this {
    const subProcess = this.moddle.create('bpmn:SubProcess', {
      id,
      name,
    });
    subProcess.flowElements = [];
    this.process.flowElements.push(subProcess);
    this.elementMap.set(id, subProcess);
 
    if (configure) {
      const originalProcess = this.process;
      this.process = subProcess;
      configure(this);
      this.process = originalProcess;
    }
 
    return this;
  }
 
  public addEventSubProcess(
    id: string,
    name?: string,
    configure?: (builder: BpmnBuilder) => void
  ): this {
    const subProcess = this.moddle.create('bpmn:SubProcess', {
      id,
      name,
      triggeredByEvent: true,
    });
    subProcess.flowElements = [];
    this.process.flowElements.push(subProcess);
    this.elementMap.set(id, subProcess);
 
    if (configure) {
      const originalProcess = this.process;
      this.process = subProcess;
      configure(this);
      this.process = originalProcess;
    }
 
    return this;
  }
 
  public addDataObjectReference(id: string, name?: string, dataObjectRef?: string): this {
    const props: Record<string, any> = { id, name };
    if (dataObjectRef) {
      props.dataObjectRef = this.elementMap.get(dataObjectRef) || dataObjectRef;
    }
    const element = this.moddle.create('bpmn:DataObjectReference', props);
    this.process.flowElements.push(element);
    this.elementMap.set(id, element);
    return this;
  }
 
  public addDataStoreReference(id: string, name?: string): this {
    const element = this.moddle.create('bpmn:DataStoreReference', { id, name });
    this.process.flowElements.push(element);
    this.elementMap.set(id, element);
    return this;
  }
 
  public addTextAnnotation(id: string, text: string): this {
    if (!this.process.artifacts) {
      this.process.artifacts = [];
    }
    const element = this.moddle.create('bpmn:TextAnnotation', { id, text });
    this.process.artifacts.push(element);
    this.elementMap.set(id, element);
    return this;
  }
 
  public addAssociation(id: string, sourceRef: string, targetRef: string): this {
    if (!this.process.artifacts) {
      this.process.artifacts = [];
    }
    const source = this.elementMap.get(sourceRef) || sourceRef;
    const target = this.elementMap.get(targetRef) || targetRef;
    const element = this.moddle.create('bpmn:Association', {
      id,
      sourceRef: source,
      targetRef: target,
    });
    this.process.artifacts.push(element);
    this.elementMap.set(id, element);
    return this;
  }
 
  public addSequenceFlow(
    idOrConfig: string | FlowConfig,
    sourceRef?: string,
    targetRef?: string
  ): this {
    const isObj = typeof idOrConfig === 'object';
    const id = isObj ? idOrConfig.id : idOrConfig;
    const srcId = isObj ? idOrConfig.sourceRef : sourceRef!;
    const tgtId = isObj ? idOrConfig.targetRef : targetRef!;
    const name = isObj ? idOrConfig.name : undefined;
 
    const source = this.elementMap.get(srcId);
    const target = this.elementMap.get(tgtId);
    const props: Record<string, any> = {
      id,
      sourceRef: source,
      targetRef: target,
    };
    if (name) {
      props.name = name;
    }
    const flow = this.moddle.create('bpmn:SequenceFlow', props);
    this.process.flowElements.push(flow);
    this.elementMap.set(id, flow);
    return this;
  }
 
  public addLane(id: string, elementIds: string[], name?: string): this {
    if (!this.process.laneSets) {
      const laneSet = this.moddle.create('bpmn:LaneSet', {
        id: `${this.process.id}_LaneSet`,
        lanes: [],
      });
      this.process.laneSets = [laneSet];
    }
    const flowNodes = elementIds.map((elementId) => this.elementMap.get(elementId) || elementId);
    const lane = this.moddle.create('bpmn:Lane', {
      id,
      name,
      flowNodeRef: flowNodes,
    });
    this.process.laneSets[0].lanes.push(lane);
    return this;
  }
 
  public addCollaboration(id = 'Collaboration_1'): this {
    if (!this.collaboration) {
      this.collaboration = this.moddle.create('bpmn:Collaboration', {
        id,
        participants: [],
        messageFlows: [],
      });
      this.definitions.rootElements.unshift(this.collaboration);
    }
    return this;
  }
 
  public addParticipant(id: string, processRef: string, name?: string): this {
    this.addCollaboration();
    const targetProcess = this.elementMap.get(processRef) || processRef;
    const participant = this.moddle.create('bpmn:Participant', {
      id,
      name,
      processRef: targetProcess,
    });
    this.collaboration.participants.push(participant);
    this.elementMap.set(id, participant);
    return this;
  }
 
  public addMessageFlow(
    idOrConfig: string | FlowConfig,
    sourceRef?: string,
    targetRef?: string
  ): this {
    this.addCollaboration();
    const isObj = typeof idOrConfig === 'object';
    const id = isObj ? idOrConfig.id : idOrConfig;
    const srcId = isObj ? idOrConfig.sourceRef : sourceRef!;
    const tgtId = isObj ? idOrConfig.targetRef : targetRef!;
    const name = isObj ? idOrConfig.name : undefined;
 
    const source = this.elementMap.get(srcId);
    const target = this.elementMap.get(tgtId);
    const props: Record<string, any> = {
      id,
      sourceRef: source || srcId,
      targetRef: target || tgtId,
    };
    if (name) {
      props.name = name;
    }
    const messageFlow = this.moddle.create('bpmn:MessageFlow', props);
    this.collaboration.messageFlows.push(messageFlow);
    this.elementMap.set(id, messageFlow);
    return this;
  }
 
  public async toXml(): Promise<string> {
    const { xml } = await this.moddle.toXML(this.definitions, { format: true });
    return xml;
  }
 
  private addFlowNode(type: string, id: string, name?: string): this {
    const element = this.moddle.create(type, { id, name });
    this.process.flowElements.push(element);
    this.elementMap.set(id, element);
    return this;
  }
}