BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
ConditionalEventObserver.cpp
Go to the documentation of this file.
2#include "Engine.h"
3#include "SystemState.h"
4#include "Token.h"
6#include <iostream>
7
8using namespace BPMNOS::Execution;
9
12
14 this->systemState = systemState;
15}
16
18 assert( systemState );
19 assert( dynamic_cast<const DataUpdate*>(observable) );
20 auto dataUpdate = static_cast<const DataUpdate*>(observable);
21
22 if ( dataUpdate->global() ) {
23 // check tokens at all conditional events
24 for ( auto& [_,waitingTokens] : systemState->tokensAwaitingCondition ) {
25 triggerConditionalEvent( dataUpdate, waitingTokens );
26 }
27 }
28 else {
29 // check tokens at conditional events for instance with updated data
30 auto it = systemState->tokensAwaitingCondition.find(dataUpdate->instanceId);
31 if ( it != systemState->tokensAwaitingCondition.end() ) {
32 triggerConditionalEvent( dataUpdate, it->second );
33 }
34 }
35}
36
37void ConditionalEventObserver::triggerConditionalEvent(const DataUpdate* dataUpdate, auto_list< std::weak_ptr<Token> >& waitingTokens) {
38 for ( auto it = waitingTokens.begin(); it != waitingTokens.end(); ) {
39 auto& [token_ptr] = *it;
40 auto token = token_ptr.lock();
41 assert( token->node->extensionElements->represents<BPMNOS::Model::Conditions>() );
42 auto extensionElements = token->node->extensionElements->as<BPMNOS::Model::Conditions>();
43
44 // determine whether data update intersects with data dependencies of conditional event
45 auto intersect = [](const std::vector<const BPMNOS::Model::Attribute*>& first, const std::set<const BPMNOS::Model::Attribute*>& second) -> bool {
46 for ( auto lhs : first ) {
47 if ( second.contains(lhs) ) {
48 return true;
49 }
50 }
51 return false;
52 };
53
54 if ( intersect(dataUpdate->attributes,extensionElements->dataDependencies) ) {
55 // advance token if conditions are satisfied
56 if ( extensionElements->conditionsSatisfied(token->status,*token->data,token->globals) ) {
57 auto engine = const_cast<Engine*>(systemState->engine);
58 engine->commands.emplace_back(std::bind(&Token::advanceToCompleted,token.get()), token.get());
59 it = waitingTokens.erase(it);
60 }
61 else {
62 ++it;
63 }
64 }
65 }
66}
67
void notice(const Observable *observable) override
void triggerConditionalEvent(const DataUpdate *dataUpdate, auto_list< std::weak_ptr< Token > > &waitingTokens)
std::list< Command > commands
List of commands to be executed.
Definition Engine.h:94
A class representing the state that the execution or simulation of a given scenario is in.
Definition SystemState.h:21
std::unordered_map< BPMNOS::number, auto_list< std::weak_ptr< Token > > > tokensAwaitingCondition
Map holding a container of all tokens at a conditional event belonginge to a process instance.
List of tuples with automatic removal of tuples containing an expired weak_ptr.
Definition auto_list.h:15
Class holding extension elements representing conditions for sequence flows and conditional events.
Definition Conditions.h:18
const std::vector< const BPMNOS::Model::Attribute * > & attributes
Definition DataUpdate.h:17