Skip to content

Commit eb1830f

Browse files
committed
adjust for #92
1 parent f9c6b17 commit eb1830f

File tree

3 files changed

+176
-3
lines changed

3 files changed

+176
-3
lines changed

lib/base/VariableResolver.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export class BaseVariableResolver {
301301

302302
const seenNames = new Set();
303303

304-
return reversedVariables.filter(variable => {
304+
const deduplicatedVariables = reversedVariables.filter(variable => {
305305

306306
const provider = variable.provider || [];
307307

@@ -319,6 +319,23 @@ export class BaseVariableResolver {
319319

320320
return false;
321321
});
322+
323+
return deduplicatedVariables.map(variable => {
324+
if (!variable.usedBy || !Array.isArray(variable.usedBy)) {
325+
return variable;
326+
}
327+
328+
const usedBy = filterUsedByForElement(variable, bo);
329+
330+
if (usedBy.length === variable.usedBy.length) {
331+
return variable;
332+
}
333+
334+
return {
335+
...variable,
336+
usedBy: usedBy.length ? usedBy : undefined
337+
};
338+
});
322339
}
323340

324341
_getScope(element, containerElement, variableName, checkYourself) {
@@ -472,4 +489,42 @@ function isUsedOutsideOwnScope(variable) {
472489
return variable.usedBy.some(usedBy => {
473490
return usedBy && usedBy.id && !isElementInScope(usedBy, variable.scope);
474491
});
492+
}
493+
494+
function filterUsedByForElement(variable, element) {
495+
const names = variable.usedBy.filter(usage => typeof usage === 'string');
496+
const elements = variable.usedBy.filter(usage => usage && usage.id);
497+
498+
if (!variable.scope) {
499+
return [ ...names, ...elements.filter(usage => isElementInScope(usage, element)) ];
500+
}
501+
502+
// Querying the variable's own scope: show local consumers.
503+
if (element.id === variable.scope.id) {
504+
return [
505+
...names,
506+
...elements.filter(usage => isElementInScope(usage, variable.scope))
507+
];
508+
}
509+
510+
// Querying an ancestor scope: show consumers outside the variable's own scope.
511+
if (isElementInScope(variable.scope, element)) {
512+
return [
513+
...names,
514+
...elements.filter(usage =>
515+
isElementInScope(usage, element)
516+
&& !isElementInScope(usage, variable.scope)
517+
)
518+
];
519+
}
520+
521+
// Querying a child scope: show consumers in that child scope only.
522+
if (isElementInScope(element, variable.scope)) {
523+
return [
524+
...names,
525+
...elements.filter(usage => isElementInScope(usage, element))
526+
];
527+
}
528+
529+
return names;
475530
}

test/fixtures/zeebe/ttt.bpmn

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1kvhh4n" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.44.0" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.7.0">
3+
<bpmn:process id="Process_1" name="Process_1" isExecutable="true">
4+
<bpmn:subProcess id="SubProcess_1" name="SubProcess_1">
5+
<bpmn:extensionElements>
6+
<zeebe:ioMapping>
7+
<zeebe:input target="approved" />
8+
</zeebe:ioMapping>
9+
</bpmn:extensionElements>
10+
<bpmn:outgoing>Flow_03u12aa</bpmn:outgoing>
11+
<bpmn:scriptTask id="Task_2" name="Task_2">
12+
<bpmn:extensionElements>
13+
<zeebe:script expression="=if approved then 1 else 0" resultVariable="taskResult" />
14+
</bpmn:extensionElements>
15+
</bpmn:scriptTask>
16+
</bpmn:subProcess>
17+
<bpmn:sequenceFlow id="Flow_03u12aa" sourceRef="SubProcess_1" targetRef="Task_1" />
18+
<bpmn:scriptTask id="Task_1" name="Task_1">
19+
<bpmn:extensionElements>
20+
<zeebe:script expression="=if approved then &#34;APPROVED&#34; else &#34;NOT APPROVED&#34;" resultVariable="taskResult" />
21+
</bpmn:extensionElements>
22+
<bpmn:incoming>Flow_03u12aa</bpmn:incoming>
23+
</bpmn:scriptTask>
24+
<bpmn:textAnnotation id="TextAnnotation_06v37so">
25+
<bpmn:text>Defines &lt;approved&gt; as local variable</bpmn:text>
26+
</bpmn:textAnnotation>
27+
<bpmn:association id="Association_1vyzo9d" associationDirection="None" sourceRef="SubProcess_1" targetRef="TextAnnotation_06v37so" />
28+
<bpmn:textAnnotation id="TextAnnotation_1gqpt1k">
29+
<bpmn:text>Uses &lt;approved&gt; variable</bpmn:text>
30+
</bpmn:textAnnotation>
31+
<bpmn:association id="Association_1613yyk" associationDirection="None" sourceRef="TextAnnotation_1gqpt1k" targetRef="Task_1" />
32+
<bpmn:association id="Association_15s62zn" associationDirection="None" sourceRef="TextAnnotation_1gqpt1k" targetRef="Task_2" />
33+
</bpmn:process>
34+
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
35+
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
36+
<bpmndi:BPMNShape id="Activity_0rpc80a_di" bpmnElement="Task_1">
37+
<dc:Bounds x="570" y="270" width="100" height="80" />
38+
<bpmndi:BPMNLabel />
39+
</bpmndi:BPMNShape>
40+
<bpmndi:BPMNShape id="Activity_13kwa1s_di" bpmnElement="SubProcess_1" isExpanded="true">
41+
<dc:Bounds x="160" y="210" width="350" height="200" />
42+
<bpmndi:BPMNLabel />
43+
</bpmndi:BPMNShape>
44+
<bpmndi:BPMNShape id="Activity_0azqo33_di" bpmnElement="Task_2">
45+
<dc:Bounds x="220" y="270" width="100" height="80" />
46+
</bpmndi:BPMNShape>
47+
<bpmndi:BPMNEdge id="Association_1613yyk_di" bpmnElement="Association_1613yyk">
48+
<di:waypoint x="480" y="530" />
49+
<di:waypoint x="613" y="350" />
50+
</bpmndi:BPMNEdge>
51+
<bpmndi:BPMNEdge id="Association_1vyzo9d_di" bpmnElement="Association_1vyzo9d">
52+
<di:waypoint x="347" y="210" />
53+
<di:waypoint x="355" y="135" />
54+
</bpmndi:BPMNEdge>
55+
<bpmndi:BPMNEdge id="Association_15s62zn_di" bpmnElement="Association_15s62zn">
56+
<di:waypoint x="436" y="530" />
57+
<di:waypoint x="279" y="350" />
58+
</bpmndi:BPMNEdge>
59+
<bpmndi:BPMNShape id="TextAnnotation_06v37so_di" bpmnElement="TextAnnotation_06v37so">
60+
<dc:Bounds x="310" y="80" width="100" height="54.999996185302734" />
61+
<bpmndi:BPMNLabel />
62+
</bpmndi:BPMNShape>
63+
<bpmndi:BPMNShape id="BPMNShape_0u3y3b2" bpmnElement="TextAnnotation_1gqpt1k">
64+
<dc:Bounds x="410" y="530" width="110" height="50" />
65+
<bpmndi:BPMNLabel />
66+
</bpmndi:BPMNShape>
67+
<bpmndi:BPMNEdge id="Flow_03u12aa_di" bpmnElement="Flow_03u12aa">
68+
<di:waypoint x="510" y="310" />
69+
<di:waypoint x="570" y="310" />
70+
</bpmndi:BPMNEdge>
71+
</bpmndi:BPMNPlane>
72+
</bpmndi:BPMNDiagram>
73+
</bpmn:definitions>

test/spec/zeebe/ZeebeVariableResolver.spec.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { bootstrapModeler, inject } from 'test/TestHelper';
1111
import { ZeebeVariableResolverModule } from 'lib/';
1212

1313
import readWriteXML from 'test/fixtures/zeebe/read-write.bpmn';
14+
import tttXML from 'test/fixtures/zeebe/ttt.bpmn';
1415
import readWriteHierarchicalXML from 'test/fixtures/zeebe/read-write.hierarchical.bpmn';
1516
import simpleXML from 'test/fixtures/zeebe/simple.bpmn';
1617
import emptyXML from 'test/fixtures/zeebe/empty.bpmn';
@@ -2677,8 +2678,6 @@ describe('ZeebeVariableResolver', function() {
26772678
// when
26782679
const variables = await variableResolver.getVariablesForElement(task);
26792680

2680-
console.log(JSON.stringify(variables, null, 2));
2681-
26822681
// then
26832682
expect(variables).to.variableInclude([
26842683
{ name: 'approved', scope: 'Process_1', origin: [ 'ValidateApprovedTask' ], usedBy: [ 'ValidateApprovedTask' ] }
@@ -2716,6 +2715,52 @@ describe('ZeebeVariableResolver', function() {
27162715

27172716
});
27182717

2718+
2719+
describe('used variables - scopes 2', function() {
2720+
2721+
beforeEach(bootstrapModeler(tttXML, {
2722+
additionalModules: [
2723+
ZeebeVariableResolverModule
2724+
],
2725+
moddleExtensions: {
2726+
zeebe: ZeebeModdle
2727+
}
2728+
}));
2729+
2730+
2731+
it('should attach <usedBy> to local scope', inject(async function(elementRegistry, variableResolver) {
2732+
2733+
// given
2734+
const subProcess = elementRegistry.get('SubProcess_1');
2735+
2736+
// when
2737+
const variables = await variableResolver.getVariablesForElement(subProcess);
2738+
2739+
// then
2740+
expect(variables).to.variableEqual([
2741+
{ name: 'taskResult' },
2742+
{ name: 'approved', usedBy: [ 'Task_2' ] }
2743+
]);
2744+
}));
2745+
2746+
2747+
it('should attach <usedBy> to global scope', inject(async function(elementRegistry, variableResolver) {
2748+
2749+
// given
2750+
const rootElement = elementRegistry.get('Process_1');
2751+
2752+
// when
2753+
const variables = await variableResolver.getVariablesForElement(rootElement);
2754+
2755+
// then
2756+
expect(variables).to.variableEqual([
2757+
{ name: 'taskResult' },
2758+
{ name: 'approved', usedBy: [ 'Task_1' ] }
2759+
]);
2760+
}));
2761+
2762+
});
2763+
27192764
});
27202765

27212766
// helpers //////////////////////

0 commit comments

Comments
 (0)