CYCLUS
product.cc
Go to the documentation of this file.
1 #include "product.h"
2 
3 #include "error.h"
4 #include "logger.h"
5 
6 namespace cyclus {
7 
8 const ResourceType Product::kType = "Product";
9 
10 std::map<std::string, int> Product::qualids_;
11 int Product::next_qualid_ = 1;
12 
13 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
14 Product::Ptr Product::Create(Agent* creator, double quantity,
15  std::string quality) {
16  if (qualids_.count(quality) == 0) {
17  qualids_[quality] = next_qualid_++;
18  creator->context()->NewDatum("Products")
19  ->AddVal("QualId", qualids_[quality])
20  ->AddVal("Quality", quality)
21  ->Record();
22  }
23 
24  // the next lines must come after qual id setting
25  Product::Ptr r(new Product(creator->context(), quantity, quality));
26  r->tracker_.Create(creator);
27  return r;
28 }
29 
30 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33  Product::Ptr r(new Product(NULL, quantity, quality));
34  r->tracker_.DontTrack();
35  return r;
36 }
37 
38 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40  Product* g = new Product(*this);
42  g->tracker_.DontTrack();
43  return c;
44 }
45 
46 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48  if (other->quality() != quality()) {
49  throw ValueError("incompatible resource types.");
50  }
51  quantity_ += other->quantity();
52  other->quantity_ = 0;
53 
54  tracker_.Absorb(&other->tracker_);
55 }
56 
57 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
59  if (quantity > quantity_) {
60  throw ValueError("Attempted to extract more quantity than exists.");
61  }
62 
63  quantity_ -= quantity;
64 
65  Product::Ptr other(new Product(ctx_, quantity, quality_));
66  tracker_.Extract(&other->tracker_);
67  return other;
68 }
69 
70 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72  return boost::static_pointer_cast<Resource>(Extract(qty));
73 }
74 
75 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
76 Product::Product(Context* ctx, double quantity, std::string quality)
77  : quality_(quality),
78  quantity_(quantity),
79  tracker_(ctx, this),
80  ctx_(ctx) {}
81 
82 } // namespace cyclus
void Absorb(ResTracker *absorbed)
Should be called when a resource is combined with another.
Definition: res_tracker.cc:57
For values that are too big, too small, etc.
Definition: error.h:41
std::string ResourceType
Definition: resource.h:12
boost::shared_ptr< Product > Ptr
Definition: product.h:24
A Product is a general type of resource in the Cyclus simulation, and is a catch-all for non-standard...
Definition: product.h:18
virtual Resource::Ptr ExtractRes(double quantity)
Splits the resource and returns the extracted portion as a new resource object.
Definition: product.cc:71
Resource defines an abstract interface implemented by types that are offered, requested, and transferred between simulation agents.
Definition: resource.h:19
void Absorb(Product::Ptr other)
Absorbs the contents of the given &#39;other&#39; resource into this resource.
Definition: product.cc:47
static Ptr Create(Agent *creator, double quantity, std::string quality)
Creates a new product that is "live" and tracked.
Definition: product.cc:14
Context * context() const
Returns this agent&#39;s simulation context.
Definition: agent.h:369
void DontTrack()
Prevent a resource&#39;s heritage from being tracked and recorded.
Definition: res_tracker.cc:14
const double g
Definition: material.h:18
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
virtual const std::string & quality() const
Returns the quality of this resource (e.g. bananas, human labor, water, etc.).
Definition: product.h:58
void Extract(ResTracker *removed)
Should be called when a resource has some quantity removed from it (e.g.
Definition: res_tracker.cc:42
Code providing rudimentary logging capability for the Cyclus core.
A simulation context provides access to necessary simulation-global functions and state...
Definition: context.h:130
boost::shared_ptr< Resource > Ptr
Definition: resource.h:24
The abstract base class used by all types of agents that live and interact in a simulation.
Definition: agent.h:51
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
static const ResourceType kType
Definition: product.h:25
virtual Resource::Ptr Clone() const
Returns an untracked (not part of the simulation) copy of the resource.
Definition: product.cc:39
virtual double quantity() const
Returns the quantity of this resource with dimensions as specified by the return value of units()...
Definition: product.h:53
void Record()
Record this datum to its Recorder.
Definition: datum.cc:35
static Ptr CreateUntracked(double quantity, std::string quality)
Creates a new product that does not actually exist as part of the simulation and is untracked...
Definition: product.cc:31
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition: context.cc:239
Product::Ptr Extract(double quantity)
Extracts the specified mass from this resource and returns it as a new product object with the same q...
Definition: product.cc:58