summaryrefslogtreecommitdiff
path: root/grc/core/utils/_complexity.py
blob: c0f3ae9de47cb4dd0e001c5f75a0110cf9362510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

def calculate_flowgraph_complexity(flowgraph):
    """ Determines the complexity of a flowgraph """
    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.get_optional()]
        source_list = [c for c in block.sources if not c.get_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
        else:
            multi = 1

        # Connection ratio multiplier
        sink_multi = max(float(sum(len(c.get_connections()) for c in sink_list) / max(sinks, 1.0)), 1.0)
        source_multi = max(float(sum(len(c.get_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