2022-08-16 20:41:37 +00:00
|
|
|
import { is } from 'bpmn-js/lib/util/ModelUtil';
|
|
|
|
|
2022-08-08 20:08:44 +00:00
|
|
|
/**
|
|
|
|
* loops up until it can find the root.
|
|
|
|
* @param element
|
|
|
|
*/
|
|
|
|
export function getRoot(element) {
|
2022-08-12 17:59:35 +00:00
|
|
|
// todo: Do we want element to be a shape or moddle object?
|
2022-08-11 18:27:42 +00:00
|
|
|
if (element.$type === 'bpmn:Definitions') {
|
|
|
|
return element;
|
2022-08-16 17:02:14 +00:00
|
|
|
}
|
|
|
|
if (typeof element.$parent !== 'undefined') {
|
2022-08-11 18:27:42 +00:00
|
|
|
return getRoot(element.$parent);
|
2022-08-08 20:08:44 +00:00
|
|
|
}
|
2022-08-16 17:02:14 +00:00
|
|
|
return element;
|
2022-08-08 20:08:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:55:14 +00:00
|
|
|
export function isMessageElement(shapeElement) {
|
|
|
|
return (
|
|
|
|
is(shapeElement, 'bpmn:SendTask') ||
|
|
|
|
is(shapeElement, 'bpmn:ReceiveTask') ||
|
|
|
|
isMessageEvent(shapeElement)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-16 20:41:37 +00:00
|
|
|
export function isMessageEvent(shapeElement) {
|
|
|
|
const { eventDefinitions } = shapeElement.businessObject;
|
|
|
|
if (eventDefinitions && eventDefinitions[0]) {
|
|
|
|
return eventDefinitions[0].$type === 'bpmn:MessageEventDefinition';
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:55:14 +00:00
|
|
|
export function canReceiveMessage(shapeElement) {
|
|
|
|
if (is(shapeElement, 'bpmn:ReceiveTask')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (isMessageEvent(shapeElement)) {
|
|
|
|
return (
|
|
|
|
is(shapeElement, 'bpmn:StartEvent') || is(shapeElement, 'bpmn:CatchEvent')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getMessageRefElement(shapeElement) {
|
|
|
|
if (isMessageEvent(shapeElement)) {
|
|
|
|
const messageEventDefinition =
|
|
|
|
shapeElement.businessObject.eventDefinitions[0];
|
2022-08-16 20:41:37 +00:00
|
|
|
if (messageEventDefinition && messageEventDefinition.messageRef) {
|
2022-08-16 20:11:46 +00:00
|
|
|
return messageEventDefinition.messageRef;
|
|
|
|
}
|
|
|
|
} else if (
|
2022-08-17 16:55:14 +00:00
|
|
|
isMessageElement(shapeElement) &&
|
|
|
|
shapeElement.businessObject.messageRef
|
2022-08-16 20:11:46 +00:00
|
|
|
) {
|
2022-08-17 16:55:14 +00:00
|
|
|
return shapeElement.businessObject.messageRef;
|
2022-08-16 20:11:46 +00:00
|
|
|
}
|
2022-08-17 16:55:14 +00:00
|
|
|
return null;
|
2022-08-16 20:11:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:55:14 +00:00
|
|
|
export function findFormalExpressions(shapeElement) {
|
2022-08-16 17:02:14 +00:00
|
|
|
const formalExpressions = [];
|
2022-08-17 16:55:14 +00:00
|
|
|
const messageRef = getMessageRefElement(shapeElement);
|
2022-08-16 20:21:30 +00:00
|
|
|
if (messageRef) {
|
2022-08-17 16:55:14 +00:00
|
|
|
const root = getRoot(shapeElement.businessObject);
|
2022-08-11 18:27:42 +00:00
|
|
|
if (root.$type === 'bpmn:Definitions') {
|
2022-08-16 20:21:30 +00:00
|
|
|
for (const childElement of root.rootElements) {
|
|
|
|
if (childElement.$type === 'bpmn:CorrelationProperty') {
|
|
|
|
const retrievalExpression = processCorrelationProperty(
|
|
|
|
childElement,
|
|
|
|
messageRef
|
2022-08-16 17:02:14 +00:00
|
|
|
);
|
2022-08-11 18:27:42 +00:00
|
|
|
// todo: is there a better test for this than length === 1?
|
2022-08-16 20:21:30 +00:00
|
|
|
if (retrievalExpression.length === 1) {
|
|
|
|
const formalExpression = {
|
|
|
|
correlationId: childElement.id,
|
|
|
|
expression: retrievalExpression[0],
|
|
|
|
};
|
2022-08-12 17:59:35 +00:00
|
|
|
formalExpressions.push(formalExpression);
|
2022-08-11 18:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 17:02:14 +00:00
|
|
|
return formalExpressions;
|
2022-08-11 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 17:05:46 +00:00
|
|
|
export function getMessageElementForShapeElement(shapeElement) {
|
|
|
|
const { businessObject } = shapeElement;
|
|
|
|
const taskMessage = getMessageRefElement(shapeElement);
|
|
|
|
const messages = findMessageModdleElements(businessObject);
|
|
|
|
if (taskMessage) {
|
|
|
|
for (const message of messages) {
|
|
|
|
if (message.id === taskMessage.id) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-11 18:27:42 +00:00
|
|
|
function processCorrelationProperty(correlationProperty, message) {
|
2022-08-16 17:02:14 +00:00
|
|
|
const expressions = [];
|
2022-08-16 20:21:30 +00:00
|
|
|
for (const retrievalExpression of correlationProperty.correlationPropertyRetrievalExpression) {
|
2022-08-16 17:02:14 +00:00
|
|
|
if (
|
2022-08-16 20:21:30 +00:00
|
|
|
retrievalExpression.$type ===
|
2022-08-16 17:02:14 +00:00
|
|
|
'bpmn:CorrelationPropertyRetrievalExpression' &&
|
2022-08-16 20:21:30 +00:00
|
|
|
retrievalExpression.messageRef &&
|
|
|
|
retrievalExpression.messageRef.id === message.id &&
|
|
|
|
retrievalExpression.messagePath.body
|
2022-08-16 17:02:14 +00:00
|
|
|
) {
|
2022-08-16 20:21:30 +00:00
|
|
|
expressions.push(retrievalExpression.messagePath.body);
|
2022-08-11 18:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-16 17:02:14 +00:00
|
|
|
return expressions;
|
2022-08-11 18:27:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 15:11:16 +00:00
|
|
|
export function findCorrelationProperties(element) {
|
2022-08-11 18:27:42 +00:00
|
|
|
const root = getRoot(element);
|
2022-08-08 20:08:44 +00:00
|
|
|
const correlationProperties = [];
|
|
|
|
for (const rootElement of root.rootElements) {
|
|
|
|
if (rootElement.$type === 'bpmn:CorrelationProperty') {
|
|
|
|
correlationProperties.push(rootElement);
|
|
|
|
}
|
2022-08-16 15:11:16 +00:00
|
|
|
}
|
2022-08-16 17:02:14 +00:00
|
|
|
return correlationProperties;
|
2022-08-16 15:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function findCorrelationKeys(element) {
|
|
|
|
const root = getRoot(element);
|
|
|
|
const correlationKeys = [];
|
|
|
|
for (const rootElement of root.rootElements) {
|
|
|
|
if (rootElement.$type === 'bpmn:Collaboration') {
|
|
|
|
const currentKeys = rootElement.correlationKeys;
|
2022-08-16 17:02:14 +00:00
|
|
|
for (const correlationKey in currentKeys) {
|
2022-08-16 15:11:16 +00:00
|
|
|
const currentCorrelation = rootElement.correlationKeys[correlationKey];
|
2022-08-16 17:02:14 +00:00
|
|
|
const currentProperty = {};
|
2022-08-16 15:11:16 +00:00
|
|
|
currentProperty.name = currentCorrelation.name;
|
|
|
|
currentProperty.refs = [];
|
2022-08-16 17:02:14 +00:00
|
|
|
for (const correlationProperty in currentCorrelation.correlationPropertyRef) {
|
|
|
|
currentProperty.refs.push(
|
|
|
|
currentCorrelation.correlationPropertyRef[correlationProperty]
|
|
|
|
);
|
2022-08-16 15:11:16 +00:00
|
|
|
}
|
|
|
|
correlationKeys.push(currentProperty);
|
|
|
|
}
|
|
|
|
}
|
2022-08-08 20:08:44 +00:00
|
|
|
}
|
2022-08-16 17:02:14 +00:00
|
|
|
return correlationKeys;
|
2022-08-08 20:08:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 15:22:08 +00:00
|
|
|
export function findMessageModdleElements(element) {
|
2022-08-08 20:08:44 +00:00
|
|
|
const messages = [];
|
2022-08-16 17:02:14 +00:00
|
|
|
const root = getRoot(element);
|
2022-08-08 20:08:44 +00:00
|
|
|
for (const rootElement of root.rootElements) {
|
|
|
|
if (rootElement.$type === 'bpmn:Message') {
|
|
|
|
messages.push(rootElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return messages;
|
|
|
|
}
|