BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
RandomChoice.cpp
Go to the documentation of this file.
1#include "RandomChoice.h"
6#include <cassert>
7
8using namespace BPMNOS::Execution;
9
11 : randomGenerator{std::random_device{}()}
12{
13}
14
15std::shared_ptr<Event> RandomChoice::dispatchEvent( const SystemState* systemState ) {
16 for ( auto& [token_ptr, request_ptr] : systemState->pendingChoiceDecisions ) {
17 if( auto request = request_ptr.lock() ) {
18 assert( request );
19 auto token = request->token;
20 assert( token );
21 assert( token->node );
22 assert( token->node->represents<BPMNOS::Model::DecisionTask>() );
23 assert( token->node->extensionElements->represents<BPMNOS::Model::ExtensionElements>() );
24
25 auto extensionElements = token->node->extensionElements->as<BPMNOS::Model::ExtensionElements>();
26
27 BPMNOS::Values choices;
28 auto status = token->status;
29 auto data = *token->data;
30 auto globals = token->globals;
31 for ( auto& choice : extensionElements->choices ) {
32 // make random choice
33 if ( !choice->enumeration.empty() ) {
34 auto values = choice->getEnumeration(status,data,globals);
35 if ( values.size() ) {
36 std::uniform_int_distribution<> random_distribution(0,(int)values.size()-1);
37 choices.push_back( values[ (size_t)random_distribution(randomGenerator) ] );
38 choice->attributeRegistry.setValue(choice->attribute, status, data, globals, choices.back());
39 }
40 else {
41 choices.clear();
42 break;
43 }
44 }
45 else if ( choice->lowerBound.has_value() && choice->upperBound.has_value() ) {
46 auto [min,max] = choice->getBounds(status,data,globals);
47 if ( min > max ) {
48 choices.clear();
49 break;
50 }
51 if ( choice->attribute->type == DECIMAL ) {
52 std::uniform_real_distribution<> random_distribution((double)min,(double)max);
53 choices.push_back( BPMNOS::to_number( random_distribution(randomGenerator), DECIMAL ) );
54 choice->attributeRegistry.setValue(choice->attribute, status, data, globals, choices.back());
55 }
56 else {
57 std::uniform_int_distribution<> random_distribution((int)min,(int)max);
58 choices.push_back( BPMNOS::to_number( random_distribution(randomGenerator), INTEGER ) );
59 choice->attributeRegistry.setValue(choice->attribute, status, data, globals, choices.back());
60 }
61 }
62 else {
63 choices.clear();
64 break;
65 }
66 }
67 if ( !choices.empty() ) {
68 return std::make_shared<ChoiceEvent>(token, std::move(choices));
69 }
70 }
71 }
72 return nullptr;
73}
74
BPMNOS::RandomGenerator randomGenerator
std::shared_ptr< Event > dispatchEvent(const SystemState *systemState) override
A class representing the state that the execution or simulation of a given scenario is in.
Definition SystemState.h:21
auto_list< std::weak_ptr< Token >, std::weak_ptr< DecisionRequest > > pendingChoiceDecisions
Definition SystemState.h:72
Class representing a task in which one or more choices have to be made.
Class holding extension elements representing execution data for nodes.
number to_number(const std::string &valueString, const ValueType &type)
Converts a string to a number.
Definition Number.cpp:57
@ INTEGER
Definition Value.h:9
@ DECIMAL
Definition Value.h:9
STL namespace.