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
29 Resource() : state_id_(nextstate_id_++), obj_id_(nextobj_id_++) {}
30
31 virtual ~Resource() {}
32
33 /// Returns the unique id corresponding to this resource object. Can be used
34 /// to track and/or associate other information with this resource object.
35 /// You should NOT track resources by pointer.
36 const int obj_id() const { return obj_id_; }
37
38 /// Returns the unique id corresponding to this resource and its current
39 /// state. All resource id's are unique - even across different resource
40 /// types/implementations. Runtime tracking of resources should generally
41 /// use the obj_id rather than this.
42 const int state_id() const {
43 return state_id_;
44 }
45
46 /// Assigns a new, unique internal id to this resource and its state. This should be
47 /// called by resource implementations whenever their state changes. A call to
48 /// BumpStateId is not necessarily accompanied by a change to the state id.
49 /// This should NEVER be called by agents.
50 void BumpStateId();
51
52 /// Returns an id representing the specific resource implementation's internal
53 /// state that is not accessible via the Resource class public interface. Any
54 /// change to the qual_id should always be accompanied by a call to
55 /// BumpStateId.
56 virtual int qual_id() const = 0;
57
58 /// A unique type/name for the concrete resource implementation.
59 virtual const ResourceType type() const = 0;
60
61 /// Returns an untracked (not part of the simulation) copy of the resource.
62 /// A cloned resource should never record anything in the output database.
63 virtual Ptr Clone() const = 0;
64
65 /// Records the resource's state to the output database. This method
66 /// should generally NOT record data accessible via the Resource class
67 /// public methods (e.g. qual_id, units, type, quantity).
68 /// @param ctx the simulation context used to record the data.
69 virtual void Record(Context* ctx) const = 0;
70
71 /// Returns the units this resource is based in (e.g. "kg").
72 virtual std::string units() const = 0;
73
74 /// Returns the quantity of this resource with dimensions as specified by
75 /// the return value of units().
76 virtual double quantity() const = 0;
77
78 /// Splits the resource and returns the extracted portion as a new resource
79 /// object. Allows for things like ResBuf and Traders to split
80 /// offers/requests of arbitrary resource implementation type.
81 ///
82 /// @return a new resource object with same state id and quantity == quantity
83 virtual Ptr ExtractRes(double quantity) = 0;
84
85 /// To enable the Decay method to be called on any child resource, define
86 /// a null op Decay method here.
87 /// @param curr_time the current time for the decay oepration
88 virtual void Decay(int curr_time) { throw Error("cannot decay resource type " + this->type()); };
89
90 /// To enable the Absorb method to be called on any child resource, define
91 /// a null op Absorb method here.
92 /// @param res pointer to a resource to be absorbed by this resource
93 virtual void Absorb(Ptr res) { throw Error("cannot absorb resource type " + this->type()); };
94
95 /// Returns the package id.
96 virtual std::string package_name() { return Package::unpackaged_name(); };
97
98 virtual Ptr PackageExtract(double qty, std::string new_package_name) = 0;
99
100 /// Changes the product's package id
102
103 /// Repackages a single resource into a package. If some quantity of the
104 /// resource cannot be packaged using the given packaging strategy and
105 /// restrictions, the remainder is left in the resource object.
106 template <class T>
107 std::vector<typename T::Ptr> Package(Package::Ptr pkg);
108
109 private:
110 static int nextstate_id_;
111 static int nextobj_id_;
112 int state_id_;
113 // Setting the state id should only be done when extracting one resource
114 void state_id(int st_id) {
115 state_id_ = st_id;
116 }
117
118
119 int obj_id_;
120};
121
122/// Casts a vector of Resources into a vector of a specific resource type T.
123template <class T>
124std::vector<typename T::Ptr> ResCast(std::vector<Resource::Ptr> rs) {
125 std::vector<typename T::Ptr> casted;
126 for (int i = 0; i < rs.size(); ++i) {
127 casted.push_back(boost::dynamic_pointer_cast<T>(rs[i]));
128 }
129 return casted;
130}
131
132/// Casts a Resource::Ptr into a pointer of a specific resource type T.
133template <class T>
134typename T::Ptr ResCast(Resource::Ptr r) {
135 return boost::dynamic_pointer_cast<T>(r);
136}
137
138
139template <class T>
140std::vector<typename T::Ptr> Resource::Package(Package::Ptr pkg) {
141 std::vector<typename T::Ptr> ts_pkgd;
142 typename T::Ptr t_pkgd;
143
144 std::vector<double> packages = pkg->GetFillMass(quantity());
145 if (packages.size() == 0) {
146 return ts_pkgd;
147 }
148
149 for (int i = 0; i < packages.size(); ++i) {
150 double pkg_fill = packages[i];
151 t_pkgd = boost::dynamic_pointer_cast<T>(PackageExtract(pkg_fill, pkg->name()));
152 ts_pkgd.push_back(t_pkgd);
153 }
154
155 return ts_pkgd;
156}
157
158
159} // namespace cyclus
160
161#endif // CYCLUS_SRC_RESOURCE_H_
a holding class for information related to a TradeExecutor
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:145
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
Resource defines an abstract interface implemented by types that are offered, requested,...
Definition resource.h:22
virtual ~Resource()
Definition resource.h:31
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:42
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.
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:93
virtual void Record(Context *ctx) const =0
Records the resource's state to the output database.
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:101
const int obj_id() const
Returns the unique id corresponding to this resource object.
Definition resource.h:36
virtual std::string package_name()
Returns the package id.
Definition resource.h:96
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:88
std::vector< typename T::Ptr > Package(Package::Ptr pkg)
Repackages a single resource into a package.
Definition resource.h:140
Handles initialization of a simulation from the output database.
Definition sim_init.h:24
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:124
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
std::string ResourceType
Definition resource.h:17