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_
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
96std::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
109 if (enter_time_ != -1) {
110 throw ValueError("cannot set the lifetime of an already-built facility");
111 }
112 lifetime_ = n_timesteps;
113}
114
116 try{
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
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
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
153void Agent::Build(Agent* parent) {
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
170void 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
193std::string Agent::PrintChildren() {
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
208std::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
226void 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
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:49
virtual void InitFrom(QueryableBackend *b)
Intializes an agent's internal state from the database.
Definition agent.cc:45
const std::string prototype() const
Returns the agent's prototype.
Definition agent.h:345
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
Agent(Context *ctx)
Creates a new agent that is managed by the given context.
Definition agent.cc:59
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
Context * context() const
Returns this agent's simulation context.
Definition agent.h:367
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
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
const int lifetime() const
Returns the number of time steps this agent operates between building and decommissioning (-1 if the ...
Definition agent.h:395
const std::set< Agent * > & children() const
Returns a list of children this agent has.
Definition agent.h:412
virtual const int id() const
The agent instance's unique ID within a simulation.
Definition agent.h:352
virtual void Decommission()
Decommissions the agent, removing it from the simulation.
Definition agent.cc:182
virtual void Snapshot(DbInit di)=0
Snapshots agent-internal state to the database via the DbInit var di.
Definition agent.cc:52
std::string PrintChildren()
recursively prints the parent-child tree
Definition agent.cc:193
virtual ~Agent()
Removes references to self from simulation context, parent, children, etc.
Definition agent.cc:72
bool InFamilyTree(Agent *other)
returns true if this agent is in the parent-child family tree of an other agent
Definition agent.cc:149
std::string kind_
describes the agent subclass (e.g.
Definition agent.h:447
virtual void EnterNotify()
Called to give the agent an opportunity to register for services (e.g.
Definition agent.cc:166
Agent * parent() const
Returns parent of this agent. Returns NULL if the agent has no parent.
Definition agent.h:373
virtual std::string str()
Description of this agent.
Definition agent.cc:96
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
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
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:145
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition context.cc:351
void DelAgent(Agent *m)
Destructs and cleans up m (and it's children recursively).
Definition context.cc:111
virtual int time()
Returns the current simulation timestep.
Definition context.cc:313
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
void Record()
Record this datum to its Recorder.
Definition datum.cc:35
DbInit provides an interface for agents to record data to the output db that automatically injects th...
Definition db_init.h:14
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:47
Meta data and results of a query.
Interface implemented by backends that support rudimentary querying.
For values that are too big, too small, etc.
Definition error.h:41
Code providing rudimentary logging capability for the Cyclus core.
#define CLOG(level)
Definition logger.h:39
#define MLOG(level)
Definition logger.h:43
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:60
@ LEV_INFO3
Information helpful for simulation users and developers alike.
Definition logger.h:55
@ LEV_DEBUG1
debugging information - least verbose
Definition logger.h:58
@ LEV_DEBUG2
debugging information
Definition logger.h:59
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters