CYCLUS
Loading...
Searching...
No Matches
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 "pyhooks.h"
6#include <sstream>
7
8namespace cyclus {
9
10// The code for this function was copied with minor adjustements from
11// XMLFileLoader::LoadInitialAgents
12void InitAgent(Agent* a, std::stringstream& config, Recorder* rec,
13 SqliteBack* back) {
14 XMLParser parser_;
15 parser_.Init(config);
16 InfileTree xqe(parser_);
17
18 a->Agent::InfileToDb(&xqe, DbInit(a, true));
19 a->InfileToDb(&xqe, DbInit(a));
20 rec->Flush();
21
22 std::vector<Cond> conds;
23 conds.push_back(Cond("SimId", "==", rec->sim_id()));
24 conds.push_back(Cond("SimTime", "==", static_cast<int>(0)));
25 conds.push_back(Cond("AgentId", "==", a->id()));
26 CondInjector ci(back, conds);
27
28 // call manually without agent impl injected
29 PrefixInjector pi(&ci, "AgentState");
30 a->Agent::InitFrom(&pi);
31
32 pi = PrefixInjector(&ci, "AgentState" + AgentSpec(a->spec()).Sanitize());
33 a->InitFrom(&pi);
34}
35
36///////// MockAgent ////////////
37
38int MockAgent::nextid_ = 0;
39
40MockAgent::MockAgent(Context* ctx, Recorder* rec, SqliteBack* b, bool is_source)
41 : ctx_(ctx),
42 rec_(rec),
43 back_(b),
44 source_(is_source),
46 lifetime_(-1),
47 start_(0) {
48 std::stringstream ss;
49 if (is_source) {
50 ss << "source";
51 } else {
52 ss << "sink";
53 }
54 ss << nextid_++;
55 proto_ = ss.str();
56}
57
59 commod_ = commod;
60 return *this;
61}
63 recipe_ = recipe;
64 return *this;
65}
67 cap_ = cap;
68 return *this;
69}
71 start_ = t;
72 return *this;
73}
75 lifetime_ = duration;
76 return *this;
77}
78
79std::string MockAgent::Finalize() {
80 AgentSpec spec(":agents:Sink");
81 if (source_) {
82 spec = AgentSpec(":agents:Source");
83 }
84
85 std::stringstream xml;
86 xml << "<facility><name>" << proto_ << "</name><lifetime>" << lifetime_
87 << "</lifetime><config><SrcSnkAgent>";
88
89 if (source_) {
90 xml << "<commod>" << commod_ << "</commod>";
91 } else {
92 xml << "<in_commods><val>" << commod_ << "</val></in_commods>";
93 }
94 xml << "<capacity>" << cap_ << "</capacity>";
95 if (recipe_ != "") {
96 xml << "<recipe_name>" << recipe_ << "</recipe_name>";
97 }
98 xml << "</SrcSnkAgent></config></facility>";
99
100 Agent* a = DynamicModule::Make(ctx_, spec);
101 InitAgent(a, xml, rec_, back_);
102
103 ctx_->AddPrototype(proto_, a);
104
105 if (start_ == 0) {
106 a = ctx_->CreateAgent<Agent>(proto_);
107 a->Build(NULL);
108 } else {
109 ctx_->SchedBuild(NULL, proto_, start_);
110 }
111 return proto_;
112}
113
114///////// MockSim ////////////
115MockSim::MockSim(int duration) : 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
123MockSim::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
142MockSim::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>" << "<lifetime>" << lifetime << "</lifetime>"
154 << "<name>agent_being_tested</name>" << "<config><foo>" << config
155 << "</foo></config>" << "</facility>";
156 InitAgent(a, xml, &rec_, back_);
157
158 ctx_.AddPrototype(a->prototype(), a);
159 agent = ctx_.CreateAgent<Agent>(a->prototype());
160}
161
162void MockSim::DummyProto(std::string name) {
163 Agent* a = DynamicModule::Make(&ctx_, AgentSpec(":agents:Source"));
164
165 std::stringstream xml;
166 xml << "<facility><name>" << name << "</name>" << "<config><foo>"
167 << "<commod>alongunusedcommodityname</commod>" << "<capacity>0</capacity>"
168 << "</foo></config></facility>";
169
170 InitAgent(a, xml, &rec_, back_);
171 ctx_.AddPrototype(name, a);
172};
173
174void MockSim::DummyProto(std::string name, std::string commod,
175 double capacity) {
176 Agent* a = DynamicModule::Make(&ctx_, AgentSpec(":agents:Source"));
177
178 std::stringstream xml;
179 xml << "<facility><name>" << name << "</name>" << "<config><foo>"
180 << "<commod>" << "commod" << "</commod>" << "<capacity>" << capacity
181 << "</capacity>" << "</foo></config></facility>";
182
183 InitAgent(a, xml, &rec_, back_);
184 ctx_.AddPrototype(name, a);
185};
186
188 warn_limit = 42;
189 rec_.Close();
190 delete back_;
191}
192
193MockAgent MockSim::AddSource(std::string commod) {
194 MockAgent ms(&ctx_, &rec_, back_, true);
195 ms.commod(commod);
196 return ms;
197}
198
199MockAgent MockSim::AddSink(std::string commod) {
200 MockAgent ms(&ctx_, &rec_, back_, false);
201 ms.commod(commod);
202 return ms;
203}
204
205void MockSim::AddRecipe(std::string name, Composition::Ptr c) {
206 ctx_.AddRecipe(name, c);
207}
208
210 // Initialize Python functionality
211 PyStart();
212
213 agent->Build(NULL);
214 int id = agent->id();
215 ti_.RunSim();
216 rec_.Flush();
217
218 PyStop();
219 return id;
220}
221
223 return SimInit::BuildMaterial(back_, resid);
224}
225
227 return SimInit::BuildProduct(back_, resid);
228}
229
231 return *back_;
232}
233
234} // namespace cyclus
std::string Sanitize()
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
std::string spec()
The string representation of the agent spec that uniquely identifies the concrete agent class/module.
Definition agent.h:353
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
virtual const int id() const
The agent instance's unique ID within a simulation.
Definition agent.h:349
boost::shared_ptr< Composition > Ptr
Definition composition.h:43
Wrapper class for QueryableBackends that injects a set of Cond's into every query before being execut...
Represents a condition used to filter rows returned by a query.
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:146
DbInit provides an interface for agents to record data to the output db that automatically injects th...
Definition db_init.h:14
static Agent * Make(Context *ctx, AgentSpec spec)
Returns a newly constructed agent for the given module spec.
static const void SetNucDataPath()
Initializes the path to the cyclus_nuc_data.h5 file.
Definition env.h:99
A class for extracting information from a given XML parser.
Definition infile_tree.h:22
boost::shared_ptr< Material > Ptr
Definition material.h:75
MockAgent is a template for accumulating configuration information used to generate a source or sink ...
Definition mock_sim.h:25
MockAgent recipe(std::string recipe)
Sets the recipe to be offered/requested by the source/sink.
Definition mock_sim.cc:62
MockAgent lifetime(int duration)
Sets the lifetime in time steps of the source/sink before it is decommissioned.
Definition mock_sim.cc:74
MockAgent commod(std::string commod)
Sets the commodity to be offered/requested by the source/sink.
Definition mock_sim.cc:58
MockAgent start(int timestep)
Sets the time step in the simulation that the source/sink should be deployed.
Definition mock_sim.cc:70
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:66
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:40
std::string Finalize()
Finalize MUST be called after configuration is complete to actually create the source/sink agent (or ...
Definition mock_sim.cc:79
MockSim(int duration)
Creates and initializes a new empty mock simulation environment where duration is the length of the s...
Definition mock_sim.cc:115
Product::Ptr GetProduct(int resid)
Reconstructs a product object from the simulation results database with the given resource state id.
Definition mock_sim.cc:226
MockAgent AddSink(std::string commod)
AddSink adds a sink facility that can request+receive material from the archetype being tested.
Definition mock_sim.cc:199
void AddRecipe(std::string name, Composition::Ptr c)
AddRecipe adds a recipe to the mock simulation environment (i.e.
Definition mock_sim.cc:205
Agent * agent
the agent being tested by the mock simulation environment.
Definition mock_sim.h:229
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:162
Material::Ptr GetMaterial(int resid)
Reconstructs a material object from the simulation results database with the given resource state id.
Definition mock_sim.cc:222
SqliteBack & db()
Returns the underlying in-memory database containing results for the simulation.
Definition mock_sim.cc:230
int Run()
Runs the simulation.
Definition mock_sim.cc:209
MockAgent AddSource(std::string commod)
AddSource adds a source facility that can offer/provide material to the archetype being tested.
Definition mock_sim.cc:193
Wrapper class for QueryableBackends that injects prefix in front of the title/table for every query b...
boost::shared_ptr< Product > Ptr
Definition product.h:23
Collects and manages output data generation for the cyclus core and agents during a simulation.
Definition recorder.h:45
boost::uuids::uuid sim_id()
returns the unique id associated with this cyclus simulation.
Definition recorder.cc:50
void Flush()
Flushes all buffered Datum objects and flushes all registered backends.
Definition recorder.cc:93
Container for a static simulation-global parameters that both describe the simulation and affect its ...
Definition context.h:49
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:566
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:552
An Recorder backend that writes data to an sqlite database.
Definition sqlite_back.h:17
A helper class to hold xml file data and provide automatic validation.
Definition xml_parser.h:15
void Init(const std::stringstream &input)
initializes a parser with an xml snippet
Definition xml_parser.cc:49
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
void InitAgent(Agent *a, std::stringstream &config, Recorder *rec, SqliteBack *back)
Definition mock_sim.cc:12
unsigned int warn_limit
This is maximum number of times to issue a warning of each kind.
Definition error.cc:13
void PyStop(void)
Closes the current Python session.
Definition pyhooks.cc:91
void PyStart(void)
Initialize Python functionality, this is a no-op if Python was not installed along with Cyclus.
Definition pyhooks.cc:89
static const double CY_LARGE_DOUBLE
maximum (+) value for a linear variable
Definition cyc_limits.h:44