From aaf5aa760a6876d7f5b878c563e002da19157cdb Mon Sep 17 00:00:00 2001
From: Seth Hitefield <sdhitefield@gmail.com>
Date: Fri, 28 Aug 2015 13:55:29 -0400
Subject: grc: Flowgraph complexity. Shows under options block when enabled.

---
 grc/base/FlowGraph.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)

(limited to 'grc/base/FlowGraph.py')

diff --git a/grc/base/FlowGraph.py b/grc/base/FlowGraph.py
index 217c4d7302..f61542c712 100644
--- a/grc/base/FlowGraph.py
+++ b/grc/base/FlowGraph.py
@@ -67,6 +67,58 @@ class FlowGraph(Element):
     def __str__(self):
         return 'FlowGraph - %s(%s)' % (self.get_option('title'), self.get_option('id'))
 
+    def get_complexity(self):
+        """
+        Determines the complexity of a flowgraph
+        """
+        dbal = 0
+        block_list = self.get_blocks()
+        for block in block_list:
+            # Skip options block
+            if block.get_key() == 'options':
+                continue
+
+            # Don't worry about optional sinks?
+            sink_list = filter(lambda c: not c.get_optional(), block.get_sinks())
+            source_list = filter(lambda c: not c.get_optional(), block.get_sources())
+            sinks = float(len(sink_list))
+            sources = float(len(source_list))
+            base = max(min(sinks, sources), 1)
+
+            # Port ratio multiplier
+            if min(sinks, sources) > 0:
+                multi = sinks / sources
+                multi = (1 / multi) if multi > 1 else multi
+            else:
+                multi = 1
+
+            # Connection ratio multiplier
+            sink_multi = max(float(sum(map(lambda c: len(c.get_connections()), sink_list)) / max(sinks, 1.0)), 1.0)
+            source_multi = max(float(sum(map(lambda c: len(c.get_connections()), source_list)) / max(sources, 1.0)), 1.0)
+            dbal = dbal + (base * multi * sink_multi * source_multi)
+
+        elements = float(len(self.get_elements()))
+        connections = float(len(self.get_connections()))
+        disabled_connections = len(filter(lambda c: not c.get_enabled(), self.get_connections()))
+        blocks = float(len(block_list))
+        variables = elements - blocks - connections
+        enabled = float(len(self.get_enabled_blocks()))
+
+        # Disabled multiplier
+        if enabled > 0:
+            disabled_multi = 1 / (max(1 - ((blocks - enabled) / max(blocks, 1)), 0.05))
+        else:
+            disabled_multi = 1
+
+        # Connection multiplier (How many connections )
+        if (connections - disabled_connections) > 0:
+            conn_multi = 1 / (max(1 - (disabled_connections / max(connections, 1)), 0.05))
+        else:
+            conn_multi = 1
+
+        final = round(max((dbal - 1) * disabled_multi * conn_multi * connections, 0.0) / 1000000, 6)
+        return final
+
     def rewrite(self):
         def refactor_bus_structure():
 
@@ -102,7 +154,7 @@ class FlowGraph(Element):
                                 get_p().append(port);
 
         for child in self.get_children(): child.rewrite()
-        refactor_bus_structure();
+        refactor_bus_structure()
 
     def get_option(self, key):
         """
-- 
cgit v1.2.3