CYCLUS
Loading...
Searching...
No Matches
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 "discovery.h"
6#include "env.h"
7#include "error.h"
8#include "infile_tree.h"
9#include "logger.h"
10#include "recorder.h"
11#include "timer.h"
12
13namespace cyclus {
14
16 std::string schema_path, std::vector<AgentSpec> specs) {
17 Timer ti;
18 Recorder rec;
19 Context ctx(&ti, &rec);
20
21 std::stringstream schema("");
22 LoadStringstreamFromFile(schema, schema_path);
23 std::string master = schema.str();
24
25 std::string subschemas;
26 for (int i = 0; i < specs.size(); ++i) {
27 Agent* m = DynamicModule::Make(&ctx, specs[i]);
28 subschemas += "<element name=\"" + specs[i].alias() + "\">\n";
29 subschemas += m->schema() + "\n";
30 subschemas += "</element>\n";
31 ctx.DelAgent(m);
32 }
33
34 // replace refs in master rng template file
35 std::string search_str = std::string("@MODEL_SCHEMAS@");
36 size_t pos = master.find(search_str);
37 if (pos != std::string::npos) {
38 master.replace(pos, search_str.size(), subschemas);
39 }
40
41 return master;
42}
43
44std::string BuildFlatMasterSchema(std::string schema_path, std::string infile) {
45 std::vector<AgentSpec> specs = ParseSpecs(infile);
46
47 return BuildFlatMasterSchema(schema_path, specs);
48}
49
50std::string BuildFlatMasterSchema(std::string schema_path) {
51 std::vector<AgentSpec> specs =
53
54 return BuildFlatMasterSchema(schema_path, specs);
55}
56
60
62 InfileTree xqe(*parser_);
63
64 // create prototypes
65 int num_protos = xqe.NMatches("/*/prototype");
66 for (int i = 0; i < num_protos; i++) {
67 InfileTree* qe = xqe.SubTree("/*/prototype", i);
68 std::string prototype = qe->GetString("name");
69 std::string alias = qe->SubTree("config")->GetElementName(0);
70 AgentSpec spec = specs_[alias];
71
72 Agent* agent = DynamicModule::Make(ctx_, spec);
73
74 // call manually without agent impl injected to keep all Agent state in a
75 // single, consolidated db table
76 agent->Agent::InfileToDb(qe, DbInit(agent, true));
77
78 agent->InfileToDb(qe, DbInit(agent));
79 rec_->Flush();
80
81 std::vector<Cond> conds;
82 conds.push_back(Cond("SimId", "==", rec_->sim_id()));
83 conds.push_back(Cond("AgentId", "==", agent->id()));
84 conds.push_back(Cond("SimTime", "==", static_cast<int>(0)));
85 CondInjector ci(b_, conds);
86 PrefixInjector pi(&ci, "AgentState");
87
88 // call manually without agent impl injected
89 agent->Agent::InitFrom(&pi);
90
91 pi = PrefixInjector(&ci, "AgentState" + spec.Sanitize());
92 agent->InitFrom(&pi);
93 ctx_->AddPrototype(prototype, agent);
94 }
95
96 // retrieve agent hierarchy and initial inventories
97 int num_agents = xqe.NMatches("/*/agent");
98 std::map<std::string, std::string> protos; // map<name, prototype>
99 std::map<std::string, std::string> parents; // map<agent, parent>
100 std::set<std::string> agents; // set<agent_name>
101 std::map<std::string, InfileTree*> invs; // map<agent, qe>;
102 for (int i = 0; i < num_agents; i++) {
103 InfileTree* qe = xqe.SubTree("/*/agent", i);
104 std::string name = qe->GetString("name");
105 std::string proto = qe->GetString("prototype");
106 std::string parent = OptionalQuery<std::string>(qe, "parent", "");
107 protos[name] = proto;
108 parents[name] = parent;
109 invs[name] = qe;
110 agents.insert(name);
111 }
112
113 // build agents starting at roots (no parent) down.
114 std::map<std::string, Agent*> built; // map<agent_name, agent_ptr>
115 std::set<std::string>::iterator it = agents.begin();
116 while (agents.size() > 0) {
117 std::string name = *it;
118 std::string proto = protos[name];
119 std::string parent = parents[name];
120 if (parent == "") {
121 built[name] = BuildAgent(proto, NULL);
122 ++it;
123 agents.erase(name);
124 } else if (built.count(parent) > 0) {
125 built[name] = BuildAgent(proto, built[parent]);
126 ++it;
127 agents.erase(name);
128 } else {
129 ++it;
130 }
131 if (it == agents.end()) {
132 it = agents.begin();
133 }
134 }
135}
136
137} // 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
virtual std::string schema()
Returns an agent's xml rng schema for initializing from input files.
Definition agent.h:334
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
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
void DelAgent(Agent *m)
Destructs and cleans up m (and it's children recursively).
Definition context.cc:110
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.
A class for extracting information from a given XML parser.
Definition infile_tree.h:22
virtual int NMatches(std::string query)
investigates the current status and returns the number of elements matching a query
virtual std::string GetElementName(int index=0)
investigates the current status and returns a string representing the name of a given index
InfileTree * SubTree(std::string query, int index=0)
populates a child infile based on a query and index
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...
Wrapper class for QueryableBackends that injects prefix in front of the title/table for every query b...
Collects and manages output data generation for the cyclus core and agents during a simulation.
Definition recorder.h:45
Controls simulation timestepping and inter-timestep phases.
Definition timer.h:22
boost::shared_ptr< XMLParser > parser_
the parser
QueryableBackend * b_
std::string schema_path_
filepath to the schema
std::map< std::string, AgentSpec > specs_
Agent * BuildAgent(std::string proto, Agent *parent)
Creates and builds an agent, notifying its parent.
std::string file_
the input file name
void LoadInitialAgents()
Creates all initial agent instances from the input file.
virtual std::string master_schema()
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
std::string BuildFlatMasterSchema(std::string schema_path, std::vector< AgentSpec > specs)
Builds and returns a master cyclus input xml schema defining a flat prototype and instance structure ...
std::set< std::string > DiscoverSpecsInCyclusPath()
Discover archetype specifications that live recursively in CYCLUS_PATH directories.
Definition discovery.cc:134
void LoadStringstreamFromFile(std::stringstream &stream, std::string file, std::string format)
Reads the given file path as XML into the passed stream.
std::vector< AgentSpec > ParseSpecs(std::set< std::string > agent_set)
Returns a list of the full module+agent spec for all agents in the given set of agent names.
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters