CYCLUS
xml_flat_loader.cc
Go to the documentation of this file.
1 #include "xml_flat_loader.h"
2 
3 #include "agent.h"
4 #include "context.h"
5 #include "env.h"
6 #include "error.h"
7 #include "infile_tree.h"
8 #include "logger.h"
9 #include "recorder.h"
10 #include "timer.h"
11 
12 namespace cyclus {
13 
14 std::string BuildFlatMasterSchema(std::string schema_path, std::string infile) {
15  Timer ti;
16  Recorder rec;
17  Context ctx(&ti, &rec);
18 
19  std::stringstream schema("");
20  LoadStringstreamFromFile(schema, schema_path);
21  std::string master = schema.str();
22 
23  std::vector<AgentSpec> specs = ParseSpecs(infile);
24  std::string subschemas;
25  for (int i = 0; i < specs.size(); ++i) {
26  Agent* m = DynamicModule::Make(&ctx, specs[i]);
27  subschemas += "<element name=\"" + specs[i].alias() + "\">\n";
28  subschemas += m->schema() + "\n";
29  subschemas += "</element>\n";
30  ctx.DelAgent(m);
31  }
32 
33  // replace refs in master rng template file
34  std::string search_str = std::string("@MODEL_SCHEMAS@");
35  size_t pos = master.find(search_str);
36  if (pos != std::string::npos) {
37  master.replace(pos, search_str.size(), subschemas);
38  }
39 
40  return master;
41 }
42 
45 }
46 
48  InfileTree xqe(*parser_);
49 
50  // create prototypes
51  int num_protos = xqe.NMatches("/*/prototype");
52  for (int i = 0; i < num_protos; i++) {
53  InfileTree* qe = xqe.SubTree("/*/prototype", i);
54  std::string prototype = qe->GetString("name");
55  std::string alias = qe->SubTree("config")->GetElementName(0);
56  AgentSpec spec = specs_[alias];
57 
58  Agent* agent = DynamicModule::Make(ctx_, spec);
59 
60  // call manually without agent impl injected to keep all Agent state in a
61  // single, consolidated db table
62  agent->Agent::InfileToDb(qe, DbInit(agent, true));
63 
64  agent->InfileToDb(qe, DbInit(agent));
65  rec_->Flush();
66 
67  std::vector<Cond> conds;
68  conds.push_back(Cond("SimId", "==", rec_->sim_id()));
69  conds.push_back(Cond("AgentId", "==", agent->id()));
70  conds.push_back(Cond("SimTime", "==", static_cast<int>(0)));
71  CondInjector ci(b_, conds);
72  PrefixInjector pi(&ci, "AgentState");
73 
74  // call manually without agent impl injected
75  agent->Agent::InitFrom(&pi);
76 
77  pi = PrefixInjector(&ci, "AgentState" + spec.Sanitize());
78  agent->InitFrom(&pi);
79  ctx_->AddPrototype(prototype, agent);
80  }
81 
82  // retrieve agent hierarchy and initial inventories
83  int num_agents = xqe.NMatches("/*/agent");
84  std::map<std::string, std::string> protos; // map<name, prototype>
85  std::map<std::string, std::string> parents; // map<agent, parent>
86  std::set<std::string> agents; // set<agent_name>
87  std::map<std::string, InfileTree*> invs; // map<agent, qe>;
88  for (int i = 0; i < num_agents; i++) {
89  InfileTree* qe = xqe.SubTree("/*/agent", i);
90  std::string name = qe->GetString("name");
91  std::string proto = qe->GetString("prototype");
92  std::string parent = OptionalQuery<std::string>(qe, "parent", "");
93  protos[name] = proto;
94  parents[name] = parent;
95  invs[name] = qe;
96  agents.insert(name);
97  }
98 
99  // build agents starting at roots (no parent) down.
100  std::map<std::string, Agent*> built; // map<agent_name, agent_ptr>
101  std::set<std::string>::iterator it = agents.begin();
102  while (agents.size() > 0) {
103  std::string name = *it;
104  std::string proto = protos[name];
105  std::string parent = parents[name];
106  if (parent == "") {
107  built[name] = BuildAgent(proto, NULL);
108  ++it;
109  agents.erase(name);
110  } else if (built.count(parent) > 0) {
111  built[name] = BuildAgent(proto, built[parent]);
112  ++it;
113  agents.erase(name);
114  } else {
115  ++it;
116  }
117  if (it == agents.end()) {
118  it = agents.begin();
119  }
120  }
121 }
122 
123 } // namespace cyclus
boost::shared_ptr< XMLParser > parser_
the parser
std::string BuildFlatMasterSchema(std::string schema_path, std::string infile)
Builds and returns a master cyclus input xml schema defining a flat prototype and instance structure ...
boost::uuids::uuid sim_id()
returns the unique id associated with this cyclus simulation.
Definition: recorder.cc:49
std::string schema_path_
filepath to the schema
std::string file_
the input file name
virtual int NMatches(std::string query)
investigates the current status and returns the number of elements matching a query ...
Definition: infile_tree.cc:49
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
Agent * BuildAgent(std::string proto, Agent *parent)
Creates and builds an agent, notifying its parent.
std::string Sanitize()
A class for extracting information from a given XML parser.
Definition: infile_tree.h:22
std::string name(int nuc)
Definition: pyne.cc:2940
virtual std::string GetElementName(int index=0)
investigates the current status and returns a string representing the name of a given index ...
Definition: infile_tree.cc:93
virtual std::string master_schema()
QueryableBackend * b_
virtual std::string schema()
Returns an agent&#39;s xml rng schema for initializing from input files.
Definition: agent.h:335
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
void LoadStringstreamFromFile(std::stringstream &stream, std::string file)
Reads the given file path as XML into the passed stream.
int parent(int nuc, unsigned int rx, std::string z="n")
Definition: pyne.cc:6621
Wrapper class for QueryableBackends that injects a set of Cond&#39;s into every query before being execut...
std::map< std::string, AgentSpec > specs_
std::vector< AgentSpec > ParseSpecs(std::string infile)
Returns a list of the full module+agent spec for all agents in the given input file.
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
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
Wrapper class for QueryableBackends that injects prefix in front of the title/table for every query b...
InfileTree * SubTree(std::string query, int index=0)
populates a child infile based on a query and index
Definition: infile_tree.cc:132
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
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
const double pi
pi = 3.14159265359
Definition: pyne.cc:10356
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 LoadInitialAgents()
Creates all initial agent instances from the input file.
void DelAgent(Agent *m)
Destructs and cleans up m (and it&#39;s children recursively).
Definition: context.cc:97
Controls simulation timestepping and inter-timestep phases.
Definition: timer.h:22