Skip to content

Commit

Permalink
added a context helper for a facility with many input and output reci…
Browse files Browse the repository at this point in the history
…pes and commodities
  • Loading branch information
gidden committed Dec 12, 2013
1 parent 22727fa commit 13cffc7
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ SET(CYCLUS_CORE_SRC ${CYCLUS_CORE_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/commodity.cc
${CMAKE_CURRENT_SOURCE_DIR}/commodity_producer.cc
${CMAKE_CURRENT_SOURCE_DIR}/commodity_producer_manager.cc
${CMAKE_CURRENT_SOURCE_DIR}/commodity_recipe_context.cc
${CMAKE_CURRENT_SOURCE_DIR}/comp_math.cc
${CMAKE_CURRENT_SOURCE_DIR}/composition.cc
${CMAKE_CURRENT_SOURCE_DIR}/context.cc
Expand Down Expand Up @@ -120,6 +121,7 @@ INSTALL(FILES
commodity.h
commodity_producer.h
commodity_producer_manager.h
commodity_recipe_context.h
comp_math.h
composition.h
context.h
Expand Down
44 changes: 44 additions & 0 deletions src/commodity_recipe_context.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <utility>

#include "commodity_recipe_context.h"

namespace cyclus {

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CommodityRecipeContext::AddInCommod(
std::string in_commod,
std::string in_recipe,
std::string out_commod,
std::string out_recipe) {

in_commods_.push_back(in_commod);
out_commods_.push_back(out_commod);
out_commod_map_.insert(std::make_pair(in_commod, out_commod));
in_recipes_.insert(std::make_pair(in_commod, in_recipe));
out_recipes_.insert(std::make_pair(in_recipe, out_recipe));
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CommodityRecipeContext::AddMat(std::string commod, Material::Ptr mat) {
mat_commod_map_.insert(std::make_pair(mat, commod));
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CommodityRecipeContext::RemoveMat(Material::Ptr mat) {
mat_commod_map_.erase(mat);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CommodityRecipeContext::UpdateMat(std::string commod, Material::Ptr mat) {
mat_commod_map_[mat] = commod;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CommodityRecipeContext::UpdateInRec(
std::string incommod,
std::string recipe) {
out_recipes_[recipe] = out_recipes_[in_recipes_[incommod]];
in_recipes_[incommod] = recipe;
}

} // namespace cyclus
79 changes: 79 additions & 0 deletions src/commodity_recipe_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef CYCLUS_COMMODITY_RECIPE_CONTEXT_H_
#define CYCLUS_COMMODITY_RECIPE_CONTEXT_H_

#include <map>
#include <string>
#include <vector>

#include "material.h"

namespace cyclus {

/// @class CommodityRecipeContext
///
/// @brief a CommodityRecipeContext contains relationships between commodities,
/// recipes and materials
class CommodityRecipeContext {
public:
/// @brief add an input commodity and its relations
void AddInCommod(
std::string in_commod,
std::string in_recipe,
std::string out_commod,
std::string out_recipe);

/// @brief add a material and its commodity affiliation
void AddMat(std::string commod, Material::Ptr mat);

/// @brief update a material and its commodity affiliation
void UpdateMat(std::string commod, Material::Ptr mat);

/// @brief removes a material from the context
void RemoveMat(Material::Ptr mat);

/// @brief update an input recipe and its commodity affiliation
void UpdateInRec(std::string in_commod, std::string recipe);

/// @return input commodities
inline const std::vector<std::string>& in_commods() const {
return in_commods_;
}

/// @return output commodities
inline const std::vector<std::string>& out_commods() const {
return out_commods_;
}

/// @return output commodity of an input commodity
inline std::string out_commod(std::string in_commod) {
return out_commod_map_[in_commod];
}

/// @return input recipe of an input commodity
inline std::string in_recipe(std::string in_commod) {
return in_recipes_[in_commod];
}

/// @return output recipe of an input recipe
inline std::string out_recipe(std::string in_recipe) {
return out_recipes_[in_recipe];
}

/// @return commodity of a material
/// @warning returns a blank string if material isn't found
inline std::string commod(Material::Ptr mat) {
return mat_commod_map_[mat];
}

private:
std::vector<std::string> in_commods_;
std::vector<std::string> out_commods_;
std::map<std::string, std::string> out_commod_map_;
std::map<std::string, std::string> in_recipes_;
std::map<std::string, std::string> out_recipes_;
std::map<Material::Ptr, std::string> mat_commod_map_;
};

} // namespace cyclus

#endif // CYCLUS_COMMODITY_RECIPE_CONTEXT_H_
1 change: 1 addition & 0 deletions src/cyclus.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "commodity.h"
#include "commodity_producer.h"
#include "commodity_producer_manager.h"
#include "commodity_recipe_context.h"
#include "comp_math.h"
#include "composition.h"
#include "context.h"
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ set ( CYCLUS_CORE_TEST_SOURCE ${CYCLUS_CORE_TEST_SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/commodity_producer_manager_tests.cc
${CMAKE_CURRENT_SOURCE_DIR}/commodity_producer_tests.cc
${CMAKE_CURRENT_SOURCE_DIR}/commodity_producer_tests.cc
${CMAKE_CURRENT_SOURCE_DIR}/commodity_recipe_context_tests.cc
${CMAKE_CURRENT_SOURCE_DIR}/commodity_test_helper.cc
${CMAKE_CURRENT_SOURCE_DIR}/comp_math_tests.cc
${CMAKE_CURRENT_SOURCE_DIR}/composition_tests.cc
Expand Down
37 changes: 37 additions & 0 deletions tests/commodity_recipe_context_tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <gtest/gtest.h>

#include "commodity_recipe_context.h"

namespace cyclus {

TEST(CommodRecCtx, All) {
std::string in_c = "in_c";
std::string out_c = "out_c";
std::string in_r = "in_r";
std::string out_r = "out_r";

std::string in_r2 = "in_r2";

Material::Ptr mat = Material::CreateBlank(42);

CommodityRecipeContext ctx;
ctx.AddInCommod(in_c, in_r, out_c, out_r);
EXPECT_EQ(ctx.out_commod(in_c), out_c);
EXPECT_EQ(ctx.in_recipe(in_c), in_r);
EXPECT_EQ(ctx.out_recipe(in_r), out_r);

ctx.UpdateInRec(in_c, in_r2);
EXPECT_EQ(ctx.in_recipe(in_c), in_r2);
EXPECT_EQ(ctx.out_recipe(in_r), out_r);
EXPECT_EQ(ctx.out_recipe(in_r2), out_r);

ctx.AddMat(in_c, mat);
EXPECT_EQ(ctx.commod(mat), in_c);
ctx.UpdateMat(out_c, mat);
EXPECT_EQ(ctx.commod(mat), out_c);
ctx.RemoveMat(mat);
EXPECT_EQ(ctx.commod(mat), "");

}

} // namespace cyclus

0 comments on commit 13cffc7

Please sign in to comment.