CYCLUS
recorder.cc
Go to the documentation of this file.
1 #include "recorder.h"
2 
3 #include <boost/uuid/uuid_generators.hpp>
4 #include <boost/uuid/uuid_io.hpp>
5 #include <boost/lexical_cast.hpp>
6 
7 #include "datum.h"
8 #include "logger.h"
9 #include "rec_backend.h"
10 
11 namespace cyclus {
12 
13 Recorder::Recorder() : index_(0), inject_sim_id_(true) {
14  uuid_ = boost::uuids::random_generator()();
16 }
17 
18 Recorder::Recorder(bool inject_sim_id) : index_(0), inject_sim_id_(inject_sim_id) {
19  uuid_ = boost::uuids::random_generator()();
21 }
22 
23 Recorder::Recorder(unsigned int dump_count) : index_(0), inject_sim_id_(true) {
24  uuid_ = boost::uuids::random_generator()();
25  set_dump_count(dump_count);
26 }
27 
28 Recorder::Recorder(boost::uuids::uuid simid) : index_(0), uuid_(simid), \
29  inject_sim_id_(true) {
31 }
32 
34  try {
35  Flush();
36  } catch (Error err) {
37  CLOG(LEV_ERROR) << "Error in Recorder destructor: " << err.what();
38  }
39 
40  for (int i = 0; i < data_.size(); ++i) {
41  delete data_[i];
42  }
43 }
44 
45 unsigned int Recorder::dump_count() {
46  return dump_count_;
47 }
48 
49 boost::uuids::uuid Recorder::sim_id() {
50  return uuid_;
51 }
52 
53 void Recorder::set_dump_count(unsigned int count) {
54  for (int i = 0; i < data_.size(); ++i) {
55  delete data_[i];
56  }
57  data_.clear();
58  data_.reserve(count);
59  for (int i = 0; i < count; ++i) {
60  Datum* d = new Datum(this, "");
61  if (inject_sim_id_) {
62  d->AddVal("SimId", uuid_);
63  }
64  data_.push_back(d);
65  }
66  dump_count_ = count;
67 }
68 
70  Datum* d = data_[index_];
71  d->title_ = title;
72  if (inject_sim_id_) {
73  d->vals_.resize(1);
74  d->shapes_.resize(1);
75  d->fields_.resize(1);
76  } else {
77  d->vals_.resize(0);
78  d->shapes_.resize(0);
79  d->fields_.resize(0);
80  }
81 
82  index_++;
83  return d;
84 }
85 
86 void Recorder::AddDatum(Datum* d) {
87  if (index_ >= data_.size()) {
88  NotifyBackends();
89  }
90 }
91 
93  if (index_ == 0)
94  return;
95  DatumList tmp = data_;
96  tmp.resize(index_);
97  index_ = 0;
98  std::list<RecBackend*>::iterator it;
99  for (it = backs_.begin(); it != backs_.end(); it++) {
100  (*it)->Notify(tmp);
101  (*it)->Flush();
102  }
103 }
104 
105 void Recorder::NotifyBackends() {
106  index_ = 0;
107  std::list<RecBackend*>::iterator it;
108  for (it = backs_.begin(); it != backs_.end(); it++) {
109  (*it)->Notify(data_);
110  }
111 }
112 
114  backs_.push_back(b);
115 }
116 
118  Flush();
119  backs_.clear();
120 }
121 
122 } // namespace cyclus
boost::uuids::uuid sim_id()
returns the unique id associated with this cyclus simulation.
Definition: recorder.cc:49
void set_dump_count(unsigned int count)
set the Recorder to flush its collected Datum objects to registered backends every [count] Datum obje...
Definition: recorder.cc:53
void Close()
Flushes all buffered Datum objects and flushes all registered backends.
Definition: recorder.cc:117
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
Definition: pyne.cc:11180
A generic mechanism to manually manage exceptions.
Definition: error.h:12
unsigned int dump_count()
Return the dump frequency, # Datum objects buffered between flushes to backends.
Definition: recorder.cc:45
friend class Datum
Definition: recorder.h:46
Recorder()
create a new recorder with default dump frequency, random simulation id, and simulation id injection...
Definition: recorder.cc:13
#define CLOG(level)
Definition: logger.h:39
static unsigned int const kDefaultDumpCount
default number of Datum objects to collect before flushing to backends.
Definition: recorder.h:21
Used to specify and send a collection of key-value pairs to the Recorder for recording.
Definition: datum.h:15
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
Definition: datum.cc:22
void Flush()
Flushes all buffered Datum objects and flushes all registered backends.
Definition: recorder.cc:92
An abstract base class for listeners (e.g.
Definition: rec_backend.h:16
void RegisterBackend(RecBackend *b)
Registers b to receive Datum notifications for all Datum objects collected by the Recorder and to rec...
Definition: recorder.cc:113
Datum * NewDatum(std::string title)
Creates a new datum namespaced under the specified title.
Definition: recorder.cc:69
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
virtual const char * what() const
Returns the error message associated with this Error.
Definition: error.cc:9
std::vector< Datum * > DatumList
Definition: rec_backend.h:12
Use for errors that require agent code or input file modification (use extremely sparingly) ...
Definition: logger.h:51
bool inject_sim_id()
returns whether or not the unique simulation id will be injected.
Definition: recorder.h:83