From a0dfc661fb95d7e40812203e639b398805f17813 Mon Sep 17 00:00:00 2001 From: Mouginot Baptiste Date: Fri, 18 Nov 2016 15:40:30 -0600 Subject: [PATCH] fix sink default pref (#442) * fix sink default pref * change default pref from 1 to cyclus::kDefaultPref * this should fix the unit test segfault * add test checking default prefs * add pref tests --- src/sink.cc | 22 ++++++++++++++++--- src/sink.h | 14 ++++++++++++ src/sink_tests.cc | 55 ++++++++++++++++++++++++++++++++++++++++++++++- src/storage.cc | 2 +- 4 files changed, 88 insertions(+), 5 deletions(-) diff --git a/src/sink.cc b/src/sink.cc index 0d4a18bdb4..5703427a11 100644 --- a/src/sink.cc +++ b/src/sink.cc @@ -36,6 +36,23 @@ Sink::~Sink() {} #pragma cyclus def initfromcopy cycamore::Sink +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void Sink::EnterNotify() { + cyclus::Facility::EnterNotify(); + + if (in_commod_prefs.size() == 0) { + for (int i = 0; i < in_commods.size(); ++i) { + in_commod_prefs.push_back(cyclus::kDefaultPref); + } + } else if (in_commod_prefs.size() != in_commods.size()) { + std::stringstream ss; + ss << "in_commod_prefs has " << in_commod_prefs.size() + << " values, expected " << in_commods.size(); + throw cyclus::ValueError(ss.str()); + } + +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - std::string Sink::str() { using std::string; @@ -77,10 +94,9 @@ Sink::GetMatlRequests() { } if (amt > cyclus::eps()) { - std::vector::const_iterator it; std::vector*> mutuals; - for (it = in_commods.begin(); it != in_commods.end(); ++it) { - mutuals.push_back(port->AddRequest(mat, this, *it)); + for (int i = 0; i < in_commods.size(); i++) { + mutuals.push_back(port->AddRequest(mat, this, in_commods[i], in_commod_prefs[i])); } port->AddMutualReqs(mutuals); ports.insert(port); diff --git a/src/sink.h b/src/sink.h index a88335b307..2446b8536a 100644 --- a/src/sink.h +++ b/src/sink.h @@ -40,6 +40,8 @@ class Sink : public cyclus::Facility { virtual std::string str(); + virtual void EnterNotify(); + virtual void Tick(); virtual void Tock(); @@ -98,6 +100,10 @@ class Sink : public cyclus::Facility { inline const std::vector& input_commodities() const { return in_commods; } + /// @return the input commodities preferences + inline const std::vector& + input_commodity_preferences() const { return in_commod_prefs; } + private: /// all facilities must have at least one input commodity #pragma cyclus var {"tooltip": "input commodities", \ @@ -106,6 +112,14 @@ class Sink : public cyclus::Facility { "uitype": ["oneormore", "incommodity"]} std::vector in_commods; + #pragma cyclus var {"default": [],\ + "doc":"preferences for each of the given commodities, in the same order."\ + "Defauts to 1 if unspecified",\ + "uilabel":"In Commody Preferences", \ + "range": [None, [1e-299, 1e299]], \ + "uitype":["oneormore", "range"]} + std::vector in_commod_prefs; + #pragma cyclus var {"default": "", \ "tooltip": "requested composition", \ "doc": "name of recipe to use for material requests, " \ diff --git a/src/sink_tests.cc b/src/sink_tests.cc index 1926840c14..789ea9c2c6 100644 --- a/src/sink_tests.cc +++ b/src/sink_tests.cc @@ -50,6 +50,11 @@ TEST_F(SinkTest, InitialState) { std::string arr[] = {commod1_, commod2_}; std::vector vexp (arr, arr + sizeof(arr) / sizeof(arr[0]) ); EXPECT_EQ(vexp, src_facility->input_commodities()); + + src_facility->EnterNotify(); + double pref[] = {cyclus::kDefaultPref, cyclus::kDefaultPref}; + std::vector vpref (pref, pref + sizeof(pref) / sizeof(pref[0]) ); + EXPECT_EQ(vpref, src_facility->input_commodity_preferences()); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -113,6 +118,7 @@ TEST_F(SinkTest, Requests) { std::string arr[] = {commod1_, commod2_}; std::vector commods (arr, arr + sizeof(arr) / sizeof(arr[0]) ); + src_facility->EnterNotify(); std::set::Ptr> ports = src_facility->GetMatlRequests(); @@ -197,7 +203,8 @@ TEST_F(SinkTest, InRecipe){ // create a sink facility to interact with the DRE cycamore::Sink* snk = new cycamore::Sink(&ctx); snk->AddCommodity("some_u"); - + snk->EnterNotify(); + std::set::Ptr> ports = snk->GetMatlRequests(); ASSERT_EQ(ports.size(), 1); @@ -211,6 +218,52 @@ TEST_F(SinkTest, InRecipe){ EXPECT_EQ(req->commodity(),"some_u"); } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +TEST_F(SinkTest, BidPrefs) { + using cyclus::QueryResult; + using cyclus::Cond; + + std::string config = + " " + " commods_1" + " commods_2" + " " + " " + " 10 " + " 1 " + " " + " 1" + " 1.0 "; + + int simdur = 1; + cyclus::MockSim sim(cyclus::AgentSpec + (":cycamore:Sink"), config, simdur); + + sim.AddSource("commods_1") + .capacity(1) + .Finalize(); + + sim.AddSource("commods_2") + .capacity(1) + .Finalize(); + + int id = sim.Run(); + + std::vector conds; + conds.push_back(Cond("Commodity", "==", std::string("commods_1"))); + QueryResult qr = sim.db().Query("Transactions", &conds); + + // should trade only with #1 since it has highier priority + EXPECT_EQ(1, qr.rows.size()); + + std::vector conds2; + conds2.push_back(Cond("Commodity", "==", std::string("commods_2"))); + QueryResult qr2 = sim.db().Query("Transactions", &conds2); + + // should trade only with #1 since it has highier priority + EXPECT_EQ(0, qr2.rows.size()); + +} // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - TEST_F(SinkTest, Print) { EXPECT_NO_THROW(std::string s = src_facility->str()); diff --git a/src/storage.cc b/src/storage.cc index 3353256772..b9fea088a4 100644 --- a/src/storage.cc +++ b/src/storage.cc @@ -57,7 +57,7 @@ void Storage::EnterNotify() { if (in_commod_prefs.size() == 0) { for (int i = 0; i < in_commods.size(); ++i) { - in_commod_prefs.push_back(1); + in_commod_prefs.push_back(cyclus::kDefaultPref); } } else if (in_commod_prefs.size() != in_commods.size()) { std::stringstream ss;