added rewrite methods to element to separate from validation logic
authorJosh Blum <josh@joshknows.com>
Wed, 26 Aug 2009 20:29:28 +0000 (13:29 -0700)
committerJosh Blum <josh@joshknows.com>
Wed, 26 Aug 2009 20:29:28 +0000 (13:29 -0700)
grc/base/Block.py
grc/base/Element.py
grc/base/FlowGraph.py
grc/gui/FlowGraph.py
grc/python/Block.py
grc/python/Port.py

index 491c594cc8bf71aeb31c1fbf3593cccd00a6671d..d2266e7836524d05ff1650d19bd4b7ce24f1823a 100644 (file)
@@ -140,6 +140,14 @@ class Block(Element):
                """
                self.get_param('_enabled').set_value(str(enabled))
 
+       def rewrite(self):
+               """
+               Rewrite critical structures.
+               Call rewrite on all sub elements.
+               """
+               Element.rewrite(self)
+               for elem in self.get_ports() + self.get_params(): elem.rewrite()
+
        def validate(self):
                """
                Validate the block.
index 16000c46c0a87068491da35f6228c58577c3be37..87cedb37f71ae40f15dc39064a0e8c28685da81d 100644 (file)
@@ -1,5 +1,5 @@
 """
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -38,6 +38,8 @@ class Element(object):
        def add_error_message(self, msg): self._error_messages.append(msg)
        def get_error_messages(self): return self._error_messages
 
+       def rewrite(self): pass
+
        def get_enabled(self): return True
 
        def get_parent(self): return self._parent
index fe493cce1f39a2918e1e150270fe3a26cb523c1e..990baf02935b99b68ea50b99f51169c0c3aa8d79 100644 (file)
@@ -147,6 +147,14 @@ class FlowGraph(Element):
                """
                raise NotImplementedError
 
+       def rewrite(self):
+               """
+               Rewrite critical structures.
+               Call rewrite on all sub elements.
+               """
+               Element.rewrite(self)
+               for elem in self.get_elements(): elem.rewrite()
+
        def validate(self):
                """
                Validate the flow graph.
@@ -198,7 +206,7 @@ class FlowGraph(Element):
                        #only load the block when the block key was valid
                        if block: block.import_data(block_n)
                        else: Messages.send_error_load('Block key "%s" not found in %s'%(key, self.get_parent()))
-               self.validate() #validate all blocks before connections are made (in case of nports)
+               self.rewrite() #rewrite all blocks before connections are made (ex: nports)
                #build the connections
                for connection_n in connections_n:
                        #try to make the connection
@@ -225,3 +233,4 @@ class FlowGraph(Element):
                                #build the connection
                                self.connect(source, sink)
                        except AssertionError: Messages.send_error_load('Connection between %s(%s) and %s(%s) could not be made.'%(source_block_id, source_key, sink_block_id, sink_key))
+               self.rewrite() #global rewrite
index 007bb622c5f04e71a94b2ca1ebf33553676f640e..5e645be7274f8e56112915312b14d4755c1a9361 100644 (file)
@@ -291,12 +291,10 @@ class FlowGraph(Element):
 
        def update(self):
                """
+               Do a global rewrite and validate.
                Call update on all elements.
-               Validate twice:
-               1) elements call special rewrite rules that may break validation
-               2) elements should come up with the same results, validation can pass
                """
-               self.validate()
+               self.rewrite()
                self.validate()
                for element in self.get_elements(): element.update()
 
index 47fe13a3cc11ee6c87d81d605e5d2e7317eda4c2..f47f764466d8a191f9a251a12b64c10ee5cf6b70 100644 (file)
@@ -54,7 +54,6 @@ class Block(_Block):
                Validate this block.
                Call the base class validate.
                Evaluate the checks: each check must evaluate to True.
-               Adjust the nports.
                """
                _Block.validate(self)
                #evaluate the checks
@@ -65,6 +64,12 @@ class Block(_Block):
                                try: assert check_eval
                                except AssertionError: self.add_error_message('Check "%s" failed.'%check)
                        except: self.add_error_message('Check "%s" did not evaluate.'%check)
+
+       def rewrite(self):
+               """
+               Add and remove ports to adjust for the nports.
+               """
+               _Block.rewrite(self)
                #adjust nports
                for get_ports, get_port in (
                        (self.get_sources, self.get_source),
index bfbac72374f11eac6a51576ede21654210939be1..dde736dbaae4e720bfa9d7dc9d52cfe446c0e2e2 100644 (file)
@@ -57,17 +57,19 @@ class Port(_Port):
                except AssertionError: self.add_error_message('Port is not connected.')
                try: assert self.is_source() or len(self.get_enabled_connections()) <= 1
                except AssertionError: self.add_error_message('Port has too many connections.')
-               ################################################################
-               # message port logic
-               ################################################################
+               #message port logic
                if self.get_type() == 'msg':
                        try: assert not self.get_nports()
                        except AssertionError: self.add_error_message('A port of type "msg" cannot have "nports" set.')
                        try: assert self.get_vlen() == 1
                        except AssertionError: self.add_error_message('A port of type "msg" must have a "vlen" of 1.')
-               ################################################################
-               # virtual sink logic
-               ################################################################
+
+       def rewrite(self):
+               """
+               Handle the port cloning for virtual blocks.
+               """
+               _Port.rewrite(self)
+               #virtual sink logic
                if self.get_parent().get_key() == 'virtual_sink':
                        if self.get_enabled_connections(): #clone type and vlen
                                source = self.get_enabled_connections()[0].get_source()