GNU Radio Manual and C++ API Reference  3.8.1.0
The Free & Open Software Radio Ecosystem
flowgraph.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2007,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef INCLUDED_GR_RUNTIME_FLOWGRAPH_H
24 #define INCLUDED_GR_RUNTIME_FLOWGRAPH_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/basic_block.h>
28 #include <gnuradio/io_signature.h>
29 #include <iostream>
30 
31 namespace gr {
32 
33 /*!
34  * \brief Class representing a specific input or output graph endpoint
35  * \ingroup internal
36  */
38 {
39 private:
40  basic_block_sptr d_basic_block;
41  int d_port;
42 
43 public:
44  endpoint() : d_basic_block(), d_port(0) {}
45  endpoint(basic_block_sptr block, int port)
46  {
47  d_basic_block = block;
48  d_port = port;
49  }
50  basic_block_sptr block() const { return d_basic_block; }
51  int port() const { return d_port; }
52  std::string identifier() const
53  {
54  return d_basic_block->alias() + ":" + std::to_string(d_port);
55  };
56 
57  bool operator==(const endpoint& other) const;
58 };
59 
60 inline bool endpoint::operator==(const endpoint& other) const
61 {
62  return (d_basic_block == other.d_basic_block && d_port == other.d_port);
63 }
64 
66 {
67 private:
68  basic_block_sptr d_basic_block;
69  pmt::pmt_t d_port;
70  bool d_is_hier;
71 
72 public:
73  msg_endpoint() : d_basic_block(), d_port(pmt::PMT_NIL) {}
74  msg_endpoint(basic_block_sptr block, pmt::pmt_t port, bool is_hier = false)
75  {
76  d_basic_block = block;
77  d_port = port;
78  d_is_hier = is_hier;
79  }
80  basic_block_sptr block() const { return d_basic_block; }
81  pmt::pmt_t port() const { return d_port; }
82  bool is_hier() const { return d_is_hier; }
83  void set_hier(bool h) { d_is_hier = h; }
84  std::string identifier() const
85  {
86  return d_basic_block->alias() + ":" + pmt::symbol_to_string(d_port);
87  }
88 
89  bool operator==(const msg_endpoint& other) const;
90 };
91 
92 inline bool msg_endpoint::operator==(const msg_endpoint& other) const
93 {
94  return (d_basic_block == other.d_basic_block && pmt::equal(d_port, other.d_port));
95 }
96 
97 // Hold vectors of gr::endpoint objects
98 typedef std::vector<endpoint> endpoint_vector_t;
99 typedef std::vector<endpoint>::iterator endpoint_viter_t;
100 
101 /*!
102  *\brief Class representing a connection between to graph endpoints
103  */
105 {
106 public:
107  edge() : d_src(), d_dst(){};
108  edge(const endpoint& src, const endpoint& dst) : d_src(src), d_dst(dst) {}
109  ~edge();
110 
111  const endpoint& src() const { return d_src; }
112  const endpoint& dst() const { return d_dst; }
113  std::string identifier() const
114  {
115  return d_src.identifier() + "->" + d_dst.identifier();
116  }
117 
118 private:
119  endpoint d_src;
120  endpoint d_dst;
121 };
122 
123 // Hold vectors of gr::edge objects
124 typedef std::vector<edge> edge_vector_t;
125 typedef std::vector<edge>::iterator edge_viter_t;
126 
127 
128 /*!
129  *\brief Class representing a msg connection between to graph msg endpoints
130  */
132 {
133 public:
134  msg_edge() : d_src(), d_dst(){};
135  msg_edge(const msg_endpoint& src, const msg_endpoint& dst) : d_src(src), d_dst(dst) {}
137 
138  const msg_endpoint& src() const { return d_src; }
139  const msg_endpoint& dst() const { return d_dst; }
140  std::string identifier() const
141  {
142  return d_src.identifier() + "->" + d_dst.identifier();
143  }
144 
145 private:
146  msg_endpoint d_src;
147  msg_endpoint d_dst;
148 };
149 
150 // Hold vectors of gr::msg_edge objects
151 typedef std::vector<msg_edge> msg_edge_vector_t;
152 typedef std::vector<msg_edge>::iterator msg_edge_viter_t;
153 
154 // Create a shared pointer to a heap allocated flowgraph
155 // (types defined in runtime_types.h)
156 GR_RUNTIME_API flowgraph_sptr make_flowgraph();
157 
158 /*!
159  * \brief Class representing a directed, acyclic graph of basic blocks
160  * \ingroup internal
161  */
163 {
164 public:
165  friend GR_RUNTIME_API flowgraph_sptr make_flowgraph();
166 
167  /*!
168  * \brief Destruct an arbitrary flowgraph
169  */
170  virtual ~flowgraph();
171 
172  /*!
173  * \brief Connect two endpoints
174  * \details
175  * Checks the validity of both endpoints, and whether the
176  * destination is unused so far, then adds the edge to the internal list of
177  * edges.
178  */
179  void connect(const endpoint& src, const endpoint& dst);
180 
181  /*!
182  * \brief Disconnect two endpoints
183  */
184  void disconnect(const endpoint& src, const endpoint& dst);
185 
186  /*!
187  * \brief convenience wrapper; used to connect two endpoints
188  */
189  void connect(basic_block_sptr src_block,
190  int src_port,
191  basic_block_sptr dst_block,
192  int dst_port);
193 
194  /*!
195  * \brief convenience wrapper; used to disconnect two endpoints
196  */
197  void disconnect(basic_block_sptr src_block,
198  int src_port,
199  basic_block_sptr dst_block,
200  int dst_port);
201 
202  /*!
203  * \brief Connect two message endpoints
204  * \details
205  * Checks the validity of both endpoints, then adds the edge to the
206  * internal list of edges.
207  */
208  void connect(const msg_endpoint& src, const msg_endpoint& dst);
209 
210  /*!
211  * \brief Disconnect two message endpoints
212  */
213  void disconnect(const msg_endpoint& src, const msg_endpoint& dst);
214 
215  /*!
216  * \brief Validate flow graph
217  * \details
218  * Gathers all used blocks, checks the contiguity of all connected in- and
219  * outputs, and calls the check_topology method of each block.
220  */
221  void validate();
222 
223  /*!
224  * \brief Clear existing flowgraph
225  */
226  void clear();
227 
228  /*!
229  * \brief Get vector of edges
230  */
231  const edge_vector_t& edges() const { return d_edges; }
232 
233  /*!
234  * \brief Get vector of message edges
235  */
236  const msg_edge_vector_t& msg_edges() const { return d_msg_edges; }
237 
238  /*!
239  * \brief calculates all used blocks in a flow graph
240  * \details
241  * Iterates over all message edges and stream edges, noting both endpoints in a
242  * vector.
243  *
244  * \return a unique vector of used blocks
245  */
246  basic_block_vector_t calc_used_blocks();
247 
248  /*!
249  * \brief topologically sort blocks
250  * \details
251  * Uses depth-first search to return a sorted vector of blocks
252  *
253  * \return toplogically sorted vector of blocks. All the sources come first.
254  */
255  basic_block_vector_t topological_sort(basic_block_vector_t& blocks);
256 
257  /*!
258  * \brief Calculate vector of disjoint graph partions
259  * \return vector of disjoint vectors of topologically sorted blocks
260  */
261  std::vector<basic_block_vector_t> partition();
262 
263 protected:
265  edge_vector_t d_edges;
266  msg_edge_vector_t d_msg_edges;
267 
268  flowgraph();
269  std::vector<int> calc_used_ports(basic_block_sptr block, bool check_inputs);
270  basic_block_vector_t calc_downstream_blocks(basic_block_sptr block, int port);
271  edge_vector_t calc_upstream_edges(basic_block_sptr block);
272  bool has_block_p(basic_block_sptr block);
273  edge calc_upstream_edge(basic_block_sptr block, int port);
274 
275 private:
276  void check_valid_port(gr::io_signature::sptr sig, int port);
277  void check_valid_port(const msg_endpoint& e);
278  void check_dst_not_used(const endpoint& dst);
279  void check_type_match(const endpoint& src, const endpoint& dst);
280  edge_vector_t calc_connections(basic_block_sptr block,
281  bool check_inputs); // false=use outputs
282  void check_contiguity(basic_block_sptr block,
283  const std::vector<int>& used_ports,
284  bool check_inputs);
285 
286  basic_block_vector_t calc_downstream_blocks(basic_block_sptr block);
287  basic_block_vector_t calc_reachable_blocks(basic_block_sptr block,
288  basic_block_vector_t& blocks);
289  void reachable_dfs_visit(basic_block_sptr block, basic_block_vector_t& blocks);
290  basic_block_vector_t calc_adjacent_blocks(basic_block_sptr block,
291  basic_block_vector_t& blocks);
292  basic_block_vector_t sort_sources_first(basic_block_vector_t& blocks);
293  bool source_p(basic_block_sptr block);
294  void topological_dfs_visit(basic_block_sptr block, basic_block_vector_t& output);
295 };
296 
297 // Convenience functions
298 inline void flowgraph::connect(basic_block_sptr src_block,
299  int src_port,
300  basic_block_sptr dst_block,
301  int dst_port)
302 {
303  connect(endpoint(src_block, src_port), endpoint(dst_block, dst_port));
304 }
305 
306 inline void flowgraph::disconnect(basic_block_sptr src_block,
307  int src_port,
308  basic_block_sptr dst_block,
309  int dst_port)
310 {
311  disconnect(endpoint(src_block, src_port), endpoint(dst_block, dst_port));
312 }
313 
314 inline std::ostream& operator<<(std::ostream& os, const endpoint endp)
315 {
316  os << endp.identifier();
317  return os;
318 }
319 
320 inline std::ostream& operator<<(std::ostream& os, const edge edge)
321 {
322  os << edge.identifier();
323  return os;
324 }
325 
326 inline std::ostream& operator<<(std::ostream& os, const msg_endpoint endp)
327 {
328  os << endp.identifier();
329  return os;
330 }
331 
332 inline std::ostream& operator<<(std::ostream& os, const msg_edge edge)
333 {
334  os << edge.identifier();
335  return os;
336 }
337 
338 std::string dot_graph_fg(flowgraph_sptr fg);
339 
340 } /* namespace gr */
341 
342 #endif /* INCLUDED_GR_RUNTIME_FLOWGRAPH_H */
const msg_edge_vector_t & msg_edges() const
Get vector of message edges.
Definition: flowgraph.h:236
int port() const
Definition: flowgraph.h:51
endpoint()
Definition: flowgraph.h:44
boost::shared_ptr< io_signature > sptr
Definition: io_signature.h:46
bool operator==(const endpoint &other) const
Definition: flowgraph.h:60
std::string dot_graph_fg(flowgraph_sptr fg)
std::vector< msg_edge >::iterator msg_edge_viter_t
Definition: flowgraph.h:152
boost::shared_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting). See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
Definition: pmt.h:96
const edge_vector_t & edges() const
Get vector of edges.
Definition: flowgraph.h:231
basic_block_sptr block() const
Definition: flowgraph.h:80
std::string identifier() const
Definition: flowgraph.h:140
~msg_edge()
Definition: flowgraph.h:136
msg_edge_vector_t d_msg_edges
Definition: flowgraph.h:266
msg_endpoint()
Definition: flowgraph.h:73
const endpoint & src() const
Definition: flowgraph.h:111
const endpoint & dst() const
Definition: flowgraph.h:112
basic_block_sptr block() const
Definition: flowgraph.h:50
Class representing a directed, acyclic graph of basic blocks.
Definition: flowgraph.h:162
std::string identifier() const
Definition: flowgraph.h:113
PMT_API const std::string symbol_to_string(const pmt_t &sym)
std::vector< endpoint > endpoint_vector_t
Definition: flowgraph.h:98
void connect(const endpoint &src, const endpoint &dst)
Connect two endpoints.
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
edge()
Definition: flowgraph.h:107
basic_block_vector_t d_blocks
Definition: flowgraph.h:264
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:43
PMT_API bool equal(const pmt_t &x, const pmt_t &y)
pmt::pmt_t port() const
Definition: flowgraph.h:81
std::string identifier() const
Definition: flowgraph.h:84
edge_vector_t d_edges
Definition: flowgraph.h:265
bool is_hier() const
Definition: flowgraph.h:82
#define PMT_NIL
Definition: pmt.h:134
std::vector< endpoint >::iterator endpoint_viter_t
Definition: flowgraph.h:99
Class representing a specific input or output graph endpoint.
Definition: flowgraph.h:37
edge(const endpoint &src, const endpoint &dst)
Definition: flowgraph.h:108
Class representing a connection between to graph endpoints.
Definition: flowgraph.h:104
endpoint(basic_block_sptr block, int port)
Definition: flowgraph.h:45
std::vector< basic_block_sptr > basic_block_vector_t
Definition: basic_block.h:411
std::ostream & operator<<(std::ostream &os, basic_block_sptr basic_block)
Definition: basic_block.h:416
std::vector< edge >::iterator edge_viter_t
Definition: flowgraph.h:125
msg_endpoint(basic_block_sptr block, pmt::pmt_t port, bool is_hier=false)
Definition: flowgraph.h:74
const msg_endpoint & dst() const
Definition: flowgraph.h:139
msg_edge(const msg_endpoint &src, const msg_endpoint &dst)
Definition: flowgraph.h:135
std::vector< edge > edge_vector_t
Definition: flowgraph.h:124
Definition: flowgraph.h:65
void set_hier(bool h)
Definition: flowgraph.h:83
std::vector< msg_edge > msg_edge_vector_t
Definition: flowgraph.h:151
msg_edge()
Definition: flowgraph.h:134
GR_RUNTIME_API flowgraph_sptr make_flowgraph()
bool operator==(const msg_endpoint &other) const
Definition: flowgraph.h:92
The abstract base class for all &#39;terminal&#39; processing blocks.A signal processing flow is constructed ...
Definition: block.h:71
void disconnect(const endpoint &src, const endpoint &dst)
Disconnect two endpoints.
Class representing a msg connection between to graph msg endpoints.
Definition: flowgraph.h:131
Definition: pmt.h:51
std::string identifier() const
Definition: flowgraph.h:52
const msg_endpoint & src() const
Definition: flowgraph.h:138