All files / src layout-warnings.ts

100% Statements 68/68
100% Branches 48/48
100% Functions 9/9
100% Lines 64/64

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                                      14x       361x 361x               1428x 1428x 1428x       72x 327x 1428x 1428x 1428x 8x   1420x 3x                             72x 34x 34x 71x 71x 31x   40x 40x 40x           1x                     72x 274x 18x   256x 256x 358x 358x 358x 1x         1x                                     384x 4639x 4639x 751x   3888x 2x                   72x 274x 274x 274x 274x   274x 384x                   72x 72x 72x 72x 72x   72x 636x 361x 361x 34x   327x   275x 274x       72x 72x 72x 72x    
import { boxesOverlap, segmentCrossesBox } from './layout-metrics';
 
export type LayoutWarningCode =
  | 'UNRESOLVED_SEQUENCE_FLOW'
  | 'CROSS_CONTAINER_FLOW'
  | 'ROUTE_INTERSECTS_OBSTACLE'
  | 'ROUTE_INVALID_CONNECTION_POINTS'
  | 'ROUTE_NOT_ORTHOGONAL'
  | 'SHAPE_OVERLAPS_SHAPE'
  | 'LABEL_OVERLAPS_ELEMENT'
  | 'CONTAINER_OVERFLOW';
 
export interface LayoutWarning {
  code: LayoutWarningCode;
  elementId?: string;
  message: string;
}
 
export function addWarning(warnings: LayoutWarning[] | undefined, warning: LayoutWarning): void {
  warnings?.push(warning);
}
 
function isContainerShape(el: any): boolean {
  const type = el.bpmnElement?.$type;
  return (
    type === 'bpmn:Participant' ||
    type === 'bpmn:Lane' ||
    (type === 'bpmn:SubProcess' && el.isExpanded === true)
  );
}
 
function areShapesAttached(s1: any, s2: any): boolean {
  const a1 = s1.bpmnElement?.attachedToRef?.id || s1.bpmnElement?.attachedToRef;
  const a2 = s2.bpmnElement?.attachedToRef?.id || s2.bpmnElement?.attachedToRef;
  return a1 === s2.bpmnElement?.id || a2 === s1.bpmnElement?.id;
}
 
function checkShapeOverlaps(flowShapes: any[], warnings: LayoutWarning[]): void {
  for (let i = 0; i < flowShapes.length; i++) {
    for (let j = i + 1; j < flowShapes.length; j++) {
      const s1 = flowShapes[i];
      const s2 = flowShapes[j];
      if (areShapesAttached(s1, s2)) {
        continue;
      }
      if (boxesOverlap(s1.bounds, s2.bounds)) {
        addWarning(warnings, {
          code: 'SHAPE_OVERLAPS_SHAPE',
          elementId: s1.bpmnElement?.id,
          message: `Shape "${s1.bpmnElement?.id}" overlaps with shape "${s2.bpmnElement?.id}"`,
        });
      }
    }
  }
}
 
function checkContainerEnclosure(
  containers: any[],
  shapesMap: Map<string, any>,
  warnings: LayoutWarning[]
): void {
  for (const c of containers) {
    const flowElements = c.bpmnElement?.flowElements || [];
    for (const child of flowElements) {
      const childShape = shapesMap.get(child.id);
      if (!childShape?.bounds) {
        continue;
      }
      const b = childShape.bounds;
      const cb = c.bounds;
      if (
        b.x < cb.x ||
        b.y < cb.y ||
        b.x + b.width > cb.x + cb.width ||
        b.y + b.height > cb.y + cb.height
      ) {
        addWarning(warnings, {
          code: 'CONTAINER_OVERFLOW',
          elementId: c.bpmnElement?.id,
          message: `Container "${c.bpmnElement?.id}" does not enclose child element "${child.id}"`,
        });
      }
    }
  }
}
 
function checkEdgeOrthogonality(edges: any[], warnings: LayoutWarning[]): void {
  for (const edge of edges) {
    if (edge.bpmnElement?.$type !== 'bpmn:SequenceFlow') {
      continue;
    }
    const waypoints = edge.waypoint;
    for (let i = 0; i < waypoints.length - 1; i++) {
      const p1 = waypoints[i];
      const p2 = waypoints[i + 1];
      if (p1.x !== p2.x && p1.y !== p2.y) {
        addWarning(warnings, {
          code: 'ROUTE_NOT_ORTHOGONAL',
          elementId: edge.bpmnElement?.id,
          message: `Sequence flow "${edge.bpmnElement?.id}" contains non-orthogonal segment`,
        });
        break;
      }
    }
  }
}
 
interface SegmentObstacleCheck {
  p1: any;
  p2: any;
  edgeId: string;
  srcId?: string;
  tgtId?: string;
}
 
function checkSegmentObstacles(
  seg: SegmentObstacleCheck,
  flowShapes: any[],
  warnings: LayoutWarning[]
): void {
  for (const shape of flowShapes) {
    const shapeId = shape.bpmnElement?.id;
    if (shapeId === seg.srcId || shapeId === seg.tgtId) {
      continue;
    }
    if (segmentCrossesBox(seg.p1, seg.p2, shape.bounds)) {
      addWarning(warnings, {
        code: 'ROUTE_INTERSECTS_OBSTACLE',
        elementId: seg.edgeId,
        message: `Edge "${seg.edgeId}" intersects shape "${shapeId}"`,
      });
    }
  }
}
 
function checkEdgeObstacles(edges: any[], flowShapes: any[], warnings: LayoutWarning[]): void {
  for (const edge of edges) {
    const waypoints = edge.waypoint;
    const srcId = edge.bpmnElement?.sourceRef?.id;
    const tgtId = edge.bpmnElement?.targetRef?.id;
    const edgeId = edge.bpmnElement?.id;
 
    for (let i = 0; i < waypoints.length - 1; i++) {
      checkSegmentObstacles(
        { p1: waypoints[i], p2: waypoints[i + 1], edgeId, srcId, tgtId },
        flowShapes,
        warnings
      );
    }
  }
}
 
export function collectPlaneDiagnostics(plane: any, warnings: LayoutWarning[]): void {
  const elements = plane?.planeElement || [];
  const flowShapes: any[] = [];
  const containers: any[] = [];
  const edges: any[] = [];
  const shapesMap = new Map<string, any>();
 
  for (const el of elements) {
    if (el.$type === 'bpmndi:BPMNShape' && el.bounds) {
      shapesMap.set(el.bpmnElement?.id, el);
      if (isContainerShape(el)) {
        containers.push(el);
      } else {
        flowShapes.push(el);
      }
    } else if (el.$type === 'bpmndi:BPMNEdge' && Array.isArray(el.waypoint)) {
      edges.push(el);
    }
  }
 
  checkShapeOverlaps(flowShapes, warnings);
  checkContainerEnclosure(containers, shapesMap, warnings);
  checkEdgeOrthogonality(edges, warnings);
  checkEdgeObstacles(edges, flowShapes, warnings);
}