Skip to content

Commit

Permalink
Reset overrides from outside (#206)
Browse files Browse the repository at this point in the history
* #190 Reset overrides from outside. Only actually set manipulators when step commences.

* #190 New World: scenario::xxx_yyy_manipulator -> scenario::xxx_manipulator. Directionality is stored in a flag in variable_action.

* #190 Further line length reduction.

* #190 Correction copy-paste driven development mishap.

* #190 Comment out unused variables.
  • Loading branch information
eidekrist authored Mar 15, 2019
1 parent 0a324ec commit 665ac37
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 218 deletions.
50 changes: 47 additions & 3 deletions include/cse.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,28 @@ int cse_manipulator_slave_set_real(
size_t nv,
const double values[]);

/**
* Resets any previously overridden values of real variables for one slave.
*
* \param [in] manipulator
* The manipulator.
* \param [in] slaveIndex
* The slave.
* \param [in] variables
* A pointer to an array of length `nv` that contains the (slave-specific)
* indices of variables to reset.
* \param [in] nv
* The length of the `variables` array.
*
* \returns
* 0 on success and -1 on error.
*/
int cse_manipulator_slave_reset_real(
cse_manipulator* manipulator,
cse_slave_index slaveIndex,
const cse_variable_index variables[],
size_t nv);

/**
* Retrieves the values of real variables for one slave.
*
Expand Down Expand Up @@ -438,6 +460,28 @@ int cse_manipulator_slave_set_integer(
size_t nv,
const int values[]);

/**
* Resets the values of any previously overridden integer variables for one slave.
*
* \param [in] manipulator
* The manipulator.
* \param [in] slaveIndex
* The slave.
* \param [in] variables
* A pointer to an array of length `nv` that contains the (slave-specific)
* indices of variables to reset.
* \param [in] nv
* The length of the `variables` array.
*
* \returns
* 0 on success and -1 on error.
*/
int cse_manipulator_slave_reset_integer(
cse_manipulator* manipulator,
cse_slave_index slaveIndex,
const cse_variable_index variables[],
size_t nv);

/**
* Retrieves the values of integer variables for one slave.
*
Expand Down Expand Up @@ -660,9 +704,9 @@ cse_manipulator* cse_scenario_manager_create();

/// Loads and executes a scenario from file.
int cse_execution_load_scenario(
cse_execution* execution,
cse_manipulator* manipulator,
const char* scenarioFile);
cse_execution* execution,
cse_manipulator* manipulator,
const char* scenarioFile);

/// Checks if a scenario is running
int cse_scenario_is_running(cse_manipulator* manipulator);
Expand Down
14 changes: 13 additions & 1 deletion include/cse/manipulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,24 @@ class override_manipulator : public manipulator
void override_real_variable(simulator_index, variable_index, double value);
void override_integer_variable(simulator_index, variable_index, int value);
void override_boolean_variable(simulator_index, variable_index, bool value);
void override_string_variable(simulator_index, variable_index, std::string value);
void override_string_variable(simulator_index, variable_index, const std::string& value);
void reset_real_variable(simulator_index, variable_index);
void reset_integer_variable(simulator_index, variable_index);
void reset_boolean_variable(simulator_index, variable_index);
void reset_string_variable(simulator_index, variable_index);

~override_manipulator() noexcept override;

private:
void add_action(
simulator_index index,
variable_index variable,
variable_type type,
const scenario::manipulators& m);

std::unordered_map<simulator_index, simulator*> simulators_;
std::vector<scenario::variable_action> actions_;
std::mutex lock_;
};

} // namespace cse
Expand Down
44 changes: 11 additions & 33 deletions include/cse/scenario.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,38 @@ namespace cse
namespace scenario
{

struct real_input_manipulator
struct real_manipulator
{
std::function<double(double)> f;
};

struct real_output_manipulator
{
std::function<double(double)> f;
};

struct integer_input_manipulator
{
std::function<int(int)> f;
};

struct integer_output_manipulator
struct integer_manipulator
{
std::function<int(int)> f;
};

struct boolean_input_manipulator
struct boolean_manipulator
{
std::function<bool(bool)> f;
};

struct boolean_output_manipulator
{
std::function<bool(bool)> f;
};

struct string_input_manipulator
struct string_manipulator
{
std::function<std::string(std::string_view)> f;
};

struct string_output_manipulator
{
std::function<std::string(std::string_view)> f;
};
using manipulators = std::variant<
real_manipulator,
integer_manipulator,
boolean_manipulator,
string_manipulator>;

struct variable_action
{
simulator_index simulator;
variable_index variable;
std::variant<
real_input_manipulator,
real_output_manipulator,
integer_input_manipulator,
integer_output_manipulator,
boolean_input_manipulator,
boolean_output_manipulator,
string_input_manipulator,
string_output_manipulator>
manipulator;
manipulators manipulator;
bool is_input;
};

struct event
Expand Down
46 changes: 44 additions & 2 deletions src/c/cse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ int cse_observer_start_observing(cse_observer* observer, cse_slave_index slave,
if (!timeSeriesObserver) {
throw std::invalid_argument("Invalid observer! The provided observer must be a time_series_observer.");
}
const auto variableId = cse::variable_id {slave, to_variable_type(type), index};
const auto variableId = cse::variable_id{slave, to_variable_type(type), index};
timeSeriesObserver->start_observing(variableId);
return success;
} catch (...) {
Expand All @@ -603,7 +603,7 @@ int cse_observer_stop_observing(cse_observer* observer, cse_slave_index slave, c
if (!timeSeriesObserver) {
throw std::invalid_argument("Invalid observer! The provided observer must be a time_series_observer.");
}
const auto variableId = cse::variable_id {slave, to_variable_type(type), index};
const auto variableId = cse::variable_id{slave, to_variable_type(type), index};
timeSeriesObserver->stop_observing(variableId);
return success;
} catch (...) {
Expand Down Expand Up @@ -706,6 +706,48 @@ int cse_manipulator_slave_set_integer(
}
}

int cse_manipulator_slave_reset_real(
cse_manipulator* manipulator,
cse_slave_index slaveIndex,
const cse_variable_index variables[],
size_t nv)
{
try {
const auto man = std::dynamic_pointer_cast<cse::override_manipulator>(manipulator->cpp_manipulator);
if (!man) {
throw std::invalid_argument("Invalid manipulator!");
}
for (size_t i = 0; i < nv; i++) {
man->reset_real_variable(slaveIndex, variables[i]);
}
return success;
} catch (...) {
handle_current_exception();
return failure;
}
}

int cse_manipulator_slave_reset_integer(
cse_manipulator* manipulator,
cse_slave_index slaveIndex,
const cse_variable_index variables[],
size_t nv)
{
try {
const auto man = std::dynamic_pointer_cast<cse::override_manipulator>(manipulator->cpp_manipulator);
if (!man) {
throw std::invalid_argument("Invalid manipulator!");
}
for (size_t i = 0; i < nv; i++) {
man->reset_integer_variable(slaveIndex, variables[i]);
}
return success;
} catch (...) {
handle_current_exception();
return failure;
}
}

cse_manipulator* cse_scenario_manager_create()
{
auto manipulator = std::make_unique<cse_manipulator>();
Expand Down
Loading

0 comments on commit 665ac37

Please sign in to comment.