-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSubModelDescription.cpp
More file actions
159 lines (148 loc) · 6.94 KB
/
SubModelDescription.cpp
File metadata and controls
159 lines (148 loc) · 6.94 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
#include "flamegpu/model/SubModelDescription.h"
#include "flamegpu/model/ModelData.h"
#include "flamegpu/model/AgentData.h"
#include "flamegpu/model/SubModelData.h"
#include "flamegpu/model/SubAgentData.h"
#include "flamegpu/model/SubAgentDescription.h"
#include "flamegpu/model/SubEnvironmentData.h"
#include "flamegpu/model/SubEnvironmentDescription.h"
namespace flamegpu {
CSubModelDescription::CSubModelDescription(std::shared_ptr<SubModelData> data)
: submodel(std::move(data)) { }
CSubModelDescription::CSubModelDescription(std::shared_ptr<const SubModelData> data)
: submodel(std::const_pointer_cast<SubModelData>(data)) { }
bool CSubModelDescription::operator==(const CSubModelDescription& rhs) const {
return *this->submodel == *rhs.submodel; // Compare content is functionally the same
}
bool CSubModelDescription::operator!=(const CSubModelDescription& rhs) const {
return !(*this == rhs);
}
/**
* Const Accessors
*/
unsigned int CSubModelDescription::getMaxSteps() const {
return submodel->max_steps;
}
const std::string CSubModelDescription::getName() const {
return submodel->name;
}
/**
* Constructors
*/
SubModelDescription::SubModelDescription(std::shared_ptr<SubModelData> data)
: CSubModelDescription(std::move(data)) { }
/**
* Accessors
*/
SubAgentDescription SubModelDescription::bindAgent(const std::string &sub_agent_name, const std::string &master_agent_name, bool auto_map_vars, bool auto_map_states) {
// Sub agent exists
const auto subagent = submodel->submodel->agents.find(sub_agent_name);
if (subagent == submodel->submodel->agents.end()) {
THROW exception::InvalidSubAgentName("SubModel '%s' does not contain Agent '%s', "
"in SubModelDescription::bindAgent()\n", submodel->submodel->name.c_str(), sub_agent_name.c_str());
}
auto mdl = submodel->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
// Master agent exists
const auto masteragent = mdl->agents.find(master_agent_name);
if (masteragent == mdl->agents.end()) {
THROW exception::InvalidAgentName("Master Model '%s' does not contain Agent '%s', "
"in SubModelDescription::bindAgent()\n", mdl->name.c_str(), master_agent_name.c_str());
}
// Sub agent has not been bound yet
{
const auto subagent_bind = submodel->subagents.find(sub_agent_name);
if (subagent_bind != submodel->subagents.end()) {
auto master_agent_ptr = subagent_bind->second->masterAgent.lock();
THROW exception::InvalidSubAgentName("SubModel '%s's Agent '%s' has already been bound to Master agent '%s', "
"in SubModelDescription::bindAgent()\n", submodel->submodel->name.c_str(), sub_agent_name.c_str(), master_agent_ptr ? master_agent_ptr->name.c_str() : "?");
}
}
// Master agent has not been bound yet
for (auto &a : submodel->subagents) {
const auto master_agent_ptr = a.second->masterAgent.lock();
if (master_agent_ptr && master_agent_ptr->name == master_agent_name) {
THROW exception::InvalidAgentName("Master Agent '%s' has already been bound to Sub agent '%s', "
"in SubModelDescription::bindAgent()\n", master_agent_name.c_str(), a.first.c_str());
}
}
// Create SubAgent
auto rtn = std::shared_ptr<SubAgentData>(new SubAgentData(mdl, submodel->shared_from_this(), subagent->second, masteragent->second));
submodel->subagents.emplace(sub_agent_name, rtn);
// If auto_map, map any matching vars
// Otherwise map all internal variables that begin _ (e.g. _id)
for (auto& sub_var : subagent->second->variables) {
if (auto_map_vars || (!sub_var.first.empty() && sub_var.first[0] == '_')) {
auto master_var = masteragent->second->variables.find(sub_var.first);
// If there exists variable with same name in both agents
if (master_var != masteragent->second->variables.end()) {
// Check type and length (is it an array var)
if (sub_var.second.type == master_var->second.type
&& sub_var.second.elements == master_var->second.elements) {
// Variables match, create mapping
rtn->variables.emplace(sub_var.first, master_var->first); // Doesn't actually matter, both strings are equal
}
}
}
}
// If auto_map, map any matching states
if (auto_map_states) {
for (auto &sub_var : subagent->second->states) {
auto master_var = masteragent->second->states.find(sub_var);
// If there exists states with same name in both agents
if (master_var != masteragent->second->states.end()) {
// States match, create mapping
rtn->states.emplace(sub_var, *master_var); // Doesn't actually matter, both strings are equal
}
}
}
// return SubAgentDescription
return SubAgentDescription(rtn);
}
SubAgentDescription SubModelDescription::SubAgent(const std::string &sub_agent_name) {
const auto rtn = submodel->subagents.find(sub_agent_name);
if (rtn != submodel->subagents.end())
return SubAgentDescription(rtn->second);
THROW exception::InvalidSubAgentName("SubAgent ('%s') either does not exist, or has not been bound yet, "
"in SubModelDescription::SubAgent().",
sub_agent_name.c_str());
}
CSubAgentDescription SubModelDescription::getSubAgent(const std::string &sub_agent_name) const {
const auto rtn = submodel->subagents.find(sub_agent_name);
if (rtn != submodel->subagents.end())
return SubAgentDescription(rtn->second);
THROW exception::InvalidSubAgentName("SubAgent ('%s') either does not exist, or has not been bound yet, "
"in SubModelDescription::getSubAgent().",
sub_agent_name.c_str());
}
SubEnvironmentDescription SubModelDescription::SubEnvironment(bool auto_map) {
if (!submodel->subenvironment) {
auto mdl = submodel->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
submodel->subenvironment = std::shared_ptr<SubEnvironmentData>(new SubEnvironmentData(mdl, submodel->shared_from_this(), submodel->submodel->environment));
}
SubEnvironmentDescription rtn(submodel->subenvironment);
if (auto_map) {
rtn.autoMapProperties();
rtn.autoMapMacroProperties();
}
return rtn;
}
CSubEnvironmentDescription SubModelDescription::getSubEnvironment() const {
if (!submodel->subenvironment) {
auto mdl = submodel->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
submodel->subenvironment = std::shared_ptr<SubEnvironmentData>(new SubEnvironmentData(mdl, submodel->shared_from_this(), submodel->submodel->environment));
}
return CSubEnvironmentDescription(submodel->subenvironment);
}
void SubModelDescription::setMaxSteps(const unsigned int max_steps) {
submodel->max_steps = max_steps;
}
} // namespace flamegpu