@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
108 lines • 6.65 kB
JavaScript
;
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.InclusiveSplitGatewayHandler = 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 InclusiveSplitGatewayHandler = class InclusiveSplitGatewayHandler extends GatewayHandler_1.GatewayHandler {
constructor(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, inclusiveGatewayModel, processInstance) {
super(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, inclusiveGatewayModel, processInstance, 'inclusive_gateway_handler');
}
get inclusiveGateway() {
return this.flowNode;
}
async startExecution() {
this.logger.debug(`Executing InclusiveGateway instance.`);
return this.executeHandler();
}
async resumeAfterExit(processFlowNodeInstances) {
const flowNodeInstancesAfterGateway = processFlowNodeInstances.filter((flowNodeInstance) => flowNodeInstance.previousFlowNodeInstanceId === this.flowNodeInstanceId);
const nextFlowNodes = [];
for (const flowNodeInstance of flowNodeInstancesAfterGateway) {
const nextFlowNode = this.processInstance.getProcessModelFacade().getFlowNodeById(flowNodeInstance.flowNodeId);
nextFlowNodes.push(nextFlowNode);
}
if (nextFlowNodes.length > 0) {
return nextFlowNodes;
}
// If no follow-up Flow Node Instances were found and no Flow Nodes follow this gateway, the process has reached its end.
if (this.processInstance.getProcessModelFacade().getNextFlowNodesFor(this.inclusiveGateway).length === 0) {
return undefined;
}
// If no follow-up Flow Node Instances were 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 nextFlowNodes = await this.determineNextFlowNodes();
return nextFlowNodes;
}
async determineNextFlowNodes() {
const outgoingSequenceFlows = this.processInstance.getProcessModelFacade().getOutgoingSequenceFlowsFor(this.inclusiveGateway.id);
const sequenceFlowsWithMatchingCondition = await this.getSequenceFlowsWithMatchingConditions(outgoingSequenceFlows);
if (sequenceFlowsWithMatchingCondition.length === 0) {
if (this.inclusiveGateway.defaultOutgoingSequenceFlowId != undefined) {
const defaultSequenceFlow = outgoingSequenceFlows.find((flow) => flow.id === this.inclusiveGateway.defaultOutgoingSequenceFlowId);
return [this.processInstance.getProcessModelFacade().getFlowNodeById(defaultSequenceFlow.targetRef)];
}
const errorToThrow = new processcube_engine_sdk_1.BadRequestError(`No outgoing SequenceFlow for InclusiveGateway \`${this.inclusiveGateway.id}\` has a fulfilled condition!`, 'process');
errorToThrow.additionalInformation = {
processInstanceId: this.processInstance.getProcessInstanceId(),
correlationId: this.processInstance.getCorrelationId(),
};
throw errorToThrow;
}
const nextFlowNodeRefs = sequenceFlowsWithMatchingCondition.map((sequenceFlow) => sequenceFlow.targetRef);
const nextFlowNodes = nextFlowNodeRefs.map((nextFlowNodeRef) => this.processInstance.getProcessModelFacade().getFlowNodeById(nextFlowNodeRef));
return nextFlowNodes;
}
async getSequenceFlowsWithMatchingConditions(sequenceFlows) {
const isConditionFulfilled = async (condition) => {
try {
return await this.processInstance.executeRuntimeExpression({
expression: condition,
currentFlowNode: this.inclusiveGateway,
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.inclusiveGateway.defaultOutgoingSequenceFlowId;
// The default Flow must not be conditional, so that it can be used as a fallback.
if (sequenceFlowIsDefaultFlow) {
continue;
}
// "Normal" Sequence Flows without any condition are treated like a Sequence Flow with a fulfilled condition.
const sequenceFlowHasNoCondition = sequenceFlow.conditionExpression?.expression == undefined;
if (sequenceFlowHasNoCondition || (await isConditionFulfilled(sequenceFlow.conditionExpression?.expression))) {
sequenceFlowsWithMatchingCondition.push(sequenceFlow);
}
}
return sequenceFlowsWithMatchingCondition;
}
};
exports.InclusiveSplitGatewayHandler = InclusiveSplitGatewayHandler;
exports.InclusiveSplitGatewayHandler = InclusiveSplitGatewayHandler = __decorate([
(0, inversify_1.injectable)(),
__metadata("design:paramtypes", [index_1.EventMiddlewareHandler,
FlowNodeHandlerFactory_1.FlowNodeHandlerFactory,
index_1.FlowNodeInstanceDatabaseAdapter, Object, ProcessInstance_1.ProcessInstance])
], InclusiveSplitGatewayHandler);
//# sourceMappingURL=InclusiveSplitGatewayHandler.js.map