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 13 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
27 changes: 27 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,19 @@ int cse_scenario_is_running(cse_manipulator* manipulator);
/// Aborts the execution of a running scenario
int cse_scenario_abort(cse_manipulator* manipulator);

/*
restenb marked this conversation as resolved.
Show resolved Hide resolved
* Retrieves a list of the currently modified variables in the simulation.
*
* \param [in] execution
* The execution.
* \param [in] ids
restenb marked this conversation as resolved.
Show resolved Hide resolved
* A list of cse_variable_id structs to contain the variable information.
*
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[]);

#ifdef __cplusplus
} // extern(C)
#endif
Expand Down
2 changes: 2 additions & 0 deletions include/cse/execution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ class execution
/// Returns the current real time factor target
double get_real_time_factor_target();

/// Returns a map of currently modified variables
std::unordered_map<simulator_index, std::vector<variable_id>> get_modified_variables();
/// 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
45 changes: 44 additions & 1 deletion src/c/cse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ int cse_slave_get_num_variables(cse_execution* execution, cse_slave_index slave)
return -1;
}

int cse_get_num_modified_variables(cse_execution* execution)
{
auto vars = execution->cpp_execution->get_modified_variables();

int counter = 0;
for (const auto& [index, variables] : vars) {
(void)index; // GCC 7
restenb marked this conversation as resolved.
Show resolved Hide resolved
counter += static_cast<int>(variables.size());
}

return counter;
}

cse_variable_variability to_variable_variability(const cse::variable_variability& vv)
{
switch (vv) {
Expand Down Expand Up @@ -316,7 +329,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 +837,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 +1069,33 @@ int cse_scenario_abort(cse_manipulator* manipulator)
return failure;
}
}

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

if (!modified_vars.empty()) {
for (const auto& [index, vars] : modified_vars) {
(void)index; // GCC 7
size_t counter = 0;
restenb marked this conversation as resolved.
Show resolved Hide resolved
if (!vars.empty()) {
for (const auto& var : vars) {
ids[counter].slave_index = var.simulator;
ids[counter].type = to_c_variable_type(var.type);
ids[counter].variable_index = var.index;
restenb marked this conversation as resolved.
Show resolved Hide resolved

++counter;
}
}
}
}

return success;
} 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();
std::unordered_set<variable_index> get_modified_integer_indexes();
std::unordered_set<variable_index> get_modified_boolean_indexes();
std::unordered_set<variable_index> get_modified_string_indexes();
restenb marked this conversation as resolved.
Show resolved Hide resolved

boost::fibers::future<void> setup(
time_point startTime,
std::optional<time_point> stopTime,
Expand Down
60 changes: 59 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,59 @@ class execution::impl
return timer_.get_real_time_factor_target();
}

std::unordered_map<simulator_index, std::vector<variable_id>> get_modified_variables()
{
std::unordered_map<simulator_index, std::vector<variable_id>> modifiedVariables;

auto index = 0;
for (const auto& simulator : simulators_) {
const auto sim = std::dynamic_pointer_cast<cse::slave_simulator>(simulator);

std::vector<variable_id> realIds;
std::vector<variable_id> intIds;
std::vector<variable_id> boolIds;
std::vector<variable_id> stringIds;
restenb marked this conversation as resolved.
Show resolved Hide resolved

auto realIndexes = sim->get_modified_real_indexes();
auto intIndexes = sim->get_modified_integer_indexes();
auto boolIndexes = sim->get_modified_boolean_indexes();
auto stringIndexes = sim->get_modified_string_indexes();
restenb marked this conversation as resolved.
Show resolved Hide resolved

for (const auto& varIndex : realIndexes) {
variable_id var = {index, variable_type::real, varIndex};
realIds.emplace_back(var);
restenb marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

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

std::vector<variable_id> indexes(realIds);
std::move(intIds.begin(), intIds.end(), std::back_inserter(indexes));
std::move(boolIds.begin(), boolIds.end(), std::back_inserter(indexes));
std::move(stringIds.begin(), stringIds.end(), std::back_inserter(indexes));

if (!indexes.empty()) {
modifiedVariables.insert(std::make_pair(index, indexes));
}

index++;
}

return modifiedVariables;
}

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

std::unordered_map<simulator_index, 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 @@ -317,60 +317,97 @@ class slave_simulator::impl
stringSetCache_.set_value(index, value);
}

void set_modified_index(std::unordered_set<variable_index>& modifiedIndexes, variable_index& index, bool modifier)
restenb marked this conversation as resolved.
Show resolved Hide resolved
{
if (modifier) {
modifiedIndexes.insert(index);
} else {
modifiedIndexes.erase(index);
}
}

void set_real_input_modifier(
variable_index index,
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 @@ -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()
{
return pimpl_->get_modified_real_indexes();
}

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

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

std::unordered_set<variable_index> slave_simulator::get_modified_string_indexes()
{
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