SyDEVS  v0.6.7
Multiscale Simulation and Systems Modeling Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
composite_node.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef SYDEVS_SYSTEMS_COMPOSITE_NODE_H_
3 #define SYDEVS_SYSTEMS_COMPOSITE_NODE_H_
4 
8 
9 namespace sydevs {
10 namespace systems {
11 
12 
24 {
25 public:
36  composite_node(const std::string& node_name, const node_context& external_context);
37 
38  virtual ~composite_node() = default;
39 
40  data_mode node_dmode() const;
41  scale time_precision() const;
42 
46  void handle_finalization_event(duration elapsed_dt);
47 
48 protected:
50 
61  template<data_mode dmode, typename T>
62  void inward_link(const port<dmode, input, T>& src_port,
63  const port<dmode, input, T>& dst_port);
64 
75  template<data_mode dmode, typename T>
76  void inner_link(const port<dmode, output, T>& src_port,
77  const port<dmode, input, T>& dst_port);
78 
89  template<data_mode dmode, typename T>
90  void outward_link(const port<dmode, output, T>& src_port,
91  const port<dmode, output, T>& dst_port);
92 
93 private:
94  node_structure& internal_structure();
95  node_interface& component_IO(const system_node& node) const;
96  discrete_event_time& event_time();
97 
98  void categorize_node_indices();
99  void process_flow_nodes(bool finalize);
100  void handle_initialization_events();
101  system_node& handle_src_event(int64 src_index);
102  void handle_dst_events(const std::set<std::pair<int64, int64>>& dsts, const pointer& val);
103  void handle_finalization_events();
104 
105  void activate_flow_inward_links();
106  void activate_flow_inner_links(int64 node_index, system_node& node);
107  void activate_flow_outward_links(int64 node_index, system_node& node);
108 
109  const std::set<std::pair<int64, int64>>& traverse_message_inward_links(int64 port_index);
110  const std::set<std::pair<int64, int64>>& traverse_message_inner_links(int64 node_index, int64 port_index);
111  const std::set<int64>& traverse_message_outward_links(int64 node_index, int64 port_index);
112 
113  void adopt_component_print_flags(const system_node& node) const;
114 
115  node_context internal_context_;
116  std::vector<int64> unprocessed_flow_node_indices_;
117  std::vector<int64> processed_flow_node_indices_;
118  std::vector<int64> uninitialized_message_node_indices_;
119  std::vector<int64> initialized_message_node_indices_;
120  time_queue t_queue_;
121  time_cache t_cache_;
122  std::map<int64, std::set<std::pair<int64, int64>>> inward_link_dsts_; // Reorders link destinations
123  std::map<std::pair<int64, int64>, std::set<std::pair<int64, int64>>> inner_link_dsts_; // Reorders link destinations
124  std::map<std::pair<int64, int64>, std::set<int64>> outward_link_dsts_; // Reorders link destinations
125  mutable data_mode composite_dmode_;
126  mutable bool composite_dmode_calculated_;
127  bool initialized_;
128  bool finalized_;
129 };
130 
131 
132 inline composite_node::composite_node(const std::string& node_name, const node_context& external_context)
133  : system_node(node_name, external_context)
134  , internal_context_(&const_cast<node_interface&>(external_interface()),
135  const_cast<node_context&>(external_context))
136  , unprocessed_flow_node_indices_()
137  , processed_flow_node_indices_()
138  , uninitialized_message_node_indices_()
139  , initialized_message_node_indices_()
140  , t_queue_()
141  , t_cache_()
142  , inward_link_dsts_()
143  , inner_link_dsts_()
144  , outward_link_dsts_()
145  , composite_dmode_(flow)
146  , composite_dmode_calculated_(false)
147  , initialized_(false)
148  , finalized_(false)
149 {
150 }
151 
152 
154 {
155  return no_scale;
156 }
157 
158 
160 {
161  return const_cast<const node_context&>(internal_context_);
162 }
163 
164 
165 template<data_mode dmode, typename T>
167  const port<dmode, input, T>& dst_port)
168 {
169  if (&src_port.external_interface() != &external_interface()) {
170  throw std::logic_error("Attempt to create inward link on composite node (" + full_name() + ") that does not originate from one of the composite node's ports");
171  }
172  if (&(const_cast<node_interface&>(dst_port.external_interface()).external_context()) != &internal_context_) {
173  throw std::logic_error("Attempt to create inward link on composite node (" + full_name() + ") that does not end up at one of the composite node's components");
174  }
175  switch (dmode) {
176  case flow:
177  internal_structure().add_flow_inward_link(src_port.port_index(),
178  dst_port.node_index(), dst_port.port_index());
179  break;
180  case message:
181  internal_structure().add_message_inward_link(src_port.port_index(),
182  dst_port.node_index(), dst_port.port_index());
183  break;
184  }
185 }
186 
187 
188 template<data_mode dmode, typename T>
190  const port<dmode, input, T>& dst_port)
191 {
192  if (&(const_cast<node_interface&>(src_port.external_interface()).external_context()) != &internal_context_) {
193  throw std::logic_error("Attempt to create inner link on composite node (" + full_name() + ") that does not originate from one of the composite node's components");
194  }
195  if (&(const_cast<node_interface&>(dst_port.external_interface()).external_context()) != &internal_context_) {
196  throw std::logic_error("Attempt to create inner link on composite node (" + full_name() + ") that does not end up at one of the composite node's components");
197  }
198  switch (dmode) {
199  case flow:
200  internal_structure().add_flow_inner_link(src_port.node_index(), src_port.port_index(),
201  dst_port.node_index(), dst_port.port_index());
202  break;
203  case message:
204  internal_structure().add_message_inner_link(src_port.node_index(), src_port.port_index(),
205  dst_port.node_index(), dst_port.port_index());
206  break;
207  }
208 }
209 
210 
211 template<data_mode dmode, typename T>
213  const port<dmode, output, T>& dst_port)
214 {
215  if (&(const_cast<node_interface&>(src_port.external_interface()).external_context()) != &internal_context_) {
216  throw std::logic_error("Attempt to create outward link on composite node (" + full_name() + ") that does not originate from one of the composite node's components");
217  }
218  if (&dst_port.external_interface() != &external_interface()) {
219  throw std::logic_error("Attempt to create outward link on composite node (" + full_name() + ") that does not end up at one of the composite node's ports");
220  }
221  switch (dmode) {
222  case flow:
223  internal_structure().add_flow_outward_link(src_port.node_index(), src_port.port_index(),
224  dst_port.port_index());
225  break;
226  case message:
227  internal_structure().add_message_outward_link(src_port.node_index(), src_port.port_index(),
228  dst_port.port_index());
229  break;
230  }
231 }
232 
233 
234 inline node_structure& composite_node::internal_structure()
235 {
236  return internal_context_.internal_structure();
237 }
238 
239 
240 inline node_interface& composite_node::component_IO(const system_node& node) const
241 {
242  return const_cast<node_interface&>(const_cast<system_node&>(node).external_interface());
243 }
244 
245 
246 inline discrete_event_time& composite_node::event_time()
247 {
249 }
250 
251 
252 } // namespace
253 } // namespace
254 
255 #endif
void add_message_outward_link(int64 src_node_index, int64 src_port_index, int64 dst_port_index)
Definition: node_structure.h:203
const std::string & node_name() const
Returns the name of the node.
Definition: system_node.h:135
int64 node_index() const
Returns the index of the node within the parent node.
Definition: system_node.h:147
A data structure which represents progress through a simulation, encapsulating both simulated time an...
Definition: discrete_event_time.h:36
discrete_event_time & event_time()
Definition: node_context.h:62
data_mode
Indicates the relevant data communication paradigm: dataflow (flow) or message-passing (message)...
Definition: data_mode.h:16
A generic port class template declaration.
Definition: port.h:53
A base class for all nodes from which systems models are constructed.
Definition: system_node.h:41
composite_node(const std::string &node_name, const node_context &external_context)
Constructs a composite_node.
Definition: composite_node.h:132
duration handle_unplanned_event(duration elapsed_dt)
Invoked whenever a message is received; calls process_unplanned_event on receiving component nodes...
Definition: composite_node.cpp:42
constexpr scale no_scale
Definition: scale.h:153
void add_flow_inner_link(int64 src_node_index, int64 src_port_index, int64 dst_node_index, int64 dst_port_index)
Definition: node_structure.h:146
A base class for all nodes defined as fixed-structure compositions of different types of nodes...
Definition: composite_node.h:23
void inner_link(const port< dmode, output, T > &src_port, const port< dmode, input, T > &dst_port)
Constructs an inner link.
Definition: composite_node.h:189
void add_message_inner_link(int64 src_node_index, int64 src_port_index, int64 dst_node_index, int64 dst_port_index)
Definition: node_structure.h:165
const std::string & full_name() const
Returns the full name of the node, including parent nodes.
Definition: system_node.h:141
A data structure which provides durations elapsed since past events.
Definition: time_cache.h:27
node_context & external_context()
Definition: node_interface.h:157
void inward_link(const port< dmode, input, T > &src_port, const port< dmode, input, T > &dst_port)
Constructs an inward link.
Definition: composite_node.h:166
void add_flow_outward_link(int64 src_node_index, int64 src_port_index, int64 dst_port_index)
Definition: node_structure.h:184
virtual ~composite_node()=default
Destructor.
duration handle_initialization_event()
Invoked at the beginning of a simulation, calls process_initialization_event on all component nodes...
Definition: composite_node.cpp:24
A data type which represents the general concept of scale as a dimensionless power of 1000...
Definition: scale.h:70
node_structure & internal_structure()
Definition: node_context.h:50
A data structure which supports the scheduling of future events.
Definition: time_queue.h:34
duration handle_planned_event(duration elapsed_dt)
Invoked when the planned duration elapses; calls process_planned_event on the source node and propaga...
Definition: composite_node.cpp:55
Definition: node_context.h:16
Definition: node_interface.h:16
void outward_link(const port< dmode, output, T > &src_port, const port< dmode, output, T > &dst_port)
Constructs an outward link.
Definition: composite_node.h:212
const auto flow
Equivalent to data_mode::flow.
Definition: data_mode.h:22
data_mode node_dmode() const
Returns flow if all component nodes have data flow elements only, and message otherwise.
Definition: composite_node.cpp:7
scale time_precision() const
Returns no_scale for all composite nodes.
Definition: composite_node.h:153
void add_flow_inward_link(int64 src_port_index, int64 dst_node_index, int64 dst_port_index)
Definition: node_structure.h:107
Indicates message-passing.
void add_message_inward_link(int64 src_port_index, int64 dst_node_index, int64 dst_port_index)
Definition: node_structure.h:126
system_node(const system_node &)=delete
No copy constructor.
const node_context & internal_context()
Returns a representation of the internal context, which is also the external context of the component...
Definition: composite_node.h:159
int64 node_index() const
Returns the index of the node within the encompassing node_structure object.
Definition: port.h:226
Definition: node_structure.h:15
void handle_finalization_event(duration elapsed_dt)
Invoked at the end of a simulation; calls process_finalization_event on all component nodes...
Definition: composite_node.cpp:80
int64 port_index() const
Returns the index of the port within the encompassing node_interface object.
Definition: port.h:219
const node_interface & external_interface() const
Returns the encomassing node_interface object.
Definition: port.h:233
node_interface & external_IO() const
Returns a non-const reference to the node's external interface.
Definition: system_node.h:288
const node_interface & external_interface() const
Returns the object responsible for exchanging information between the node and its surrounding contex...
Definition: system_node.h:153
A data type which represents a pointer to anything.
Definition: pointer.h:27