Skip to content

Commit

Permalink
Time dependent modifiers (#522)
Browse files Browse the repository at this point in the history
* Doodling an idea.

* Be sure to run modifier even if 1) a new value has not been set, or 2) a new modifier has not been set.

* Include current step size as an argument to modifier functions.

* Add test for output modifier as well.

* Declare test_manipulator copy members as deleted. Declare accumulator in capture list, make lambda mutable.
  • Loading branch information
eidekrist authored Feb 6, 2020
1 parent ef8a9aa commit ab37c48
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 73 deletions.
56 changes: 48 additions & 8 deletions include/cse/manipulator/manipulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,73 +36,113 @@ class manipulable : public observable
* Sets a modifier for the value of a real input variable.
*
* The variable must previously have been exposed with `expose_for_setting()`.
* \param reference The variable's value reference.
* \param modifier A function which takes the original value and the step size,
* and returns a modified value. The function is guaranteed to be called each
* time the `manipulable` is stepped. The function can be removed by setting
* the argument to `nullptr`.
*/
virtual void set_real_input_modifier(
value_reference reference,
std::function<double(double)> modifier) = 0;
std::function<double(double, duration)> modifier) = 0;

/**
* Sets a modifier for the value of an integer input variable.
*
* The variable must previously have been exposed with `expose_for_setting()`.
* \param reference The variable's value reference.
* \param modifier A function which takes the original value and the step size,
* and returns a modified value. The function is guaranteed to be called each
* time the `manipulable` is stepped. The function can be removed by setting
* the argument to `nullptr`.
*/
virtual void set_integer_input_modifier(
value_reference reference,
std::function<int(int)> modifier) = 0;
std::function<int(int, duration)> modifier) = 0;

/**
* Sets a modifier for the value of a boolean input variable.
*
* The variable must previously have been exposed with `expose_for_setting()`.
* \param reference The variable's value reference.
* \param modifier A function which takes the original value and the step size,
* and returns a modified value. The function is guaranteed to be called each
* time the `manipulable` is stepped. The function can be removed by setting
* the argument to `nullptr`.
*/
virtual void set_boolean_input_modifier(
value_reference reference,
std::function<bool(bool)> modifier) = 0;
std::function<bool(bool, duration)> modifier) = 0;

/**
* Sets a modifier for the value of a string input variable.
*
* The variable must previously have been exposed with `expose_for_setting()`.
* \param reference The variable's value reference.
* \param modifier A function which takes the original value and the step size,
* and returns a modified value. The function is guaranteed to be called each
* time the `manipulable` is stepped. The function can be removed by setting
* the argument to `nullptr`.
*/
virtual void set_string_input_modifier(
value_reference reference,
std::function<std::string(std::string_view)> modifier) = 0;
std::function<std::string(std::string_view, duration)> modifier) = 0;

/**
* Sets a modifier for the value of a real output variable.
*
* The variable must previously have been exposed with `expose_for_setting()`.
* \param reference The variable's value reference.
* \param modifier A function which takes the original value and the step size,
* and returns a modified value. The function is guaranteed to be called each
* time the `manipulable` is stepped. The function can be removed by setting
* the argument to `nullptr`.
*/
virtual void set_real_output_modifier(
value_reference reference,
std::function<double(double)> modifier) = 0;
std::function<double(double, duration)> modifier) = 0;

/**
* Sets a modifier for the value of an integer output variable.
*
* The variable must previously have been exposed with `expose_for_setting()`.
* \param reference The variable's value reference.
* \param modifier A function which takes the original value and the step size,
* and returns a modified value. The function is guaranteed to be called each
* time the `manipulable` is stepped. The function can be removed by setting
* the argument to `nullptr`.
*/
virtual void set_integer_output_modifier(
value_reference reference,
std::function<int(int)> modifier) = 0;
std::function<int(int, duration)> modifier) = 0;

/**
* Sets a modifier for the value of a boolean output variable.
*
* The variable must previously have been exposed with `expose_for_setting()`.
* \param reference The variable's value reference.
* \param modifier A function which takes the original value and the step size,
* and returns a modified value. The function is guaranteed to be called each
* time the `manipulable` is stepped. The function can be removed by setting
* the argument to `nullptr`.
*/
virtual void set_boolean_output_modifier(
value_reference reference,
std::function<bool(bool)> modifier) = 0;
std::function<bool(bool, duration)> modifier) = 0;

/**
* Sets a modifier for the value of a string output variable.
*
* The variable must previously have been exposed with `expose_for_setting()`.
* \param reference The variable's value reference.
* \param modifier A function which takes the original value and the step size,
* and returns a modified value. The function is guaranteed to be called each
* time the `manipulable` is stepped. The function can be removed by setting
* the argument to `nullptr`.
*/
virtual void set_string_output_modifier(
value_reference reference,
std::function<std::string(std::string_view)> modifier) = 0;
std::function<std::string(std::string_view, duration)> modifier) = 0;
};

/**
Expand Down
8 changes: 4 additions & 4 deletions include/cse/scenario.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ namespace scenario
struct real_modifier
{
/// A function which may be called any number of times. Can be `nullptr`.
std::function<double(double)> f;
std::function<double(double, duration)> f;
};

/// The modification of the value of a variable with type `integer`.
struct integer_modifier
{
/// A function which may be called any number of times. Can be `nullptr`.
std::function<int(int)> f;
std::function<int(int, duration)> f;
};

/// The modification of the value of a variable with type `boolean`.
struct boolean_modifier
{
/// A function which may be called any number of times. Can be `nullptr`.
std::function<bool(bool)> f;
std::function<bool(bool, duration)> f;
};

/// The modification of the value of a variable with type `string`.
struct string_modifier
{
/// A function which may be called any number of times. Can be `nullptr`.
std::function<std::string(std::string_view)> f;
std::function<std::string(std::string_view, duration)> f;
};

/// A struct specifying a variable and the modification of its value.
Expand Down
16 changes: 8 additions & 8 deletions src/cpp/cse/slave_simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,28 @@ class slave_simulator : public simulator

void set_real_input_modifier(
value_reference reference,
std::function<double(double)> modifier) override;
std::function<double(double, duration)> modifier) override;
void set_integer_input_modifier(
value_reference reference,
std::function<int(int)> modifier) override;
std::function<int(int, duration)> modifier) override;
void set_boolean_input_modifier(
value_reference reference,
std::function<bool(bool)> modifier) override;
std::function<bool(bool, duration)> modifier) override;
void set_string_input_modifier(
value_reference reference,
std::function<std::string(std::string_view)> modifier) override;
std::function<std::string(std::string_view, duration)> modifier) override;
void set_real_output_modifier(
value_reference reference,
std::function<double(double)> modifier) override;
std::function<double(double, duration)> modifier) override;
void set_integer_output_modifier(
value_reference reference,
std::function<int(int)> modifier) override;
std::function<int(int, duration)> modifier) override;
void set_boolean_output_modifier(
value_reference reference,
std::function<bool(bool)> modifier) override;
std::function<bool(bool, duration)> modifier) override;
void set_string_output_modifier(
value_reference reference,
std::function<std::string(std::string_view)> modifier) override;
std::function<std::string(std::string_view, duration)> modifier) override;

std::unordered_set<value_reference>& get_modified_real_variables() const override;
std::unordered_set<value_reference>& get_modified_integer_variables() const override;
Expand Down
8 changes: 4 additions & 4 deletions src/cpp/manipulator/override_manipulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void override_manipulator::override_real_variable(
value_reference variable,
double value)
{
auto f = [value](double /*original*/) { return value; };
auto f = [value](double /*original*/, duration) { return value; };
add_action(index, variable, variable_type::real, scenario::real_modifier{f});
}

Expand All @@ -141,7 +141,7 @@ void override_manipulator::override_integer_variable(
value_reference variable,
int value)
{
auto f = [value](int /*original*/) { return value; };
auto f = [value](int /*original*/, duration) { return value; };
add_action(index, variable, variable_type::integer, scenario::integer_modifier{f});
}

Expand All @@ -150,7 +150,7 @@ void override_manipulator::override_boolean_variable(
value_reference variable,
bool value)
{
auto f = [value](bool /*original*/) { return value; };
auto f = [value](bool /*original*/, duration) { return value; };
add_action(index, variable, variable_type::boolean, scenario::boolean_modifier{f});
}

Expand All @@ -159,7 +159,7 @@ void override_manipulator::override_string_variable(
value_reference variable,
const std::string& value)
{
auto f = [value](std::string_view /*original*/) { return value; };
auto f = [value](std::string_view /*original*/, duration) { return value; };
add_action(index, variable, variable_type::string, scenario::string_modifier{f});
}

Expand Down
10 changes: 5 additions & 5 deletions src/cpp/scenario_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool is_input(cse::variable_causality causality)
}

template<typename T>
std::function<T(T)> generate_modifier(
std::function<T(T, duration)> generate_modifier(
const std::string& kind,
const YAML::Node& event)
{
Expand All @@ -68,16 +68,16 @@ std::function<T(T)> generate_modifier(
}
T value = event["value"].as<T>();
if ("bias" == kind) {
return [value](T original) { return original + value; };
return [value](T original, duration) { return original + value; };
} else if ("override" == kind) {
return [value](T /*original*/) { return value; };
return [value](T /*original*/, duration) { return value; };
}
std::ostringstream oss;
oss << "Can't process unrecognized modifier kind: " << kind;
throw std::invalid_argument(oss.str());
}

std::function<std::string(std::string_view)> generate_string_modifier(
std::function<std::string(std::string_view, duration)> generate_string_modifier(
const std::string& kind,
const YAML::Node& event)
{
Expand All @@ -86,7 +86,7 @@ std::function<std::string(std::string_view)> generate_string_modifier(
}
auto value = event["value"].as<std::string>();
if ("override" == kind) {
return [value](std::string_view /*original*/) { return value; };
return [value](std::string_view /*original*/, duration) { return value; };
}
std::ostringstream oss;
oss << "Can't process unsupported modifier kind: " << kind << " for type " << to_text(cse::variable_type::string);
Expand Down
Loading

0 comments on commit ab37c48

Please sign in to comment.