CYCLUS
mock_sim.cc
Go to the documentation of this file.
1 #include "mock_sim.h"
2 
3 #include "cyclus.h"
4 #include "sim_init.h"
5 #include <sstream>
6 
7 namespace cyclus {
8 
9 // The code for this function was copied with minor adjustements from
10 // XMLFileLoader::LoadInitialAgents
11 void InitAgent(Agent* a, std::stringstream& config, Recorder* rec,
12  SqliteBack* back) {
13  XMLParser parser_;
14  parser_.Init(config);
15  InfileTree xqe(parser_);
16 
17  a->Agent::InfileToDb(&xqe, DbInit(a, true));
18  a->InfileToDb(&xqe, DbInit(a));
19  rec->Flush();
20 
21  std::vector<Cond> conds;
22  conds.push_back(Cond("SimId", "==", rec->sim_id()));
23  conds.push_back(Cond("SimTime", "==", static_cast<int>(0)));
24  conds.push_back(Cond("AgentId", "==", a->id()));
25  CondInjector ci(back, conds);
26 
27  // call manually without agent impl injected
28  PrefixInjector pi(&ci, "AgentState");
29  a->Agent::InitFrom(&pi);
30 
31  pi = PrefixInjector(&ci, "AgentState" + AgentSpec(a->spec()).Sanitize());
32  a->InitFrom(&pi);
33 }
34 
35 ///////// MockAgent ////////////
36 
37 int MockAgent::nextid_ = 0;
38 
39 MockAgent::MockAgent(Context* ctx, Recorder* rec, SqliteBack* b, bool is_source)
40  : ctx_(ctx),
41  rec_(rec),
42  back_(b),
43  source_(is_source),
44  cap_(1e299),
45  lifetime_(-1),
46  start_(0) {
47  std::stringstream ss;
48  if (is_source) {
49  ss << "source";
50  } else {
51  ss << "sink";
52  }
53  ss << nextid_++;
54  proto_ = ss.str();
55 }
56 
58  commod_ = commod;
59  return *this;
60 }
62  recipe_ = recipe;
63  return *this;
64 }
66  cap_ = cap;
67  return *this;
68 }
70  start_ = t;
71  return *this;
72 }
74  lifetime_ = duration;
75  return *this;
76 }
77 
78 std::string MockAgent::Finalize() {
79  AgentSpec spec(":agents:Sink");
80  if (source_) {
81  spec = AgentSpec(":agents:Source");
82  }
83 
84  std::stringstream xml;
85  xml << "<facility><name>" << proto_ << "</name><lifetime>" << lifetime_
86  << "</lifetime><config><SrcSnkAgent>";
87 
88  if (source_) {
89  xml << "<commod>" << commod_ << "</commod>";
90  } else {
91  xml << "<in_commods><val>" << commod_ << "</val></in_commods>";
92  }
93  xml << "<capacity>" << cap_ << "</capacity>";
94  if (recipe_ != "") {
95  xml << "<recipe_name>" << recipe_ << "</recipe_name>";
96  }
97  xml << "</SrcSnkAgent></config></facility>";
98 
99  Agent* a = DynamicModule::Make(ctx_, spec);
100  InitAgent(a, xml, rec_, back_);
101 
102  ctx_->AddPrototype(proto_, a);
103 
104  if (start_ == 0) {
105  a = ctx_->CreateAgent<Agent>(proto_);
106  a->Build(NULL);
107  } else {
108  ctx_->SchedBuild(NULL, proto_, start_);
109  }
110  return proto_;
111 }
112 
113 ///////// MockSim ////////////
114 MockSim::MockSim(int duration)
115  : ctx_(&ti_, &rec_), back_(NULL), agent(NULL) {
117  warn_limit = 0;
118  back_ = new SqliteBack(":memory:");
119  rec_.RegisterBackend(back_);
120  ctx_.InitSim(SimInfo(duration));
121 }
122 
123 MockSim::MockSim(AgentSpec spec, std::string config, int duration)
124  : ctx_(&ti_, &rec_), back_(NULL), agent(NULL) {
126  warn_limit = 0;
127  back_ = new SqliteBack(":memory:");
128  rec_.RegisterBackend(back_);
129  ctx_.InitSim(SimInfo(duration));
130 
131  Agent* a = DynamicModule::Make(&ctx_, spec);
132 
133  std::stringstream xml;
134  xml << "<facility><name>agent_being_tested</name><config><foo>" << config
135  << "</foo></config></facility>";
136  InitAgent(a, xml, &rec_, back_);
137 
138  ctx_.AddPrototype(a->prototype(), a);
139  agent = ctx_.CreateAgent<Agent>(a->prototype());
140 }
141 
142 MockSim::MockSim(AgentSpec spec, std::string config, int duration, int lifetime)
143  : ctx_(&ti_, &rec_), back_(NULL), agent(NULL) {
145  warn_limit = 0;
146  back_ = new SqliteBack(":memory:");
147  rec_.RegisterBackend(back_);
148  ctx_.InitSim(SimInfo(duration));
149 
150  Agent* a = DynamicModule::Make(&ctx_, spec);
151 
152  std::stringstream xml;
153  xml << "<facility>"
154  << "<lifetime>" << lifetime << "</lifetime>"
155  << "<name>agent_being_tested</name>"
156  << "<config><foo>" << config << "</foo></config>"
157  << "</facility>";
158  InitAgent(a, xml, &rec_, back_);
159 
160  ctx_.AddPrototype(a->prototype(), a);
161  agent = ctx_.CreateAgent<Agent>(a->prototype());
162 }
163 
164 void MockSim::DummyProto(std::string name) {
165  Agent* a = DynamicModule::Make(&ctx_, AgentSpec(":agents:Source"));
166 
167  std::stringstream xml;
168  xml << "<facility><name>" << name << "</name>"
169  << "<config><foo>"
170  << "<commod>alongunusedcommodityname</commod>"
171  << "<capacity>0</capacity>"
172  << "</foo></config></facility>";
173 
174  InitAgent(a, xml, &rec_, back_);
175  ctx_.AddPrototype(name, a);
176 };
177 
179  warn_limit = 42;
180  rec_.Close();
181  delete back_;
182 }
183 
184 MockAgent MockSim::AddSource(std::string commod) {
185  MockAgent ms(&ctx_, &rec_, back_, true);
186  ms.commod(commod);
187  return ms;
188 }
189 
190 MockAgent MockSim::AddSink(std::string commod) {
191  MockAgent ms(&ctx_, &rec_, back_, false);
192  ms.commod(commod);
193  return ms;
194 }
195 
197  ctx_.AddRecipe(name, c);
198 }
199 
201  agent->Build(NULL);
202  int id = agent->id();
203  ti_.RunSim();
204  rec_.Flush();
205  return id;
206 }
207 
209  return SimInit::BuildMaterial(back_, resid);
210 }
211 
213  return SimInit::BuildProduct(back_, resid);
214 }
215 
216 SqliteBack& MockSim::db() { return *back_; }
217 
218 } // namespace cyclus
int Run()
Runs the simulation.
Definition: mock_sim.cc:200
boost::uuids::uuid sim_id()
returns the unique id associated with this cyclus simulation.
Definition: recorder.cc:49
boost::shared_ptr< Composition > Ptr
Definition: composition.h:43
void Close()
Flushes all buffered Datum objects and flushes all registered backends.
Definition: recorder.cc:115
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
Definition: pyne.cc:11180
void AddRecipe(std::string name, Composition::Ptr c)
Adds a composition recipe to a simulation-wide accessible list.
Definition: context.cc:164
An Recorder backend that writes data to an sqlite database.
Definition: sqlite_back.h:17
MockAgent AddSource(std::string commod)
AddSource adds a source facility that can offer/provide material to the archetype being tested...
Definition: mock_sim.cc:184
Material::Ptr GetMaterial(int resid)
Reconstructs a material object from the simulation results database with the given resource state id...
Definition: mock_sim.cc:208
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
A class for extracting information from a given XML parser.
Definition: infile_tree.h:22
boost::shared_ptr< Material > Ptr
Definition: material.h:75
std::string name(int nuc)
Definition: pyne.cc:2940
MockAgent is a template for accumulating configuration information used to generate a source or sink ...
Definition: mock_sim.h:25
boost::shared_ptr< Product > Ptr
Definition: product.h:24
MockAgent(Context *ctx, Recorder *rec, SqliteBack *b, bool is_source)
Initializes a MockAgent to create a source (is_source == true) or a sink (is_source == false) in the ...
Definition: mock_sim.cc:39
void DummyProto(std::string name)
Adds a dummy prototype to the simulation that can be used by institutions and other agents for deploy...
Definition: mock_sim.cc:164
MockSim(int duration)
Creates and initializes a new empty mock simulation environment where duration is the length of the s...
Definition: mock_sim.cc:114
A helper class to hold xml file data and provide automatic validation.
Definition: xml_parser.h:16
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
unsigned int warn_limit
This is maximum number of times to issue a warning of each kind.
Definition: error.cc:13
Product::Ptr GetProduct(int resid)
Reconstructs a product object from the simulation results database with the given resource state id...
Definition: mock_sim.cc:212
std::string spec()
The string representation of the agent spec that uniquely identifies the concrete agent class/module...
Definition: agent.h:358
void InitSim(SimInfo si)
Initializes the simulation time parameters.
Definition: context.cc:179
static const void SetNucDataPath()
Initializes the path to the cyclus_nuc_data.h5 file.
Definition: env.h:98
static Product::Ptr BuildProduct(QueryableBackend *b, int resid)
Convenience function for reconstructing an untracked product object with the given resource state id ...
Definition: sim_init.cc:500
Wrapper class for QueryableBackends that injects a set of Cond&#39;s into every query before being execut...
void AddRecipe(std::string name, Composition::Ptr c)
AddRecipe adds a recipe to the mock simulation environment (i.e.
Definition: mock_sim.cc:196
MockAgent lifetime(int duration)
Sets the lifetime in time steps of the source/sink before it is decommissioned.
Definition: mock_sim.cc:73
T * CreateAgent(std::string proto_name)
Create a new agent by cloning the named prototype.
Definition: context.h:178
MockAgent commod(std::string commod)
Sets the commodity to be offered/requested by the source/sink.
Definition: mock_sim.cc:57
void AddPrototype(std::string name, Agent *m)
Adds a prototype to a simulation-wide accessible list, a prototype can not be added more than once...
Definition: context.cc:135
DbInit provides an interface for agents to record data to the output db that automatically injects th...
Definition: db_init.h:14
MockAgent AddSink(std::string commod)
AddSink adds a sink facility that can request+receive material from the archetype being tested...
Definition: mock_sim.cc:190
void Flush()
Flushes all buffered Datum objects and flushes all registered backends.
Definition: recorder.cc:90
Collects and manages output data generation for the cyclus core and agents during a simulation...
Definition: recorder.h:45
void RegisterBackend(RecBackend *b)
Registers b to receive Datum notifications for all Datum objects collected by the Recorder and to rec...
Definition: recorder.cc:111
MockAgent recipe(std::string recipe)
Sets the recipe to be offered/requested by the source/sink.
Definition: mock_sim.cc:61
std::string Finalize()
Finalize MUST be called after configuration is complete to actually create the source/sink agent (or ...
Definition: mock_sim.cc:78
MockAgent start(int timestep)
Sets the time step in the simulation that the source/sink should be deployed.
Definition: mock_sim.cc:69
void RunSim()
Runs the simulation.
Definition: timer.cc:14
Wrapper class for QueryableBackends that injects prefix in front of the title/table for every query b...
Container for a static simulation-global parameters that both describe the simulation and affect its ...
Definition: context.h:37
A simulation context provides access to necessary simulation-global functions and state...
Definition: context.h:128
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
MockAgent capacity(double cap)
Sets the per time step capacity/throughput limit for provided/received material in kg for the source/...
Definition: mock_sim.cc:65
const double pi
pi = 3.14159265359
Definition: pyne.cc:10356
static Material::Ptr BuildMaterial(QueryableBackend *b, int resid)
Convenience function for reconstructing an untracked material object with the given resource state id...
Definition: sim_init.cc:486
Agent * agent
the agent being tested by the mock simulation environment.
Definition: mock_sim.h:226
int id(int nuc)
Definition: pyne.cc:2716
virtual void InitFrom(QueryableBackend *b)
Intializes an agent&#39;s internal state from the database.
Definition: agent.cc:45
Represents a condition used to filter rows returned by a query.
virtual const int id() const
The agent instance&#39;s unique ID within a simulation.
Definition: agent.h:354
static Agent * Make(Context *ctx, AgentSpec spec)
Returns a newly constructed agent for the given module spec.
void InitAgent(Agent *a, std::stringstream &config, Recorder *rec, SqliteBack *back)
Definition: mock_sim.cc:11
void Init(const std::stringstream &input)
initializes a parser with an xml snippet
Definition: xml_parser.cc:24
SqliteBack & db()
Returns the underlying in-memory database containing results for the simulation.
Definition: mock_sim.cc:216
void SchedBuild(Agent *parent, std::string proto_name, int t=-1)
Schedules the named prototype to be built for the specified parent at timestep t. ...
Definition: context.cc:105