-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSubEnvironmentDescription.cpp
More file actions
322 lines (313 loc) · 17.6 KB
/
Copy pathSubEnvironmentDescription.cpp
File metadata and controls
322 lines (313 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "flamegpu/model/SubEnvironmentDescription.h"
#include "flamegpu/model/ModelData.h"
#include "flamegpu/model/SubModelData.h"
#include "flamegpu/model/SubEnvironmentData.h"
#include "flamegpu/model/EnvironmentDescription.h"
#include "flamegpu/model/EnvironmentDirectedGraphData.cuh"
namespace flamegpu {
CSubEnvironmentDescription::CSubEnvironmentDescription(std::shared_ptr<SubEnvironmentData> data)
: subenvironment(std::move(data)) { }
CSubEnvironmentDescription::CSubEnvironmentDescription(std::shared_ptr<const SubEnvironmentData> data)
: subenvironment(std::const_pointer_cast<SubEnvironmentData>(data)) { }
bool CSubEnvironmentDescription::operator==(const CSubEnvironmentDescription& rhs) const {
return *this->subenvironment == *rhs.subenvironment; // Compare content is functionally the same
}
bool CSubEnvironmentDescription::operator!=(const CSubEnvironmentDescription& rhs) const {
return !(*this == rhs);
}
/**
* Const Accessors
*/
std::string CSubEnvironmentDescription::getPropertyMapping(const std::string& sub_property_name) const {
const auto v = subenvironment->properties.find(sub_property_name);
if (v != subenvironment->properties.end())
return v->second;
THROW exception::InvalidAgentState("SubEnvironment property '%s', either does not exist or has not been mapped yet, "
"in SubEnvironmentDescription::getPropertyMapping()\n", sub_property_name.c_str());
}
std::string CSubEnvironmentDescription::getMacroPropertyMapping(const std::string& sub_property_name) const {
const auto v = subenvironment->macro_properties.find(sub_property_name);
if (v != subenvironment->properties.end())
return v->second;
THROW exception::InvalidAgentState("SubEnvironment macro property '%s', either does not exist or has not been mapped yet, "
"in SubEnvironmentDescription::getMacroPropertyMapping()\n", sub_property_name.c_str());
}
std::string CSubEnvironmentDescription::getDirectedGraphMapping(const std::string& sub_graph_name) const {
const auto v = subenvironment->directed_graphs.find(sub_graph_name);
if (v != subenvironment->directed_graphs.end())
return v->second;
THROW exception::InvalidEnvGraph("SubEnvironment directed graph '%s', either does not exist or has not been mapped yet, "
"in SubEnvironmentDescription::getDirectedGraphMapping()\n", sub_graph_name.c_str());
}
/**
* Constructors
*/
SubEnvironmentDescription::SubEnvironmentDescription(std::shared_ptr<SubEnvironmentData> data)
: CSubEnvironmentDescription(std::move(data)) { }
void SubEnvironmentDescription::mapProperty(const std::string &sub_property_name, const std::string &master_property_name) {
// Neither are reserved properties
if (!sub_property_name.empty() && sub_property_name[0] == '_') {
THROW exception::ReservedName("Sub-model environment property '%s is internal and cannot be mapped, "
"in SubEnvironmentDescription::mapProperty()\n", sub_property_name.c_str());
}
if (!master_property_name.empty() && master_property_name[0] == '_') {
THROW exception::ReservedName("Master-model environment property '%s is internal and cannot be mapped, "
"in SubEnvironmentDescription::mapProperty()\n", master_property_name.c_str());
}
// Sub property exists
auto subEnv = subenvironment->subEnvironment.lock();
if (!subEnv) {
THROW exception::InvalidParent("SubEnvironment pointer has expired, "
"in SubEnvironmentDescription::mapProperty()\n");
}
const auto subProp = subEnv->properties.find(sub_property_name);
if (subProp == subEnv->properties.end()) {
const auto parent = subenvironment->parent.lock();
THROW exception::InvalidEnvProperty("SubModel '%s's Environment does not contain property '%s', "
"in SubEnvironmentDescription::mapProperty()\n", parent ? parent->submodel->name.c_str() : "?", sub_property_name.c_str());
}
// Master property exists
auto masterEnv = subenvironment->masterEnvironment.lock();
if (!masterEnv) {
THROW exception::InvalidParent("MasterEnvironment pointer has expired, "
"in SubEnvironmentDescription::mapProperty()\n");
}
const auto masterProp = masterEnv->properties.find(master_property_name);
if (masterProp == masterEnv->properties.end()) {
THROW exception::InvalidEnvProperty("MasterEnvironment does not contain property '%s', "
"in SubEnvironmentDescription::mapProperty()\n", master_property_name.c_str());
}
// Sub property has not been bound yet
if (subenvironment->properties.find(sub_property_name) != subenvironment->properties.end()) {
const auto parent = subenvironment->parent.lock();
THROW exception::InvalidEnvProperty("SubModel '%s's Environment property '%s' has already been mapped, "
"in SubEnvironmentDescription::mapProperty()\n", parent ? parent->submodel->name.c_str() : "?", sub_property_name.c_str());
}
// Master property has already been bound
for (auto &v : subenvironment->properties) {
if (v.second == master_property_name) {
THROW exception::InvalidEnvProperty("MasterEnvironment property '%s' has already been mapped, "
"in SubEnvironmentDescription::mapProperty()\n", master_property_name.c_str());
}
}
// Check properties are the same
if (subProp->second.data.type != masterProp->second.data.type) {
THROW exception::InvalidEnvProperty("Property types do not match, '%s' != '%s', "
"in SubEnvironmentDescription::mapProperty()\n", subProp->second.data.type.name(), masterProp->second.data.type.name());
}
if (subProp->second.data.elements != masterProp->second.data.elements) {
THROW exception::InvalidEnvProperty("Property lengths do not match, '%u' != '%u'",
"in SubEnvironmentDescription::mapProperty()\n", subProp->second.data.elements, masterProp->second.data.elements);
}
if (masterProp->second.isConst && !subProp->second.isConst) {
THROW exception::InvalidEnvProperty("SubEnvironment property '%s' must be const, if mapped to const MasterEnvironment property '%s', "
"in SubEnvironmentDescription::mapProperty()\n", sub_property_name.c_str(), master_property_name.c_str());
}
// Properties match, create mapping
subenvironment->properties.emplace(sub_property_name, master_property_name);
}
void SubEnvironmentDescription::mapMacroProperty(const std::string& sub_property_name, const std::string& master_property_name) {
// Neither are reserved properties
if (!sub_property_name.empty() && sub_property_name[0] == '_') {
THROW exception::ReservedName("Sub-model environment macro property '%s is internal and cannot be mapped, "
"in SubEnvironmentDescription::mapMacroProperty()\n", sub_property_name.c_str());
}
if (!master_property_name.empty() && master_property_name[0] == '_') {
THROW exception::ReservedName("Master-model environment macro property '%s is internal and cannot be mapped, "
"in SubEnvironmentDescription::mapMacroProperty()\n", master_property_name.c_str());
}
// Sub macro property exists
auto subEnv = subenvironment->subEnvironment.lock();
if (!subEnv) {
THROW exception::InvalidParent("SubEnvironment pointer has expired, "
"in SubEnvironmentDescription::mapMacroProperty()\n");
}
const auto subProp = subEnv->macro_properties.find(sub_property_name);
if (subProp == subEnv->macro_properties.end()) {
const auto parent = subenvironment->parent.lock();
THROW exception::InvalidEnvProperty("SubModel '%s's Environment does not contain macro property '%s', "
"in SubEnvironmentDescription::mapMacroProperty()\n", parent ? parent->submodel->name.c_str() : "?", sub_property_name.c_str());
}
// Master macro property exists
auto masterEnv = subenvironment->masterEnvironment.lock();
if (!masterEnv) {
THROW exception::InvalidParent("MasterEnvironment pointer has expired, "
"in SubEnvironmentDescription::mapMacroProperty()\n");
}
const auto masterProp = masterEnv->macro_properties.find(master_property_name);
if (masterProp == masterEnv->macro_properties.end()) {
THROW exception::InvalidEnvProperty("MasterEnvironment does not contain macro property '%s', "
"in SubEnvironmentDescription::mapMacroProperty()\n", master_property_name.c_str());
}
// Sub macro property has not been bound yet
if (subenvironment->macro_properties.find(sub_property_name) != subenvironment->macro_properties.end()) {
const auto parent = subenvironment->parent.lock();
THROW exception::InvalidEnvProperty("SubModel '%s's Environment macro property '%s' has already been mapped, "
"in SubEnvironmentDescription::mapMacroProperty()\n", parent ? parent->submodel->name.c_str() : "?", sub_property_name.c_str());
}
// Master macro property has already been bound
for (auto& v : subenvironment->macro_properties) {
if (v.second == master_property_name) {
THROW exception::InvalidEnvProperty("MasterEnvironment macro property '%s' has already been mapped, "
"in SubEnvironmentDescription::mapMacroProperty()\n", master_property_name.c_str());
}
}
// Check macro properties are the same
if (subProp->second.type != masterProp->second.type) {
THROW exception::InvalidEnvProperty("Macro property types do not match, '%s' != '%s', "
"in SubEnvironmentDescription::mapMacroProperty()\n", subProp->second.type.name(), masterProp->second.type.name());
}
if (subProp->second.elements != masterProp->second.elements) {
THROW exception::InvalidEnvProperty("Macro property dimensions do not match, (%u, %u, %u, %u) != (%u, %u, %u, %u)",
"in SubEnvironmentDescription::mapMacroProperty()\n",
subProp->second.elements[0], subProp->second.elements[1], subProp->second.elements[2], subProp->second.elements[3],
masterProp->second.elements[0], masterProp->second.elements[1], masterProp->second.elements[2], masterProp->second.elements[3]);
}
// Macro properties match, create mapping
subenvironment->macro_properties.emplace(sub_property_name, master_property_name);
}
void SubEnvironmentDescription::mapDirectedGraph(const std::string& sub_graph_name, const std::string& master_graph_name) {
// Neither are reserved properties
if (!sub_graph_name.empty() && sub_graph_name[0] == '_') {
THROW exception::ReservedName("Sub-model environment directed graph '%s is internal and cannot be mapped, "
"in SubEnvironmentDescription::mapDirectedGraph()\n", sub_graph_name.c_str());
}
if (!master_graph_name.empty() && master_graph_name[0] == '_') {
THROW exception::ReservedName("Master-model environment directed graph '%s is internal and cannot be mapped, "
"in SubEnvironmentDescription::mapDirectedGraph()\n", master_graph_name.c_str());
}
// Sub directed graph exists
auto subEnv = subenvironment->subEnvironment.lock();
if (!subEnv) {
THROW exception::InvalidParent("SubEnvironment pointer has expired, "
"in SubEnvironmentDescription::mapDirectedGraph()\n");
}
const auto subProp = subEnv->directed_graphs.find(sub_graph_name);
if (subProp == subEnv->directed_graphs.end()) {
const auto parent = subenvironment->parent.lock();
THROW exception::InvalidEnvProperty("SubModel '%s's Environment does not contain directed graph '%s', "
"in SubEnvironmentDescription::mapDirectedGraph()\n", parent ? parent->submodel->name.c_str() : "?", sub_graph_name.c_str());
}
// Master macro property exists
auto masterEnv = subenvironment->masterEnvironment.lock();
if (!masterEnv) {
THROW exception::InvalidParent("MasterEnvironment pointer has expired, "
"in SubEnvironmentDescription::mapDirectedGraph()\n");
}
const auto masterProp = masterEnv->directed_graphs.find(master_graph_name);
if (masterProp == masterEnv->directed_graphs.end()) {
THROW exception::InvalidEnvGraph("MasterEnvironment does not contain directed graph '%s', "
"in SubEnvironmentDescription::mapDirectedGraph()\n", sub_graph_name.c_str());
}
// Sub macro property has not been bound yet
if (subenvironment->directed_graphs.find(sub_graph_name) != subenvironment->directed_graphs.end()) {
const auto parent = subenvironment->parent.lock();
THROW exception::InvalidEnvGraph("SubModel '%s's Environment directed graph '%s' has already been mapped, "
"in SubEnvironmentDescription::mapDirectedGraph()\n", parent ? parent->submodel->name.c_str() : "?", sub_graph_name.c_str());
}
// Master macro property has already been bound
for (auto& v : subenvironment->directed_graphs) {
if (v.second == master_graph_name) {
THROW exception::InvalidEnvGraph("MasterEnvironment directed graph '%s' has already been mapped, "
"in SubEnvironmentDescription::mapDirectedGraph()\n", master_graph_name.c_str());
}
}
// Check macro properties are the same
if (*subProp->second != *masterProp->second) {
THROW exception::InvalidEnvGraph("Directed graphs are not identical, '%s' != '%s', "
"in SubEnvironmentDescription::mapMacroProperty()\n", master_graph_name.c_str(), sub_graph_name.c_str());
}
// Directed graphs match, create mapping
subenvironment->directed_graphs.emplace(sub_graph_name, master_graph_name);
}
void SubEnvironmentDescription::autoMap() {
autoMapProperties();
autoMapMacroProperties();
autoMapDirectedGraphs();
}
void SubEnvironmentDescription::autoMapProperties() {
// Sub env exists
auto subEnv = subenvironment->subEnvironment.lock();
if (!subEnv) {
THROW exception::InvalidParent("SubEnvironment pointer has expired, "
"in SubEnvironmentDescription::autoMapProperties()\n");
}
// Master env exists
auto masterEnv = subenvironment->masterEnvironment.lock();
if (!masterEnv) {
THROW exception::InvalidParent("MasterEnvironment pointer has expired, "
"in SubEnvironmentDescription::autoMapProperties()\n");
}
for (auto &subProp : subEnv->properties) {
// If it's a reserved environment property, don't map it
// (Previously, there was a bug where _stepCount was being mixed between parent and child models)
if (subProp.first[0] == '_')
continue;
auto masterProp = masterEnv->properties.find(subProp.first);
// If there exists variable with same name in both environments
if (masterProp != masterEnv->properties.end()) {
// Check properties are the same
if ((subProp.second.data.type == masterProp->second.data.type) &&
(subProp.second.data.elements == masterProp->second.data.elements) &&
!(masterProp->second.isConst && !subProp.second.isConst)) {
subenvironment->properties.emplace(subProp.first, masterProp->first); // Doesn't actually matter, both strings are equal
}
}
}
}
void SubEnvironmentDescription::autoMapMacroProperties() {
// Sub env exists
auto subEnv = subenvironment->subEnvironment.lock();
if (!subEnv) {
THROW exception::InvalidParent("SubEnvironment pointer has expired, "
"in SubEnvironmentDescription::autoMapMacroProperties()\n");
}
// Master env exists
auto masterEnv = subenvironment->masterEnvironment.lock();
if (!masterEnv) {
THROW exception::InvalidParent("MasterEnvironment pointer has expired, "
"in SubEnvironmentDescription::autoMapMacroProperties()\n");
}
for (auto& subProp : subEnv->macro_properties) {
// If it's a reserved environment property, don't map it
if (subProp.first[0] == '_')
continue;
auto masterProp = masterEnv->macro_properties.find(subProp.first);
// If there exists variable with same name in both environments
if (masterProp != masterEnv->macro_properties.end()) {
// Check properties are the same
if ((subProp.second.type == masterProp->second.type) &&
(subProp.second.elements == masterProp->second.elements)) {
subenvironment->macro_properties.emplace(subProp.first, masterProp->first); // Doesn't actually matter, both strings are equal
}
}
}
}
void SubEnvironmentDescription::autoMapDirectedGraphs() {
// Sub env exists
auto subEnv = subenvironment->subEnvironment.lock();
if (!subEnv) {
THROW exception::InvalidParent("SubEnvironment pointer has expired, "
"in SubEnvironmentDescription::autoMapDirectedGraphs()\n");
}
// Master env exists
auto masterEnv = subenvironment->masterEnvironment.lock();
if (!masterEnv) {
THROW exception::InvalidParent("MasterEnvironment pointer has expired, "
"in SubEnvironmentDescription::autoMapDirectedGraphs()\n");
}
for (auto& subGraph : subEnv->directed_graphs) {
// If it's a reserved environment property, don't map it
if (subGraph.first[0] == '_')
continue;
auto masterGraph = masterEnv->directed_graphs.find(subGraph.first);
// If there exists variable with same name in both environments
if (masterGraph != masterEnv->directed_graphs.end()) {
// Check properties are the same
if (*subGraph.second == *masterGraph->second) {
subenvironment->directed_graphs.emplace(subGraph.first, masterGraph->first); // Doesn't actually matter, both strings are equal
}
}
}
}
} // namespace flamegpu