CYCLUS
Public Member Functions | Public Attributes | List of all members
cyclus::MockSim Class Reference

Detailed Description

MockSim is a helper for running full simulations entirely in-code to test archetypes/agents without having to deal with input files, output database files, and other pieces of the full Cyclus stack.

This is especially convenient for writing unit-like tests (e.g. using gtest) for your archetype's in-simulation behavior. Initialize the MockSim indicating the archetype you want to test and the simulation duration. Then add any number of sources and/or sinks to transact with your agent. They can have specific recipes (or not) and their deployment and lifetime (before decommissioning) can be specified too. Here is an example using the agents:Source archetype in Cyclus as the tested agent:

m[922350000] = .05;
m[922380000] = .95;
std::string config =
"<commod>enriched_u</commod>"
"<recipe_name>fresh_fuel</recipe_name>"
"<capacity>10</capacity>";
int dur = 10;
cyclus::MockSim sim(cyclus::AgentSpec(":agents:Source"), config, dur);
sim.AddSink("enriched_u").Finalize();
sim.AddRecipe("fresh_fuel", fresh);
int src_id = sim.Run(); // capture the agent ID of the facility being tested

Querying the results can be accomplished by getting a reference to the in-memory database generated. Not all data that is present in normal full-stack simulations is available. However, most of the key core tables are fully available. Namely, the Transactions, Composition, Resources, ResCreators, AgentEntry, and AgentExit tables are available. Any custom-tables created by the tested archetype will also be available. Here is a sample query and test you might write if using the googletest framework:

// return all transactions where our source facility is the sender
std::vector<cyclus::Cond> conds;
conds.push_back("SenderId", "==", src_id);
cyclus::QueryResult qr = sim.db().Query("Transactions", &conds);
int n_trans = qr.rows.size();
EXPECT_EQ(10, n_trans) << "expected 10 transactions, got " << n_trans;
// reconstruct the material object for the first transaction
int res_id = qr.GetVal<int>("ResourceId", 0);
cyclus::Material::Ptr m = sim.GetMaterial(res_id);
EXPECT_DOUBLE_EQ(10, m->quantity());
// confirm composition is as expected
EXPECT_DOUBLE_EQ(0.5, mq.mass(922350000));
EXPECT_DOUBLE_EQ(9.5, mq.mass(922380000));

Definition at line 132 of file mock_sim.h.

#include <mock_sim.h>

Public Member Functions

void AddRecipe (std::string name, Composition::Ptr c)
 
MockAgent AddSink (std::string commod)
 
MockAgent AddSource (std::string commod)
 
Contextcontext ()
 
SqliteBackdb ()
 
void DummyProto (std::string name)
 
Material::Ptr GetMaterial (int resid)
 
Product::Ptr GetProduct (int resid)
 
 MockSim (int duration)
 
 MockSim (AgentSpec spec, std::string config, int duration)
 
 MockSim (AgentSpec spec, std::string config, int duration, int lifetime)
 
int Run ()
 
 ~MockSim ()
 

Public Attributes

Agentagent
 

Constructor & Destructor Documentation

cyclus::MockSim::MockSim ( int  duration)

Creates and initializes a new empty mock simulation environment where duration is the length of the simulation in time steps.

An agent must be specified before running an empty-constructed MockSim (i.e. manually construct an agent using MockSim::context() and setting MockSim::agent.

Definition at line 114 of file mock_sim.cc.

cyclus::MockSim::MockSim ( AgentSpec  spec,
std::string  config,
int  duration 
)

Creates and initializes a new mock simulation environment to test the archetype identified by spec.

config should contain the archetype-specific xml snippet excluding the wrapping "<config>" and "<[AgentName]>" tags. duration is the length of the simulation in time steps.

Definition at line 123 of file mock_sim.cc.

cyclus::MockSim::MockSim ( AgentSpec  spec,
std::string  config,
int  duration,
int  lifetime 
)

Creates and initializes a new mock simulation environment to test the archetype identified by spec.

config should contain the archetype-specific xml snippet excluding the wrapping "<config>" and "<[AgentName]>" tags. duration is the length of the simulation in time steps. 'lifetime' is the lifetime of the agent being tested.

Definition at line 142 of file mock_sim.cc.

cyclus::MockSim::~MockSim ( )

Definition at line 178 of file mock_sim.cc.

Member Function Documentation

void cyclus::MockSim::AddRecipe ( std::string  name,
Composition::Ptr  c 
)

AddRecipe adds a recipe to the mock simulation environment (i.e.

to the simulation context). Any recipes that your archetype expects to find in the simulation context must be added this way; if the xml configuration snippet for the archetype being tested contains a recipe name, add it with this function.

Definition at line 196 of file mock_sim.cc.

MockAgent cyclus::MockSim::AddSink ( std::string  commod)

AddSink adds a sink facility that can request+receive material from the archetype being tested.

commod specifies the commodity the sink will request on. AddSink can be called multiple times to generate many sinks for the simulation. The returned MockAgent object has several functions that can be called to configure the sink's behavior further. Don't forget to call the MockAgent object's "Finalize" function when you are done configuring it. MockAgent's functions support chaining:

cyclus::MockSim sim(...);
sim.AddSink("spent_mox")
.start(7).lifetime(3).recipe("spent_fresh_mox")
.Finalize();

Definition at line 190 of file mock_sim.cc.

MockAgent cyclus::MockSim::AddSource ( std::string  commod)

AddSource adds a source facility that can offer/provide material to the archetype being tested.

commod specifies the commodity the source will offer on. AddSource can be called multiple times to generate many sources for the simulation. The returned MockAgent object has several functions that can be called to configure the source's behavior further. Don't forget to call the MockAgent object's "Finalize" function when you are done configuring it. MockAgent's functions support chaining:

cyclus::MockSim sim(...);
sim.AddSource("fresh_mox")
.start(7).lifetime(3).recipe("mox_fuel")
.Finalize();

Definition at line 184 of file mock_sim.cc.

Context* cyclus::MockSim::context ( )
inline

Returns the context for the mock simulation environment.

Definition at line 223 of file mock_sim.h.

SqliteBack & cyclus::MockSim::db ( )

Returns the underlying in-memory database containing results for the simulation.

Run must be called before the database will contain anything.

Definition at line 216 of file mock_sim.cc.

void cyclus::MockSim::DummyProto ( std::string  name)

Adds a dummy prototype to the simulation that can be used by institutions and other agents for deployment/decommission testing.

Definition at line 164 of file mock_sim.cc.

Material::Ptr cyclus::MockSim::GetMaterial ( int  resid)

Reconstructs a material object from the simulation results database with the given resource state id.

Definition at line 208 of file mock_sim.cc.

Product::Ptr cyclus::MockSim::GetProduct ( int  resid)

Reconstructs a product object from the simulation results database with the given resource state id.

Definition at line 212 of file mock_sim.cc.

int cyclus::MockSim::Run ( )

Runs the simulation.

This can only be called once. After the simulation has been run, this MockSim object CANNOT be reused to run other simulations. Run returns the agent ID for the agent being tested for use in queries.

Definition at line 200 of file mock_sim.cc.

Member Data Documentation

Agent* cyclus::MockSim::agent

the agent being tested by the mock simulation environment.

Definition at line 226 of file mock_sim.h.


The documentation for this class was generated from the following files: