CYCLUS
agent.cc
Go to the documentation of this file.
1 // Implements the Agent Class
2 #include "agent.h"
3 
4 #include <algorithm>
5 #include <iostream>
6 #include <sstream>
7 #include <string>
8 
9 #include "context.h"
10 #include "error.h"
11 #include "logger.h"
12 #include "resource.h"
13 
14 namespace cyclus {
15 
16 // static members
17 int Agent::next_id_ = 0;
18 
20  prototype_ = m->prototype_;
21  kind_ = m->kind_;
22  spec_ = m->spec_;
23  lifetime_ = m->lifetime_;
24  ctx_ = m->ctx_;
25 }
26 
28  std::stringstream ret;
29  ret << "A(n) " << spec_ << " named " << prototype_
30  << " at time " << context()->time()
31  << " received the following error:\n"
32  << msg;
33  return ret.str();
34 }
35 
37  std::string proto = qe->GetString("name");
38  int lifetime = OptionalQuery<int>(qe, "lifetime", -1);
39  di.NewDatum("Agent")
40  ->AddVal("Prototype", proto)
41  ->AddVal("Lifetime", lifetime)
42  ->Record();
43 }
44 
46  QueryResult qr = b->Query("Agent", NULL);
47  prototype_ = qr.GetVal<std::string>("Prototype");
48  id_ = qr.GetVal<int>("AgentId");
49  lifetime_ = qr.GetVal<int>("Lifetime");
50 }
51 
53  di.NewDatum("Agent")
54  ->AddVal("Prototype", prototype_)
55  ->AddVal("Lifetime", lifetime_)
56  ->Record();
57 }
58 
60  : ctx_(ctx),
61  id_(next_id_++),
62  kind_("Agent"),
63  parent_id_(-1),
64  enter_time_(-1),
65  lifetime_(-1),
66  parent_(NULL),
67  spec_("UNSPECIFIED") {
68  ctx_->agent_list_.insert(this);
69  MLOG(LEV_DEBUG3) << "Agent ID=" << id_ << ", ptr=" << this << " created.";
70 }
71 
73  MLOG(LEV_DEBUG3) << "Deleting agent '" << prototype() << "' ID=" << id_;
74  context()->agent_list_.erase(this);
75 
76  std::set<Agent*>::iterator it;
77  if (parent_ != NULL) {
78  CLOG(LEV_DEBUG2) << "Agent '" << parent_->prototype() << "' ID="
79  << parent_->id()
80  << " has removed child '" << prototype() << "' ID="
81  << id() << " from its list of children.";
82  it = find(parent_->children_.begin(), parent_->children_.end(), this);
83  if (it != parent_->children_.end()) {
84  parent_->children_.erase(it);
85  }
86  }
87 
88  // set children's parents to NULL
89  for (it = children_.begin(); it != children_.end(); ++it) {
90  Agent* child = *it;
91  child->parent_ = NULL;
92  child->parent_id_ = -1;
93  }
94 }
95 
97  std::stringstream ss;
98  ss << kind_ << "_" << prototype_
99  << " ( "
100  << "ID=" << id_
101  << ", implementation=" << spec_
102  << ", name=" << prototype_
103  << ", parentID=" << parent_id_
104  << " ) ";
105  return ss.str();
106 }
107 
108 void Agent::lifetime(int n_timesteps) {
109  if (enter_time_ != -1) {
110  throw ValueError("cannot set the lifetime of an already-built facility");
111  }
112  lifetime_ = n_timesteps;
113 }
114 
115 void Agent::lifetime_force(int n_timesteps) {
116  try{
117  lifetime(n_timesteps);
118  }
119  catch (ValueError e){
120  if(enter_time_+n_timesteps <= context()->time()){
121  lifetime(context()->time() - enter_time_ + 1);
122  }
123  else{
124  lifetime_ = n_timesteps;
125  }
126  }
127 }
128 
129 bool Agent::AncestorOf(Agent* other) {
130  other = other->parent();
131  while (other != NULL) {
132  if (this == other)
133  return true;
134  other = other->parent();
135  }
136  return false;
137 }
138 
139 bool Agent::DecendentOf(Agent* other) {
140  const std::set<Agent*>& children = other->children();
141  std::set<Agent*>::const_iterator it = children.begin();
142  for (; it != children.end(); ++it) {
143  if (this == *(it) || this->DecendentOf(*(it)))
144  return true;
145  }
146  return false;
147 }
148 
150  return (this == other) || AncestorOf(other) || DecendentOf(other);
151 }
152 
154  CLOG(LEV_DEBUG1) << "Agent '" << prototype()
155  << "' is entering the simulation.";
156  CLOG(LEV_DEBUG3) << "It has:";
157  CLOG(LEV_DEBUG3) << " * Spec: " << spec_;
158  CLOG(LEV_DEBUG3) << " * ID: " << id();
159 
160  Connect(parent);
161  enter_time_ = ctx_->time();
162  EnterNotify();
163  this->AddToTable();
164 }
165 
167  ctx_->RegisterAgent(this);
168 }
169 
170 void Agent::Connect(Agent* parent) {
171  if (parent == this) {
172  throw KeyError("Agent " + prototype() +
173  "is trying to add itself as its own child.");
174  }
175  if (parent != NULL) {
176  parent_ = parent;
177  parent_id_ = parent->id();
178  parent->children_.insert(this);
179  }
180 }
181 
183  CLOG(LEV_INFO3) << prototype() << "(" << this << ")"
184  << " is being decommissioned";
185  ctx_->NewDatum("AgentExit")
186  ->AddVal("AgentId", id())
187  ->AddVal("ExitTime", ctx_->time())
188  ->Record();
189  ctx_->UnregisterAgent(this);
190  ctx_->DelAgent(this);
191 }
192 
194  std::stringstream ss("");
195  ss << "Children of " << prototype() << ":" << std::endl;
196 
197  std::set<Agent*>::iterator it;
198  for (it = children_.begin(); it != children_.end(); ++it) {
199  Agent* child = *it;
200  std::vector<std::string> print_outs = GetTreePrintOuts(child);
201  for (int j = 0; j < print_outs.size(); j++) {
202  ss << "\t" << print_outs.at(j);
203  }
204  }
205  return ss.str();
206 }
207 
208 std::vector<std::string> Agent::GetTreePrintOuts(Agent* m) {
209  std::vector<std::string> ret;
210  std::stringstream ss("");
211  ss << m->prototype() << std::endl;
212  ret.push_back(ss.str());
213  std::set<Agent*>::iterator it;
214  for (it = children_.begin(); it != children_.end(); ++it) {
215  Agent* child = *it;
216  std::vector<std::string> outs = GetTreePrintOuts(child);
217  for (int j = 0; j < outs.size(); j++) {
218  ss.str("");
219  ss << "\t" << outs.at(j) << std::endl;
220  ret.push_back(ss.str());
221  }
222  }
223  return ret;
224 }
225 
226 void Agent::AddToTable() {
227  ctx_->NewDatum("AgentEntry")
228  ->AddVal("AgentId", id_)
229  ->AddVal("Kind", kind_)
230  ->AddVal("Spec", spec_)
231  ->AddVal("Prototype", prototype_)
232  ->AddVal("ParentId", parent_id_)
233  ->AddVal("Lifetime", lifetime_)
234  ->AddVal("EnterTime", enter_time_)
235  ->Record();
236 }
237 
238 } // namespace cyclus
virtual void Decommission()
Decommissions the agent, removing it from the simulation.
Definition: agent.cc:182
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
Definition: pyne.cc:11180
bool InFamilyTree(Agent *other)
returns true if this agent is in the parent-child family tree of an other agent
Definition: agent.cc:149
Meta data and results of a query.
virtual std::string str()
Description of this agent.
Definition: agent.cc:96
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...
Definition: agent.cc:36
debugging information
Definition: logger.h:60
const std::string prototype() const
Returns the agent&#39;s prototype.
Definition: agent.h:347
For values that are too big, too small, etc.
Definition: error.h:41
A class for extracting information from a given XML parser.
Definition: infile_tree.h:22
virtual const int id() const
The agent instance&#39;s unique ID within a simulation.
Definition: agent.h:354
virtual std::string InformErrorMsg(std::string msg)
adds agent-specific information prefix to an error message
Definition: agent.cc:27
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...
Definition: agent.cc:129
Datum * NewDatum(std::string title)
Returns a new datum to be used exactly as the Context::NewDatum method.
Definition: db_init.cc:12
virtual ~Agent()
Removes references to self from simulation context, parent, children, etc.
Definition: agent.cc:72
std::vector< std::string > GetTreePrintOuts(Agent *m)
returns a vector of strings representing the parent-child tree at the node for Agent m ...
Definition: agent.cc:208
#define CLOG(level)
Definition: logger.h:39
virtual void Build(Agent *parent)
Called when the agent enters the smiulation as an active participant and is only ever called once...
Definition: agent.cc:153
T GetVal(std::string field, int row=0)
Convenience method for retrieving a value from a specific row and named field (column).
int child(int nuc, unsigned int rx, std::string z="n")
Definition: pyne.cc:6594
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...
Definition: infile_tree.cc:54
Agent(Context *ctx)
Creates a new agent that is managed by the given context.
Definition: agent.cc:59
virtual int time()
Returns the current simulation timestep.
Definition: context.cc:227
debugging information
Definition: logger.h:59
virtual void EnterNotify()
Called to give the agent an opportunity to register for services (e.g.
Definition: agent.cc:166
Context * context() const
Returns this agent&#39;s simulation context.
Definition: agent.h:369
std::string kind_
describes the agent subclass (e.g.
Definition: agent.h:449
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
Definition: datum.cc:22
DbInit provides an interface for agents to record data to the output db that automatically injects th...
Definition: db_init.h:14
virtual void Snapshot(DbInit di)=0
Snapshots agent-internal state to the database via the DbInit var di.
Definition: agent.cc:52
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...
Definition: agent.cc:139
Agent * parent() const
Returns parent of this agent. Returns NULL if the agent has no parent.
Definition: agent.h:375
#define MLOG(level)
Definition: logger.h:43
const int lifetime() const
Returns the number of time steps this agent operates between building and decommissioning (-1 if the ...
Definition: agent.h:397
Code providing rudimentary logging capability for the Cyclus core.
A simulation context provides access to necessary simulation-global functions and state...
Definition: context.h:130
virtual QueryResult Query(std::string table, std::vector< Cond > *conds)=0
Return a set of rows from the specificed table that match all given conditions.
The abstract base class used by all types of agents that live and interact in a simulation.
Definition: agent.h:51
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
void lifetime_force(int n_timesteps)
Sets the number of time steps this agent operates between building and decommissioning (-1 if the age...
Definition: agent.cc:115
debugging information - least verbose
Definition: logger.h:58
Information helpful for simulation users and developers alike.
Definition: logger.h:55
virtual void InitFrom(QueryableBackend *b)
Intializes an agent&#39;s internal state from the database.
Definition: agent.cc:45
void Record()
Record this datum to its Recorder.
Definition: datum.cc:35
const std::set< Agent * > & children() const
Returns a list of children this agent has.
Definition: agent.h:414
Interface implemented by backends that support rudimentary querying.
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition: context.cc:239
For failed retrieval/insertion of key-based data into/from data structures.
Definition: error.h:47
std::string PrintChildren()
recursively prints the parent-child tree
Definition: agent.cc:193
void DelAgent(Agent *m)
Destructs and cleans up m (and it&#39;s children recursively).
Definition: context.cc:98