Skip to content

Commit

Permalink
Observe and manipulate boolean and string variables (#257)
Browse files Browse the repository at this point in the history
* #253 Observe and provide booleans and strings in cse::last_value_observer

* #253 Getters and setters for boolean and string vars in C API.
Combined override_manipulator::reset_xxx into one method.

* Include string.h in multiple_fmus_execution_test.c

* Review followup. Replace cse_string_value with const char* and internal memory management for string values.
  • Loading branch information
eidekrist authored Jun 7, 2019
1 parent af90857 commit 998eef3
Show file tree
Hide file tree
Showing 12 changed files with 505 additions and 120 deletions.
237 changes: 162 additions & 75 deletions include/cse.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef CSE_H
#define CSE_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Expand Down Expand Up @@ -410,11 +411,11 @@ typedef struct cse_manipulator_s cse_manipulator;
* The slave.
* \param [in] variables
* A pointer to an array of length `nv` that contains the (slave-specific)
* indices of variables to retrieve.
* indices of variables to set.
* \param [in] nv
* The length of the `variables` and `values` arrays.
* \param [out] values
* A pointer to an array of length `nv` which will be filled with the
* A pointer to an array of length `nv` with the
* values of the variables specified in `variables`, in the same order.
*
* \returns
Expand All @@ -428,107 +429,59 @@ int cse_manipulator_slave_set_real(
const double values[]);

/**
* Resets any previously overridden values of real variables for one slave.
* Sets the values of 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.
* indices of variables to set.
* \param [in] nv
* The length of the `variables` array.
* The length of the `variables` and `values` arrays.
* \param [out] values
* A pointer to an array of length `nv` with the
* values of the variables specified in `variables`, in the same order.
*
* \returns
* 0 on success and -1 on error.
*/
int cse_manipulator_slave_reset_real(
int cse_manipulator_slave_set_integer(
cse_manipulator* manipulator,
cse_slave_index slaveIndex,
const cse_variable_index variables[],
size_t nv);
size_t nv,
const int values[]);

/**
* Retrieves the values of real variables for one slave.
* Sets the values of boolean variables for one slave.
*
* \param [in] observer
* The observer.
* \param [in] 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 retrieve.
* indices of variables to set.
* \param [in] nv
* The length of the `variables` and `values` arrays.
* \param [out] values
* A pointer to an array of length `nv` which will be filled with the
* A pointer to an array of length `nv` with the
* values of the variables specified in `variables`, in the same order.
*
* \returns
* 0 on success and -1 on error.
*/
int cse_observer_slave_get_real(
cse_observer* observer,
cse_slave_index slave,
int cse_manipulator_slave_set_boolean(
cse_manipulator* manipulator,
cse_slave_index slaveIndex,
const cse_variable_index variables[],
size_t nv,
double values[]);

/**
* Retrieves a series of observed values, step numbers and times for a real variable.
*
* \param [in] observer the observer
* \param [in] slave index of the slave
* \param [in] variableIndex the variable index
* \param [in] fromStep the step number to start from
* \param [in] nSamples the number of samples to read
* \param [out] values the series of observed values
* \param [out] steps the corresponding step numbers
* \param [out] times the corresponding simulation times
*
* \returns
* The number of samples actually read, which may be smaller than `nSamples`.
*/
int64_t cse_observer_slave_get_real_samples(
cse_observer* observer,
cse_slave_index slave,
cse_variable_index variableIndex,
cse_step_number fromStep,
size_t nSamples,
double values[],
cse_step_number steps[],
cse_time_point times[]);

/**
* Retrieves two time-synchronized series of observed values for two real variables.
*
* \param [in] observer the observer
* \param [in] slave1 index of the first slave
* \param [in] variableIndex1 the first variable index
* \param [in] slave2 index of the second slave
* \param [in] variableIndex2 the second variable index
* \param [in] fromStep the step number to start from
* \param [in] nSamples the number of samples to read
* \param [out] values1 the first series of observed values
* \param [out] values2 the second series of observed values
*
* \returns
* The number of samples actually read, which may be smaller than `nSamples`.
*/
int64_t cse_observer_slave_get_real_synchronized_series(
cse_observer* observer,
cse_slave_index slave1,
cse_variable_index variableIndex1,
cse_slave_index slave2,
cse_variable_index variableIndex2,
cse_step_number fromStep,
size_t nSamples,
double values1[],
double values2[]);
const bool values[]);

/**
* Sets the values of integer variables for one slave.
* Sets the values of string variables for one slave.
*
* \param [in] manipulator
* The manipulator.
Expand All @@ -540,26 +493,28 @@ int64_t cse_observer_slave_get_real_synchronized_series(
* \param [in] nv
* The length of the `variables` and `values` arrays.
* \param [out] values
* A pointer to an array of length `nv` which will be filled with the
* A pointer to an array of length `nv` with the
* values of the variables specified in `variables`, in the same order.
*
* \returns
* 0 on success and -1 on error.
*/
int cse_manipulator_slave_set_integer(
int cse_manipulator_slave_set_string(
cse_manipulator* manipulator,
cse_slave_index slaveIndex,
const cse_variable_index variables[],
size_t nv,
const int values[]);
const char* values[]);

/**
* Resets the values of any previously overridden integer variables for one slave.
* Resets any previously overridden variable values of a certain type for one slave.
*
* \param [in] manipulator
* The manipulator.
* \param [in] slaveIndex
* The slave.
* \param [in] type
* The variable type.
* \param [in] variables
* A pointer to an array of length `nv` that contains the (slave-specific)
* indices of variables to reset.
Expand All @@ -569,12 +524,39 @@ int cse_manipulator_slave_set_integer(
* \returns
* 0 on success and -1 on error.
*/
int cse_manipulator_slave_reset_integer(
int cse_manipulator_slave_reset(
cse_manipulator* manipulator,
cse_slave_index slaveIndex,
cse_variable_type type,
const cse_variable_index variables[],
size_t nv);

/**
* Retrieves the values of real variables for one slave.
*
* \param [in] observer
* The observer.
* \param [in] slave
* The slave.
* \param [in] variables
* A pointer to an array of length `nv` that contains the (slave-specific)
* indices of variables to retrieve.
* \param [in] nv
* The length of the `variables` and `values` arrays.
* \param [out] values
* A pointer to an array of length `nv` which will be filled with the
* values of the variables specified in `variables`, in the same order.
*
* \returns
* 0 on success and -1 on error.
*/
int cse_observer_slave_get_real(
cse_observer* observer,
cse_slave_index slave,
const cse_variable_index variables[],
size_t nv,
double values[]);

/**
* Retrieves the values of integer variables for one slave.
*
Expand All @@ -601,6 +583,84 @@ int cse_observer_slave_get_integer(
size_t nv,
int values[]);

/**
* Retrieves the values of boolean variables for one slave.
*
* \param [in] observer
* The observer.
* \param [in] slave
* The slave index.
* \param [in] variables
* A pointer to an array of length `nv` that contains the (slave-specific)
* indices of variables to retrieve.
* \param [in] nv
* The length of the `variables` and `values` arrays.
* \param [out] values
* A pointer to an array of length `nv` which will be filled with the
* values of the variables specified in `variables`, in the same order.
*
* \returns
* 0 on success and -1 on error.
*/
int cse_observer_slave_get_boolean(
cse_observer* observer,
cse_slave_index slave,
const cse_variable_index variables[],
size_t nv,
bool values[]);

/**
* Retrieves the values of string variables for one slave.
*
* \param [in] observer
* The observer.
* \param [in] slave
* The slave index.
* \param [in] variables
* A pointer to an array of length `nv` that contains the (slave-specific)
* indices of variables to retrieve.
* \param [in] nv
* The length of the `variables` and `values` arrays.
* \param [out] values
* A pointer to an array of length `nv` which will be filled with pointers
* to the values of the variables specified in `variables`, in the same order.
* The pointers are valid until the next call to `cse_observer_slave_get_string()`.
*
* \returns
* 0 on success and -1 on error.
*/
int cse_observer_slave_get_string(
cse_observer* observer,
cse_slave_index slave,
const cse_variable_index variables[],
size_t nv,
const char* values[]);

/**
* Retrieves a series of observed values, step numbers and times for a real variable.
*
* \param [in] observer the observer
* \param [in] slave index of the slave
* \param [in] variableIndex the variable index
* \param [in] fromStep the step number to start from
* \param [in] nSamples the number of samples to read
* \param [out] values the series of observed values
* \param [out] steps the corresponding step numbers
* \param [out] times the corresponding simulation times
*
* \returns
* The number of samples actually read, which may be smaller than `nSamples`.
*/
int64_t cse_observer_slave_get_real_samples(
cse_observer* observer,
cse_slave_index slave,
cse_variable_index variableIndex,
cse_step_number fromStep,
size_t nSamples,
double values[],
cse_step_number steps[],
cse_time_point times[]);

/**
* Retrieves a series of observed values, step numbers and times for an integer variable.
*
Expand All @@ -626,6 +686,33 @@ int64_t cse_observer_slave_get_integer_samples(
cse_step_number steps[],
cse_time_point times[]);

/**
* Retrieves two time-synchronized series of observed values for two real variables.
*
* \param [in] observer the observer
* \param [in] slave1 index of the first slave
* \param [in] variableIndex1 the first variable index
* \param [in] slave2 index of the second slave
* \param [in] variableIndex2 the second variable index
* \param [in] fromStep the step number to start from
* \param [in] nSamples the number of samples to read
* \param [out] values1 the first series of observed values
* \param [out] values2 the second series of observed values
*
* \returns
* The number of samples actually read, which may be smaller than `nSamples`.
*/
int64_t cse_observer_slave_get_real_synchronized_series(
cse_observer* observer,
cse_slave_index slave1,
cse_variable_index variableIndex1,
cse_slave_index slave2,
cse_variable_index variableIndex2,
cse_step_number fromStep,
size_t nSamples,
double values1[],
double values2[]);

/**
* Retrieves the step numbers for a range given by a duration.
*
Expand Down
10 changes: 2 additions & 8 deletions include/cse/manipulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,8 @@ class override_manipulator : public manipulator
void override_boolean_variable(simulator_index, variable_index, bool value);
/// Override the value of a variable with type `string`
void override_string_variable(simulator_index, variable_index, const std::string& value);
/// Reset override of a variable with type `real`
void reset_real_variable(simulator_index, variable_index);
/// Reset override of a variable with type `integer`
void reset_integer_variable(simulator_index, variable_index);
/// Reset override of a variable with type `boolean`
void reset_boolean_variable(simulator_index, variable_index);
/// Reset override of a variable with type `string`
void reset_string_variable(simulator_index, variable_index);
/// Reset override of a variable
void reset_variable(simulator_index, variable_type, variable_index);

~override_manipulator() noexcept override;

Expand Down
10 changes: 10 additions & 0 deletions include/cse/observer/last_value_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ class last_value_observer : public last_value_provider
gsl::span<const variable_index> variables,
gsl::span<int> values) override;

void get_boolean(
simulator_index sim,
gsl::span<const variable_index> variables,
gsl::span<bool> values) override;

void get_string(
simulator_index sim,
gsl::span<const variable_index> variables,
gsl::span<std::string> values) override;

~last_value_observer() noexcept override;

private:
Expand Down
Loading

0 comments on commit 998eef3

Please sign in to comment.