Skip to content

Commit

Permalink
Support file observer on/off toggling (#509)
Browse files Browse the repository at this point in the history
* #508 Support toggling file observer on or off.

* #508 Remove unused variable.

* #508 Remove superfluous value reference collections.

* #508 Ensure value writer is present.

* Fix typo

* Add panic message

* find_variables_of_type() is obsolete. Consolidate usage of find_variables().

* Add concurrency support to file observer.

* Review follow-up
  • Loading branch information
eidekrist authored Jan 13, 2020
1 parent a74e7a0 commit f467a9d
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 232 deletions.
7 changes: 0 additions & 7 deletions include/cse/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,6 @@ struct model_description
/// Getter for returning a variable description.
const variable_description find_variable(const model_description& description, const std::string& variable_name);

/// Getter for returning a variable description.
const variable_description find_variable(const model_description& description, variable_type type, value_reference reference);

/// Getter for returning all variable descriptions of the given datatype.
const std::vector<variable_description> find_variables_of_type(const model_description& description, variable_type type);


/// Possible outcomes of a subsimulator time step
enum class step_result
{
Expand Down
52 changes: 44 additions & 8 deletions include/cse/observer/file_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <boost/filesystem/path.hpp>
#include <boost/property_tree/ptree.hpp>

#include <atomic>
#include <memory>
#include <unordered_map>

Expand All @@ -19,15 +20,49 @@ namespace cse


/**
* An observer implementation, for saving observed variable values to file in the preferred format (csv or binary).
* An observer implementation, for saving observed variable values to file in csv format.
*
* Recording may be toggled on or off mid simulation. This functionality is thread safe.
*/
class file_observer : public observer
{
public:
/**
* Creates an observer which logs all variable values to file in csv format.
*
* \param logDir the directory where log files will be created.
*/
file_observer(const boost::filesystem::path& logDir);

/**
* Creates an observer which logs selected variable values to file in csv format.
*
* \param logDir the directory where log files will be created.
* \param configPath the path to an xml file containing the logging configuration.
*/
file_observer(const boost::filesystem::path& logDir, const boost::filesystem::path& configPath);

/**
* Returns whether the observer is currently recording values.
*
* This method can safely be called from different threads.
*/
bool is_recording();

/**
* Starts recording values. Throws an exception if the observer is already recording.
*
* This method can safely be called from different threads.
*/
void start_recording();

/**
* Stops recording values. Throws an exception if the observer is not recording.
*
* This method can safely be called from different threads.
*/
void stop_recording();

void simulator_added(simulator_index, observable*, time_point) override;

void simulator_removed(simulator_index, time_point) override;
Expand Down Expand Up @@ -56,22 +91,23 @@ class file_observer : public observer
~file_observer() override;

private:
void parse_config(const std::string& simulatorName);
struct simulator_logging_config
{
std::vector<variable_description> variables;
size_t decimationFactor;
};

simulator_logging_config parse_config(const std::string& simulatorName);

class slave_value_writer;
std::unordered_map<simulator_index, std::unique_ptr<slave_value_writer>> valueWriters_;
std::unordered_map<simulator_index, observable*> simulators_;
std::vector<variable_description> loggableRealVariables_;
std::vector<variable_description> loggableIntVariables_;
std::vector<variable_description> loggableBoolVariables_;
std::vector<variable_description> loggableStringVariables_;
boost::property_tree::ptree ptree_;
boost::filesystem::path configPath_;
boost::filesystem::path logDir_;
boost::filesystem::path logPath_;
bool logFromConfig_ = false;
size_t decimationFactor_;
size_t defaultDecimationFactor_ = 1;
std::atomic<bool> recording_ = true;
};


Expand Down
12 changes: 0 additions & 12 deletions src/cpp/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,4 @@ const variable_description find_variable(const model_description& description, c
throw std::invalid_argument("Can't find variable descriptor with name " + variable_name + " for model with name " + description.name);
}

const std::vector<variable_description> find_variables_of_type(const model_description& description, variable_type type)
{
std::vector<variable_description> vars;

for (const auto& variable : description.variables) {
if (variable.type == type) {
vars.push_back(variable);
}
}
return vars;
}

} // namespace cse
Loading

0 comments on commit f467a9d

Please sign in to comment.