Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Result variables: Minor improvements #33

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions src/zeebe/extractors/extractResultVariables.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
import { forEach, isArray } from 'min-dash';
import { getCalledDecision, getScript } from '../util/ExtensionElementsUtil.js';
import { getCalledDecision, getOutMappings, getScript } from '../util/ExtensionElementsUtil.js';

import { createProcessVariable, addVariableToList } from '../util/ProcessVariablesUtil.js';

/**
* Retrieves process variables defined in result variables, e.g.
* Extracts process variables from extension elements that have a result
* variable, e.g. given the following element:
*
* <bpmn:businessRuleTask id="Activity_1">
* <bpmn:extensionElements>
* <zeebe:calledDecision resultVariable="variable1" />
* </bpmn:extensionElements>
* </bpmn:businessRuleTask>
* <bpmn:businessRuleTask id="Task_1">
* <bpmn:extensionElements>
* <zeebe:calledDecision resultVariable="foo" />
* </bpmn:extensionElements>
* </bpmn:businessRuleTask>
*
* a process variable with name 'foo' is extracted.
*
* => Adds one variable "variable1"to the list.
* If output variables exist, the scope is set to the element.
*
* If an output variable with the same name exists, e.g. given the following
* element:
*
* <bpmn:businessRuleTask id="Task_1">
* <bpmn:extensionElements>
* <zeebe:calledDecision resultVariable="foo" />
* <zeebe:ioMapping>
* <zeebe:output target="foo" />
* </zeebe:ioMapping>
* </bpmn:extensionElements>
* </bpmn:businessRuleTask>
*
* no process variable is created.
*
* @param {Object} options
* @param {Array<ModdleElement>} options.elements
* @param {ModdleElement} options.containerElement
* @param {Array<ProcessVariable>} options.processVariables
*
* @return {Array<ProcessVariable>}
*/
export default function(options) {
var elements = options.elements,
Expand All @@ -27,26 +49,20 @@ export default function(options) {

forEach(elements, function(element) {

var baseElement = getCalledDecision(element) ||
getScript(element);
var extensionElement = getCalledDecision(element) || getScript(element);

if (!baseElement) {
if (!extensionElement) {
return;
}

var resultVariable = baseElement.resultVariable;
var resultVariable = extensionElement.resultVariable;

// Checks if output variable exists, the scope gets redefined
if (processVariables.some(x => x.origin[0] === element && x.scope === containerElement)) {

// result variable will have local scope
if (hasOutMappings(element)) {
containerElement = element;

// check if the output have same name as resultVariable, only proceed with output variable
if (processVariables.some(variable => variable.name === resultVariable)) {
return processVariables;
}

}

if (resultVariable) {
Expand All @@ -62,3 +78,9 @@ export default function(options) {

return processVariables;
}

function hasOutMappings(element) {
var outMappings = getOutMappings(element);

return outMappings && outMappings.length;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<bpmn:process id="Process_1" isExecutable="true">
<bpmn:scriptTask id="Task_1">
<bpmn:extensionElements>
<zeebe:script expression="=123" resultVariable="foo" />
<zeebe:script resultVariable="foo" />
<zeebe:ioMapping>
<zeebe:output source="={}" target="foo" />
<zeebe:output target="foo" />
</zeebe:ioMapping>
</bpmn:extensionElements>
</bpmn:scriptTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNShape id="Activity_0258i89_di" bpmnElement="Task_1">
<bpmndi:BPMNShape id="Task_1_di" bpmnElement="Task_1">
<dc:Bounds x="160" y="60" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<bpmn:process id="Process_1" isExecutable="true">
<bpmn:scriptTask id="Task_1">
<bpmn:extensionElements>
<zeebe:script expression="={}" resultVariable="foo" />
<zeebe:script resultVariable="foo" />
<zeebe:ioMapping>
<zeebe:output target="output" />
<zeebe:output target="bar" />
</zeebe:ioMapping>
</bpmn:extensionElements>
</bpmn:scriptTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNShape id="Activity_074uvug_di" bpmnElement="Task_1">
<bpmndi:BPMNShape id="Task_1_di" bpmnElement="Task_1">
<dc:Bounds x="160" y="80" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
Expand Down
12 changes: 6 additions & 6 deletions test/zeebe/spec/ProcessVariablesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ describe('zeebe/process variables module', function() {
});


it('should extract variables with re define resultVariable scope if output mapping exists - script task', async function() {
it('should extract variables - result variable, output exists - script task', async function() {

// given
const xml = read('test/zeebe/fixtures/script-task-output.bpmn');
const xml = read('test/zeebe/fixtures/script-task-result-variable-output.bpmn');

const definitions = await parse(xml);

Expand All @@ -229,16 +229,16 @@ describe('zeebe/process variables module', function() {

// then
expect(convertToTestable(variables)).to.eql([
{ name: 'output', origin: [ 'Task_1' ], scope: 'Process_1' },
{ name: 'foo', origin: [ 'Task_1' ], scope: 'Task_1' },
{ name: 'bar', origin: [ 'Task_1' ], scope: 'Process_1' },
{ name: 'foo', origin: [ 'Task_1' ], scope: 'Task_1' }
]);
});


it('should extract only output variable if same name result variable exists - simple process with script', async function() {
it('should not extract variables - result variable, output exists with same name - script task', async function() {

// given
const xml = read('test/zeebe/fixtures/script-task-same-output-and-result-variable.bpmn');
const xml = read('test/zeebe/fixtures/script-task-result-variable-output-same-name.bpmn');

const definitions = await parse(xml);

Expand Down
Loading