CYCLUS
Loading...
Searching...
No Matches
discovery.cc
Go to the documentation of this file.
1#include "discovery.h"
2
3#include <fstream>
4#include <iostream>
5#include <streambuf>
6#include <vector>
7
8#include <boost/filesystem.hpp>
9#include <boost/foreach.hpp>
10#include <boost/algorithm/string.hpp>
11#include <boost/algorithm/string/predicate.hpp>
12
13#include "agent.h"
14#include "context.h"
15#include "dynamic_module.h"
16#include "env.h"
17#include "platform.h"
18#include "recorder.h"
19#include "timer.h"
20
21namespace cyclus {
22
23std::set<std::string> DiscoverArchetypes(const std::string s) {
24 // Note that 9 is the length of the word "Construct"
25 using std::string;
26 std::set<string> archs;
27 size_t offset = 0;
28 size_t end_offset = 0;
29 const string words =
30 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
31 "abcdefghijklmnopqrstuvwxyz";
32 string construct = "Construct";
33 if (SUFFIX == ".dylib") construct = "_Construct";
34 size_t lenconstruct = construct.length();
35 while ((offset = s.find(construct, offset)) != string::npos) {
36 end_offset = s.find_first_not_of(words, offset + lenconstruct);
37 if (words.find(s[offset - 1]) != string::npos ||
38 offset + lenconstruct == end_offset) {
39 // make sure construct starts the word
40 offset += lenconstruct;
41 continue;
42 }
43 archs.insert(
44 string(s, offset + lenconstruct, end_offset - offset - lenconstruct));
45 offset = end_offset;
46 }
47 return archs;
48}
49
50std::set<std::string> DiscoverSpecs(std::string p, std::string lib) {
51 using std::set;
52 using std::string;
53 namespace fs = boost::filesystem;
54 // find file
55 string libpath = (fs::path(p) / fs::path("lib" + lib + SUFFIX)).string();
56 libpath = Env::FindModule(libpath);
57
58 // read in file, pre-allocates space
59 std::ifstream f(libpath.c_str());
60 std::string s;
61 f.seekg(0, std::ios::end);
62 s.reserve(f.tellg());
63 f.seekg(0, std::ios::beg);
64 s.assign((std::istreambuf_iterator<char>(f)),
65 std::istreambuf_iterator<char>());
66
67 // find specs
68 set<string> archs = DiscoverArchetypes(s);
69 set<string> specs;
70 string spec;
71 AgentSpec agentspec;
72 for (set<string>::iterator it = archs.begin(); it != archs.end(); ++it) {
73 spec = p + ":" + lib + ":" + (*it);
74 agentspec = AgentSpec(spec);
75 if (DynamicModule::Exists(agentspec)) {
76 specs.insert(spec);
77 }
78 }
79 return specs;
80}
81
82std::set<std::string> DiscoverSpecsInDir(std::string d) {
83 using std::set;
84 using std::string;
85 namespace fs = boost::filesystem;
86 set<string> specs;
87 set<string> libspecs;
88 fs::path pth;
89 boost::system::error_code errc;
90 boost::system::error_code no_err;
91 pth = d;
92 fs::recursive_directory_iterator it(pth, errc);
93 fs::recursive_directory_iterator last;
94 for (; it != last; it.increment(errc)) {
95 if (errc != no_err) {
96 if (it.depth() > 0) {
97 it.pop();
98 }
99 continue;
100 }
101 pth = it->path();
102 string pthstr = pth.string();
103 bool irf = fs::is_regular_file(pth, errc);
104 if (errc != no_err || !irf) {
105 it.disable_recursion_pending();
106 if (it.depth() > 0) {
107 it.pop();
108 }
109 continue;
110 } else if (fs::is_directory(pth, errc)) {
111 continue;
112 } else if (!boost::algorithm::ends_with(pthstr, SUFFIX)) {
113 continue;
114 }
115 string p = pth.parent_path().string();
116 string lib = pth.filename().string();
117 if (d.length() < p.length())
118 p = p.substr(d.length() + 1, string::npos);
119 else
120 p = "";
121 lib = lib.substr(3, lib.rfind(".") - 3); // remove 'lib' prefix and suffix
122 try {
123 libspecs = DiscoverSpecs(p, lib);
124 } catch (cyclus::IOError& e) {
125 }
126 for (set<string>::iterator ls = libspecs.begin(); ls != libspecs.end();
127 ++ls) {
128 specs.insert(*ls);
129 }
130 }
131 return specs;
132}
133
134std::set<std::string> DiscoverSpecsInCyclusPath() {
135 using std::set;
136 using std::string;
137 using std::vector;
138 set<string> specs;
139 set<string> dirspecs;
140 vector<string> cycpath = Env::cyclus_path();
141 for (vector<string>::iterator it = cycpath.begin(); it != cycpath.end();
142 ++it) {
143 dirspecs = DiscoverSpecsInDir((*it).length() == 0 ? "." : (*it));
144 for (set<string>::iterator ds = dirspecs.begin(); ds != dirspecs.end();
145 ++ds) {
146 specs.insert(*ds);
147 }
148 }
149 return specs;
150}
151
153 std::set<std::string> specs = cyclus::DiscoverSpecsInCyclusPath();
158 Recorder rec;
159 Timer ti;
160 Context* ctx = new Context(&ti, &rec);
161 std::string s;
162 std::set<std::string>::iterator it;
163
164 for (it = specs.begin(); it != specs.end(); ++it) {
165 s = *it;
166 Agent* m = DynamicModule::Make(ctx, s);
167 spec.append(s);
168 anno[s] = m->annotations();
169 schm[s] = m->schema();
170 ctx->DelAgent(m);
171 }
172 delete ctx;
173
174 root["specs"] = spec;
175 root["annotations"] = anno;
176 root["schema"] = schm;
177
178 return root;
179}
180
181} // namespace cyclus
Represents a JSON value.
Definition pyne.h:3088
Value & append(const Value &value)
Append value to array at the end.
Definition pyne.cc:12863
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:50
virtual Json::Value annotations()
Returns an agent's json annotations for all state variables and any other information the developer w...
Definition agent.h:339
virtual std::string schema()
Returns an agent's xml rng schema for initializing from input files.
Definition agent.h:334
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
static Agent * Make(Context *ctx, AgentSpec spec)
Returns a newly constructed agent for the given module spec.
static bool Exists(AgentSpec spec)
Tests that an agent spec really exists.
static std::string FindModule(std::string path)
Returns the full path to a module by searching through default install and CYCLUS_PATH directories.
Definition env.cc:179
static const std::vector< std::string > cyclus_path()
Definition env.cc:155
for failed reading/writing to files, network connections, etc..
Definition error.h:55
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
@ arrayValue
array value (ordered list)
Definition pyne.h:3018
@ objectValue
object value (collection of name/value pairs).
Definition pyne.h:3019
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
std::set< std::string > DiscoverSpecsInDir(std::string d)
Discover archetype specifications that live recursively in modules in a dir.
Definition discovery.cc:82
std::set< std::string > DiscoverSpecsInCyclusPath()
Discover archetype specifications that live recursively in CYCLUS_PATH directories.
Definition discovery.cc:134
Json::Value DiscoverMetadataInCyclusPath()
Discover archetype metadata in cyclus path.
Definition discovery.cc:152
std::set< std::string > DiscoverArchetypes(const std::string s)
This function returns a vector of archetype names in a given string that is the binary represnetation...
Definition discovery.cc:23
std::set< std::string > DiscoverSpecs(std::string p, std::string lib)
Discover archetype specifications for a path and library.
Definition discovery.cc:50
#define SUFFIX
Definition platform.h:1