CYCLUS
Loading...
Searching...
No Matches
resource.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_RESOURCE_H_
2#define CYCLUS_SRC_RESOURCE_H_
3
4#include <string>
5#include <vector>
6#include <boost/shared_ptr.hpp>
7
8#include "package.h"
9#include "cyc_limits.h"
10
11class SimInitTest;
12
13namespace cyclus {
14
15class Context;
16
17typedef std::string ResourceType;
18
19/// Resource defines an abstract interface implemented by types that are
20/// offered, requested, and transferred between simulation agents. Resources
21/// represent the lifeblood of a simulation.
22class Resource {
23 friend class SimInit;
24 friend class ::SimInitTest;
25
26 public:
27 typedef boost::shared_ptr<Resource> Ptr;
28
30 : state_id_(nextstate_id_++), unit_value_(0.0), obj_id_(nextobj_id_++) {}
31
32 virtual ~Resource() {}
33
34 /// Returns the unique id corresponding to this resource object. Can be used
35 /// to track and/or associate other information with this resource object.
36 /// You should NOT track resources by pointer.
37 const int obj_id() const { return obj_id_; }
38
39 /// Returns the unit value of this resource.
40 double UnitValue() const { return unit_value_; }
41
42 /// Sets the unit value of this resource.
43 void SetUnitValue(double unit_value) { unit_value_ = unit_value; }
44
45 /// Returns the unique id corresponding to this resource and its current
46 /// state. All resource id's are unique - even across different resource
47 /// types/implementations. Runtime tracking of resources should generally
48 /// use the obj_id rather than this.
49 const int state_id() const { return state_id_; }
50
51 /// Assigns a new, unique internal id to this resource and its state. This
52 /// should be called by resource implementations whenever their state changes.
53 /// A call to BumpStateId is not necessarily accompanied by a change to the
54 /// state id. This should NEVER be called by agents.
55 void BumpStateId();
56
57 /// Returns an id representing the specific resource implementation's internal
58 /// state that is not accessible via the Resource class public interface. Any
59 /// change to the qual_id should always be accompanied by a call to
60 /// BumpStateId.
61 virtual int qual_id() const = 0;
62
63 /// A unique type/name for the concrete resource implementation.
64 virtual const ResourceType type() const = 0;
65
66 /// Returns an untracked (not part of the simulation) copy of the resource.
67 /// A cloned resource should never record anything in the output database.
68 virtual Ptr Clone() const = 0;
69
70 /// Records the resource's state to the output database. This method
71 /// should generally NOT record data accessible via the Resource class
72 /// public methods (e.g. qual_id, units, type, quantity).
73 /// @param ctx the simulation context used to record the data.
74 virtual void Record(Context* ctx) const = 0;
75
76 /// Returns the units this resource is based in (e.g. "kg").
77 virtual std::string units() const = 0;
78
79 /// Returns the quantity of this resource with dimensions as specified by
80 /// the return value of units().
81 virtual double quantity() const = 0;
82
83 /// Splits the resource and returns the extracted portion as a new resource
84 /// object. Allows for things like ResBuf and Traders to split
85 /// offers/requests of arbitrary resource implementation type.
86 ///
87 /// @return a new resource object with same state id and quantity == quantity
88 virtual Ptr ExtractRes(double quantity) = 0;
89
90 /// To enable the Decay method to be called on any child resource, define
91 /// a null op Decay method here.
92 /// @param curr_time the current time for the decay oepration
93 virtual void Decay(int curr_time) {
94 throw Error("cannot decay resource type " + this->type());
95 };
96
97 /// To enable the Absorb method to be called on any child resource, define
98 /// a null op Absorb method here.
99 /// @param res pointer to a resource to be absorbed by this resource
100 virtual void Absorb(Ptr res) {
101 throw Error("cannot absorb resource type " + this->type());
102 };
103
104 /// Returns the package id.
105 virtual std::string package_name() { return Package::unpackaged_name(); };
106
107 virtual Ptr PackageExtract(double qty, std::string new_package_name) = 0;
108
109 /// Changes the product's package id
110 virtual void ChangePackage(
111 std::string new_package_name = Package::unpackaged_name()) {};
112
113 /// Repackages a single resource into a package. If some quantity of the
114 /// resource cannot be packaged using the given packaging strategy and
115 /// restrictions, the remainder is left in the resource object.
116 template <class T> std::vector<typename T::Ptr> Package(Package::Ptr pkg);
117
118 protected:
119 constexpr static double kUnsetUnitValue =
120 std::numeric_limits<double>::quiet_NaN();
121
122 private:
123 double unit_value_;
124 static int nextstate_id_;
125 static int nextobj_id_;
126 int state_id_;
127 // Setting the state id should only be done when extracting one resource
128 void state_id(int st_id) { state_id_ = st_id; }
129
130 int obj_id_;
131};
132
133/// Casts a vector of Resources into a vector of a specific resource type T.
134template <class T>
135std::vector<typename T::Ptr> ResCast(std::vector<Resource::Ptr> rs) {
136 std::vector<typename T::Ptr> casted;
137 for (int i = 0; i < rs.size(); ++i) {
138 casted.push_back(boost::dynamic_pointer_cast<T>(rs[i]));
139 }
140 return casted;
141}
142
143/// Casts a Resource::Ptr into a pointer of a specific resource type T.
144template <class T> typename T::Ptr ResCast(Resource::Ptr r) {
145 return boost::dynamic_pointer_cast<T>(r);
146}
147
148template <class T>
149std::vector<typename T::Ptr> Resource::Package(Package::Ptr pkg) {
150 std::vector<typename T::Ptr> ts_pkgd;
151 typename T::Ptr t_pkgd;
152
153 std::vector<double> packages = pkg->GetFillMass(quantity());
154 if (packages.size() == 0) {
155 return ts_pkgd;
156 }
157
158 for (int i = 0; i < packages.size(); ++i) {
159 double pkg_fill = packages[i];
160 t_pkgd =
161 boost::dynamic_pointer_cast<T>(PackageExtract(pkg_fill, pkg->name()));
162 ts_pkgd.push_back(t_pkgd);
163 }
164
165 return ts_pkgd;
166}
167
168} // namespace cyclus
169
170#endif // CYCLUS_SRC_RESOURCE_H_
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:146
A generic mechanism to manually manage exceptions.
Definition error.h:12
boost::shared_ptr< Package > Ptr
Definition package.h:21
static std::string unpackaged_name()
Definition package.h:68
virtual ~Resource()
Definition resource.h:32
double UnitValue() const
Returns the unit value of this resource.
Definition resource.h:40
virtual Ptr PackageExtract(double qty, std::string new_package_name)=0
const int state_id() const
Returns the unique id corresponding to this resource and its current state.
Definition resource.h:49
virtual Ptr ExtractRes(double quantity)=0
Splits the resource and returns the extracted portion as a new resource object.
virtual const ResourceType type() const =0
A unique type/name for the concrete resource implementation.
virtual Ptr Clone() const =0
Returns an untracked (not part of the simulation) copy of the resource.
static constexpr double kUnsetUnitValue
Definition resource.h:119
virtual std::string units() const =0
Returns the units this resource is based in (e.g. "kg").
boost::shared_ptr< Resource > Ptr
Definition resource.h:27
virtual void Absorb(Ptr res)
To enable the Absorb method to be called on any child resource, define a null op Absorb method here.
Definition resource.h:100
virtual void Record(Context *ctx) const =0
Records the resource's state to the output database.
void SetUnitValue(double unit_value)
Sets the unit value of this resource.
Definition resource.h:43
friend class SimInit
Definition resource.h:23
virtual double quantity() const =0
Returns the quantity of this resource with dimensions as specified by the return value of units().
virtual void ChangePackage(std::string new_package_name=Package::unpackaged_name())
Changes the product's package id.
Definition resource.h:110
const int obj_id() const
Returns the unique id corresponding to this resource object.
Definition resource.h:37
virtual std::string package_name()
Returns the package id.
Definition resource.h:105
void BumpStateId()
Assigns a new, unique internal id to this resource and its state.
Definition resource.cc:8
virtual int qual_id() const =0
Returns an id representing the specific resource implementation's internal state that is not accessib...
virtual void Decay(int curr_time)
To enable the Decay method to be called on any child resource, define a null op Decay method here.
Definition resource.h:93
std::vector< typename T::Ptr > Package(Package::Ptr pkg)
Repackages a single resource into a package.
Definition resource.h:149
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
std::vector< typename T::Ptr > ResCast(std::vector< Resource::Ptr > rs)
Casts a vector of Resources into a vector of a specific resource type T.
Definition resource.h:135
std::string ResourceType
Definition resource.h:17