@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
116 lines • 7.21 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExclusiveGatewayHandler = void 0;
const inversify_1 = require("inversify");
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
const index_1 = require("../../../Tools/index");
const ProcessInstance_1 = require("../../ProcessInstance");
const FlowNodeHandlerFactory_1 = require("../FlowNodeHandlerFactory");
const GatewayHandler_1 = require("./GatewayHandler");
let ExclusiveGatewayHandler = class ExclusiveGatewayHandler extends GatewayHandler_1.GatewayHandler {
constructor(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, exclusiveGatewayModel, processInstance) {
super(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, exclusiveGatewayModel, processInstance, 'exclusive_gateway_handler');
}
get exclusiveGateway() {
return this.flowNode;
}
async startExecution() {
this.logger.debug(`Executing ExclusiveGateway instance.`);
return this.executeHandler();
}
async resumeAfterExit(processFlowNodeInstances) {
const flowNodeInstanceAfterGateway = processFlowNodeInstances.find((flowNodeInstance) => flowNodeInstance.previousFlowNodeInstanceId === this.flowNodeInstanceId);
if (flowNodeInstanceAfterGateway) {
const nextFlowNode = this.processInstance.getProcessModelFacade().getFlowNodeById(flowNodeInstanceAfterGateway.flowNodeId);
return [nextFlowNode];
}
// If no follow-up Flow Node Instance was found and no Flow Node follows this gateway, the process has reached its end.
if (this.processInstance.getProcessModelFacade().getNextFlowNodesFor(this.exclusiveGateway).length === 0) {
return undefined;
}
// If no follow-up Flow Node Instance was found, but a Flow Node follows this gateway, the Process Instance died before persisting the next Flow Node Instance.
return this.executeHandler();
}
async executeHandler() {
const isExclusiveJoinGateway = this.exclusiveGateway.gatewayDirection === processcube_engine_sdk_1.Model.Gateways.GatewayDirection.Converging;
if (isExclusiveJoinGateway) {
return this.processInstance.getProcessModelFacade().getNextFlowNodesFor(this.exclusiveGateway);
}
const nextFlowNode = await this.determineNextFlowNode();
return [nextFlowNode];
}
async determineNextFlowNode() {
const outgoingSequenceFlows = this.processInstance.getProcessModelFacade().getOutgoingSequenceFlowsFor(this.exclusiveGateway.id);
const sequenceFlowsWithMatchingCondition = await this.getSequenceFlowsWithMatchingConditions(outgoingSequenceFlows);
if (sequenceFlowsWithMatchingCondition.length === 0) {
if (this.exclusiveGateway.defaultOutgoingSequenceFlowId != undefined) {
const defaultSequenceFlow = outgoingSequenceFlows.find((flow) => flow.id === this.exclusiveGateway.defaultOutgoingSequenceFlowId);
return this.processInstance.getProcessModelFacade().getFlowNodeById(defaultSequenceFlow.targetRef);
}
const errorToThrow = new processcube_engine_sdk_1.BadRequestError(`No outgoing SequenceFlow for ExclusiveGateway \`${this.exclusiveGateway.id}\` has a fulfilled condition!`, 'process');
errorToThrow.additionalInformation = {
processInstanceId: this.processInstance.getProcessInstanceId(),
correlationId: this.processInstance.getCorrelationId(),
};
throw errorToThrow;
}
if (sequenceFlowsWithMatchingCondition.length > 1) {
const errorToThrow = new processcube_engine_sdk_1.BadRequestError(`More than one outgoing SequenceFlow for ExclusiveGateway \`${this.exclusiveGateway.id}\` has a fulfilled condition!`, 'process');
errorToThrow.additionalInformation = {
processInstanceId: this.processInstance.getProcessInstanceId(),
correlationId: this.processInstance.getCorrelationId(),
};
throw errorToThrow;
}
const nextFlowNodeRef = sequenceFlowsWithMatchingCondition[0].targetRef;
return this.processInstance.getProcessModelFacade().getFlowNodeById(nextFlowNodeRef);
}
async getSequenceFlowsWithMatchingConditions(sequenceFlows) {
const isConditionFulfilled = async (condition) => {
try {
return await this.processInstance.executeRuntimeExpression({
expression: condition,
currentFlowNode: this.exclusiveGateway,
currentToken: this.processToken,
previousFlowNode: this.processInstance.getProcessModelFacade().getPreviousFlowNodesFor(this.flowNode)?.pop(),
allowNonObjectResults: true,
});
}
catch (err) {
return false;
}
};
const sequenceFlowsWithMatchingCondition = [];
for (const sequenceFlow of sequenceFlows) {
const sequenceFlowIsDefaultFlow = sequenceFlow.id === this.exclusiveGateway.defaultOutgoingSequenceFlowId;
const sequenceFlowHasNoCondition = sequenceFlow.conditionExpression == undefined;
// The default Flow must not be conditional, so that it can be used as a fallback.
// "Normal" Sequence Flows without any condition are treated like a Sequence Flow with an unfulfilled condition.
if (sequenceFlowIsDefaultFlow || sequenceFlowHasNoCondition) {
continue;
}
const conditionIsFulfilled = await isConditionFulfilled(sequenceFlow.conditionExpression.expression);
if (conditionIsFulfilled) {
sequenceFlowsWithMatchingCondition.push(sequenceFlow);
}
}
return sequenceFlowsWithMatchingCondition;
}
};
exports.ExclusiveGatewayHandler = ExclusiveGatewayHandler;
exports.ExclusiveGatewayHandler = ExclusiveGatewayHandler = __decorate([
(0, inversify_1.injectable)(),
__metadata("design:paramtypes", [index_1.EventMiddlewareHandler,
FlowNodeHandlerFactory_1.FlowNodeHandlerFactory,
index_1.FlowNodeInstanceDatabaseAdapter, Object, ProcessInstance_1.ProcessInstance])
], ExclusiveGatewayHandler);
//# sourceMappingURL=ExclusiveGatewayHandler.js.map