CYCLUS
Loading...
Searching...
No Matches
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
14namespace cyclus {
15
16// static members
17int 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
27std::string Agent::InformErrorMsg(std::string msg) {
28 std::stringstream ret;
29 ret << "A(n) " << spec_ << " named " << prototype_ << " at time "
30 << context()->time() << " received the following error:\n"
31 << msg;
32 return ret.str();
33}
34
36 std::string proto = qe->GetString("name");
37 int lifetime = OptionalQuery<int>(qe, "lifetime", -1);
38 di.NewDatum("Agent")
39 ->AddVal("Prototype", proto)
40 ->AddVal("Lifetime", lifetime)
41 ->Record();
42}
43
45 QueryResult qr = b->Query("Agent", NULL);
46 prototype_ = qr.GetVal<std::string>("Prototype");
47 id_ = qr.GetVal<int>("AgentId");
48 lifetime_ = qr.GetVal<int>("Lifetime");
49}
50
52 di.NewDatum("Agent")
53 ->AddVal("Prototype", prototype_)
54 ->AddVal("Lifetime", lifetime_)
55 ->Record();
56}
57
59 : ctx_(ctx),
60 id_(next_id_++),
61 kind_("Agent"),
62 parent_id_(-1),
63 enter_time_(-1),
64 lifetime_(-1),
65 parent_(NULL),
66 spec_("UNSPECIFIED") {
67 ctx_->agent_list_.insert(this);
68 MLOG(LEV_DEBUG3) << "Agent ID=" << id_ << ", ptr=" << this << " created.";
69}
70
72 MLOG(LEV_DEBUG3) << "Deleting agent '" << prototype() << "' ID=" << id_;
73 context()->agent_list_.erase(this);
74
75 std::set<Agent*>::iterator it;
76 if (parent_ != NULL) {
77 CLOG(LEV_DEBUG2) << "Agent '" << parent_->prototype()
78 << "' ID=" << parent_->id() << " has removed child '"
79 << prototype() << "' ID=" << id()
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);
84 }
85 }
86
87 // set children's parents to NULL
88 for (it = children_.begin(); it != children_.end(); ++it) {
89 Agent* child = *it;
90 child->parent_ = NULL;
91 child->parent_id_ = -1;
92 }
93}
94
95std::string Agent::str() {
96 std::stringstream ss;
97 ss << kind_ << "_" << prototype_ << " ( " << "ID=" << id_
98 << ", implementation=" << spec_ << ", name=" << prototype_
99 << ", parentID=" << parent_id_ << " ) ";
100 return ss.str();
101}
102
103void Agent::lifetime(int n_timesteps) {
104 if (enter_time_ != -1) {
105 throw ValueError("cannot set the lifetime of an already-built facility");
106 }
107 lifetime_ = n_timesteps;
108}
109
110void Agent::lifetime_force(int n_timesteps) {
111 try {
112 lifetime(n_timesteps);
113 } catch (ValueError e) {
114 if (enter_time_ + n_timesteps <= context()->time()) {
115 lifetime(context()->time() - enter_time_ + 1);
116 } else {
117 lifetime_ = n_timesteps;
118 }
119 }
120}
121
123 other = other->parent();
124 while (other != NULL) {
125 if (this == other) return true;
126 other = other->parent();
127 }
128 return false;
129}
130
132 const std::set<Agent*>& children = other->children();
133 std::set<Agent*>::const_iterator it = children.begin();
134 for (; it != children.end(); ++it) {
135 if (this == *(it) || this->DecendentOf(*(it))) return true;
136 }
137 return false;
138}
139
141 return (this == other) || AncestorOf(other) || DecendentOf(other);
142}
143
145 CLOG(LEV_DEBUG1) << "Agent '" << prototype()
146 << "' is entering the simulation.";
147 CLOG(LEV_DEBUG3) << "It has:";
148 CLOG(LEV_DEBUG3) << " * Spec: " << spec_;
149 CLOG(LEV_DEBUG3) << " * ID: " << id();
150
151 Connect(parent);
152 enter_time_ = ctx_->time();
153 EnterNotify();
154 this->AddToTable();
155}
156
158 ctx_->RegisterAgent(this);
159}
160
161void Agent::Connect(Agent* parent) {
162 if (parent == this) {
163 throw KeyError("Agent " + prototype() +
164 "is trying to add itself as its own child.");
165 }
166 if (parent != NULL) {
167 parent_ = parent;
168 parent_id_ = parent->id();
169 parent->children_.insert(this);
170 }
171}
172
174 CLOG(LEV_INFO3) << prototype() << "(" << this << ")"
175 << " is being decommissioned";
176 ctx_->NewDatum("AgentExit")
177 ->AddVal("AgentId", id())
178 ->AddVal("ExitTime", ctx_->time())
179 ->Record();
180 ctx_->UnregisterAgent(this);
181 ctx_->DelAgent(this);
182}
183
184std::string Agent::PrintChildren() {
185 std::stringstream ss("");
186 ss << "Children of " << prototype() << ":" << std::endl;
187
188 std::set<Agent*>::iterator it;
189 for (it = children_.begin(); it != children_.end(); ++it) {
190 Agent* child = *it;
191 std::vector<std::string> print_outs = GetTreePrintOuts(child);
192 for (int j = 0; j < print_outs.size(); j++) {
193 ss << "\t" << print_outs.at(j);
194 }
195 }
196 return ss.str();
197}
198
199std::vector<std::string> Agent::GetTreePrintOuts(Agent* m) {
200 std::vector<std::string> ret;
201 std::stringstream ss("");
202 ss << m->prototype() << std::endl;
203 ret.push_back(ss.str());
204 std::set<Agent*>::iterator it;
205 for (it = children_.begin(); it != children_.end(); ++it) {
206 Agent* child = *it;
207 std::vector<std::string> outs = GetTreePrintOuts(child);
208 for (int j = 0; j < outs.size(); j++) {
209 ss.str("");
210 ss << "\t" << outs.at(j) << std::endl;
211 ret.push_back(ss.str());
212 }
213 }
214 return ret;
215}
216
217void Agent::AddToTable() {
218 ctx_->NewDatum("AgentEntry")
219 ->AddVal("AgentId", id_)
220 ->AddVal("Kind", kind_)
221 ->AddVal("Spec", spec_)
222 ->AddVal("Prototype", prototype_)
223 ->AddVal("ParentId", parent_id_)
224 ->AddVal("Lifetime", lifetime_)
225 ->AddVal("EnterTime", enter_time_)
226 ->Record();
227}
228} // namespace cyclus
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:50
virtual void InitFrom(QueryableBackend *b)
Intializes an agent's internal state from the database.
Definition agent.cc:44
const std::string prototype() const
Returns the agent's prototype.
Definition agent.h:342
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:144
void lifetime(int n_timesteps)
Sets the number of time steps this agent operates between building and decommissioning (-1 if the age...
Definition agent.cc:103
Agent(Context *ctx)
Creates a new agent that is managed by the given context.
Definition agent.cc:58
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:199
Context * context() const
Returns this agent's simulation context.
Definition agent.h:364
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:131
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:35
const int lifetime() const
Returns the number of time steps this agent operates between building and decommissioning (-1 if the ...
Definition agent.h:392
const std::set< Agent * > & children() const
Returns a list of children this agent has.
Definition agent.h:408
virtual const int id() const
The agent instance's unique ID within a simulation.
Definition agent.h:349
virtual void Decommission()
Decommissions the agent, removing it from the simulation.
Definition agent.cc:173
virtual void Snapshot(DbInit di)=0
Snapshots agent-internal state to the database via the DbInit var di.
Definition agent.cc:51
std::string PrintChildren()
recursively prints the parent-child tree
Definition agent.cc:184
virtual ~Agent()
Removes references to self from simulation context, parent, children, etc.
Definition agent.cc:71
bool InFamilyTree(Agent *other)
returns true if this agent is in the parent-child family tree of an other agent
Definition agent.cc:140
std::string kind_
describes the agent subclass (e.g.
Definition agent.h:443
virtual void EnterNotify()
Called to give the agent an opportunity to register for services (e.g.
Definition agent.cc:157
Agent * parent() const
Returns parent of this agent. Returns NULL if the agent has no parent.
Definition agent.h:370
virtual std::string str()
Description of this agent.
Definition agent.cc:95
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:122
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:110
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:146
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition context.cc:351
virtual int time()
Returns the current simulation timestep.
Definition context.cc:314
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:21
void Record()
Record this datum to its Recorder.
Definition datum.cc:34
DbInit provides an interface for agents to record data to the output db that automatically injects th...
Definition db_init.h:14
Datum * NewDatum(std::string title)
Returns a new datum to be used exactly as the Context::NewDatum method.
Definition db_init.cc:12
A class for extracting information from a given XML parser.
Definition infile_tree.h:22
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.
Definition error.h:43
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.
Definition error.h:37
Code providing rudimentary logging capability for the Cyclus core.
#define CLOG(level)
Definition logger.h:40
#define MLOG(level)
Definition logger.h:46
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
@ LEV_DEBUG3
debugging information
Definition logger.h:72
@ LEV_INFO3
Information helpful for simulation users and developers alike.
Definition logger.h:64
@ LEV_DEBUG1
debugging information - least verbose
Definition logger.h:70
@ LEV_DEBUG2
debugging information
Definition logger.h:71
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters