Skip to content

Commit

Permalink
feat: resolve variables from function definition
Browse files Browse the repository at this point in the history
  • Loading branch information
barmac committed Apr 29, 2024
1 parent 4281a5f commit 8ba8d85
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export function resolveVariables(moddleElement) {
function handle(element, context) {
if (is(element, 'dmn:Decision')) {
handleDecision(element, context);
} else if (is(element, 'dmn:Expression')) {
handleExpression(element, context);
}
}

Expand Down Expand Up @@ -187,6 +189,40 @@ function handleBusinessKnowledgeModel(bkm, context) {
context.variables.push(variable);
}

function handleExpression(expression, context) {
if (is(expression, 'dmn:FunctionDefinition')) {
handleFunctionDefinition(expression, context);
}
}

function handleFunctionDefinition(functionDefinition, context) {
const parameters = functionDefinition.get('formalParameter');

for (const parameter of parameters) {
handleFormalParameter(parameter, context);
}
}

function handleFormalParameter(parameter, context) {
const name = parameter.get('name');

// prevent invalid variables in suggestions
if (!name) {
return;
}

/** @type Variable */
const variable = {
name,
origin: parameter
};

if (parameter.get('typeRef')) {
variable.detail = parameter.get('typeRef');
}

context.variables.push(variable);
}


// helpers //////////////////////
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/function-definition.dmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/DMN/20191111/MODEL/" xmlns:dmndi="https://www.omg.org/spec/DMN/20191111/DMNDI/" xmlns:dc="http://www.omg.org/spec/DMN/20180521/DC/" xmlns:modeler="http://camunda.org/schema/modeler/1.0" xmlns:di="http://www.omg.org/spec/DMN/20180521/DI/" id="Definitions_0zw4l66" name="DRD" namespace="http://camunda.org/schema/1.0/dmn" exporter="Camunda Modeler" exporterVersion="5.9.0" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.1.0">
<businessKnowledgeModel name="BKM" id="Bkm_1">
<encapsulatedLogic>
<formalParameter name="noType" />
<formalParameter name="string" typeRef="string" />
<formalParameter name="num" typeRef="number" />
<literalExpression>
<text>noType + num - string</text>
</literalExpression>
</encapsulatedLogic>
</businessKnowledgeModel>
<dmndi:DMNDI>
<dmndi:DMNDiagram>
<dmndi:DMNShape dmnElementRef="Bkm_1">
<dc:Bounds height="80" width="180" x="160" y="100" />
</dmndi:DMNShape>
</dmndi:DMNDiagram>
</dmndi:DMNDI>
</definitions>
33 changes: 33 additions & 0 deletions test/spec/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SimpleDmn from '../fixtures/simple.dmn';
import RequiredDecisionDmn from '../fixtures/required-decision.dmn';
import RequiredKnowledgeDmn from '../fixtures/required-knowledge.dmn';
import MissingNamesDmn from '../fixtures/missing-names.dmn';
import FunctionDefinitionDmn from '../fixtures/function-definition.dmn';

import { resolveVariables } from '../../lib';

Expand Down Expand Up @@ -128,4 +129,36 @@ describe('#resolveVariables', function() {
}
]);
});


it('should resolve formal parameters of function definition', async function() {

// given
const parsed = await parse(FunctionDefinitionDmn);
const element = findElementById(parsed, 'Bkm_1'),
fn = element.get('encapsulatedLogic'),
parameters = fn.get('formalParameter'),
functionBody = fn.get('body');

// when
const variables = resolveVariables(functionBody);

// then
expect(variables).to.eql([
{
name: 'noType',
origin: parameters[0]
},
{
name: 'string',
detail: 'string',
origin: parameters[1]
},
{
name: 'num',
detail: 'number',
origin: parameters[2]
}
]);
});
});

0 comments on commit 8ba8d85

Please sign in to comment.