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 
69 Datum* Recorder::NewDatum(std::string title) {
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  } else {
76  d->vals_.resize(0);
77  d->shapes_.resize(0);
78  }
79 
80  index_++;
81  return d;
82 }
83 
84 void Recorder::AddDatum(Datum* d) {
85  if (index_ >= data_.size()) {
86  NotifyBackends();
87  }
88 }
89 
91  if (index_ == 0)
92  return;
93  DatumList tmp = data_;
94  tmp.resize(index_);
95  index_ = 0;
96  std::list<RecBackend*>::iterator it;
97  for (it = backs_.begin(); it != backs_.end(); it++) {
98  (*it)->Notify(tmp);
99  (*it)->Flush();
100  }
101 }
102 
103 void Recorder::NotifyBackends() {
104  index_ = 0;
105  std::list<RecBackend*>::iterator it;
106  for (it = backs_.begin(); it != backs_.end(); it++) {
107  (*it)->Notify(data_);
108  }
109 }
110 
112  backs_.push_back(b);
113 }
114 
116  Flush();
117  backs_.clear();
118 }
119 
120 } // 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:115
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:12
void Flush()
Flushes all buffered Datum objects and flushes all registered backends.
Definition: recorder.cc:90
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:111
Datum * NewDatum(std::string title)
Creates a new datum namespaced under the specified title.
Definition: recorder.cc:69
virtual const char * what() const
Returns the error message associated with this Error.
Definition: error.cc:9
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
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