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

FEEL variable scope resolution #43

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
42 changes: 5 additions & 37 deletions lib/zeebe/VariableResolver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { getProcessVariables } from '@bpmn-io/extract-process-variables/zeebe';
import { BaseVariableResolver } from '../base/VariableResolver';
import { parseVariables } from './util/feelUtility';
import {
parseVariables,
getElementNamesToRemove
} from './util/feelUtility';
import {
getBusinessObject,
is
Expand Down Expand Up @@ -36,42 +39,7 @@ export default class ZeebeVariableResolver extends BaseVariableResolver {
return variables;
}

const namesToFilter = [];

// Input: remove all inputs defined after the current input definition
if (is(moddleElement, 'zeebe:Input')) {
const allInputs = inputOutput.inputParameters;

const inputsToFilter =
allInputs
.slice(allInputs.indexOf(moddleElement))
.map(o => o.target);

namesToFilter.push(...inputsToFilter);
}

const allOutputs = inputOutput.outputParameters;

// Output: remove all outputs defined after the current output definition
if (is(moddleElement, 'zeebe:Output')) {

// Get all output mappings defined after the current element, including own name
const outputsToFilter = allOutputs
.slice(allOutputs.indexOf(moddleElement))
.map(o => o.target);

namesToFilter.push(...outputsToFilter);
}

// Input or general property: remove all outputs
else if (allOutputs) {

// Input or execution-related element, remove all outputs
const outputsToFilter = allOutputs
.map(o => o.target);

namesToFilter.push(...outputsToFilter);
}
const namesToFilter = getElementNamesToRemove(moddleElement, inputOutput);

return variables.filter(v => {

Expand Down
68 changes: 66 additions & 2 deletions lib/zeebe/util/feelUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import {
import { EntriesContext } from './VariableContext';
import { getExtensionElementsList } from '../../base/util/ExtensionElementsUtil';
import { getParents } from '../../base/util/scopeUtil';
import { is } from 'bpmn-js/lib/util/ModelUtil';

export function parseVariables(variables) {

const variablesToResolve = [];

// remove same elements // TODO :Step 1
filterSameVariableNames(variables);


// Step 1 - Parse all variables and populate all that don't have references
// to other variables
variables.forEach(variable => {
Expand Down Expand Up @@ -158,7 +163,7 @@ export function getResultContext(expression, variables = {}) {
* @returns {{ expression: String, unresolved: Array<String> }}}
*/
function getExpressionDetails(variable, origin) {
const expression = getIoExpression(variable, origin) || getScriptExpression(variable, origin);
const expression = getScriptExpression(variable, origin) || getIoExpression(variable, origin);

if (!expression) {
return;
Expand Down Expand Up @@ -197,7 +202,7 @@ function getIoExpression(variable, origin) {
return;
}

const mapping = mappings.find(mapping => mapping.target === variable.name);
const mapping = mappings.filter(mapping => mapping.target === variable.name).pop();

if (!mapping || !mapping.source) {
return;
Expand Down Expand Up @@ -374,4 +379,63 @@ function filterForScope(context, variable) {
}

return scopedResults;
}

/**
* Remove input/output element name after current definition
*/
export function getElementNamesToRemove(moddleElement, inputOutput) {
const namesToFilter = [];

// Input: remove all inputs defined after the current input definition
if (is(moddleElement, 'zeebe:Input')) {
const allInputs = inputOutput.inputParameters;

const inputsToFilter =
allInputs
.slice(allInputs.indexOf(moddleElement))
.map(o => o.target);

namesToFilter.push(...inputsToFilter);
}

const allOutputs = inputOutput.outputParameters;

// Output: remove all outputs defined after the current output definition
if (is(moddleElement, 'zeebe:Output')) {

// Get all output mappings defined after the current element, including own name
const outputsToFilter = allOutputs
.slice(allOutputs.indexOf(moddleElement))
.map(o => o.target);

namesToFilter.push(...outputsToFilter);
}

// Input or general property: remove all outputs
else if (allOutputs) {

// Input or execution-related element, remove all outputs
const outputsToFilter = allOutputs
.map(o => o.target);

namesToFilter.push(...outputsToFilter);
}

return namesToFilter;
}

function filterSameVariableNames(variables) {
const duplicateNames = variables.filter((item, index, self) =>
self.findIndex(t => t.name === item.name) !== index
).map(x => x.name);

// removing redundant entries for input mappings if output mappings exists
duplicateNames.forEach(duplicate => {
variables.splice(
variables.findIndex(variable =>
variable.name === duplicate &&
variable.origin[0].id === variable.scope.id)
,1);
});
}
98 changes: 94 additions & 4 deletions test/fixtures/zeebe/mappings/scope.bpmn
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<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:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1noegy9" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.6.0-rc.1" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.1.0">
<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:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1noegy9" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.28.0" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.1.0">
<bpmn:collaboration id="Collaboration_0e60zir">
<bpmn:participant id="Participant_1" processRef="Process_1" />
<bpmn:participant id="Participant_2" processRef="Process_2" />
<bpmn:participant id="Participant_3" processRef="Process_3" />
<bpmn:participant id="Participant_4" processRef="Process_4" />
<bpmn:participant id="Participant_5" processRef="Process_5" />
</bpmn:collaboration>
<bpmn:process id="Process_1" isExecutable="true">
<bpmn:serviceTask id="scopedContext" name="Scoped Context">
<bpmn:extensionElements>
Expand All @@ -17,14 +24,97 @@
</bpmn:extensionElements>
</bpmn:serviceTask>
</bpmn:process>
<bpmn:process id="Process_2" isExecutable="false">
<bpmn:scriptTask id="Activity_1" name="Script Task">
<bpmn:extensionElements>
<zeebe:script expression="={}" resultVariable="foo" />
<zeebe:ioMapping>
<zeebe:input source="=123" target="foo" />
<zeebe:output target="output" />
</zeebe:ioMapping>
</bpmn:extensionElements>
</bpmn:scriptTask>
</bpmn:process>
<bpmn:process id="Process_3" isExecutable="false">
<bpmn:scriptTask id="Activity_2" name="Script Task">
<bpmn:extensionElements>
<zeebe:script expression="={}" resultVariable="resultVariable" />
<zeebe:ioMapping>
<zeebe:output source="={}" target="output" />
</zeebe:ioMapping>
</bpmn:extensionElements>
</bpmn:scriptTask>
</bpmn:process>
<bpmn:process id="Process_4" isExecutable="false">
<bpmn:scriptTask id="Activity_4" name="Script Task">
<bpmn:extensionElements>
<zeebe:script expression="={}" resultVariable="resultVariable" />
</bpmn:extensionElements>
</bpmn:scriptTask>
</bpmn:process>
<bpmn:process id="Process_5" isExecutable="false">
<bpmn:scriptTask id="Activity_5a" name="Script Task">
<bpmn:extensionElements>
<zeebe:script expression="={}" resultVariable="foo" />
<zeebe:ioMapping>
<zeebe:input source="=123" target="foo" />
<zeebe:output source="=&#34;bar&#34;" target="foo" />
<zeebe:output source="=foo" target="output" />
</zeebe:ioMapping>
</bpmn:extensionElements>
</bpmn:scriptTask>
<bpmn:userTask id="Activity_5b" name="User Task">
<bpmn:extensionElements>
<zeebe:ioMapping>
<zeebe:input source="={}" target="foo" />
<zeebe:output source="=123" target="foo" />
<zeebe:output source="=foo" target="OutputVariable_02b2dg1" />
</zeebe:ioMapping>
</bpmn:extensionElements>
</bpmn:userTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Collaboration_0e60zir">
<bpmndi:BPMNShape id="Participant_10jajek_di" bpmnElement="Participant_1" isHorizontal="true">
<dc:Bounds x="160" y="85" width="600" height="250" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1n52sm3_di" bpmnElement="scopedContext">
<dc:Bounds x="120" y="70" width="100" height="80" />
<dc:Bounds x="330" y="170" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1hlau9e_di" bpmnElement="Activity_0kafybg">
<dc:Bounds x="270" y="70" width="100" height="80" />
<dc:Bounds x="480" y="170" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Participant_0o4apj6_di" bpmnElement="Participant_2" isHorizontal="true">
<dc:Bounds x="160" y="380" width="600" height="250" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_06g1sod_di" bpmnElement="Activity_1">
<dc:Bounds x="290" y="460" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Participant_13eqjzi_di" bpmnElement="Participant_3" isHorizontal="true">
<dc:Bounds x="160" y="690" width="600" height="250" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1c85kz4_di" bpmnElement="Activity_2">
<dc:Bounds x="280" y="780" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_0o2k4bk" bpmnElement="Participant_4" isHorizontal="true">
<dc:Bounds x="160" y="1010" width="600" height="250" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_1wqur3c" bpmnElement="Activity_4">
<dc:Bounds x="280" y="1100" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Participant_08w9t3p_di" bpmnElement="Participant_5" isHorizontal="true">
<dc:Bounds x="160" y="1330" width="600" height="250" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0jkek8a_di" bpmnElement="Activity_5a">
<dc:Bounds x="280" y="1410" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1g0cp8w_di" bpmnElement="Activity_5b">
<dc:Bounds x="430" y="1410" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
Expand Down
46 changes: 44 additions & 2 deletions test/spec/zeebe/Mappings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { is } from 'bpmn-js/lib/util/ModelUtil';
import { bootstrapModeler, inject } from 'test/TestHelper';

import { ZeebeVariableResolverModule } from 'lib/';
import { getInputOutput } from '../../../lib/base/util/ExtensionElementsUtil';

import chainedMappingsXML from 'test/fixtures/zeebe/mappings/chained-mappings.bpmn';
import primitivesXML from 'test/fixtures/zeebe/mappings/primitives.bpmn';
Expand Down Expand Up @@ -283,7 +284,7 @@ describe('ZeebeVariableResolver - Variable Mappings', function() {
it('should only resolve variables in scope', inject(async function(variableResolver, elementRegistry) {

// given
const root = elementRegistry.get('Process_1');
const root = elementRegistry.get('Participant_1');

const initialVariables = [ {
name: 'globalVariable',
Expand All @@ -298,7 +299,7 @@ describe('ZeebeVariableResolver - Variable Mappings', function() {
});

// when
const variables = await variableResolver.getVariablesForElement(root.businessObject);
const variables = await variableResolver.getVariablesForElement(root.businessObject.processRef);

// then
expect(variables).to.variableEqual([
Expand All @@ -316,6 +317,47 @@ describe('ZeebeVariableResolver - Variable Mappings', function() {
]);
}));


it('should only resolve the script result variable if input and result variable names conflict', inject(async function(variableResolver, elementRegistry) {

// given
const root = elementRegistry.get('Activity_1');
const bo = root.businessObject;
const output = getInputOutput(bo).outputParameters[0];

// when
const variables = await variableResolver.getVariablesForElement(root.businessObject, output);

// then
expect(variables).to.variableEqual([
{
name: 'foo',
type: '',
info: '',
}
]);
}));

it('should only resolve the output variable if script result variable and output names conflict', inject(async function(variableResolver, elementRegistry) {

// given
const root = elementRegistry.get('Activity_5a');
const bo = root.businessObject;
const output = getInputOutput(bo).outputParameters[1];

// when
const variables = await variableResolver.getVariablesForElement(root.businessObject, output);

// then
expect(variables).to.variableEqual([
{
name: 'foo',
type: '',
info: '',
}
]);
}));

});


Expand Down
Loading