BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
AttributeRegistry.cpp
Go to the documentation of this file.
1#include "AttributeRegistry.h"
2
3using namespace BPMNOS::Model;
4
5void AttributeRegistry::add(Attribute* attribute) {
6 if ( contains(attribute->name) ) {
7 throw std::runtime_error("AttributeRegistry: duplicate attribute name '" + attribute->name + "'");
8 }
9 if ( attribute->category == Attribute::Category::STATUS ) {
10 attribute->index = statusAttributes.size();
11 statusAttributes[attribute->name] = attribute;
12 }
13 else if ( attribute->category == Attribute::Category::DATA ) {
14 attribute->index = dataAttributes.size();
15 dataAttributes[attribute->name] = attribute;
16 }
17 else /* if ( attribute->category == Attribute::Category::GLOBAL )*/ {
18 attribute->index = globalAttributes.size();
19 globalAttributes[attribute->name] = attribute;
20 }
21}
22
23Attribute* AttributeRegistry::operator[](const std::string& name) const {
24 std::map< std::string, Attribute*>::const_iterator it;
25 if ( it = statusAttributes.find(name);
26 it != statusAttributes.end()
27 ) {
28 return it->second;
29 }
30 else if ( it = dataAttributes.find(name);
31 it != dataAttributes.end()
32 ) {
33 return it->second;
34 }
35 else if ( it = globalAttributes.find(name);
36 it != globalAttributes.end()
37 ) {
38 return it->second;
39 }
40 else {
41 throw std::runtime_error("AttributeRegistry: cannot find attribute with name '" + name + "'");
42 }
43 return nullptr;
44}
45
46bool AttributeRegistry::contains(const std::string& name) const {
47 return statusAttributes.contains(name) || dataAttributes.contains(name) || globalAttributes.contains(name);
48}
49
50
51std::optional<BPMNOS::number> AttributeRegistry::getValue(const Attribute* attribute, const Values& status, const Values& data, const Values& globals) const {
52 if ( attribute->category == Attribute::Category::STATUS ) {
53 assert(attribute->index < status.size());
54 return status[attribute->index];
55 }
56 else if ( attribute->category == Attribute::Category::DATA ) {
57 assert(attribute->index < data.size());
58 return data[attribute->index];
59 }
60 else /* if ( attribute->category == Attribute::Category::GLOBAL )*/ {
61 assert(attribute->index < globals.size());
62 return globals[attribute->index];
63 }
64}
65
66std::optional<BPMNOS::number> AttributeRegistry::getValue(const Attribute* attribute, const Values& status, const SharedValues& data, const Values& globals) const {
67 if ( attribute->category == Attribute::Category::STATUS ) {
68 assert(attribute->index < status.size());
69 return status[attribute->index];
70 }
71 else if ( attribute->category == Attribute::Category::DATA ) {
72 assert(attribute->index < data.size());
73 return data[attribute->index].get();
74 }
75 else /* if ( attribute->category == Attribute::Category::GLOBAL )*/ {
76 assert(attribute->index < globals.size());
77 return globals[attribute->index];
78 }
79}
80
81void AttributeRegistry::setValue(const Attribute* attribute, Values& status, Values& data, Values& globals, std::optional<BPMNOS::number> value) const {
82 if ( attribute->category == Attribute::Category::STATUS ) {
83 assert(attribute->index < status.size());
84 status[attribute->index] = value;
85 }
86 else if ( attribute->category == Attribute::Category::DATA ) {
87 assert(attribute->index < data.size());
88 data[attribute->index] = value;
89 }
90 else /* if ( attribute->category == Attribute::Category::GLOBAL )*/ {
91 assert(attribute->index < globals.size());
92 globals[attribute->index] = value;
93 }
94}
95
96void AttributeRegistry::setValue(const Attribute* attribute, Values& status, SharedValues& data, Values& globals, std::optional<BPMNOS::number> value) const {
97 if ( attribute->category == Attribute::Category::STATUS ) {
98 assert(attribute->index < status.size());
99 status[attribute->index] = value;
100 }
101 else if ( attribute->category == Attribute::Category::DATA ) {
102 assert(attribute->index < data.size());
103 data[attribute->index].get() = value;
104 }
105 else /* if ( attribute->category == Attribute::Category::GLOBAL )*/ {
106 assert(attribute->index < globals.size());
107 globals[attribute->index] = value;
108 }
109}
bool contains(const std::string &name) const
void setValue(const Attribute *attribute, Values &status, Values &data, Values &globals, std::optional< BPMNOS::number > value) const
Attribute * operator[](const std::string &name) const
std::map< std::string, Attribute * > dataAttributes
std::map< std::string, Attribute * > globalAttributes
std::map< std::string, Attribute * > statusAttributes
std::optional< BPMNOS::number > getValue(const Attribute *attribute, const Values &status, const Values &data, const Values &globals) const
const std::string name
Name of attribute and optial initial assignment.
Definition Attribute.h:32
size_t index
Index of attribute (is automatically set by attribute registry).
Definition Attribute.h:28