CYCLUS
resource_buff.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_TOOLKIT_RESOURCE_BUFF_H_
2 #define CYCLUS_SRC_TOOLKIT_RESOURCE_BUFF_H_
3 
4 #include <limits>
5 #include <list>
6 #include <set>
7 #include <vector>
8 
9 #include "cyc_limits.h"
10 #include "error.h"
11 #include "product.h"
12 #include "material.h"
13 #include "resource.h"
14 
15 namespace cyclus {
16 namespace toolkit {
17 
18 static double const kBuffInfinity = std::numeric_limits<double>::max();
19 
20 typedef std::vector<Resource::Ptr> Manifest;
21 
22 /// ResourceBuff is a helper function that provides semi-automated management of
23 /// resource buffers (e.g. agent stocks and inventories).
24 ///
25 /// Methods that begin with a "set", "make", "push", or "pop" prefix change the
26 /// state/behavior of the store; other methods do not. Default constructed
27 /// resource store has infinite capacity. Resource popping occurs in the order
28 /// the resources were pushed (i.e. oldest resources are popped first), unless
29 /// explicitly specified otherwise.
30 ///
31 /// @warn In the near future this class will become deprecated in favor of
32 /// ResBuf.
33 class ResourceBuff {
34  public:
35  enum AccessDir {
38  };
39 
40  ResourceBuff() : capacity_(kBuffInfinity), qty_(0) {}
41 
42  virtual ~ResourceBuff() {}
43 
44  /// Capacity returns the maximum resource quantity this store can hold (units
45  /// based on constituent resource objects' units).
46  /// Never throws.
47  inline double capacity() const {
48  return capacity_;
49  }
50 
51  /// Set_capacity sets the maximum quantity this store can hold (units based
52  /// on constituent resource objects' units).
53  ///
54  /// @throws ValueError the new capacity is lower (by eps_rsrc()) than the
55  /// quantity of resources that already exist in the store.
56  void set_capacity(double cap);
57 
58  /// Count returns the total number of constituent resource objects
59  /// in the store. Never throws.
60  inline int count() const {
61  return mats_.size();
62  }
63 
64  /// Quantity returns the total resource quantity of constituent resource
65  /// objects in the store. Never throws.
66  inline double quantity() const {
67  return qty_;
68  }
69 
70  /// Space returns the quantity of space remaining in this store.
71  ///
72  /// It is effectively the difference between the capacity and the quantity
73  /// and is never negative. Never throws.
74  inline double space() const {
75  return std::max(0.0, capacity_ - qty_);
76  }
77 
78  /// Returns true if there are no mats in mats_
79  inline bool empty() const {
80  return mats_.empty();
81  }
82 
83  /// PopQty pops the specified quantity of resources from the buffer.
84  ///
85  /// Resources are split if necessary in order to pop the exact quantity
86  /// specified (within eps_rsrc()). Resources are retrieved in the order they were
87  /// pushed (i.e. oldest first).
88  ///
89  /// @throws ValueError the specified pop quantity is larger than the
90  /// buffer's current quantity.
91  Manifest PopQty(double qty);
92 
93  /// Same behavior as PopQty(double) except a non-zero eps may be specified
94  /// for cases where qty might be larger than the buffer's current quantity.
95  Manifest PopQty(double qty, double eps);
96 
97  /// PopN pops the specified number or count of resource objects from the
98  /// store.
99  ///
100  /// Resources are not split. Resources are retrieved in the order they were
101  /// pushed (i.e. oldest first).
102  ///
103  /// @throws ValueError the specified pop number is larger than the
104  /// store's current inventoryNum or the specified number is negative.
105  Manifest PopN(int num);
106 
107  /// Pop pops one resource object from the store.
108  ///
109  /// Resources are not split. Resources are retrieved by default in the order
110  /// they were pushed (i.e. oldest first).
111  ///
112  /// @param dir the access direction, which is the front by default
113  ///
114  /// @throws ValueError the store is empty.
116 
117  /// A convenience method identical to Pop for auto-casting to specific
118  /// Resource types.
119  template <class T>
120  typename T::Ptr Pop() {
121  return boost::dynamic_pointer_cast<T>(Pop());
122  }
123 
124  /// Push pushs a single resource object to the store.
125  ///
126  /// Resource objects are never combined in the store; they are stored as
127  /// unique objects. The resource object is only pushed to the store if it does not
128  /// cause the store to exceed its capacity
129  ///
130  /// @throws ValueError the pushing of the given resource object would
131  /// cause the store to exceed its capacity.
132  ///
133  /// @throws KeyError the resource object to be pushed is already present
134  /// in the store.
135  void Push(Resource::Ptr r);
136 
137  /// PushAll pushess one or more resource objects (as a std::vector) to the store.
138  ///
139  /// Resource objects are never combined in the store; they are stored as
140  /// unique objects. The resource objects are only pushed to the store if they do
141  /// not cause the store to exceed its capacity; otherwise none of the given
142  /// resource objects are pushed to the store.
143  ///
144  /// @throws ValueError the pushing of the given resource objects would
145  /// cause the store to exceed its capacity.
146  ///
147  /// @throws KeyError one or more of the resource objects to be pushed
148  /// are already present in the store.
149  template <class B>
150  void PushAll(std::vector<B> rs) {
151  double tot_qty = 0;
152  for (int i = 0; i < rs.size(); i++) {
153  tot_qty += rs.at(i)->quantity();
154  }
155  if (tot_qty - space() > eps_rsrc()) {
156  throw ValueError("Resource pushing breaks capacity limit.");
157  }
158 
159  for (int i = 0; i < rs.size(); i++) {
160  if (mats_present_.count(rs.at(i)) == 1) {
161  throw KeyError("Duplicate resource pushing attempted");
162  }
163  }
164 
165  for (int i = 0; i < rs.size(); i++) {
166  mats_.push_back(rs[i]);
167  mats_present_.insert(rs[i]);
168  }
169  qty_ += tot_qty;
170  }
171 
172  private:
173  void UpdateQty();
174 
175  double qty_;
176 
177  /// Maximum quantity of resources this store can hold
178  double capacity_;
179 
180  /// List of constituent resource objects forming the store's inventory
181  std::list<Resource::Ptr> mats_;
182  std::set<Resource::Ptr> mats_present_;
183 };
184 
185 } // namespace toolkit
186 } // namespace cyclus
187 
188 #endif // CYCLUS_SRC_TOOLKIT_RESOURCE_BUFF_H_
std::vector< Resource::Ptr > Manifest
Definition: resource_buff.h:20
double space() const
Space returns the quantity of space remaining in this store.
Definition: resource_buff.h:74
Manifest PopN(int num)
PopN pops the specified number or count of resource objects from the store.
void Push(Resource::Ptr r)
Push pushs a single resource object to the store.
int count() const
Count returns the total number of constituent resource objects in the store.
Definition: resource_buff.h:60
T::Ptr Pop()
A convenience method identical to Pop for auto-casting to specific Resource types.
bool empty() const
Returns true if there are no mats in mats_.
Definition: resource_buff.h:79
For values that are too big, too small, etc.
Definition: error.h:41
double capacity() const
Capacity returns the maximum resource quantity this store can hold (units based on constituent resour...
Definition: resource_buff.h:47
ResourceBuff is a helper function that provides semi-automated management of resource buffers (e...
Definition: resource_buff.h:33
double quantity() const
Quantity returns the total resource quantity of constituent resource objects in the store...
Definition: resource_buff.h:66
void set_capacity(double cap)
Set_capacity sets the maximum quantity this store can hold (units based on constituent resource objec...
Definition: resource_buff.cc:9
double eps_rsrc()
an epsilon value to be used by resources
Definition: cyc_limits.h:19
static double const kBuffInfinity
Definition: resource_buff.h:18
boost::shared_ptr< Resource > Ptr
Definition: resource.h:24
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
void PushAll(std::vector< B > rs)
PushAll pushess one or more resource objects (as a std::vector) to the store.
Manifest PopQty(double qty)
PopQty pops the specified quantity of resources from the buffer.
For failed retrieval/insertion of key-based data into/from data structures.
Definition: error.h:47
double eps()
a generic epsilon value
Definition: cyc_limits.h:12