Source

grrdrr / example-polarity.py

Full commit

from model import WithGraphStyle
from database import build_schema
from draw import measure, draw, draw_smooth
from algorithm import rank, rank_greedy, layer, order, place, polarize, place_polarized
import sys

if len(sys.argv) != 2:
    print "Usage: %s <db>" % sys.argv[0]
    sys.exit(1)
db = sys.argv[1]

example = build_schema(db)
example.upgrade(WithGraphStyle)
example.freeze()

measure(example)
rank_greedy(example)
polarize(example)
place_polarized(example)

example = example.clone()

print
print repr(example)

print
for node in example.nodes:
    print repr(node)

print
for arc in example.arcs:
    print repr(arc), arc.weight.value

print
for node in example.nodes_by_rank:
    print node, node.rank.value

print
for arc in example.arcs:
    print repr(arc), arc.polarity.value

print
total = 0.0
for node1 in example.nodes:
    for node2 in example.nodes:
        if node1.rank.value >= node2.rank.value:
            continue
        for arc1 in node1.outgoing:
            if arc1.polarity.value == 0:
                continue
            t1 = min(arc1.origin.rank.value, arc1.target.rank.value)
            b1 = max(arc1.origin.rank.value, arc1.target.rank.value)
            for arc2 in node2.outgoing:
                if arc2.polarity.value != arc1.polarity.value:
                    continue
                t2 = min(arc2.origin.rank.value, arc2.target.rank.value)
                b2 = max(arc2.origin.rank.value, arc2.target.rank.value)
                if t1 < t2 < b1 < b2 or t2 < t1 < b2 < b1:
                    total += arc1.weight.value+arc2.weight.value
print "CROSSINGS:", total

stream = open("example-polarity.png", "wb")
draw_smooth(example, stream)