BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
StateMachine.h
Go to the documentation of this file.
1#ifndef BPMNOS_Execution_StateMachine_H
2#define BPMNOS_Execution_StateMachine_H
3
4#include <bpmn++.h>
5#include "Token.h"
9
10namespace BPMNOS::Execution {
11
12class StateMachine;
13typedef std::vector< std::shared_ptr<StateMachine> > StateMachines;
14
15class SystemState;
16
17/**
18 * @brief Represents a state machine for BPMN execution of a scope in the model.
19 *
20 * This class manages all tokens for BPMN execution of a given scope. It also holds
21 * a state machine for each child scope instantiated.
22 *
23 * @note A state machine without @ref parentToken represents a @ref BPMN::Process.
24 * @par
25 * @note Inclusive gateways are not yet supported.
26 *
27 * @attention Event subprocesses within event subprocesses are not yet tested (and may not be supported).
28 */
29class StateMachine : public std::enable_shared_from_this<StateMachine> {
30public:
31 StateMachine(const SystemState* systemState, const BPMN::Process* process, Values dataAttributes);
32 StateMachine(const SystemState* systemState, const BPMN::Scope* scope, Token* parentToken, Values dataAttributes, std::optional<BPMNOS::number> instance = std::nullopt);
33 StateMachine(const StateMachine* other);
35
37
39 const BPMN::Process* process; ///< Pointer to the top-level process.
40 const BPMN::Scope* scope; ///< Pointer to the current scope.
41 const StateMachine* root; ///< Pointer to the root state machine
42 std::optional<BPMNOS::number> instance; ///< Numeric representation of instance id (TODO: can we const this?)
43
45 Values ownedData; ///< Container holding data attributes owned by the state machine.
46 SharedValues data; ///< Container holding references to all data attributes.
47
48 Tokens tokens; ///< Container with all tokens within the scope of the state machine.
49 std::shared_ptr<StateMachine> interruptingEventSubProcess; ///< State machines representing an active event subprocess that is interrupting.
50 StateMachines nonInterruptingEventSubProcesses; ///< Container with state machines of all active event subprocesses that are not interrupting.
51 StateMachines pendingEventSubProcesses; ///< Container with state machines of all inactive event subprocesses that may be triggered.
52
53 Tokens compensationTokens; ///< Container with all tokens created for a compensation activity.
54 StateMachines compensationEventSubProcesses; ///< Container with state machines created for a compensation event subprocesses of a child subprocess
55 StateMachines compensableSubProcesses; ///< Container holding state machines for completed subprocesses with a compensation event subprocess and compensation tokens
56
57 Tokens getCompensationTokens(const BPMN::Activity* activity = nullptr) const; ///< Returns the compensation tokens for a given activity or for all activities
58 void run(Values status); ///< Create initial token and advance it.
59
60private:
61 friend class Engine;
62 friend class SystemState;
63 friend class Token;
64
65 std::map< const BPMN::FlowNode*, unsigned int > instantiations; ///< Instantiation counter for start events of non-interrupting event subprocesses
66
67 void registerRecipient(); ///< Register new state machine to allow directed message delivery
68 void unregisterRecipient(); ///< Register new state machine id to withdraw directed messages
69
70 void createChild(Token* parent, const BPMN::Scope* scope, Values data, std::optional<BPMNOS::number> instance = std::nullopt); ///< Method creating the state machine for a (sub)process
71
72 void createCompensationTokenForBoundaryEvent(const BPMN::BoundaryEvent* compensateBoundaryEvent, BPMNOS::Values status); ///< Method creating a compensation token at a compensate boundary event of an activity
73// void createCompensationTokenForEventSubProcess(const BPMN::EventSubProcess* compensationEventSubProcess, Token* token); ///< Method creating a compensation token at a compensation event subproces of an activity
74
75 void createCompensationEventSubProcess(const BPMN::EventSubProcess* eventSubProcess, BPMNOS::Values status); ///< Method creating the compensation event subproces of an activity
76
77// void createInterruptingEventSubprocess(const StateMachine* pendingEventSubProcess, const BPMNOS::Values& status); ///< Method creating the state machine for an interrupting event subprocess
78
79// void createNonInterruptingEventSubprocess(const StateMachine* pendingEventSubProcess, const BPMNOS::Values& status); ///< Method creating the state machine for an non-interrupting event subprocess
80
81 void initiateBoundaryEvents(Token* token); ///< Method placing tokens on all boundary events
82 void initiateBoundaryEvent(Token* token, const BPMN::FlowNode*); ///< Method placing tokens on a boundary event
83 void initiateEventSubprocesses(Token* token); ///< Method initiating pending event subprocesses
84 void createMultiInstanceActivityTokens(Token* token); ///< Method creating tokens for multi-instance activities
85 void deleteMultiInstanceActivityToken(Token* token); ///< Method creating tokens for multi-instance activities
86 void deleteAdHocSubProcessToken(Token* token);
87 void compensateActivity(Token* token); ///< Method creating the compensation activity of an activity
88
89 std::vector<Token*> createTokenCopies(Token* token, const std::vector<BPMN::SequenceFlow*>& sequenceFlows);
90 void createMergedToken(const BPMN::FlowNode* gateway);
91
92 void shutdown(); ///< Shutdown state machine after successful execution
93 void interruptActivity(Token* token);
94 void clearObsoleteTokens();
95
96 void handleDivergingGateway(Token* token);
97 void handleEventBasedGatewayActivation(Token* token);
98 void handleEscalation(Token* token);
99 void handleFailure(Token* token);
100 void attemptGatewayActivation(const BPMN::FlowNode* node);
101 void attemptShutdown();
102// void deleteChild(StateMachine* child); ///< Method removing completed state machine from parent
103 void deleteNonInterruptingEventSubProcess(StateMachine* eventSubProcess); ///< Method removing completed event subprocess from context
104 void deleteCompensationEventSubProcess(StateMachine* eventSubProcess); ///< Method removing completed compensation event subprocess from context
105 void deleteTokensAwaitingBoundaryEvent(Token* token); ///< Method removing all waiting tokens attached to activity of token
106 void completeCompensationActivity(Token* token); ///< Method handling the completion of a compensation activity
107 void completeCompensationEventSubProcess(); ///< Method handling the completion of a compensation event subprocess
108 void advanceTokenWaitingForCompensation(Token* waitingToken); ///< Method advancing a token that was waiting for a compensation to be completed
109 void compensate(Tokens compensations, Token* waitingToken); ///< Method compensating all activities in reverse order before the waiting token may advance
110
111 Token* findTokenAwaitingErrorBoundaryEvent(Token* activityToken); ///< Method finding the token at a boundary event catching an error thrown in activity
112};
113
114
115} // namespace BPMNOS::Execution
116
117#endif // BPMNOS_Execution_StateMachine_H
Represents a state machine for BPMN execution of a scope in the model.
const BPMN::Scope * scope
Pointer to the current scope.
StateMachine(const SystemState *systemState, const BPMN::Process *process, Values dataAttributes)
StateMachines pendingEventSubProcesses
Container with state machines of all inactive event subprocesses that may be triggered.
const SystemState * systemState
const BPMN::Process * process
Pointer to the top-level process.
StateMachines compensationEventSubProcesses
Container with state machines created for a compensation event subprocesses of a child subprocess.
StateMachines nonInterruptingEventSubProcesses
Container with state machines of all active event subprocesses that are not interrupting.
const StateMachine * root
Pointer to the root state machine.
Values getData(const BPMN::Scope *scope)
std::shared_ptr< StateMachine > interruptingEventSubProcess
State machines representing an active event subprocess that is interrupting.
void run(Values status)
Create initial token and advance it.
std::optional< BPMNOS::number > instance
Numeric representation of instance id (TODO: can we const this?)
StateMachines compensableSubProcesses
Container holding state machines for completed subprocesses with a compensation event subprocess and ...
Tokens tokens
Container with all tokens within the scope of the state machine.
Values ownedData
Container holding data attributes owned by the state machine.
Tokens compensationTokens
Container with all tokens created for a compensation activity.
SharedValues data
Container holding references to all data attributes.
Tokens getCompensationTokens(const BPMN::Activity *activity=nullptr) const
Returns the compensation tokens for a given activity or for all activities.
A class representing the state that the execution or simulation of a given scenario is in.
Definition SystemState.h:21
Represents a token running through a (sub)process.
Definition Token.h:35
Base class for all boundary events attached to an Activity.
Definition bpmn++.h:17200
Base class for BPMN elements that may contain incoming and outgoing sequence flows.
Definition bpmn++.h:16670
Base class for BPMN elements that may contain a ChildNode elements.
Definition bpmn++.h:16510
std::vector< std::shared_ptr< StateMachine > > StateMachines
std::vector< std::shared_ptr< Token > > Tokens
Definition Token.h:19