-
Notifications
You must be signed in to change notification settings - Fork 11
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
Reset overrides from outside #206
Changes from 1 commit
64d819a
d269aed
02719b5
d01c7c7
6759847
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#include "../../include/cse/scenario.hpp" | ||
|
||
#include "cse/manipulator.hpp" | ||
|
||
namespace cse | ||
|
@@ -6,6 +8,17 @@ namespace cse | |
namespace | ||
{ | ||
|
||
template<typename... Functors> | ||
struct visitor : Functors... | ||
{ | ||
visitor(const Functors&... functors) | ||
: Functors(functors)... | ||
{ | ||
} | ||
|
||
using Functors::operator()...; | ||
}; | ||
|
||
cse::variable_causality find_variable_causality(const std::vector<variable_description>& variables, | ||
const cse::variable_type type, | ||
const cse::variable_index index) | ||
|
@@ -32,90 +45,115 @@ void override_manipulator::simulator_removed(simulator_index index, time_point) | |
|
||
void override_manipulator::step_commencing(time_point /*currentTime*/) | ||
{ | ||
std::lock_guard<std::mutex> lock(lock_); | ||
if (!actions_.empty()) { | ||
for (const auto& action : actions_) { | ||
auto sim = simulators_.at(action.simulator); | ||
std::visit( | ||
visitor( | ||
[=](scenario::real_input_manipulator m) { | ||
sim->expose_for_setting(variable_type::real, action.variable); | ||
sim->set_real_input_manipulator(action.variable, m.f); | ||
}, | ||
[=](scenario::real_output_manipulator m) { | ||
sim->expose_for_getting(variable_type::real, action.variable); | ||
sim->set_real_output_manipulator(action.variable, m.f); | ||
}, | ||
[=](scenario::integer_input_manipulator m) { | ||
sim->expose_for_setting(variable_type::integer, action.variable); | ||
sim->set_integer_input_manipulator(action.variable, m.f); | ||
}, | ||
[=](scenario::integer_output_manipulator m) { | ||
sim->expose_for_getting(variable_type::integer, action.variable); | ||
sim->set_integer_output_manipulator(action.variable, m.f); | ||
}, | ||
[=](scenario::boolean_input_manipulator m) { | ||
sim->expose_for_setting(variable_type::boolean, action.variable); | ||
sim->set_boolean_input_manipulator(action.variable, m.f); | ||
}, | ||
[=](scenario::boolean_output_manipulator m) { | ||
sim->expose_for_getting(variable_type::boolean, action.variable); | ||
sim->set_boolean_output_manipulator(action.variable, m.f); | ||
}, | ||
[=](scenario::string_input_manipulator m) { | ||
sim->expose_for_setting(variable_type::string, action.variable); | ||
sim->set_string_input_manipulator(action.variable, m.f); | ||
}, | ||
[=](scenario::string_output_manipulator m) { | ||
sim->expose_for_getting(variable_type::string, action.variable); | ||
sim->set_string_output_manipulator(action.variable, m.f); | ||
}), | ||
action.manipulator); | ||
} | ||
actions_.clear(); | ||
} | ||
} | ||
|
||
void override_manipulator::override_real_variable(simulator_index index, variable_index variable, double value) | ||
|
||
template<typename I, typename O, typename M, typename N> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With so many template parameters it is no longer obvious what they mean. I suggest some more descriptive names, or at least an explanatory comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree! I also don't really like how this turned out, so I'm working on a cleaner way to do it. |
||
void override_manipulator::add_action(simulator_index index, variable_index variable, variable_type type, std::function<O(I)> f) | ||
{ | ||
auto f = [value](double /*original*/) { return value; }; | ||
auto sim = simulators_.at(index); | ||
auto causality = find_variable_causality(sim->model_description().variables, variable_type::real, variable); | ||
auto causality = find_variable_causality(sim->model_description().variables, type, variable); | ||
std::lock_guard<std::mutex> lock(lock_); | ||
switch (causality) { | ||
case input: | ||
case parameter: | ||
sim->expose_for_setting(variable_type::real, variable); | ||
sim->set_real_input_manipulator(variable, f); | ||
actions_.emplace_back(scenario::variable_action{index, variable, M{f}}); | ||
break; | ||
case calculated_parameter: | ||
case output: | ||
sim->expose_for_getting(variable_type::real, variable); | ||
sim->set_real_output_manipulator(variable, f); | ||
actions_.emplace_back(scenario::variable_action{index, variable, N{f}}); | ||
break; | ||
default: | ||
throw std::invalid_argument("No support for manipulating a variable with this causality"); | ||
} | ||
} | ||
|
||
void override_manipulator::override_real_variable(simulator_index index, variable_index variable, double value) | ||
{ | ||
auto f = [value](double /*original*/) { return value; }; | ||
add_action<double, double, scenario::real_input_manipulator, scenario::real_output_manipulator>(index, variable, cse::variable_type::real, f); | ||
} | ||
|
||
void override_manipulator::override_integer_variable(simulator_index index, variable_index variable, int value) | ||
{ | ||
auto f = [value](int /*original*/) { return value; }; | ||
auto sim = simulators_.at(index); | ||
auto causality = find_variable_causality(sim->model_description().variables, variable_type::integer, variable); | ||
switch (causality) { | ||
case input: | ||
case parameter: | ||
sim->expose_for_setting(variable_type::integer, variable); | ||
sim->set_integer_input_manipulator(variable, f); | ||
break; | ||
case calculated_parameter: | ||
case output: | ||
sim->expose_for_getting(variable_type::integer, variable); | ||
sim->set_integer_output_manipulator(variable, f); | ||
break; | ||
default: | ||
throw std::invalid_argument("No support for manipulating a variable with this causality"); | ||
} | ||
add_action<int, int, scenario::integer_input_manipulator, scenario::integer_output_manipulator>(index, variable, cse::variable_type::integer, f); | ||
} | ||
|
||
void override_manipulator::override_boolean_variable(simulator_index index, variable_index variable, bool value) | ||
{ | ||
auto f = [value](bool /*original*/) { return value; }; | ||
auto sim = simulators_.at(index); | ||
auto causality = find_variable_causality(sim->model_description().variables, variable_type::boolean, variable); | ||
switch (causality) { | ||
case input: | ||
case parameter: | ||
sim->expose_for_setting(variable_type::boolean, variable); | ||
sim->set_boolean_input_manipulator(variable, f); | ||
break; | ||
case calculated_parameter: | ||
case output: | ||
sim->expose_for_getting(variable_type::boolean, variable); | ||
sim->set_boolean_output_manipulator(variable, f); | ||
break; | ||
default: | ||
throw std::invalid_argument("No support for manipulating a variable with this causality"); | ||
} | ||
add_action<bool, bool, scenario::boolean_input_manipulator, scenario::boolean_output_manipulator>(index, variable, cse::variable_type::boolean, f); | ||
} | ||
void override_manipulator::override_string_variable(simulator_index index, variable_index variable, std::string value) | ||
|
||
void override_manipulator::override_string_variable(simulator_index index, variable_index variable, const std::string& value) | ||
{ | ||
auto f = [value](std::string_view /*original*/) { return value; }; | ||
auto sim = simulators_.at(index); | ||
auto causality = find_variable_causality(sim->model_description().variables, variable_type::string, variable); | ||
switch (causality) { | ||
case input: | ||
case parameter: | ||
sim->expose_for_setting(variable_type::string, variable); | ||
sim->set_string_input_manipulator(variable, f); | ||
break; | ||
case calculated_parameter: | ||
case output: | ||
sim->expose_for_getting(variable_type::string, variable); | ||
sim->set_string_output_manipulator(variable, f); | ||
break; | ||
default: | ||
throw std::invalid_argument("No support for manipulating a variable with this causality"); | ||
} | ||
add_action<std::string_view, std::string, scenario::string_input_manipulator, scenario::string_output_manipulator>(index, variable, cse::variable_type::string, f); | ||
} | ||
|
||
void override_manipulator::reset_real_variable(simulator_index index, variable_index variable) | ||
{ | ||
add_action<double, double, scenario::real_input_manipulator, scenario::real_output_manipulator>(index, variable, cse::variable_type::real, nullptr); | ||
} | ||
|
||
void override_manipulator::reset_integer_variable(simulator_index index, variable_index variable) | ||
{ | ||
add_action<int, int, scenario::integer_input_manipulator, scenario::integer_output_manipulator>(index, variable, cse::variable_type::integer, nullptr); | ||
} | ||
|
||
void override_manipulator::reset_boolean_variable(simulator_index index, variable_index variable) | ||
{ | ||
add_action<bool, bool, scenario::boolean_input_manipulator, scenario::boolean_output_manipulator>(index, variable, cse::variable_type::boolean, nullptr); | ||
} | ||
|
||
void override_manipulator::reset_string_variable(simulator_index index, variable_index variable) | ||
{ | ||
add_action<std::string_view, std::string, scenario::string_input_manipulator, scenario::string_output_manipulator>(index, variable, cse::variable_type::string, nullptr); | ||
} | ||
|
||
override_manipulator::~override_manipulator() = default; | ||
|
||
} // namespace cse | ||
} // namespace cse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
#include "cse/scenario.hpp"
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CLion works in mysterious ways, but I should have caught it! I'll remove it, as
scenario.hpp
is already included inmanipulator.hpp
.