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 
27 std::string Agent::InformErrorMsg(std::string msg) {
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 
96 std::string Agent::str() {
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 bool Agent::AncestorOf(Agent* other) {
116  other = other->parent();
117  while (other != NULL) {
118  if (this == other)
119  return true;
120  other = other->parent();
121  }
122  return false;
123 }
124 
125 bool Agent::DecendentOf(Agent* other) {
126  const std::set<Agent*>& children = other->children();
127  std::set<Agent*>::const_iterator it = children.begin();
128  for (; it != children.end(); ++it) {
129  if (this == *(it) || this->DecendentOf(*(it)))
130  return true;
131  }
132  return false;
133 }
134 
136  return (this == other) || AncestorOf(other) || DecendentOf(other);
137 }
138 
140  CLOG(LEV_DEBUG1) << "Agent '" << prototype()
141  << "' is entering the simulation.";
142  CLOG(LEV_DEBUG3) << "It has:";
143  CLOG(LEV_DEBUG3) << " * Spec: " << spec_;
144  CLOG(LEV_DEBUG3) << " * ID: " << id();
145 
146  Connect(parent);
147  enter_time_ = ctx_->time();
148  EnterNotify();
149  this->AddToTable();
150 }
151 
153  ctx_->RegisterAgent(this);
154 }
155 
156 void Agent::Connect(Agent* parent) {
157  if (parent == this) {
158  throw KeyError("Agent " + prototype() +
159  "is trying to add itself as its own child.");
160  }
161  if (parent != NULL) {
162  parent_ = parent;
163  parent_id_ = parent->id();
164  parent->children_.insert(this);
165  }
166 }
167 
169  CLOG(LEV_INFO3) << prototype() << "(" << this << ")"
170  << " is being decommissioned";
171 
172  ctx_->NewDatum("AgentExit")
173  ->AddVal("AgentId", id())
174  ->AddVal("ExitTime", ctx_->time())
175  ->Record();
176  ctx_->UnregisterAgent(this);
177  ctx_->DelAgent(this);
178 }
179 
180 std::string Agent::PrintChildren() {
181  std::stringstream ss("");
182  ss << "Children of " << prototype() << ":" << std::endl;
183 
184  std::set<Agent*>::iterator it;
185  for (it = children_.begin(); it != children_.end(); ++it) {
186  Agent* child = *it;
187  std::vector<std::string> print_outs = GetTreePrintOuts(child);
188  for (int j = 0; j < print_outs.size(); j++) {
189  ss << "\t" << print_outs.at(j);
190  }
191  }
192  return ss.str();
193 }
194 
195 std::vector<std::string> Agent::GetTreePrintOuts(Agent* m) {
196  std::vector<std::string> ret;
197  std::stringstream ss("");
198  ss << m->prototype() << std::endl;
199  ret.push_back(ss.str());
200  std::set<Agent*>::iterator it;
201  for (it = children_.begin(); it != children_.end(); ++it) {
202  Agent* child = *it;
203  std::vector<std::string> outs = GetTreePrintOuts(child);
204  for (int j = 0; j < outs.size(); j++) {
205  ss.str("");
206  ss << "\t" << outs.at(j) << std::endl;
207  ret.push_back(ss.str());
208  }
209  }
210  return ret;
211 }
212 
213 void Agent::AddToTable() {
214  ctx_->NewDatum("AgentEntry")
215  ->AddVal("AgentId", id_)
216  ->AddVal("Kind", kind_)
217  ->AddVal("Spec", spec_)
218  ->AddVal("Prototype", prototype_)
219  ->AddVal("ParentId", parent_id_)
220  ->AddVal("Lifetime", lifetime_)
221  ->AddVal("EnterTime", enter_time_)
222  ->Record();
223 }
224 
225 } // namespace cyclus
virtual void Decommission()
Decommissions the agent, removing it from the simulation.
Definition: agent.cc:168
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:135
Meta data and results of a query.
const int lifetime() const
Returns the number of time steps this agent operates between building and decommissioning (-1 if the ...
Definition: agent.h:393
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
For values that are too big, too small, etc.
Definition: error.h:41
const std::set< Agent * > & children() const
Returns a list of children this agent has.
Definition: agent.h:410
A class for extracting information from a given XML parser.
Definition: infile_tree.h:22
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:115
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:195
#define CLOG(level)
Definition: logger.h:39
const std::string prototype() const
Returns the agent&#39;s prototype.
Definition: agent.h:347
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:139
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:225
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:152
std::string kind_
describes the agent subclass (e.g.
Definition: agent.h:445
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:12
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:125
#define MLOG(level)
Definition: logger.h:43
Code providing rudimentary logging capability for the Cyclus core.
A simulation context provides access to necessary simulation-global functions and state...
Definition: context.h:128
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
Agent * parent() const
Returns parent of this agent. Returns NULL if the agent has no parent.
Definition: agent.h:375
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
Context * context() const
Returns this agent&#39;s simulation context.
Definition: agent.h:369
void Record()
Record this datum to its Recorder.
Definition: datum.cc:24
virtual const int id() const
The agent instance&#39;s unique ID within a simulation.
Definition: agent.h:354
Interface implemented by backends that support rudimentary querying.
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition: context.cc:237
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:180
void DelAgent(Agent *m)
Destructs and cleans up m (and it&#39;s children recursively).
Definition: context.cc:97