10 #include <Atoms/Globals.h>
11 #include <Atoms/GlobalNames.h>
12 #include <AtomsCore/Metadata/All.h>
13 #include <AtomsUtils/AtomsMath.h>
14 #include <Atoms/Agent.h>
17 #include <AtomsJIT/AtomsScriptLang.h>
18 #include <Atoms/AtomsScriptUtils.h>
19 #include <ToolchefsSTL/String.h>
44 typedef typename AttributeCacheScriptType<T>::Type InternalType;
54 m_agentGroup =
nullptr;
63 for (
auto v : listPtr->get())
64 m_listValue.push_back(v);
71 const std::string& behaviourModuleName,
class AgentGroup* agentGroup):
72 m_agentGroupWrapper(agentGroup)
79 m_agentGroup = agentGroup;
81 m_metadataName = behaviourModuleName +
"@" + attributeName;
87 m_defaultValue = attrPtr->get();
92 m_mode = modePtr->value();
96 m_seed = seedPtr->value();
100 m_minValue = minValuePtr->value();
104 m_maxValue = maxValuePtr->value();
114 fillOverrideList(attributes, attributeName);
116 auto expressionMetaPtr = attributes.
getTypedEntry<AtomsCore::StringMetadata>(attributeName +
"_expression");
117 if (m_mode == 3 && expressionMetaPtr && !expressionMetaPtr->get().empty())
120 hasher.append(expressionMetaPtr->get());
122 if (baseType == AtomsCore::StringMetadata::staticBaseTypeStr())
126 std::string functionName =
"__expression_" + behaviourModuleName +
"__" + attributeName +
"_" + hasher.string();
127 std::string expressionCode;
128 expressionCode +=
"#include <Atoms/AtomsScriptUtils.h>\n";
129 expressionCode +=
"using namespace AtomsScriptUtils;\n";
130 expressionCode +=
"using namespace AtomsMath;\n";
131 expressionCode +=
"using namespace AtomsUtils;\n";
132 expressionCode +=
"using namespace ToolchefsSTL;\n";
133 expressionCode +=
"void " + functionName +
"(";
134 expressionCode += baseType +
"& output";
135 expressionCode +=
", const AgentWrapper& agent, const AgentGroupWrapper& agentGroup";
136 expressionCode +=
", const "+ baseType +
"& defaultValue";
137 expressionCode +=
"){\n";
138 expressionCode += expressionMetaPtr->get();
139 expressionCode +=
"\n }";
141 auto& compiler = Atoms::AtomsScriptLang::instance();
142 if (!compiler.addCode(functionName, expressionCode))
144 AtomsUtils::Logger::error() <<
"Unable to compile expression for attribute " << attributeName <<
" in module " << behaviourModuleName;
158 const auto& agentMetadata = agent->
metadata();
162 return agentDataPtr->
value();
170 return dataPtr->
value();
174 return m_defaultValue;
176 AtomsCore::Rand48 random;
177 random.init(m_seed + groupId);
183 return m_defaultValue;
191 if (m_listValue.size() > 0)
192 return m_listValue[random.nexti() % m_listValue.size()];
197 T outValue = m_defaultValue;
199 m_script(outValue, wrapper, m_agentGroupWrapper, m_defaultValue);
202 return m_defaultValue;
205 return m_defaultValue;
211 return m_overrideMap ? m_overrideMap->hasKey(groupId) :
false;
214 inline const T& defaultValue()
const {
return m_defaultValue; }
217 std::vector<T> m_listValue;
218 std::string m_metadataName;
231 AtomsPtr<const AtomsCore::MapMetadata> m_overrideMap;
233 class AgentGroup* m_agentGroup;
238 template<> ATOMS_EXPORT
void BehaviourModuleAttributeCache<bool>::fillOverrideList(
const AtomsCore::MapMetadata& attributes,
const std::string& attributeName);
Agent group.
Definition: AgentGroup.h:36
Agent.
Definition: Agent.h:44
AtomsCore::MapMetadata & metadata()
Gets the agent metadata map.
Definition: Agent.impl.h:32
Behaviour Module Attribute Cache.
Definition: BehaviourModuleAttributeCache.h:41
const bool hasOverride(const std::string &groupId)
check if there is any override for the specified groupId
Definition: BehaviourModuleAttributeCache.h:209
const T getAttributeValue(const int groupId, const std::string &groupIdStr, class Agent *agent) const
generates a random value
Definition: BehaviourModuleAttributeCache.h:154
Definition: AtomsMath.h:340
Definition: AtomsMath.h:196
Definition: AtomsScriptUtils.h:260
Definition: AtomsScriptUtils.h:125
static LogProxy error()
Get logger proxy error.
Atoms namespace.
Definition: Agent.h:29
Definition: BehaviourModuleAttributeCache.h:26