Drake
context.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <vector>
5 
11 
12 namespace drake {
13 namespace systems {
14 
17 // TODO(sherm1) Add step information.
18 template <typename T>
19 struct StepInfo {
22  // TODO(sherm1): Consider whether this is sufficiently robust.
23  T time_sec{};
24 };
25 
36 // TODO(david-german-tri): Manage cache invalidation.
37 template <typename T>
38 class Context {
39  public:
40  Context() {}
41  virtual ~Context() {}
42 
44  const T& get_time() const { return get_step_info().time_sec; }
45 
47  void set_time(const T& time_sec) {
48  get_mutable_step_info()->time_sec = time_sec;
49  }
50 
54  void SetInputPort(int index, std::unique_ptr<InputPort<T>> port) {
55  if (index < 0 || index >= get_num_input_ports()) {
56  throw std::out_of_range("Input port out of range.");
57  }
58  // TODO(david-german-tri): Set invalidation callbacks.
59  inputs_[index] = std::move(port);
60  }
61 
64  void ClearInputPorts() {
65  inputs_.clear();
66  }
67 
70  void SetNumInputPorts(int n) {
71  ClearInputPorts();
72  inputs_.resize(n);
73  }
74 
75  int get_num_input_ports() const { return static_cast<int>(inputs_.size()); }
76 
80  const VectorInterface<T>* get_vector_input(int index) const {
81  if (index >= get_num_input_ports()) {
82  throw std::out_of_range("Input port out of range.");
83  }
84  if (inputs_[index] == nullptr) {
85  return nullptr;
86  }
87  return inputs_[index]->get_vector_data();
88  }
89 
90  const State<T>& get_state() const { return state_; }
91 
94  State<T>* get_mutable_state() { return &state_; }
95 
99  const Cache<T>& get_cache() const { return cache_; }
100 
104  Cache<T>* get_mutable_cache() const { return &cache_; }
105 
109  std::unique_ptr<Context<T>> Clone() const {
110  return std::unique_ptr<Context<T>>(DoClone());
111  }
112 
113  protected:
117  virtual Context<T>* DoClone() const {
118  Context<T>* context = new Context<T>();
119 
120  // Make a deep copy of the state using LeafStateVector::Clone().
121  const ContinuousState<T>& xc = *this->get_state().continuous_state;
122  const int num_q = xc.get_generalized_position().size();
123  const int num_v = xc.get_generalized_velocity().size();
124  const int num_z = xc.get_misc_continuous_state().size();
125  const LeafStateVector<T>& xc_vector =
126  dynamic_cast<const LeafStateVector<T>&>(xc.get_state());
127  context->get_mutable_state()->continuous_state.reset(
128  new ContinuousState<T>(xc_vector.Clone(), num_q, num_v, num_z));
129 
130  // Make deep copies of the inputs into FreestandingInputPorts.
131  // TODO(david-german-tri): Preserve version numbers as well.
132  for (const auto& port : this->inputs_) {
133  context->inputs_.emplace_back(
135  port->get_vector_data()->Clone(), port->get_sample_time_sec()));
136  }
137 
138  // Make deep copies of everything else using the default copy constructors.
139  *context->get_mutable_step_info() = this->get_step_info();
140  *context->get_mutable_cache() = this->get_cache();
141  return context;
142  }
143 
144  private:
145  // Returns a const reference to current time and step information.
146  const StepInfo<T>& get_step_info() const { return step_info_; }
147 
148  // Provides writable access to time and step information, with the side
149  // effect of invaliding any computation that is dependent on them.
150  // TODO(david-german-tri) Invalidate all cached time- and step-dependent
151  // computations.
152  StepInfo<T>* get_mutable_step_info() { return &step_info_; }
153 
154  // Context objects are neither copyable nor moveable.
155  Context(const Context& other) = delete;
156  Context& operator=(const Context& other) = delete;
157  Context(Context&& other) = delete;
158  Context& operator=(Context&& other) = delete;
159 
160  // Current time and step information.
161  StepInfo<T> step_info_;
162 
163  // The external inputs to the System.
164  std::vector<std::unique_ptr<InputPort<T>>> inputs_;
165 
166  // The internal state of the System.
167  State<T> state_;
168 
169  // The cache. The System may insert arbitrary key-value pairs, and configure
170  // invalidation on a per-line basis.
171  mutable Cache<T> cache_;
172 };
173 
174 } // namespace systems
175 } // namesapce drake
VectorInterface is a pure abstract interface that real-valued signals between Systems must satisfy...
Definition: vector_interface.h:25
virtual Context< T > * DoClone() const
The Context implementation for Diagrams must override this method, since the state of a Diagram will ...
Definition: context.h:117
const State< T > & get_state() const
Definition: context.h:90
std::unique_ptr< Context< T > > Clone() const
Returns a deep copy of this Context.
Definition: context.h:109
std::unique_ptr< LeafStateVector< T > > Clone() const
Copies the entire state to a new LeafStateVector.
Definition: leaf_state_vector.h:23
void ClearInputPorts()
Removes all the input ports, and deregisters them from the output ports on which they depend...
Definition: context.h:64
Cache< T > * get_mutable_cache() const
Access to the cache is always read-write, and is permitted even on const references to the Context...
Definition: context.h:104
Definition: constants.h:3
void set_time(const T &time_sec)
Set the current time in seconds.
Definition: context.h:47
State< T > * get_mutable_state()
Returns writable access to the State.
Definition: context.h:94
The Context is a container for all of the data necessary to uniquely determine the computations perfo...
Definition: context.h:38
int get_num_input_ports() const
Definition: context.h:75
virtual ~Context()
Definition: context.h:41
The FreestandingInputPort encapsulates a vector of data for use as an input to a System.
Definition: system_input.h:110
Context()
Definition: context.h:40
Definition: cache.h:9
T time_sec
The time, in seconds.
Definition: context.h:23
const T & get_time() const
Returns the current time in seconds.
Definition: context.h:44
void SetNumInputPorts(int n)
Clears the input ports and allocates n new input ports, not connected to anything.
Definition: context.h:70
LeafStateVector is an abstract class template that implements StateVector for leaf Systems...
Definition: leaf_state_vector.h:17
The State is a container for all the data comprising the complete state of a particular System at a p...
Definition: state.h:140
const Cache< T > & get_cache() const
Returns a const reference to the Cache, which is expected to contain precalculated values of interest...
Definition: context.h:99
const VectorInterface< T > * get_vector_input(int index) const
Returns the vector data of the input port at index.
Definition: context.h:80
Contains information about the independent variable including time and step number.
Definition: context.h:19
The ContinuousState is a container for all the State variables that are unique to continuous Systems...
Definition: state.h:22
void SetInputPort(int index, std::unique_ptr< InputPort< T >> port)
Connects the input port port to this Context at the given index.
Definition: context.h:54
The InputPort describes a single input to a System, from another System or from an external driver...
Definition: system_input.h:20