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
65 bool ready() const { return state == State::READY; };
66 bool entered() const { return state == State::ENTERED; };
67 bool busy() const { return state == State::BUSY; };
68 bool completed() const { return state == State::COMPLETED; };
69 bool arrived() const { return state == State::ARRIVED; };
70 bool waiting() const { return state == State::WAITING; };
71 bool done() const { return state == State::DONE; };
72 bool failed() const { return state == State::FAILED; };
73
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 completionIsFeasible() const; ///< Check restrictions within current and ancestor scopes
82 bool exitIsFeasible() const; ///< Check restrictions within current and ancestor scopes
83// bool satisfiesInheritedRestrictions() const; ///< Check restrictions within ancestor scopes
84
85 void computeInitialValues( const BPMNOS::Model::ExtensionElements* extensionElements );
86
87 void advanceFromCreated();
88 void advanceToReady();
89 void advanceToEntered();
90 void advanceToIdle();
91 void advanceToBusy();
92
93 void advanceToCompleted();
94
95 void advanceToExiting();
96 void advanceToDone();
97 void advanceToDeparting();
98 void advanceToDeparted(const BPMN::SequenceFlow* sequenceFlow);
99 void advanceToArrived();
100
101 void advanceToFailed();
102
103 void terminate(); ///< Terminates state machine owned by token
104
105 void awaitCompensation();
106
107 void awaitReadyEvent(); ///< Wait for ready event for activities
108
109 void awaitEntryEvent(); ///< Wait for entry event for activities
110
111 void awaitChoiceEvent(); ///< Wait for choices to be made for decision tasks
112 void awaitTaskCompletionEvent(); ///< Wait for completion event for tasks (except for decision tasks)
113 void awaitExitEvent(); ///< Wait for exit event for activities
114
115 void awaitTimer(BPMNOS::number time); ///< Wait for message trigger at catching timer events
116 void awaitSignal(BPMNOS::number name); ///< Wait for signal with given name at catching signal events
117 void awaitConditions(BPMNOS::number instanceId); ///< Wait for conditions at catching conditional events within root instance
118 void awaitMessageDelivery(); ///< Wait for message delivery event
119
120 void awaitEventBasedGateway(); ///< Wait for catching event at event-based gateways
121
122 void awaitGatewayActivation(); ///< Wait for activiation of merging gateway
123
124 template<typename DecisionType, typename... Args>
125 std::shared_ptr<DecisionType> createDecisionRequest(Args&&... args);
126
127 void withdraw(); ///< Remove token
128
129 void emitSignal();
130
131 BPMNOS::VariedValueMap getSignalContent(const BPMNOS::Model::ContentMap& contentMap); ///< Returns content of signal
132 void setSignalContent(BPMNOS::VariedValueMap& sourceMap); // Applies content of signal
133
134 void sendMessage( size_t index = 0 );
135public:
136 Token* getSequentialPerformerToken() const; ///< Returns token at sequential performer for tokens at activities within sequential adhoc subprocesses
137private:
138 void update(State newState); ///< Updates token state and timestamp before calling notify()
139
140 void notify() const; ///< Inform all listeners about token update
141
142 /**
143 * Returns a merged status from the status of each token
144 **/
145 template <typename TokenPtr>
146 static BPMNOS::Values mergeStatus(const std::vector<TokenPtr>& tokens) {
147 assert( !tokens.empty() );
148 size_t n = tokens.front()->status.size();
149 BPMNOS::Values result;
150 result.resize(n);
152
153 for ( size_t i = 0; i < n; i++ ) {
154 for ( auto& token : tokens ) {
156 if ( result[i].value() < token->status[i].value() ) {
157 result[i] = token->status[i];
158 }
159 }
160 else if ( !result[i].has_value() ) {
161 result[i] = token->status[i];
162 }
163 else if ( token->status[i].has_value() && token->status[i].value() != result[i].value() ) {
164 result[i] = std::nullopt;
165 break;
166 }
167 }
168 }
169 return result;
170 }
171};
172
173} // namespace BPMNOS::Execution
174
175#endif // BPMNOS_Execution_Token_H
176
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
BPMNOS::number getInstanceId() const
Definition Token.cpp:156
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:69
bool failed() const
Definition Token.h:72
SharedValues * data
Pointer to the data of the owner or owned state machine subprocesses)
Definition Token.h:58
bool entered() const
Definition Token.h:66
std::shared_ptr< DecisionRequest > decisionRequest
Definition Token.h:60
const BPMN::SequenceFlow * sequenceFlow
Definition Token.h:47
bool completed() const
Definition Token.h:68
static std::string stateName[]
Definition Token.h:50
Token * getSequentialPerformerToken() const
Returns token at sequential performer for tokens at activities within sequential adhoc subprocesses.
Definition Token.cpp:1449
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:65
bool waiting() const
Definition Token.h:70
bool done() const
Definition Token.h:71
bool busy() const
Definition Token.h:67
nlohmann::ordered_json jsonify() const
Definition Token.cpp:160
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:16694
The SequenceFlow class encapsulates the information and relationships associated with a sequence flow...
Definition bpmn++.h:16657
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:69
BPMNOS_NUMBER_TYPE number
Definition Number.h:50
Represents a pending decision.