BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
Token.h
Go to the documentation of this file.
1#ifndef BPMNOS_Execution_Token_H
2#define BPMNOS_Execution_Token_H
3
4#include <bpmn++.h>
9#include "Observable.h"
10#include <nlohmann/json.hpp>
11#include <iostream>
12#include <cassert>
13
14namespace BPMNOS::Execution {
15
16class Engine;
17class StateMachine;
18class Token;
19typedef std::vector< std::shared_ptr<Token> > Tokens;
20class EventDispatcher;
21class DecisionRequest;
22
23/**
24 * @brief Represents a token running through a (sub)process.
25 *
26 * The `Token` class is used to represent tokens running through the execution flow of a (sub)process.
27 * The execution logic is documented in section @ref execution_logic.
28 *
29 * @note All tokens can be WITHDRAWN at any time, e.g., when the respective state machine terminates.
30 *
31 * @note It is assumed that @ref XML::bpmn::tCompensateEventDefinition::waitForCompletion is `true`.
32 *
33 * @note It is assumed that @ref XML::bpmn::tAdHocSubProcess::ordering is `Sequential`.
34 */
35class Token : public Observable, public std::enable_shared_from_this<Token> {
36private:
37 friend class SystemState;
38 friend class StateMachine;
39 friend class Engine;
41
42public:
43 constexpr Type getObservableType() const override { return Type::Token; };
44 const StateMachine* owner; ///< State machine owning the token
45 std::shared_ptr<StateMachine> owned; ///< State machine owned by the token
48 enum class State { CREATED, READY, ENTERED, BUSY, COMPLETED, EXITING, DEPARTED, ARRIVED, WAITING, DONE, FAILED, FAILING, WITHDRAWN }; ///< The states that a token can be in
50 static inline std::string stateName[] = { "CREATED", "READY", "ENTERED", "BUSY", "COMPLETED", "EXITING", "DEPARTED", "ARRIVED", "WAITING", "DONE", "FAILED", "FAILING", "WITHDRAWN" };
51 Token(const StateMachine* owner, const BPMN::FlowNode* node, const Values& status);
52 Token(const Token* other);
53 Token(const std::vector<Token*>& others);
54 ~Token();
55
58 SharedValues* data; ///< Pointer to the data of the owner or owned state machine subprocesses)
60 std::shared_ptr<DecisionRequest> decisionRequest;
61 Token* performing; ///< Pointer to the activity token currently performed (only applies if node is a performer referenced by sequential ad-hoc subprocesses)
62 auto_list< std::weak_ptr<Token> > pendingSequentialEntries; ///< List of tokens awaiting an activity entry (only applies if node is a performer referenced by sequential ad-hoc subprocesses)
63
64 void setStatus(const BPMNOS::Values& other); ///< Copies all elements except the instance id from `other` to `status`
65
66 bool ready() const { return state == State::READY; };
67 bool entered() const { return state == State::ENTERED; };
68 bool busy() const { return state == State::BUSY; };
69 bool completed() const { return state == State::COMPLETED; };
70 bool arrived() const { return state == State::ARRIVED; };
71 bool waiting() const { return state == State::WAITING; };
72 bool done() const { return state == State::DONE; };
73 bool failed() const { return state == State::FAILED; };
74
75 nlohmann::ordered_json jsonify() const;
76private:
77 void occupySequentialPerformer();
78 void releaseSequentialPerformer();
79
80 bool entryIsFeasible() const; ///< Check restrictions within current and ancestor scopes
81 bool exitIsFeasible() const; ///< Check restrictions within current and ancestor scopes
82// bool satisfiesInheritedRestrictions() const; ///< Check restrictions within ancestor scopes
83
84 void computeInitialValues( const BPMNOS::Model::ExtensionElements* extensionElements );
85
86 void advanceFromCreated();
87 void advanceToReady();
88 void advanceToEntered();
89 void advanceToIdle();
90 void advanceToBusy();
91
92 void advanceToCompleted();
93
94 void advanceToExiting();
95 void advanceToDone();
96 void advanceToDeparting();
97 void advanceToDeparted(const BPMN::SequenceFlow* sequenceFlow);
98 void advanceToArrived();
99
100 void advanceToFailed();
101
102 void terminate(); ///< Terminates state machine owned by token
103
104 void awaitCompensation();
105
106 void awaitReadyEvent(); ///< Wait for ready event for activities
107
108 void awaitEntryEvent(); ///< Wait for entry event for activities
109
110 void awaitChoiceEvent(); ///< Wait for choices to be made for decision tasks
111 void awaitTaskCompletionEvent(); ///< Wait for completion event for tasks (except for decision tasks)
112 void awaitExitEvent(); ///< Wait for exit event for activities
113
114 void awaitTimer(BPMNOS::number time); ///< Wait for message trigger at catching timer events
115 void awaitSignal(BPMNOS::number name); ///< Wait for signal with given name at catching signal events
116 void awaitConditions(BPMNOS::number instanceId); ///< Wait for conditions at catching conditional events within root instance
117 void awaitMessageDelivery(); ///< Wait for message delivery event
118
119 void awaitEventBasedGateway(); ///< Wait for catching event at event-based gateways
120
121 void awaitGatewayActivation(); ///< Wait for activiation of merging gateway
122
123 template<typename DecisionType, typename... Args>
124 std::shared_ptr<DecisionType> createDecisionRequest(Args&&... args);
125
126 void withdraw(); ///< Remove token
127
128 void emitSignal();
129
130 BPMNOS::VariedValueMap getSignalContent(const BPMNOS::Model::ContentMap& contentMap); ///< Returns content of signal
131 void setSignalContent(BPMNOS::VariedValueMap& sourceMap); // Applies content of signal
132
133 void sendMessage( size_t index = 0 );
134
135 Token* getSequentialPerfomerToken() const; ///< Returns token at sequential performer for tokens at activities within sequential adhoc subprocesses
136
137 void update(State newState); ///< Updates token state and timestamp before calling notify()
138
139 void notify() const; ///< Inform all listeners about token update
140
141 /**
142 * Returns a merged status from the status of each token
143 **/
144 template <typename TokenPtr>
145 static BPMNOS::Values mergeStatus(const std::vector<TokenPtr>& tokens) {
146 assert( !tokens.empty() );
147 size_t n = tokens.front()->status.size();
148 BPMNOS::Values result;
149 result.resize(n);
151
152 for ( size_t i = 0; i < n; i++ ) {
153 for ( auto& token : tokens ) {
155 if ( result[i].value() < token->status[i].value() ) {
156 result[i] = token->status[i];
157 }
158 }
159 else if ( !result[i].has_value() ) {
160 result[i] = token->status[i];
161 }
162 else if ( token->status[i].has_value() && token->status[i].value() != result[i].value() ) {
163 result[i] = std::nullopt;
164 break;
165 }
166 }
167 }
168 return result;
169 }
170};
171
172} // namespace BPMNOS::Execution
173
174#endif // BPMNOS_Execution_Token_H
175
Represents a state machine for BPMN execution of a scope in the model.
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
constexpr Type getObservableType() const override
Definition Token.h:43
const BPMN::FlowNode * node
Definition Token.h:46
Token * performing
Pointer to the activity token currently performed (only applies if node is a performer referenced by ...
Definition Token.h:61
const BPMNOS::Model::AttributeRegistry & getAttributeRegistry() const
Definition Token.cpp:139
const StateMachine * owner
State machine owning the token.
Definition Token.h:44
std::shared_ptr< StateMachine > owned
State machine owned by the token.
Definition Token.h:45
bool arrived() const
Definition Token.h:70
void setStatus(const BPMNOS::Values &other)
Copies all elements except the instance id from other to status
Definition Token.cpp:156
bool failed() const
Definition Token.h:73
SharedValues * data
Pointer to the data of the owner or owned state machine subprocesses)
Definition Token.h:58
bool entered() const
Definition Token.h:67
std::shared_ptr< DecisionRequest > decisionRequest
Definition Token.h:60
const BPMN::SequenceFlow * sequenceFlow
Definition Token.h:47
bool completed() const
Definition Token.h:69
static std::string stateName[]
Definition Token.h:50
auto_list< std::weak_ptr< Token > > pendingSequentialEntries
List of tokens awaiting an activity entry (only applies if node is a performer referenced by sequenti...
Definition Token.h:62
bool ready() const
Definition Token.h:66
bool waiting() const
Definition Token.h:71
bool done() const
Definition Token.h:72
bool busy() const
Definition Token.h:68
nlohmann::ordered_json jsonify() const
Definition Token.cpp:162
List of tuples with automatic removal of tuples containing an expired weak_ptr.
Definition auto_list.h:15
Class holding extension elements representing execution data for nodes.
Base class for BPMN elements that may contain incoming and outgoing sequence flows.
Definition bpmn++.h:16670
The SequenceFlow class encapsulates the information and relationships associated with a sequence flow...
Definition bpmn++.h:16633
std::vector< std::shared_ptr< Token > > Tokens
Definition Token.h:19
std::unordered_map< std::string, std::unique_ptr< Content > > ContentMap
Definition Content.h:27
std::unordered_map< std::string, std::variant< std::optional< number >, std::string > > VariedValueMap
Definition Number.h:59
BPMNOS_NUMBER_TYPE number
Definition Number.h:42
Represents a pending decision.