17int Agent::next_id_ = 0;
20 prototype_ = m->prototype_;
23 lifetime_ = m->lifetime_;
28 std::stringstream ret;
29 ret <<
"A(n) " << spec_ <<
" named " << prototype_ <<
" at time "
30 <<
context()->
time() <<
" received the following error:\n"
36 std::string proto = qe->
GetString(
"name");
39 ->
AddVal(
"Prototype", proto)
46 prototype_ = qr.
GetVal<std::string>(
"Prototype");
47 id_ = qr.
GetVal<
int>(
"AgentId");
48 lifetime_ = qr.
GetVal<
int>(
"Lifetime");
53 ->
AddVal(
"Prototype", prototype_)
54 ->
AddVal(
"Lifetime", lifetime_)
66 spec_(
"UNSPECIFIED") {
67 ctx_->agent_list_.insert(
this);
68 MLOG(
LEV_DEBUG3) <<
"Agent ID=" << id_ <<
", ptr=" <<
this <<
" created.";
73 context()->agent_list_.erase(
this);
75 std::set<Agent*>::iterator it;
76 if (parent_ != NULL) {
78 <<
"' ID=" << parent_->id() <<
" has removed child '"
80 <<
" from its list of children.";
81 it = find(parent_->children_.begin(), parent_->children_.end(),
this);
82 if (it != parent_->children_.end()) {
83 parent_->children_.erase(it);
88 for (it = children_.begin(); it != children_.end(); ++it) {
90 child->parent_ = NULL;
91 child->parent_id_ = -1;
97 ss <<
kind_ <<
"_" << prototype_ <<
" ( " <<
"ID=" << id_
98 <<
", implementation=" << spec_ <<
", name=" << prototype_
99 <<
", parentID=" << parent_id_ <<
" ) ";
104 if (enter_time_ != -1) {
105 throw ValueError(
"cannot set the lifetime of an already-built facility");
107 lifetime_ = n_timesteps;
114 if (enter_time_ + n_timesteps <=
context()->time()) {
117 lifetime_ = n_timesteps;
124 while (other != NULL) {
125 if (
this == other)
return true;
133 std::set<Agent*>::const_iterator it =
children.begin();
134 for (; it !=
children.end(); ++it) {
135 if (
this == *(it) || this->
DecendentOf(*(it)))
return true;
146 <<
"' is entering the simulation.";
152 enter_time_ = ctx_->time();
158 ctx_->RegisterAgent(
this);
161void Agent::Connect(
Agent* parent) {
164 "is trying to add itself as its own child.");
169 parent->children_.insert(
this);
175 <<
" is being decommissioned";
176 ctx_->NewDatum(
"AgentExit")
177 ->AddVal(
"AgentId",
id())
178 ->AddVal(
"ExitTime", ctx_->time())
180 ctx_->UnregisterAgent(
this);
181 ctx_->DelAgent(
this);
185 std::stringstream ss(
"");
186 ss <<
"Children of " <<
prototype() <<
":" << std::endl;
188 std::set<Agent*>::iterator it;
189 for (it = children_.begin(); it != children_.end(); ++it) {
192 for (
int j = 0; j < print_outs.size(); j++) {
193 ss <<
"\t" << print_outs.at(j);
200 std::vector<std::string> ret;
201 std::stringstream ss(
"");
203 ret.push_back(ss.str());
204 std::set<Agent*>::iterator it;
205 for (it = children_.begin(); it != children_.end(); ++it) {
208 for (
int j = 0; j < outs.size(); j++) {
210 ss <<
"\t" << outs.at(j) << std::endl;
211 ret.push_back(ss.str());
217void Agent::AddToTable() {
222 ->
AddVal(
"Prototype", prototype_)
223 ->
AddVal(
"ParentId", parent_id_)
224 ->
AddVal(
"Lifetime", lifetime_)
225 ->
AddVal(
"EnterTime", enter_time_)
The abstract base class used by all types of agents that live and interact in a simulation.
virtual void InitFrom(QueryableBackend *b)
Intializes an agent's internal state from the database.
const std::string prototype() const
Returns the agent's prototype.
virtual void Build(Agent *parent)
Called when the agent enters the smiulation as an active participant and is only ever called once.
void lifetime(int n_timesteps)
Sets the number of time steps this agent operates between building and decommissioning (-1 if the age...
Agent(Context *ctx)
Creates a new agent that is managed by the given context.
std::vector< std::string > GetTreePrintOuts(Agent *m)
returns a vector of strings representing the parent-child tree at the node for Agent m
Context * context() const
Returns this agent's simulation context.
bool DecendentOf(Agent *other)
returns true if this agent is an decendent of an other agent (i.e., resides below an other agent in t...
virtual void InfileToDb(InfileTree *qe, DbInit di)
Translates info for the agent from an input file to the database by reading parameters from the passe...
const int lifetime() const
Returns the number of time steps this agent operates between building and decommissioning (-1 if the ...
const std::set< Agent * > & children() const
Returns a list of children this agent has.
virtual const int id() const
The agent instance's unique ID within a simulation.
virtual void Decommission()
Decommissions the agent, removing it from the simulation.
virtual void Snapshot(DbInit di)=0
Snapshots agent-internal state to the database via the DbInit var di.
std::string PrintChildren()
recursively prints the parent-child tree
virtual ~Agent()
Removes references to self from simulation context, parent, children, etc.
bool InFamilyTree(Agent *other)
returns true if this agent is in the parent-child family tree of an other agent
std::string kind_
describes the agent subclass (e.g.
virtual void EnterNotify()
Called to give the agent an opportunity to register for services (e.g.
Agent * parent() const
Returns parent of this agent. Returns NULL if the agent has no parent.
virtual std::string str()
Description of this agent.
virtual std::string InformErrorMsg(std::string msg)
adds agent-specific information prefix to an error message
bool AncestorOf(Agent *other)
returns true if this agent is an ancestor of an other agent (i.e., resides above an other agent in th...
void lifetime_force(int n_timesteps)
Sets the number of time steps this agent operates between building and decommissioning (-1 if the age...
A simulation context provides access to necessary simulation-global functions and state.
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
virtual int time()
Returns the current simulation timestep.
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
void Record()
Record this datum to its Recorder.
DbInit provides an interface for agents to record data to the output db that automatically injects th...
Datum * NewDatum(std::string title)
Returns a new datum to be used exactly as the Context::NewDatum method.
A class for extracting information from a given XML parser.
virtual std::string GetString(std::string query, int index=0)
investigates the current status and returns a string representing the content of a query at a given i...
For failed retrieval/insertion of key-based data into/from data structures.
Meta data and results of a query.
T GetVal(std::string field, int row=0)
Convenience method for retrieving a value from a specific row and named field (column).
Interface implemented by backends that support rudimentary querying.
For values that are too big, too small, etc.
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
@ LEV_DEBUG3
debugging information
@ LEV_INFO3
Information helpful for simulation users and developers alike.
@ LEV_DEBUG1
debugging information - least verbose
@ LEV_DEBUG2
debugging information
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters