-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathOwnership.ts
More file actions
133 lines (116 loc) · 3.46 KB
/
Copy pathOwnership.ts
File metadata and controls
133 lines (116 loc) · 3.46 KB
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
import { is } from '../../di/DiUtil.js';
import { isBpmnElement } from '../bpmn/Types.js';
import type { BpmnElementFor } from '../bpmn/Types.js';
import type {
BpmnElement,
Bounds,
LayoutRecord
} from '../Types.js';
type ArtifactAssociation =
| BpmnElementFor<'bpmn:Association'>
| BpmnElementFor<'bpmn:DataAssociation'>;
type ArtifactOwnerReference = {
association: ArtifactAssociation;
owner: BpmnElement;
ownerBounds: Bounds | undefined;
ownerConnectionIndex?: number;
ownerConnectionCount?: number;
};
type ArtifactOwnershipContext = {
artifactRecords: LayoutRecord[];
associations: ArtifactAssociation[];
graphElements: Set<BpmnElement>;
graphShapes: Map<BpmnElement, Bounds>;
owners: Map<BpmnElement, ArtifactOwnerReference[]>;
};
export function discoverArtifactOwnership(
context: ArtifactOwnershipContext
): void {
const {
artifactRecords,
associations,
graphElements,
graphShapes,
owners
} = context;
for (const association of associations) {
const endpoints = associationEndpoints(association);
const artifact = endpoints.find(endpoint => {
return artifactRecords.some(record => record.element === endpoint);
});
const candidateOwner = is(association, 'bpmn:DataAssociation')
? association.$parent
: endpoints.find(endpoint => {
return endpoint !== artifact && graphElements.has(endpoint);
});
const owner = isBpmnElement(candidateOwner)
? candidateOwner
: undefined;
if (!artifact || !owner || !graphElements.has(owner)) {
continue;
}
const references = owners.get(artifact);
if (references) {
references.push({
association,
owner,
ownerBounds: graphShapes.get(owner)
});
} else {
owners.set(artifact, [ {
association,
owner,
ownerBounds: graphShapes.get(owner)
} ]);
}
}
for (const references of owners.values()) {
assignOwnerConnectionSlots(references);
}
}
function associationEndpoints(association: ArtifactAssociation): BpmnElement[] {
return [
...(Array.isArray(association.sourceRef)
? association.sourceRef
: [ association.sourceRef ]),
association.targetRef
].filter(isBpmnElement);
}
function assignOwnerConnectionSlots(
references: ArtifactOwnerReference[]
): void {
const counts = new Map<BpmnElement, number>();
const indices = new Map<BpmnElement, number>();
for (const reference of references) {
counts.set(reference.owner, (counts.get(reference.owner) || 0) + 1);
}
for (const reference of references) {
const index = indices.get(reference.owner) || 0;
reference.ownerConnectionIndex = index;
reference.ownerConnectionCount = counts.get(reference.owner);
indices.set(reference.owner, index + 1);
}
}
export function findContainingArtifactContainers(
elementBounds: Bounds,
shapes: Map<BpmnElement, Bounds>
): Bounds[] {
const center = {
x: elementBounds.x + elementBounds.width / 2,
y: elementBounds.y + elementBounds.height / 2
};
return [ ...shapes.entries() ]
.filter(([ element, rect ]) => {
return (
is(element, 'bpmn:Lane') ||
is(element, 'bpmn:Participant') ||
is(element, 'bpmn:SubProcess')
) &&
center.x >= rect.x &&
center.x <= rect.x + rect.width &&
center.y >= rect.y &&
center.y <= rect.y + rect.height;
})
.map(([ , rect ]) => rect)
.sort((a, b) => a.width * a.height - b.width * b.height);
}