BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
CPController.h
Go to the documentation of this file.
1#ifndef BPMNOS_Execution_CPController_H
2#define BPMNOS_Execution_CPController_H
3
4#include <bpmn++.h>
5#include "Controller.h"
6#include "Evaluator.h"
7#include "FlattenedGraph.h"
9#include <cp.h>
10#include <unordered_map>
11#include <utility>
12#include <memory>
13
14
15
16namespace BPMNOS::Execution {
17
18
19
20/**
21 * @brief A controller dispatching decisions obtained from a solution of a constraint program
22 */
23class CPController : public Controller {
25public:
27 void connect(Mediator* mediator);
28// std::vector< std::unique_ptr<EventDispatcher> > eventDispatchers;
29 const CP::Model& getModel() { return model; }
30protected:
32 std::shared_ptr<Event> dispatchEvent(const SystemState* systemState);
34// std::vector< const BPMNOS::Model::Scenario::InstanceData* > instances;
36 CP::Model model;
37public:
38protected:
39 void createCP(); /// Method creating the constraint program
42 void createMessageContent(const Vertex& vertex);
43 std::vector< std::reference_wrapper<const Vertex> > getReachableVertices(const Vertex& initialVertex); /// Returns a topologically sorted vector of all vertices reachable from the given vertex
44 void initializeVertices(const Vertex& initialVertex);
45 void createVertexVariables(const Vertex& vertex);
46 void createEntryVariables(const Vertex& vertex);
47 void createExitVariables(const Vertex& vertex);
48
49 void createSequenceConstraints(const Vertex& vertex);
50
51 void createGlobalIndexVariable(const Vertex& vertex);
52 void createDataVariables(const Vertex& vertex);
53 void createDataIndexVariables(const Vertex& vertex);
54
56 const CP::Variable& defined;
57 const CP::Variable& value;
58 // Constructor
59 AttributeVariables(const CP::Variable& defined, const CP::Variable& value)
61 // Copy Constructor
63 : defined(other.defined), value(other.value) {}
64 // Assignment Operator
66 return AttributeVariables(other.defined,other.value);
67 }
68 };
69
71 CP::IndexedVariables& defined;
72 CP::IndexedVariables& value;
73 };
74
75 void createStatus(const Vertex& vertex);
76 void createEntryStatus(const Vertex& vertex);
77 void createExitStatus(const Vertex& vertex);
78 std::vector<AttributeVariables> createAlternativeEntryStatus(const Vertex& vertex, const BPMNOS::Model::AttributeRegistry& attributeRegistry, std::vector< std::pair<const CP::Variable&, std::vector<AttributeVariables>& > > alternatives);
79 std::vector<AttributeVariables> createMergedStatus(const Vertex& vertex, const BPMNOS::Model::AttributeRegistry& attributeRegistry, std::vector< std::pair<const CP::Variable&, std::vector<AttributeVariables>& > > inputs);
80
81/*
82 std::unordered_map<const BPMN::MessageThrowEvent*, std::vector<BPMNOS::number> > originInstances; /// Map containing all instance identifiers for all message throw events
83
84 auto getReachableEndNodes( const BPMN::Scope* scope ) {
85 // Check if the given scope exists in the map
86 auto it = reachableFlowNodes.find(scope);
87 if (it == reachableFlowNodes.end()) {
88 assert(!"Unknown scope");
89 }
90
91 // Use ranges and views to filter nodes with empty outgoing vectors
92 auto reachableEndNodes = it->second | std::ranges::views::filter([](const BPMN::FlowNode* node) {
93 return node->outgoing.empty();
94 });
95
96 return reachableEndNodes;
97 };
98*/
99
100 struct pair_hash {
101 template <class T1, class T2>
102 std::size_t operator () (const std::pair<T1,T2> &p) const {
103 auto h1 = std::hash<T1>{}(p.first);
104 auto h2 = std::hash<T2>{}(p.second);
105 // Mainly for demonstration purposes, i.e. works but is overly simple
106 // In the real world, use sth. like boost.hash_combine
107 // return h1 ^ h2;
108 return h1 * 31 + h2; // Prime multiplication
109 }
110 };
111
112 std::vector<const Vertex*> vertices; /// Container of all vertices considered
113 std::vector<const Vertex*> messageRecipients; /// Container of all vertices catching a message
114// CP::reference_vector<const CP::Variable> sequence; /// Container of sequence positions
115 std::unordered_map< const Vertex*, const CP::Variable& > position; /// Variables holding sequence positions for all vertices
116 std::unordered_map< const Vertex*, const CP::Variable& > visit; /// Variables indicating whether the a token enters or leaves a vertex
117
118 std::unordered_map< std::pair< const Vertex*, const Vertex* >, const CP::Variable&, pair_hash > tokenFlow; /// Variables indicating whether the a token flows from one vertex to another
119 std::unordered_map< std::pair< const Vertex*, const Vertex* >, const CP::Variable&, pair_hash > messageFlow; /// Variables indicating whether the a token flows from one vertex to another
120
121 std::vector< IndexedAttributeVariables > globals; /// Variables representing global attributes after i-th modification
122 std::unordered_map< const Vertex*, const CP::Variable& > globalIndex; /// Variables representing an index representing the state of the global attributes
123
124 std::unordered_map< const Vertex*, std::vector< IndexedAttributeVariables > > data; /// Variables representing data attributes owned by an entry vertex after i-th modification
125 std::unordered_map< const Vertex*, std::vector< CP::reference_vector< const CP::Variable > > > dataIndex; /// Variables representing an index representing the state of the data attributes for each data owner
126
127 std::unordered_map< const Vertex*, std::vector<AttributeVariables> > status; /// Variables representing status attributes of a vertex
128 std::unordered_map< std::pair< const Vertex*, const Vertex* >, std::vector<AttributeVariables>, pair_hash > statusFlow; /// Variables representing status attributes flowing from one vertex to another
129
130 std::unordered_map< const Vertex*, std::vector< std::tuple< std::string_view, size_t, AttributeVariables> > > messageContent; /// Variables representing status attributes of a vertex
131
132/*
133 std::unordered_map<const BPMNOS::Model::Attribute*, const BPMN::Scope* > dataOwner;/// Map allowing to look up the scope owning a data attribute
134 std::unordered_map<const BPMN::Scope*, std::vector<const BPMN::Node* > > sequentialActivities;/// Map allowing to look up the sequential activities that may change a data attribute (assumimng that intermediate changes are not propagated)
135
136 using NodeReference = std::pair<size_t, const BPMN::Node*>;
137 using DataReference = std::pair<size_t, const BPMNOS::Model::Attribute*>;
138 using MessageCatchReference = std::pair<size_t, const BPMN::MessageCatchEvent*>;
139 using MessageThrowReference = std::pair<size_t, const BPMN::MessageThrowEvent*>;
140 using ScopeReference = std::pair<size_t, const BPMN::Scope*>;
141 using SequenceFlowReference = std::pair<size_t,const BPMN::SequenceFlow*>;
142
143 template <typename T>
144 std::string identifier(const std::pair<size_t, const T*>& reference) {
145 return BPMNOS::to_string(reference.first,STRING) + "," + reference.second->id;
146 }
147
148 template <typename ReferenceType, typename ValueType>
149 using variable_map = std::unordered_map<ReferenceType, ValueType, pair_hash>;
150
151
152 variable_map<NodeReference, const CP::Variable&> nodeVariables;
153 variable_map<SequenceFlowReference, const CP::Variable&> sequenceFlowVariables;
154 variable_map<MessageCatchReference, variable_map<MessageThrowReference, const CP::Variable&> > messageFlowVariables;
155
156 std::vector<AttributeVariables> globals;
157 variable_map<DataReference, std::vector< AttributeVariables> > dataState; /// Variables representing data attributes after i-th activity of a sequential performer is conducted
158
159 variable_map<NodeReference, variable_map<ScopeReference, std::vector< std::reference_wrapper< const CP::Variable > > > > entryDataState; /// Binary variables indicating the data states upon entry
160 variable_map<NodeReference, variable_map<ScopeReference, std::vector< std::reference_wrapper< const CP::Variable > > > > exitDataState; /// Binary variables indicating the data states upon exit
161
162 variable_map<NodeReference, std::vector<AttributeVariables> > entryData; /// Variables representing data state upon entry to a node
163 variable_map<NodeReference, std::vector<AttributeVariables> > exitData; /// Variables representing data state upon exit of a node
164
165 variable_map<NodeReference, std::vector<AttributeVariables> > entryStatus;
166 variable_map<NodeReference, std::vector<AttributeVariables> > exitStatus;
167
168 variable_map<NodeReference, std::vector< std::vector<AttributeVariables> > > interimData; /// Variables representing data attributes after the i-th operator is applied
169 variable_map<NodeReference, std::vector< std::vector<AttributeVariables> > > interimStatus; /// Variables representing data attributes after i-th operator is applied
170
171
172private:
173 std::unordered_map<NodeReference, std::set<const BPMN::Scope*>, pair_hash > scopesOwningData;
174 std::set<NodeReference> pendingEntry;
175 std::list<NodeReference> pendingExit;
176 void addChildren(ScopeReference reference);
177 void addSuccessors(NodeReference reference);
178
179 void addGlobalVariable( const BPMNOS::Model::Attribute* attribute, std::optional<BPMNOS::number> value );
180
181 bool checkEntryDependencies(NodeReference reference);
182 bool checkExitDependencies(NodeReference reference);
183
184 void addEntryVariables(NodeReference reference);
185 void createNodeTraversalVariables(NodeReference reference); /// x_n
186 void createEntryDataStateVariables(NodeReference reference); /// y^entry_{n,s,i} where n is a node, s is a scope with data, and i is a non-negative index for the entry data state at n for data belonging to s
187 void deduceEntryAttributeVariable(std::vector<AttributeVariables>& attributeVariables, const BPMNOS::Model::Attribute* attribute, NodeReference reference, variable_map<NodeReference, std::vector<AttributeVariables> >& entryAttributes, variable_map<NodeReference, std::vector<AttributeVariables> >& exitAttributes);
188
189 void addExitVariables(NodeReference reference);
190 void createExitDataStateVariables(NodeReference reference); /// y^exit_{n,s,i} where n is a node, s is a scope with data, and i is a non-negative index for the exit data state at n for data belonging to s
191 void constrainExitDataStateVariables(ScopeReference reference); /// y^exit_{n,s,i} where n is a node, s is a scope with data, and i is a non-negative index for the exit data state at n for data belonging to s
192// void createSequenceFlowTraversalVariables(SequenceFlowReference reference);
193*/
194};
195
196} // namespace BPMNOS::Execution
197
198#endif // BPMNOS_Execution_CPController_H
199
A controller dispatching decisions obtained from a solution of a constraint program.
std::vector< std::reference_wrapper< const Vertex > > getReachableVertices(const Vertex &initialVertex)
std::unordered_map< const Vertex *, const CP::Variable & > visit
Variables holding sequence positions for all vertices.
std::unordered_map< const Vertex *, const CP::Variable & > globalIndex
Variables representing global attributes after i-th modification.
void createEntryVariables(const Vertex &vertex)
void connect(Mediator *mediator)
std::unordered_map< const Vertex *, const CP::Variable & > position
Container of all vertices catching a message.
void createGlobalVariables()
Method creating the constraint program.
std::shared_ptr< Event > dispatchEvent(const SystemState *systemState)
const BPMNOS::Model::Scenario * scenario
std::unordered_map< std::pair< const Vertex *, const Vertex * >, std::vector< AttributeVariables >, pair_hash > statusFlow
Variables representing status attributes of a vertex.
void createEntryStatus(const Vertex &vertex)
std::unordered_map< const Vertex *, std::vector< CP::reference_vector< const CP::Variable > > > dataIndex
Variables representing data attributes owned by an entry vertex after i-th modification.
void createGlobalIndexVariable(const Vertex &vertex)
std::unordered_map< const Vertex *, std::vector< AttributeVariables > > status
Variables representing an index representing the state of the data attributes for each data owner.
std::unordered_map< const Vertex *, std::vector< IndexedAttributeVariables > > data
Variables representing an index representing the state of the global attributes.
void createDataIndexVariables(const Vertex &vertex)
CPController(const BPMNOS::Model::Scenario *scenario)
void createVertexVariables(const Vertex &vertex)
void createMessageContent(const Vertex &vertex)
std::vector< IndexedAttributeVariables > globals
Variables indicating whether the a token flows from one vertex to another.
std::vector< const Vertex * > messageRecipients
Container of all vertices considered.
std::vector< AttributeVariables > createMergedStatus(const Vertex &vertex, const BPMNOS::Model::AttributeRegistry &attributeRegistry, std::vector< std::pair< const CP::Variable &, std::vector< AttributeVariables > & > > inputs)
void createExitVariables(const Vertex &vertex)
std::unordered_map< const Vertex *, std::vector< std::tuple< std::string_view, size_t, AttributeVariables > > > messageContent
Variables representing status attributes flowing from one vertex to another.
void createSequenceConstraints(const Vertex &vertex)
std::unordered_map< std::pair< const Vertex *, const Vertex * >, const CP::Variable &, pair_hash > messageFlow
Variables indicating whether the a token flows from one vertex to another.
const CP::Model & getModel()
std::vector< AttributeVariables > createAlternativeEntryStatus(const Vertex &vertex, const BPMNOS::Model::AttributeRegistry &attributeRegistry, std::vector< std::pair< const CP::Variable &, std::vector< AttributeVariables > & > > alternatives)
void initializeVertices(const Vertex &initialVertex)
Returns a topologically sorted vector of all vertices reachable from the given vertex.
std::unordered_map< std::pair< const Vertex *, const Vertex * >, const CP::Variable &, pair_hash > tokenFlow
Variables indicating whether the a token enters or leaves a vertex.
void createDataVariables(const Vertex &vertex)
void createStatus(const Vertex &vertex)
void createExitStatus(const Vertex &vertex)
const FlattenedGraph flattenedGraph
std::vector< const Vertex * > vertices
Base class for an execution controller which dispatches events to the engine and listens to notificat...
Definition Controller.h:15
Represents an abstract base class for a pending Evaluator.
Definition Evaluator.h:15
Represents a graph containing all BPMN nodes that may receive a token during execution of a scenario.
A class representing the state that the execution or simulation of a given scenario is in.
Definition SystemState.h:21
The Scenario class holds data for all BPMN instances.
Definition Scenario.h:20
AttributeVariables operator=(const AttributeVariables &other)
AttributeVariables(const CP::Variable &defined, const CP::Variable &value)
AttributeVariables(const AttributeVariables &other)
std::size_t operator()(const std::pair< T1, T2 > &p) const
Represents an abstract base class for a class that is an event listener and notifier.
Definition Mediator.h:13