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
5AttributeRegistry::AttributeRegistry(const LIMEX::Handle<double>& limexHandle)
6 : limexHandle(limexHandle)
7{
8}
9
10void AttributeRegistry::add(Attribute* attribute) {
11 if ( contains(attribute->name) ) {
12 throw std::runtime_error("AttributeRegistry: duplicate attribute name '" + attribute->name + "'");
13 }
14 if ( attribute->category == Attribute::Category::STATUS ) {
15 attribute->index = statusAttributes.size();
16 statusAttributes.push_back(attribute);
17 statusMap[attribute->name] = attribute;
18 }
19 else if ( attribute->category == Attribute::Category::DATA ) {
20 attribute->index = dataAttributes.size();
21 dataAttributes.push_back(attribute);
22 dataMap[attribute->name] = attribute;
23 }
24 else /* if ( attribute->category == Attribute::Category::GLOBAL )*/ {
25 attribute->index = globalAttributes.size();
26 globalAttributes.push_back(attribute);
27 globalMap[attribute->name] = attribute;
28 }
29}
30
31Attribute* AttributeRegistry::operator[](const std::string& name) const {
32 std::unordered_map< std::string, Attribute*>::const_iterator it;
33 if ( it = statusMap.find(name);
34 it != statusMap.end()
35 ) {
36 return it->second;
37 }
38 else if ( it = dataMap.find(name);
39 it != dataMap.end()
40 ) {
41 return it->second;
42 }
43 else if ( it = globalMap.find(name);
44 it != globalMap.end()
45 ) {
46 return it->second;
47 }
48 else {
49 throw std::runtime_error("AttributeRegistry: cannot find attribute with name '" + name + "'");
50 }
51 return nullptr;
52}
53
54bool AttributeRegistry::contains(const std::string& name) const {
55 return statusMap.contains(name) || dataMap.contains(name) || globalMap.contains(name);
56}
57
58bool AttributeRegistry::contains(const Attribute* attribute) const {
59 if (attribute->category == Attribute::Category::STATUS) {
60 return attribute->index < statusAttributes.size() &&
61 statusAttributes[attribute->index] == attribute;
62 }
63 else if (attribute->category == Attribute::Category::DATA) {
64 return attribute->index < dataAttributes.size() &&
65 dataAttributes[attribute->index] == attribute;
66 }
67 else /* if (attribute->category == Attribute::Category::GLOBAL) */ {
68 return attribute->index < globalAttributes.size() &&
69 globalAttributes[attribute->index] == attribute;
70 }
71}
72
73
74std::optional<BPMNOS::number> AttributeRegistry::getValue(const Attribute* attribute, const Values& status, const Values& data, const Values& globals) const {
75 if ( attribute->category == Attribute::Category::STATUS ) {
76 assert(attribute->index < status.size());
77 return status[attribute->index];
78 }
79 else if ( attribute->category == Attribute::Category::DATA ) {
80 assert(attribute->index < data.size());
81 return data[attribute->index];
82 }
83 else /* if ( attribute->category == Attribute::Category::GLOBAL )*/ {
84 assert(attribute->index < globals.size());
85 return globals[attribute->index];
86 }
87}
88
89std::optional<BPMNOS::number> AttributeRegistry::getValue(const Attribute* attribute, const Values& status, const SharedValues& data, const Values& globals) const {
90 if ( attribute->category == Attribute::Category::STATUS ) {
91 assert(attribute->index < status.size());
92 return status[attribute->index];
93 }
94 else if ( attribute->category == Attribute::Category::DATA ) {
95 assert(attribute->index < data.size());
96 return data[attribute->index].get();
97 }
98 else /* if ( attribute->category == Attribute::Category::GLOBAL )*/ {
99 assert(attribute->index < globals.size());
100 return globals[attribute->index];
101 }
102}
103
104void AttributeRegistry::setValue(const Attribute* attribute, Values& status, Values& data, Values& globals, std::optional<BPMNOS::number> value) const {
105 if ( attribute->category == Attribute::Category::STATUS ) {
106 assert(attribute->index < status.size());
107 status[attribute->index] = value;
108 }
109 else if ( attribute->category == Attribute::Category::DATA ) {
110 assert(attribute->index < data.size());
111 data[attribute->index] = value;
112 }
113 else /* if ( attribute->category == Attribute::Category::GLOBAL )*/ {
114 assert(attribute->index < globals.size());
115 globals[attribute->index] = value;
116 }
117}
118
119void AttributeRegistry::setValue(const Attribute* attribute, Values& status, SharedValues& data, Values& globals, std::optional<BPMNOS::number> value) const {
120 if ( attribute->category == Attribute::Category::STATUS ) {
121 assert(attribute->index < status.size());
122 status[attribute->index] = value;
123 }
124 else if ( attribute->category == Attribute::Category::DATA ) {
125 assert(attribute->index < data.size());
126 data[attribute->index].get() = value;
127 }
128 else /* if ( attribute->category == Attribute::Category::GLOBAL )*/ {
129 assert(attribute->index < globals.size());
130 globals[attribute->index] = value;
131 }
132}
bool contains(const std::string &name) const
std::unordered_map< std::string, Attribute * > dataMap
void setValue(const Attribute *attribute, Values &status, Values &data, Values &globals, std::optional< BPMNOS::number > value) const
AttributeRegistry(const LIMEX::Handle< double > &limexHandle)
std::unordered_map< std::string, Attribute * > globalMap
Attribute * operator[](const std::string &name) const
std::vector< Attribute * > statusAttributes
std::vector< Attribute * > dataAttributes
std::unordered_map< std::string, Attribute * > statusMap
std::vector< Attribute * > globalAttributes
std::optional< BPMNOS::number > getValue(const Attribute *attribute, const Values &status, const Values &data, const Values &globals) const
const std::string name
Definition Attribute.h:32
size_t index
Index of attribute (is automatically set by attribute registry).
Definition Attribute.h:28