Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/212 provide overview of modified variables #296

Merged
merged 34 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1dccc2e
#212 Map of modified variables now exposed through execution
Jun 24, 2019
3df90fb
Merge branch 'master' into feature/212-provide-overview-of-modified-v…
Jun 24, 2019
4e4d8cf
#212 Using variable_id instead of variable_index, finished test
Jun 25, 2019
3bd16cd
Merge remote-tracking branch 'origin/feature/212-provide-overview-of-…
Jun 25, 2019
b9e0bd3
#212 Added to C API
Jun 25, 2019
fa848b4
#212 Remove unused function
Jun 26, 2019
1d3d365
#212 Updated C API for modified vars
Jul 2, 2019
9d76d9a
#212 Fixed duplication issue in list of modified variables
Jul 3, 2019
3d51279
#212 Use unordered_set instead to avoid registering multiple modifier…
Jul 3, 2019
07ed0a9
Merge branch 'master' into feature/212-provide-overview-of-modified-v…
Jul 4, 2019
6a074dd
#212 Add exception import
Jul 4, 2019
6b3631b
#212 Remove logging
Jul 8, 2019
f86c838
#212 GCC 7 adaptation
Jul 8, 2019
57b2761
#212 Review follow-up changes
Jul 9, 2019
71734d7
#212 Review follow-up changes
Jul 9, 2019
6c2b5f4
#212 More review follow-up changes
Jul 12, 2019
5c9e730
#212 Should not count twice
Jul 12, 2019
ae8e577
Update include/cse/execution.hpp
restenb Jul 12, 2019
782f1ff
Update include/cse.h
restenb Jul 12, 2019
c32f3a1
Update include/cse.h
restenb Jul 12, 2019
fbe6dbc
Update include/cse.h
restenb Jul 12, 2019
f73f772
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
468caf0
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
2d62a07
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
ce12052
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
4c14cae
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
e8a5929
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
60cca9d
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
97a6155
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
7c2b96f
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
417b898
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
d5f28d4
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
a8027de
#212 Fixed const references all the way down
Jul 12, 2019
a666017
Merge remote-tracking branch 'origin/feature/212-provide-overview-of-…
Jul 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions include/cse.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ int cse_slave_get_num_variables(cse_execution* execution, cse_slave_index slave)
*/
int cse_slave_get_variables(cse_execution* execution, cse_slave_index slave, cse_variable_description variables[], size_t numVariables);

/// Returns the number of variables in the execution that currently has an active modifier (all slaves).
int cse_get_num_modified_variables(cse_execution* execution);

/// A struct containing information about a slave which has been added to an execution.
typedef struct
{
Expand All @@ -374,6 +377,17 @@ typedef struct
cse_slave_index index;
} cse_slave_info;

/// A struct containing variable information.
typedef struct
{
/// The index of the slave containing the variable.
cse_slave_index slave_index;
/// The type of the variable.
cse_variable_type type;
/// The index of the variable.
cse_variable_index variable_index;
} cse_variable_id;


/// Returns the number of slaves which have been added to an execution.
size_t cse_execution_get_num_slaves(cse_execution* execution);
Expand Down Expand Up @@ -912,6 +926,21 @@ int cse_scenario_is_running(cse_manipulator* manipulator);
/// Aborts the execution of a running scenario
int cse_scenario_abort(cse_manipulator* manipulator);

/**
* Retrieves a list of the currently modified variables in the simulation.
*
* \param [in] execution
* The execution.
* \param [out] ids
* A list of cse_variable_id structs to contain the variable information.
* \param [in] numVariables
* The length of the `ids` array.
*
restenb marked this conversation as resolved.
Show resolved Hide resolved
* \returns
* 0 on success and -1 on error.
*/
int cse_get_modified_variables(cse_execution* execution, cse_variable_id ids[], size_t numVariables);

#ifdef __cplusplus
} // extern(C)
#endif
Expand Down
35 changes: 35 additions & 0 deletions include/cse/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,41 @@ class simulator : public observable
std::optional<time_point> stopTime,
std::optional<double> relativeTolerance) = 0;

/**
*
* Returns all variable_indexes of real type with an active modifier.
*
* @return modifiedRealIndexes
* Unordered set of all variable_indexes that currently have an active modifier.
*/
virtual const std::unordered_set<variable_index>& get_modified_real_indexes() const = 0;

/**
*
* Returns all variable_indexes of integer type with an active modifier.
*
* @return modifiedIntegerIndexes
* Unordered set of all variable_indexes that currently have an active modifier.
*/
virtual const std::unordered_set<variable_index>& get_modified_integer_indexes() const = 0;

/**
*
* Returns all variable_indexes of boolean type with an active modifier.
*
* @return modifiedBooleanIndexes
* Unordered set of all variable_indexes that currently have an active modifier.
*/
virtual const std::unordered_set<variable_index>& get_modified_boolean_indexes() const = 0;

/**
*
* Returns all variable_indexes of string type with an active modifier.
*
* @return modifiedStringIndexes
* Unordered set of all variable_indexes that currently have an active modifier.
*/
virtual const std::unordered_set<variable_index>& get_modified_string_indexes() const = 0;

/**
* Updates the simulator with new input values and makes it calculate
Expand Down
3 changes: 3 additions & 0 deletions include/cse/execution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ class execution
/// Returns the current real time factor target
double get_real_time_factor_target();

/// Returns a map of currently modified variables
std::vector<variable_id> get_modified_variables();
restenb marked this conversation as resolved.
Show resolved Hide resolved

/// Set initial value for a variable of type real. Must be called before simulation is started.
void set_real_initial_value(simulator_index sim, variable_index var, double value);

Expand Down
30 changes: 29 additions & 1 deletion src/c/cse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ int cse_slave_get_num_variables(cse_execution* execution, cse_slave_index slave)
return -1;
}

int cse_get_num_modified_variables(cse_execution* execution)
{
return static_cast<int>(execution->cpp_execution->get_modified_variables().size());
}

cse_variable_variability to_variable_variability(const cse::variable_variability& vv)
{
switch (vv) {
Expand Down Expand Up @@ -316,7 +321,6 @@ int cse_slave_get_variables(cse_execution* execution, cse_slave_index slave, cse
}
}


struct cse_slave_s
{
std::string address;
Expand Down Expand Up @@ -825,6 +829,7 @@ int cse_observer_start_observing(cse_observer* observer, cse_slave_index slave,
return failure;
}
}

int cse_observer_stop_observing(cse_observer* observer, cse_slave_index slave, cse_variable_type type, cse_variable_index index)
{
try {
Expand Down Expand Up @@ -1056,3 +1061,26 @@ int cse_scenario_abort(cse_manipulator* manipulator)
return failure;
}
}

int cse_get_modified_variables(cse_execution* execution, cse_variable_id ids[], size_t numVariables)
{
try {
auto modified_vars = execution->cpp_execution->get_modified_variables();
size_t counter = 0;

if (!modified_vars.empty()) {
for (; counter < std::min(numVariables, modified_vars.size()); counter++) {
ids[counter].slave_index = modified_vars[counter].simulator;
ids[counter].type = to_c_variable_type(modified_vars[counter].type);
ids[counter].variable_index = modified_vars[counter].index;
}
}

return static_cast<int>(counter);
} catch (...) {
execution->state = CSE_EXECUTION_ERROR;
execution->error_code = CSE_ERRC_UNSPECIFIED;
handle_current_exception();
return failure;
}
}
5 changes: 5 additions & 0 deletions src/cpp/cse/slave_simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class slave_simulator : public simulator
variable_index index,
std::function<std::string(std::string_view)> modifier) override;

std::unordered_set<variable_index>& get_modified_real_indexes() const override;
std::unordered_set<variable_index>& get_modified_integer_indexes() const override;
std::unordered_set<variable_index>& get_modified_boolean_indexes() const override;
std::unordered_set<variable_index>& get_modified_string_indexes() const override;

boost::fibers::future<void> setup(
time_point startTime,
std::optional<time_point> stopTime,
Expand Down
45 changes: 44 additions & 1 deletion src/cpp/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include "cse/algorithm.hpp"
#include "cse/slave_simulator.hpp"
#include "cse/timer.hpp"
#include <cse/exception.hpp>

#include <boost/functional/hash.hpp>

#include <cse/exception.hpp>
#include <sstream>
#include <unordered_map>
#include <utility>
Expand Down Expand Up @@ -195,6 +195,44 @@ class execution::impl
return timer_.get_real_time_factor_target();
}

std::vector<variable_id> get_modified_variables()
restenb marked this conversation as resolved.
Show resolved Hide resolved
{
std::vector<variable_id> modifiedVariables;

auto index = 0;
for (const auto& sim : simulators_) {

const auto& realIndexes = sim->get_modified_real_indexes();
const auto& intIndexes = sim->get_modified_integer_indexes();
const auto& boolIndexes = sim->get_modified_boolean_indexes();
const auto& stringIndexes = sim->get_modified_string_indexes();

for (const auto& varIndex : realIndexes) {
variable_id var = {index, variable_type::real, varIndex};
modifiedVariables.push_back(var);
}

for (const auto& varIndex : intIndexes) {
variable_id var = {index, variable_type::integer, varIndex};
modifiedVariables.push_back(var);
}

for (const auto& varIndex : boolIndexes) {
variable_id var = {index, variable_type::boolean, varIndex};
modifiedVariables.push_back(var);
}

for (const auto& varIndex : stringIndexes) {
variable_id var = {index, variable_type::string, varIndex};
modifiedVariables.push_back(var);
}

index++;
}

return modifiedVariables;
}

void set_real_initial_value(simulator_index sim, variable_index var, double value)
{
if (initialized_) {
Expand Down Expand Up @@ -359,6 +397,11 @@ double execution::get_real_time_factor_target() {
return pimpl_->get_real_time_factor_target();
}

std::vector<variable_id> execution::get_modified_variables()
{
return pimpl_->get_modified_variables();
}

void execution::set_real_initial_value(simulator_index sim, variable_index var, double value)
{
pimpl_->set_real_initial_value(sim, var, value);
Expand Down
62 changes: 62 additions & 0 deletions src/cpp/slave_simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,55 +322,83 @@ class slave_simulator::impl
std::function<double(double)> modifier)
{
realSetCache_.set_modifier(index, modifier);
set_modified_index(modifiedRealIndexes_, index, modifier ? true : false);
}

void set_integer_input_modifier(
variable_index index,
std::function<int(int)> modifier)
{
integerSetCache_.set_modifier(index, modifier);
set_modified_index(modifiedIntegerIndexes_, index, modifier ? true : false);
}

void set_boolean_input_modifier(
variable_index index,
std::function<bool(bool)> modifier)
{
booleanSetCache_.set_modifier(index, modifier);
set_modified_index(modifiedBooleanIndexes_, index, modifier ? true : false);
}

void set_string_input_modifier(
variable_index index,
std::function<std::string(std::string_view)> modifier)
{
stringSetCache_.set_modifier(index, modifier);
set_modified_index(modifiedStringIndexes_, index, modifier ? true : false);
}

void set_real_output_modifier(
variable_index index,
std::function<double(double)> modifier)
{
realGetCache_.set_modifier(index, modifier);
set_modified_index(modifiedRealIndexes_, index, modifier ? true : false);
}

void set_integer_output_modifier(
variable_index index,
std::function<int(int)> modifier)
{
integerGetCache_.set_modifier(index, modifier);
set_modified_index(modifiedIntegerIndexes_, index, modifier ? true : false);
}

void set_boolean_output_modifier(
variable_index index,
std::function<bool(bool)> modifier)
{
booleanGetCache_.set_modifier(index, modifier);
set_modified_index(modifiedBooleanIndexes_, index, modifier ? true : false);
}

void set_string_output_modifier(
variable_index index,
std::function<std::string(std::string_view)> modifier)
{
stringGetCache_.set_modifier(index, modifier);
set_modified_index(modifiedStringIndexes_, index, modifier ? true : false);
}

std::unordered_set<variable_index>& get_modified_real_indexes()
{
return modifiedRealIndexes_;
}

std::unordered_set<variable_index>& get_modified_integer_indexes()
{
return modifiedIntegerIndexes_;
}

std::unordered_set<variable_index>& get_modified_boolean_indexes()
{
return modifiedBooleanIndexes_;
}

std::unordered_set<variable_index>& get_modified_string_indexes()
{
return modifiedStringIndexes_;
}

boost::fibers::future<void> setup(
Expand Down Expand Up @@ -465,6 +493,15 @@ class slave_simulator::impl
return *it;
}

void set_modified_index(std::unordered_set<variable_index>& modifiedIndexes, variable_index& index, bool modifier)
{
if (modifier) {
modifiedIndexes.insert(index);
} else {
modifiedIndexes.erase(index);
}
}

private:
std::shared_ptr<async_slave> slave_;
std::string name_;
Expand All @@ -479,6 +516,11 @@ class slave_simulator::impl
set_variable_cache<int> integerSetCache_;
set_variable_cache<bool> booleanSetCache_;
set_variable_cache<std::string> stringSetCache_;

std::unordered_set<variable_index> modifiedRealIndexes_;
std::unordered_set<variable_index> modifiedIntegerIndexes_;
std::unordered_set<variable_index> modifiedBooleanIndexes_;
std::unordered_set<variable_index> modifiedStringIndexes_;
};


Expand Down Expand Up @@ -624,6 +666,26 @@ void slave_simulator::set_string_output_modifier(
pimpl_->set_string_output_modifier(index, modifier);
}

std::unordered_set<variable_index>& slave_simulator::get_modified_real_indexes() const
{
return pimpl_->get_modified_real_indexes();
}

std::unordered_set<variable_index>& slave_simulator::get_modified_integer_indexes() const
{
return pimpl_->get_modified_integer_indexes();
}

std::unordered_set<variable_index>& slave_simulator::get_modified_boolean_indexes() const
{
return pimpl_->get_modified_boolean_indexes();
}

std::unordered_set<variable_index>& slave_simulator::get_modified_string_indexes() const
{
return pimpl_->get_modified_string_indexes();
}

boost::fibers::future<void> slave_simulator::setup(
time_point startTime,
std::optional<time_point> stopTime,
Expand Down
1 change: 1 addition & 0 deletions test/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(tests
"ssp_parser_test"
"time_series_observer_test"
"trend_buffer_test"
"monitor_modified_variables_test"
"scenario_manager_test"
"scenario_parser_test"
"synchronized_xy_series_test"
Expand Down
Loading