BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
Number.cpp
Go to the documentation of this file.
1#include "Number.h"
2#include "Keywords.h"
3#include "StringRegistry.h"
6#include <cassert>
7
8namespace BPMNOS {
9
11 for (const auto& value : values) {
12 if (value.get().has_value()) {
13 push_back(value.get().value());
14 }
15 else {
16 push_back(std::nullopt);
17 }
18 }
19}
20
22 : SharedValues(other)
23{
24 add(values);
25}
26
28 add(values);
29}
30
32 for ( auto& value : values ) {
33 push_back(value);
34 }
35}
36
37double stod(const std::string& str) {
38 try {
39 double result = std::stod(str);
40 return result;
41 }
42 catch( ... ) {
43 throw std::runtime_error("Cannot convert '" + str + "' to double" );
44 }
45}
46
47int stoi(const std::string& str) {
48 try {
49 int result = std::stoi(str);
50 return result;
51 }
52 catch( ... ) {
53 throw std::runtime_error("Cannot convert '" + str + "' to int" );
54 }
55}
56
57number to_number(const std::string& valueString, const ValueType& type) {
58 switch ( type ) {
60 return number(stringRegistry( valueString ));
62 return number(BPMNOS::stoi( valueString ));
64 return number(BPMNOS::stod( valueString ));
66 return number(stringRegistry( valueString ));
68 return number(collectionRegistry( valueString ));
69 }
70 throw std::logic_error("to_number: unknown value type " + type );
71}
72
73number to_number(const Value& value, const ValueType& type) {
74 switch ( type ) {
76 if (std::holds_alternative<std::string>(value)) {
77 return number(std::get<std::string>(value) == Keyword::True ? 1 : 0);
78 }
79 else if (std::holds_alternative<bool>(value)) [[likely]] {
80 return number(std::get<bool>(value) ? 1 : 0);
81 }
82 else if (std::holds_alternative<int>(value)) {
83 return number(std::get<int>(value) ? 1 : 0);
84 }
85 else if (std::holds_alternative<double>(value)) {
86 return number(std::get<double>(value) ? 1 : 0);
87 }
88 else [[unlikely]] {
89 throw std::logic_error("to_number: value holds no alternative" );
90 }
92 if (std::holds_alternative<std::string>(value)) {
93 return number(BPMNOS::stoi(std::get<std::string>(value)));
94 }
95 else if (std::holds_alternative<bool>(value)) {
96 return number(std::get<bool>(value) ? 1 : 0);
97 }
98 else if (std::holds_alternative<int>(value)) [[likely]] {
99 return number(std::get<int>(value));
100 }
101 else if (std::holds_alternative<double>(value)) {
102 return number((int)std::get<double>(value));
103 }
104 else [[unlikely]] {
105 throw std::logic_error("to_number: value holds no alternative" );
106 }
108 if (std::holds_alternative<std::string>(value)) {
109 return number(BPMNOS::stod(std::get<std::string>(value)));
110 }
111 else if (std::holds_alternative<bool>(value)) {
112 return number(std::get<bool>(value) ? 1 : 0);
113 }
114 else if (std::holds_alternative<int>(value)) {
115 return number(std::get<int>(value));
116 }
117 else if (std::holds_alternative<double>(value)) [[likely]] {
118 return number(std::get<double>(value));
119 }
120 else [[unlikely]] {
121 throw std::logic_error("to_number: value holds no alternative" );
122 }
124 if (std::holds_alternative<std::string>(value)) [[likely]] {
125 return number(stringRegistry(std::get<std::string>(value)));
126 }
127 else if (std::holds_alternative<bool>(value)) {
128 return number(std::get<bool>(value) ? 1 : 0);
129 }
130 else if (std::holds_alternative<int>(value)) {
131 return number(stringRegistry(std::to_string(std::get<int>(value))));
132 }
133 else if (std::holds_alternative<double>(value)) {
134 return number(stringRegistry( std::to_string(std::get<double>(value))));
135 }
136 else [[unlikely]] {
137 throw std::logic_error("to_number: value holds no alternative" );
138 }
140 if (std::holds_alternative<std::string>(value)) [[likely]] {
141 return number(collectionRegistry(std::get<std::string>(value)));
142 }
143 else [[unlikely]] {
144 throw std::logic_error("to_number: illegal parameters" );
145 }
146 }
147 throw std::logic_error("to_number: unknown value type " + type );
148}
149
150std::string to_string(number numericValue, const ValueType& type) {
151 switch ( type ) {
153 return numericValue ? Keyword::True : Keyword::False;
155 return std::to_string((int)numericValue);
157 return std::to_string((double)numericValue);
159 return stringRegistry[(std::size_t)numericValue];
161 return collectionRegistry[(std::size_t)numericValue].collection;
162 }
163 throw std::logic_error("to_string: unknown value type " + type );
164}
165
166BPMNOS::Values mergeValues(const std::vector<BPMNOS::Values>& valueSets) {
167 assert( !valueSets.empty() );
168 size_t n = valueSets.front().size();
169 BPMNOS::Values result;
170 result.resize(n);
172
173 for ( size_t i = 0; i < n; i++ ) {
174 for ( auto& values : valueSets ) {
176 if ( result[i].value() < values[i].value() ) {
177 result[i] = values[i];
178 }
179 }
180 else if ( !result[i].has_value() ) {
181 result[i] = values[i];
182 }
183 else if ( values[i].has_value() && values[i].value() != result[i].value() ) {
184 result[i] = std::nullopt;
185 break;
186 }
187 }
188 }
189 return result;
190}
191
192} // namespace BPMNOS::Model
CollectionRegistry collectionRegistry
StringRegistry stringRegistry
const std::string False
Definition Keywords.h:8
const std::string True
Definition Keywords.h:9
std::string to_string(number numericValue, const ValueType &type)
Converts a number to a string.
Definition Number.cpp:150
number to_number(const std::string &valueString, const ValueType &type)
Converts a string to a number.
Definition Number.cpp:57
BPMNOS_NUMBER_TYPE number
Definition Number.h:42
double stod(const std::string &str)
Definition Number.cpp:37
BPMNOS::Values mergeValues(const std::vector< BPMNOS::Values > &valueSets)
Definition Number.cpp:166
ValueType
Definition Value.h:9
@ INTEGER
Definition Value.h:9
@ STRING
Definition Value.h:9
@ BOOLEAN
Definition Value.h:9
@ COLLECTION
Definition Value.h:9
@ DECIMAL
Definition Value.h:9
int stoi(const std::string &str)
Definition Number.cpp:47
std::variant< bool, int, double, std::string > Value
Definition Value.h:10
void add(Values &values)
Definition Number.cpp:31
Values()=default