2 Copyright 2008 Free Software Foundation, Inc.
3 This file is part of GNU Radio
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 ##@package grc_gnuradio.FlowGraph
20 #Primative flow graph.
22 from utils import expr_utils
23 from grc.elements.FlowGraph import FlowGraph as _FlowGraph
24 from Block import Block
25 from Connection import Connection
27 class FlowGraph(_FlowGraph):
29 def _get_io_signature(self, pad_key):
31 Get an io signature for this flow graph.
32 The pad key determines the directionality of the io signature.
33 @param pad_key a string of pad_source or pad_sink
34 @return a dict with: type, nports, vlen, size
36 pads = filter(lambda b: b.get_key() == pad_key, self.get_enabled_blocks())
43 pad = pads[0] #take only the first, user should not have more than 1
46 'nports': str(pad.get_param('nports').evaluate()),
47 'type': str(pad.get_param('type').evaluate()),
48 'vlen': str(pad.get_param('vlen').evaluate()),
49 'size': pad.get_param('type').get_opt('size'),
52 def get_input_signature(self):
54 Get the io signature for the input side of this flow graph.
55 The io signature with be "0", "0" if no pad source is present.
56 @return a string tuple of type, num_ports, port_size
58 return self._get_io_signature('pad_source')
60 def get_output_signature(self):
62 Get the io signature for the output side of this flow graph.
63 The io signature with be "0", "0" if no pad sink is present.
64 @return a string tuple of type, num_ports, port_size
66 return self._get_io_signature('pad_sink')
68 def get_imports(self):
70 Get a set of all import statments in this flow graph namespace.
71 @return a set of import statements
73 imports = sum([block.get_imports() for block in self.get_enabled_blocks()], [])
74 imports = sorted(set(imports))
77 def get_variables(self):
79 Get a list of all variables in this flow graph namespace.
80 Exclude paramterized variables.
81 @return a sorted list of variable blocks in order of dependency (indep -> dep)
83 variables = filter(lambda b: b.get_key() in (
84 'variable', 'variable_slider', 'variable_chooser', 'variable_text_box'
85 ), self.get_enabled_blocks())
86 #map var id to variable block
87 id2var = dict([(var.get_id(), var) for var in variables])
88 #map var id to variable code
89 #variable code is a concatenation of all param code (without the id param)
90 id2expr = dict([(var.get_id(),
91 ' '.join([param.to_code() for param in filter(lambda p: p.get_key() != 'id',var.get_params())])
92 ) for var in variables])
93 #sort according to dependency
94 sorted_ids = expr_utils.sort_variables(id2expr)
95 #create list of sorted variable blocks
96 variables = [id2var[id] for id in sorted_ids]
99 def get_parameters(self):
101 Get a list of all paramterized variables in this flow graph namespace.
102 @return a list of paramterized variables
104 parameters = filter(lambda b: b.get_key() == 'parameter', self.get_enabled_blocks())
107 def evaluate(self, expr):
109 Evaluate the expression.
110 @param expr the string expression
111 @throw Exception bad expression
112 @return the evaluated data
114 if self.is_flagged():
119 for imp in self.get_imports():
124 for parameter in self.get_parameters():
126 e = eval(parameter.get_param('value').to_code(), n, n)
127 np[parameter.get_id()] = e
129 n.update(np) #merge param namespace
131 for variable in self.get_variables():
133 if variable.get_key() == 'variable_chooser':
134 choices = variable.get_param('choices').to_code()
135 value_index = variable.get_param('value_index').to_code()
136 e = eval("%s[%s]"%(choices, value_index), n, n)
138 e = eval(variable.get_param('value').to_code(), n, n)
139 n[variable.get_id()] = e
141 #make namespace public
144 e = eval(expr, self.n, self.n)