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"
5#include "encode_collection.h"
8#include <cassert>
9
10namespace BPMNOS {
11
13 for (const auto& value : values) {
14 if (value.get().has_value()) {
15 push_back(value.get().value());
16 }
17 else {
18 push_back(std::nullopt);
19 }
20 }
21}
22
24 : SharedValues(other)
25{
26 add(values);
27}
28
30 add(values);
31}
32
34 for ( auto& value : values ) {
35 push_back(value);
36 }
37}
38
39double stod(const std::string& str) {
40 try {
41 double result = std::stod(str);
42 return result;
43 }
44 catch( ... ) {
45 throw std::runtime_error("Cannot convert '" + str + "' to double" );
46 }
47}
48
49int stoi(const std::string& str) {
50 try {
51 int result = std::stoi(str);
52 return result;
53 }
54 catch( ... ) {
55 throw std::runtime_error("Cannot convert '" + str + "' to int" );
56 }
57}
58
59number to_number(const std::string& valueString, const ValueType& type) {
60 switch ( type ) {
62 return number(stringRegistry( valueString ));
64 return number(BPMNOS::stoi( valueString ));
66 return number(BPMNOS::stod( valueString ));
68 return number(stringRegistry( valueString ));
70 // it is assumed that all collections are already encoded
71 return number(BPMNOS::stoi( valueString ));
72// return number(collectionRegistry( valueString ));
73 }
74 throw std::logic_error("to_number: unknown value type " + type );
75}
76
77number to_number(const Value& value, const ValueType& type) {
78 switch ( type ) {
80 if (std::holds_alternative<std::string>(value)) {
81 return number(std::get<std::string>(value) == Keyword::True ? 1 : 0);
82 }
83 else if (std::holds_alternative<bool>(value)) [[likely]] {
84 return number(std::get<bool>(value) ? 1 : 0);
85 }
86 else if (std::holds_alternative<int>(value)) {
87 return number(std::get<int>(value) ? 1 : 0);
88 }
89 else if (std::holds_alternative<double>(value)) {
90 return number(std::get<double>(value) ? 1 : 0);
91 }
92 else [[unlikely]] {
93 throw std::logic_error("to_number: value holds no alternative" );
94 }
96 if (std::holds_alternative<std::string>(value)) {
97 return number(BPMNOS::stoi(std::get<std::string>(value)));
98 }
99 else if (std::holds_alternative<bool>(value)) {
100 return number(std::get<bool>(value) ? 1 : 0);
101 }
102 else if (std::holds_alternative<int>(value)) [[likely]] {
103 return number(std::get<int>(value));
104 }
105 else if (std::holds_alternative<double>(value)) {
106 return number((int)std::get<double>(value));
107 }
108 else [[unlikely]] {
109 throw std::logic_error("to_number: value holds no alternative" );
110 }
112 if (std::holds_alternative<std::string>(value)) {
113 return number(BPMNOS::stod(std::get<std::string>(value)));
114 }
115 else if (std::holds_alternative<bool>(value)) {
116 return number(std::get<bool>(value) ? 1 : 0);
117 }
118 else if (std::holds_alternative<int>(value)) {
119 return number(std::get<int>(value));
120 }
121 else if (std::holds_alternative<double>(value)) [[likely]] {
122 return number(std::get<double>(value));
123 }
124 else [[unlikely]] {
125 throw std::logic_error("to_number: value holds no alternative" );
126 }
128 if (std::holds_alternative<std::string>(value)) [[likely]] {
129 return number(stringRegistry(std::get<std::string>(value)));
130 }
131 else if (std::holds_alternative<bool>(value)) {
132 return number(std::get<bool>(value) ? 1 : 0);
133 }
134 else if (std::holds_alternative<int>(value)) {
135 return number(stringRegistry(std::to_string(std::get<int>(value))));
136 }
137 else if (std::holds_alternative<double>(value)) {
138 return number(stringRegistry( std::to_string(std::get<double>(value))));
139 }
140 else [[unlikely]] {
141 throw std::logic_error("to_number: value holds no alternative" );
142 }
144 if (std::holds_alternative<std::string>(value)) [[likely]] {
145 return number( std::stoi( encodeCollection( encodeQuotedStrings( std::get<std::string>(value) ) ) ) );
146 }
147 else [[unlikely]] {
148 throw std::logic_error("to_number: illegal conversion" );
149 }
150 }
151 throw std::logic_error("to_number: unknown value type " + type );
152}
153
154std::string to_string(number numericValue, const ValueType& type) {
155 switch ( type ) {
157 return numericValue ? Keyword::True : Keyword::False;
159 return std::to_string((int)numericValue);
161 return BPMNOS::to_string((double)numericValue);
163 return stringRegistry[(std::size_t)numericValue];
165 std::string result;
166 for ( auto value : collectionRegistry[(std::size_t)numericValue] ) {
167 result += ", " + BPMNOS::to_string(value);
168 }
169 result.front() = '[';
170 result += " ]";
171 return result;
172 }
173 throw std::logic_error("to_string: unknown value type " + type );
174}
175
176std::string to_string(double value) {
177 std::string result = std::to_string(value);
178 if ( result.contains('.') ) {
179 while ( result.back() == '0' ) {
180 result.pop_back();
181 }
182 if ( result.back() == '.' ) {
183 result.pop_back();
184 }
185 }
186 return result;
187}
188
189BPMNOS::Values mergeValues(const std::vector<BPMNOS::Values>& valueSets) {
190 assert( !valueSets.empty() );
191 size_t n = valueSets.front().size();
192 BPMNOS::Values result;
193 result.resize(n);
195
196 for ( size_t i = 0; i < n; i++ ) {
197 for ( auto& values : valueSets ) {
199 if ( result[i].value() < values[i].value() ) {
200 result[i] = values[i];
201 }
202 }
203 else if ( !result[i].has_value() ) {
204 result[i] = values[i];
205 }
206 else if ( values[i].has_value() && values[i].value() != result[i].value() ) {
207 result[i] = std::nullopt;
208 break;
209 }
210 }
211 }
212 return result;
213}
214
215} // 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:154
number to_number(const std::string &valueString, const ValueType &type)
Converts a string to a number.
Definition Number.cpp:59
std::string encodeQuotedStrings(std::string text)
std::string encodeCollection(std::string text)
Function to replace a collection that is not preceded by an alphanumeric or underscore.
BPMNOS_NUMBER_TYPE number
Definition Number.h:50
double stod(const std::string &str)
Definition Number.cpp:39
BPMNOS::Values mergeValues(const std::vector< BPMNOS::Values > &valueSets)
Definition Number.cpp:189
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:49
std::variant< bool, int, double, std::string > Value
Definition Value.h:10
void add(Values &values)
Definition Number.cpp:33
Values()=default