Example #1
0
class OverlapTests(unittest.TestCase):
    def setUp(self):
        class FakeGui:
            def stateofthenation(self,
                                 recalibrate=False,
                                 auto_resize_canvas=True):
                pass

        self.g = Graph()
        self.overlap_remover = OverlapRemoval(self.g, margin=5, gui=FakeGui())

    def tearDown(self):
        pass

    def testStress1(self):

        for i in range(10):
            self.g.LoadGraphFromStrings(TEST_GRAPH5_STRESS)
            print i,
            were_all_overlaps_removed = self.overlap_remover.RemoveOverlaps()
            self.assertTrue(were_all_overlaps_removed)

            self.g.Clear()

    def testStress2_InitialBoot(self):
        """
        This is the slowest stress test because it runs the spring layout several times.
        """

        from layout.layout_spring import GraphLayoutSpring
        from layout.coordinate_mapper import CoordinateMapper

        self.g.LoadGraphFromStrings(
            GRAPH_INITIALBOOT)  # load the scenario ourselves

        layouter = GraphLayoutSpring(self.g)
        coordmapper = CoordinateMapper(self.g, (800, 800))

        def AllToLayoutCoords():
            coordmapper.AllToLayoutCoords()

        def AllToWorldCoords():
            coordmapper.AllToWorldCoords()

        for i in range(8):
            print i,

            AllToLayoutCoords()
            layouter.layout(keep_current_positions=False)
            AllToWorldCoords()

            were_all_overlaps_removed = self.overlap_remover.RemoveOverlaps()
            self.assertTrue(were_all_overlaps_removed)
Example #2
0
    def test_1_Basics(self):
        g = Graph()

        n1 = GraphNode('A', 0, 0, 200, 200)
        n2 = GraphNode('B', 0, 0, 200, 200)
        g.AddEdge(n1, n2)

        #for node in g.nodes:
        #    print node, "layout info:", (node.layoutPosX, node.layoutPosY)

        #print g.GraphToString().strip()

        assert len(g.nodes) == 2
        assert len(g.nodeSet.keys()) == 2
        assert len(g.edges) == 1
        g.DeleteNodeById('B')
        assert len(g.nodes) == 1
        assert len(g.nodeSet.keys()) == 1
        assert len(g.edges) == 0

        # Old persistence format - very simple, I call this 0.9 format.
        filedata = """
{'type':'node', 'id':'c', 'x':230, 'y':174, 'width':60, 'height':120}
{'type':'node', 'id':'c1', 'x':130, 'y':174, 'width':60, 'height':120}
{'type':'edge', 'id':'c_to_c1', 'source':'c', 'target':'c1'}
    """
        g.Clear()
        assert len(g.nodes) == 0
        assert g.GraphToString().strip() == ""

        g.LoadGraphFromStrings(filedata)
        #for node in g.nodes:
        #    print node, "layout info:", (node.layoutPosX, node.layoutPosY)
        #assert g.GraphToString().strip() == filedata.strip(), g.GraphToString().strip() # no longer true since upgrades to persistence format will translate the incoming text

        # Line intersection tests

        res = FindLineIntersection((0, 0), (200, 200), (10, 10), (10, 50))
        assert res == [10, 10]

        res = FindLineIntersection((0, 30), (200, 30), (10, 10), (10, 50))
        assert res == [10, 30]

        node = GraphNode("A", 10, 10, 30, 40)
        assert len(node.lines) == 4
        assert (10, 10) in node.lines[0]
        assert (40, 10) in node.lines[0]
        assert (40, 10) in node.lines[1]
        assert (40, 50) in node.lines[1]
        assert (40, 50) in node.lines[2]
        assert (10, 50) in node.lines[2]
        assert (10, 50) in node.lines[3]
        assert (10, 10) in node.lines[3]

        res = node.CalcLineIntersectionPoints((0, 0), (200, 200))
        assert len(res) == 2
        assert (10, 10) in res
        assert (40, 40) in res

        res = node.CalcLineIntersectionPoints((20, 0), (20, 1000))
        assert len(res) == 2
        assert (20, 10) in res
        assert (20, 50) in res