From dbf58fa349002d94c26883b130ed8864ead79ea6 Mon Sep 17 00:00:00 2001
From: Seth Hitefield <sdhitefield@gmail.com>
Date: Tue, 1 Aug 2017 16:52:59 -0400
Subject: grc: Bug fixes for flowgraph complexity

---
 grc/core/utils/flow_graph_complexity.py | 97 +++++++++++++++++----------------
 1 file changed, 50 insertions(+), 47 deletions(-)

(limited to 'grc/core/utils/flow_graph_complexity.py')

diff --git a/grc/core/utils/flow_graph_complexity.py b/grc/core/utils/flow_graph_complexity.py
index 15ac131d9b..e8962b0ae3 100644
--- a/grc/core/utils/flow_graph_complexity.py
+++ b/grc/core/utils/flow_graph_complexity.py
@@ -1,51 +1,54 @@
 
 def calculate(flowgraph):
     """ Determines the complexity of a flowgraph """
-    return "  *** DISABLED ***  "  # TODO: Temporarily disabled.
-    dbal = 0
-    for block in flowgraph.blocks:
-        # Skip options block
-        if block.key == 'options':
-            continue
-
-        # Don't worry about optional sinks?
-        sink_list = [c for c in block.sinks if not c.optional]
-        source_list = [c for c in block.sources if not c.optional]
-        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
+
+    try:
+        dbal = 0.0
+        for block in flowgraph.blocks:
+            if block.key == "options":
+                continue
+
+            # Determine the base value for this block
+            sinks = sum(1.0 for port in block.sinks if not port.optional)
+            sources = sum(1.0 for port in block.sources if not port.optional)
+            base = max(min(sinks, sources), 1)
+
+            # Determine the port multiplier
+            block_connections = 0.0
+            for port in block.sources:
+                block_connections += sum(1.0 for c in port.connections())
+            source_multi = max(block_connections / max(sources, 1.0), 1.0)
+
+            # Port ratio multiplier
+            multi = 1.0
+            if min(sinks, sources) > 0:
+                multi = float(sinks / sources)
+                multi = float(1 / multi) if multi > 1 else multi
+
+            dbal += base * multi * source_multi
+
+        blocks = float(len(flowgraph.blocks) - 1)
+        connections = float(len(flowgraph.connections))
+        variables = float(len(flowgraph.get_variables()))
+
+        enabled = float(len(flowgraph.get_enabled_blocks()))
+        enabled_connections = float(len(flowgraph.get_enabled_connections()))
+        disabled_connections = connections - enabled_connections
+
+        # 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:
-            multi = 1
-
-        # Connection ratio multiplier
-        sink_multi = max(float(sum(len(c.connections()) for c in sink_list) / max(sinks, 1.0)), 1.0)
-        source_multi = max(float(sum(len(c.connections()) for c in source_list) / max(sources, 1.0)), 1.0)
-        dbal += base * multi * sink_multi * source_multi
-
-    blocks = float(len(flowgraph.blocks))
-    connections = float(len(flowgraph.connections))
-    elements = blocks + connections
-    disabled_connections = sum(not c.enabled for c in flowgraph.connections)
-
-    variables = elements - blocks - connections
-    enabled = float(len(flowgraph.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
+            conn_multi = 1
+
+        final = round(max((dbal - 1) * disabled_multi * conn_multi * connections, 0.0) / 1000000, 6)
+        return final
+
+    except Exception:
+        return "<Error>"
-- 
cgit v1.2.3