import { Definitions, CallActivity, UserTask, FlowNode, FlowElement } from 'bpmn-moddle'; import { ProcessState, ExportedProcessState } from './state/reducers'; import { Token } from './state/reducers/tokens'; import { LogState } from './state/reducers/log'; import { VariablesState } from './state/reducers/variables'; import { AdaptationLogState } from './state/reducers/adaptationLog'; interface FromModdleDefinitionsArgs { processId: string; id: string; globalStartTime: number; definitions: Definitions; variables: { [key: string]: { value: unknown; log?: { changedTime: number; changedBy?: string; oldValue?: any; newValue: any; }[]; }; }; log: LogState; tokens: { tokenId: string; currentFlowElement: { id: string; }; [key: string]: any; }[]; shouldPassTokenHook?: (processInstanceId: string, from: string, to: string, tokenId: string, state: ProcessState) => Promise; shouldActivateFlowNodeHook?: (processInstanceId: string, tokenId: string, flowNode: FlowNode, state: ExportedProcessState) => Promise; sendSignalHook?: (processInstanceId: string, signalId: string) => any; sendMessageHook?: (processInstanceId: string, messageId: string) => any; } interface MigrationArgs { moddleDefinitions: Definitions; tokenMapping?: { add?: { targetFlowElementId: string; [key: string]: any; }[]; move?: { tokenId: string; targetFlowElementId: string; [key: string]: any; }[]; remove?: string[]; }; } /** * @class Holds the state for a BPMN process instance */ export default class BpmnProcessInstance { private id; private stateDelegate; private log; private constructor(); /** * Create a process instance * @param {FromModdleDefinitionsArgs} args * @param {Definitions} args.definitions Definitions obtained out of Moddle BPMN XML parser * @param {string} args.globalStartTime global startTime of instance * @param {string} args.processId id of process for this instance * @param {string} args.id optional id for instance, defaults to auto-generated uuid.v4() * @param {object} args.variables optional initial variables for process instance * @param {LogState} args.log optional initial log for process instance * @param {Array} args.tokens initial tokens of the instance * @param {Function} args.shouldPassTokenHook hook to be called when token is passed to next flowNode * @param {Function} args.shouldActivateFlowNodeHook hook to be called when a new flowNode is activated * @param {Function} args.sendSignalHook hook to be called when signal is send * @param {Function} args.sendMessageHook hook to be called when message is send * @returns {BpmnProcessInstance} a new process instance created from the moddle definitions */ static fromModdleDefinitions(args: FromModdleDefinitionsArgs): BpmnProcessInstance; /****************************** Instance State ******************************/ /** * Get the id of the process instance * @returns {string} id of the process instance */ getId(): string; /** * Get the state stream */ getState$(): import("rxjs").Observable; /** * Will return the current internal state of the instance */ private getInternalState; /** * Get a copy of the current state * @returns {BpmnProcessInstanceState} state snapshot of the current state */ getState(): ExportedProcessState; /** * Pause the process instance */ pause(): void; /** * Resume the process instance */ resume(): void; /** * Stop the process instance * @param reason log the reason for abrupt stop */ stop(reason?: string): void; /** * Clear up any data or functionality (like rxjs subscriptions) */ tearDown(): void; /** * update status of process * @param status - new status of process */ updateProcessStatus(status: string): void; /** * Is the process currently paused? */ isPaused(): boolean; /** * Has the process instance ended? */ isEnded(): boolean; /** * Notify on process pause * @param callback */ onPaused(callback: Function): void; /** * Notify on process end * @param callback */ onEnded(callback: Function): void; /** * Notify on process abortion * @param callback */ onAborted(callback: Function): void; /** * given callback called with current instanceState when changed * @param callback */ onInstanceStateChange(callback: Function): void; /** * Receive a message on behalf of the process. The send message is invoked via hooks * @param messageId - the id of the message */ receiveMessage(messageId: string): void; /** * Receive a signal on behalf of the process. The send signal is invoked via hooks * @param signalid - the id of the signal */ receiveSignal(signalId: string): void; migrate(newProcessId: string, { moddleDefinitions, tokenMapping }: MigrationArgs): void; /****************************** Token State ******************************/ /** * update token with the given id * @param tokenId - the id of the token * @param {attributes} - attributes to be updated */ updateToken(tokenId: string, attributes: { [key: string]: any; }): void; /** * Pause the execution of flowNode at given token * @param tokenId */ pauseToken(tokenId: string): void; /** * Continue the execution of a paused token * @param tokenId */ continueToken(tokenId: string): void; /** * end token with the given id: * current execution will be ended and logged with given state and token state will be set to given state * @param tokenId - the id of the token * @param {attributes} - attributes to be updated */ endToken(tokenId: string, attributes: { state: string; [key: string]: any; }): void; /** * Interrupt the execution at given token: state of token remains * * @param tokenId */ interruptToken(tokenId: string): void; /** * A function that will ensure that a possibly executing flowNode is stopped when the activating token is changed (removed/moved somewhere else) * * @param token The token that will be changed * @param logMessage The message to print on flowNodeChange */ private stopFlowNodeExecutionOnTokenChange; /** * Remove the token with the given id from the instance * * @param tokenId - the id of the token * @param silent if true the removal will not be logged as an adaptation */ removeToken(tokenId: string, silent?: boolean): void; /** * Remove the token with the given id from the instance * * @param tokenId - the id of the token * @param silent if true the removal will not be logged as an adaptation */ private internalRemoveToken; /** * Add a new token to the instance * * @param elementId the element the token should be placed at * @param token optional information about the token to add * @param silent if true the addition will not be logged as an adaptation * @returns the id of the added token */ addToken(elementId: string, token?: { [key: string]: any; }, silent?: boolean): any; /** * Will move an existing token from one flow element to another * * @param elementId - the point of token placement * @param {token} - the id of the token and optional attributes * @param silent if true the move will not be logged as an adaptation */ moveToken(elementId: string, token: { tokenId: string; [key: string]: any; }, silent?: boolean): string; /** * Will move an existing token from one flow element to another * * @param elementId - the point of token placement * @param {token} - the id of the token and optional attributes * @param targetDefinitions used when the definitions will change after the token change to allow registering moves to elements in the new model * @param silent if true the move will not be logged as an adaptation */ private internalMoveToken; /** * Place a token at an arbitrary sequence (wrapper function for addToken or moveToken) * * @param sequenceId - the point of token placement, must be a valid sequence * @param {token} - the id of the token and optional attributes * @param silent if true the change will not be logged as an adaptation */ placeTokenAt(sequenceId: string, token?: { [key: string]: any; }, silent?: boolean): any; /** * Notifiy on end of token * @param callback */ onTokenEnded(callback: Function): void; /****************************** Flow Element State ******************************/ /** * Get a list of all elements of a specific type that are currently open for work * * @param bpmnType the type of element to get a list of * @returns an array of all open elements of the given type alongside the id of the token that activated them for each one */ getOpenElements(bpmnType: string): { tokenId: string; element: FlowElement; }[]; /** * Get a list of currently active user tasks */ getUserTasks(): { tokenId: string; element: UserTask; }[]; /** * Get a list of currently active call activities */ getCallActivities(): { tokenId: string; element: CallActivity; }[]; /** * Get Id of sequenceFlow between 2 elements * @param from - the element id of sourceRef of searched sequenceFlow * @param to - the element id of targetRef of searched sequenceFlow */ getSequenceFlowId(from: string, to: string): string; /** * Helper function for getting all the flow elements inside the instance * * @param moddleDefinitions the definitions to get the flow elements from (defaults to the one of the instance) * @returns the flow elements inside the instance or the given model */ getFlowElements(moddleDefinitions?: Definitions): FlowElement[]; /** * Helper function for getting a specific flow element inside the instance * * @param moddleDefinitions the definitions to get the flow element from (defaults to the one of the instance) * @returns the flow element inside the instance or the given model if it exists */ getFlowElement(flowElementId: string, moddleDefinitions?: Definitions): FlowElement | undefined; /** * Function that allows to end an activity that is currently active * * Used for completeActivity, terminateActivity, failActivity functions * * @param activityId * @param tokenId * @param state the state to end the activity with * @returns if the activity was ended and additional actions can be run */ private endActivity; /** * Manually complete an activity with a tokenId * @param id the id of the activity * @param tokenId the token id of the activity when it was activated */ completeActivity(activityId: string, tokenId: string, variables?: { [key: string]: unknown; }): void; /** * Manually terminate an activity with a tokenId * @param id the id of the activity * @param tokenId the token id of the activity when it was activated */ terminateActivity(activityId: string, tokenId: string): void; /** * Manually abort an activity with a tokenId * @param id the id of the activity * @param tokenId the token id of the activity when it was activated */ failActivity(activityId: string, tokenId: string): void; setFlowNodeState(tokenId: string, state: string): void; resumeFlowNode(tokenId: string, flowNodeId: string): void; /** * Creates a new rxjs stream that emits when a flowNode is completed with one of the given states * * @param allowedEndstates the end states on which to emit * * @returns a rxjs stream that emits every time a flowNode is completed with one of the given states */ private flowNodeExecution$; /** * Notifiy on execution of flow node * @param callback */ onFlowNodeExecuted(callback: Function): void; onActivityInterrupted(activityType: string, callback: Function): void; /** * given callback called with token when user task interrupted * @param callback */ onUserTaskInterrupted(callback: Function): void; /** * given callback called with token when call activity is interrupted * @param callback */ onCallActivityInterrupted(callback: Function): void; /** * given callback called with token when error occured in script Task * @param callback */ onScriptTaskError(callback: Function): void; /****************************** Log State ******************************/ /** * Get log stream of processinstance */ getLog$(): import("rxjs").Observable; /****************************** Execution Log State ******************************/ /** * Creates an execution log stream that notifies about (flowNode completion) events in the instance */ getExecutionLog$(): import("rxjs").Observable<{ execution: { [key: string]: any; flowElementId: string; tokenId: string; executionState: string; startTime?: number | undefined; endTime?: number | undefined; errorMessage?: string | undefined; variableChanges?: { [variableName: string]: { oldValue?: any; newValue: any; changedTime: number; }[]; } | undefined; }; token: Token | undefined; }>; getExecutionLog(): LogState; /** * Add a flowNode execution in log * @param {string} flowElementId - Id of the element * @param {string} tokenId - id of the token * @param {} attributes - attributes to be inserted into the flowNode log */ logExecution(flowElementId: string, tokenId: string, attributes: {}): void; /** * Update a flowNode execution in log * @param {string} flowElementId - Id of the element * @param {string} tokenId - id of the token * @param {} attributes - attributes to be updated in the flowNode log */ updateLog(flowElementId: string, tokenId: string, attributes: {}): void; /** * Merge given execution-log with existing log * @param [log] - log of flow node executions to be added/updated to existing log */ mergeFlowNodeLog(log: LogState): void; /****************************** Adaptation Log State ******************************/ /** * Creates an adaptation log stream that notifies about (manual change) events in the instance */ getAdaptationLog$(): import("rxjs").Observable; getAdaptationLog(): AdaptationLogState; /** * Add a adaptation in the adaptation log * @param type the type of the adaptation * @param time optional if a specific time should be used * @param attributes attributes to be inserted into the adaptation log entry */ logAdaptation(type: string, time?: number, attributes?: {}): void; /** * Update a adaptation entry in the log * @param flowElementId type of the adaptation to update * @param time time the adaptation was logged * @param attributes attributes to be updated in the found adaptation log entry */ updateAdaptationLog(type: string, time: number, attributes: {}): void; /** * Merge given adaptation-log with existing log * @param [log] - log of flow adaptations to be added/updated to existing log */ mergeAdaptationLog(log: AdaptationLogState): void; /****************************** Variable State ******************************/ /** * Get a copy of the current variables * @param {string} tokenId id of a token for which the current variable values (including intermediate values) are requested * @returns {object} variables */ getVariables(tokenId?: string): { [key: string]: any; }; /** * Get a copy of the current variables along with log information * @param {string} tokenId id of a token for which the current variable informations (including intermediate values) are requested * @returns {object} variables */ getVariablesWithLogs(tokenId?: string): { [x: string]: { value: any; log: { changedTime: number; changedBy?: string | undefined; oldValue?: any; newValue: any; }[]; }; }; /** * Set the intermediate value of a variable, by dispatching event * * @param {string} tokenId - Id of the token the variable should be changed in * @param {string} name - name of the variable * @param {*} value - value of the variable * @param global - if the variable should be written directly to the instance state */ setVariable(tokenId: string, name: string, value: any, global?: boolean): void; /** * Merge new entries into variables log * @param variables variables to be merged */ mergeVariableChanges(variables: VariablesState): void; /** * Will change the variables in the instance, this change is considered a manual adaptation * * @param variables a list of variables with the new values * @param silent if set to true the change will not be logged as an adaptation */ updateVariables(variables: { [key: string]: any; }, silent?: boolean): void; } export {}; //# sourceMappingURL=BpmnProcessInstance.d.ts.map