Beispiel #1
0
 def test_get_edge_by_id(self):
     fnode = Node('node_1')  # From Node
     tnode = Node('node_2')  # To Node
     edge = Edge('id_test', fnode, tnode)
     self.graph.nodes.add(fnode)
     self.graph.nodes.add(tnode)
     self.graph.edges.add(edge)
     self.assertEqual(self.graph.edges['id_test'], edge)
Beispiel #2
0
    def test_create_edge(self):
        # Test values
        fnode = Node('node_1')  # From Node
        tnode = Node('node_2')  # To Node

        self.graph.create_edge(fnode, tnode, id='3')
        self.assertEqual(1, len(self.graph.edges))
        self.assertEqual(self.graph.edges['3'].from_node, fnode)
        self.assertEqual(self.graph.edges['3'].to_node, tnode)
Beispiel #3
0
    def test_create_edge(self):
        # Test values
        fnode = Node('node_1') # From Node
        tnode = Node('node_2') # To Node

        self.graph.create_edge(fnode, tnode, id='3')
        assert(1 == len(self.graph.edges))
        assert(self.graph.edges['3'].from_node == fnode)
        assert(self.graph.edges['3'].to_node == tnode)
Beispiel #4
0
 def test_get_edge_by_nodes(self):
     fnode = Node('node_1')  # From Node
     tnode = Node('node_2')  # To Node
     edge = Edge('id_test', fnode, tnode)
     self.graph.nodes.add(fnode)
     self.graph.nodes.add(tnode)
     self.graph.edges.add(edge)
     self.assertEqual(self.graph.find_edge(fnode, tnode), edge)
     self.assertEqual(self.graph.find_edge(fnode.id, tnode.id), edge)
Beispiel #5
0
    def test_add_edge(self):
        # Test values
        fnode = Node('node_1')  # From Node
        tnode = Node('node_2')  # To Node

        edge = Edge('id_test', fnode, tnode)
        self.graph.nodes.add(fnode)
        self.graph.nodes.add(tnode)
        self.graph.edges.add(edge)
        self.assertListEqual(list(self.graph.edges), [edge])
Beispiel #6
0
    def test_renderer(self):
        filename = os.path.dirname(__file__) + '/sample_files/rend-file.xml'

        comparation_filename = os.path.dirname(__file__) + \
                               '/sample_files/expected-rend-file.xml'

        from_node = Node('node_zero')
        node = Node('node_one')

        anchors = ('1', '2')
        region = Region('region_one', *anchors)

        node.add_region(region)

        features = {'name': 'feature_name',
                    'value': 'feature_value'}

        annotation = Annotation('Annotation_label', features, 'annotation-1')

        node.annotations.add(annotation)

        edge = Edge('edge1', from_node, node)

        self.graph.edges.add(edge)
        self.graph.nodes.add(node)
        self.graph.regions.add(region)

        graf_render = GrafRenderer(filename)
        graf_render.render(self.graph)

        expected_tree = ElementTree.parse(comparation_filename)
        result_tree = ElementTree.parse(filename)

        expected_result = [ElementTree.tostring(i) for i in
                           expected_tree.getroot()]
        result = [ElementTree.tostring(i) for i in
                  result_tree.getroot()]

        assert (result[0] == expected_result[0])
        assert (result[1] == expected_result[1])
        assert (result[2] == expected_result[2])
        assert (result[3] == expected_result[3])
Beispiel #7
0
    def test_renderer(self):
        filename = os.path.dirname(__file__) + '/sample_files/rend-file.xml'

        comparation_filename = os.path.dirname(__file__) + \
                               '/sample_files/expected-rend-file.xml'

        from_node = Node('node_zero')
        node = Node('node_one')

        anchors = ('1', '2')
        region = Region('region_one', *anchors)

        node.add_region(region)

        features = {'name': 'feature_name', 'value': 'feature_value'}

        annotation = Annotation('Annotation_label', features, 'annotation-1')

        node.annotations.add(annotation)

        edge = Edge('edge1', from_node, node)

        self.graph.edges.add(edge)
        self.graph.nodes.add(node)
        self.graph.regions.add(region)

        graf_render = GrafRenderer(filename)
        graf_render.render(self.graph)

        expected_tree = ElementTree.parse(comparation_filename)
        result_tree = ElementTree.parse(filename)

        expected_result = [
            ElementTree.tostring(i) for i in expected_tree.getroot()
        ]
        result = [ElementTree.tostring(i) for i in result_tree.getroot()]

        assert (result[0] == expected_result[0])
        assert (result[1] == expected_result[1])
        assert (result[2] == expected_result[2])
        assert (result[3] == expected_result[3])
Beispiel #8
0
 def test_add_node(self):
     node = Node('test_node')
     self.graph.nodes.add(node)
     self.assertListEqual(list(self.graph.nodes), [node])
Beispiel #9
0
    def test_parents_and_children(self):
        n1 = Node('n1')
        n2 = Node('n2')
        n3 = Node('n3')
        n4 = Node('n4')
        self.graph.nodes.add(n1)
        self.graph.nodes.add(n2)
        self.graph.nodes.add(n3)
        self.graph.nodes.add(n4)
        self.graph.create_edge(n1, n2)
        self.graph.create_edge(n2, n1)
        self.graph.create_edge(n1, n3)
        self.graph.create_edge(n3, n4)

        self.assertListEqual(list(n1.iter_children()), [n2, n3])
        self.assertListEqual(list(n2.iter_children()), [n1])
        self.assertListEqual(list(n3.iter_children()), [n4])
        self.assertListEqual(list(n4.iter_children()), [])
        self.assertListEqual(list(n1.iter_parents()), [n2])
        self.assertListEqual(list(n2.iter_parents()), [n1])
        self.assertListEqual(list(n3.iter_parents()), [n1])
        self.assertListEqual(list(n4.iter_parents()), [n3])
Beispiel #10
0
 def test_iter_roots(self):
     node = Node('test_node')
     self.graph.nodes.add(node)
     self.graph.root = node
     self.assertListEqual(list(self.graph.iter_roots()), [node])
Beispiel #11
0
 def test_get_root(self):
     node = Node('test_node')
     self.graph.nodes.add(node)
     self.graph.root = node
     assert(self.graph.root == node)
Beispiel #12
0
# -*- coding: utf-8 -*-
#
# Poio Tools for Linguists
#
# Copyright (C) 2009-2012 Poio Project
# Author: António Lopes <*****@*****.**>
# URL: <http://media.cidles.eu/poio/>
# For license information, see LICENSE.TXT

import sys
from graf import Node, Graph, Edge, FeatureStructure, Annotation, GrafRenderer

# Create three nodes
node_one = Node('node_one')
node_two = Node('node_two')
node_three = Node('node_three')

# Create the Annotation Graph and set the values
graph = Graph()

#Adding the nodes
graph.nodes.add(node_one)
graph.nodes.add(node_two)
graph.nodes.add(node_three)

# Create the edge
edge = Edge(node_one, node_three)

# Add an the edge
#graph.add_edge(edge)
Beispiel #13
0
 def test_get_region(self):
     node = Node('test_node')
     self.graph.nodes.add(node)
     self.assertEqual(self.graph.nodes['test_node'], node)
Beispiel #14
0
 def test_add_node(self):
     node = Node('test_node')
     self.graph.nodes.add(node)
     assert(list(self.graph.nodes) == [node])
Beispiel #15
0
    def test_parents_and_children(self):
        n1 = Node('n1')
        n2 = Node('n2')
        n3 = Node('n3')
        n4 = Node('n4')
        self.graph.nodes.add(n1)
        self.graph.nodes.add(n2)
        self.graph.nodes.add(n3)
        self.graph.nodes.add(n4)
        self.graph.create_edge(n1, n2)
        self.graph.create_edge(n2, n1)
        self.graph.create_edge(n1, n3)
        self.graph.create_edge(n3, n4)

        assert(list(n1.iter_children()) == [n2, n3])
        assert(list(n2.iter_children()) == [n1])
        assert(list(n3.iter_children()) == [n4])
        assert(list(n4.iter_children()) == [])
        assert(list(n1.iter_parents()) == [n2])
        assert(list(n2.iter_parents()) == [n1])
        assert(list(n3.iter_parents()) == [n1])
        assert(list(n4.iter_parents()) == [n3])

    # TODO: Test makes wrong assumption. The problem is not that
    # Annotations might get added twice, but that one file might
    # be parsed twice.
    # def test_verify_annotation_existence(self):
    #     """ Verification if the same annotation is parsed
    #     more then one time. The same annotation can only
    #     exist and allowed to be added one time.

    #     """

    #     node = Node('test_node')
    #     annotation_1 = Annotation('annotation_value', None, 'id-1')
    #     # Same id
    #     annotation_2 = Annotation('annotation_value', None, 'id-1')

    #     # Add the first node
    #     node.annotations.add(annotation_1)

    #     # Try to add again the same annotation
    #     node.annotations.add(annotation_2)

    #     self.graph.nodes.add(node)

    #     expected_result = 1
    #     element = self.graph.get_element('test_node')

    #     assert(len(element.annotations) == expected_result)

    # TODO: Test makes wrong assumption. The problem is not that
    # Annotations might get added twice, but that one file might
    # be parsed twice.
    # def test_verify_edge_existence(self):
    #     """ Verification if the same edge is parsed
    #     more then one time. The same edge can only
    #     exist and allowed to be added one time.

    #     """

    #     # Test values
    #     fnode = Node('node_1') # From Node
    #     tnode = Node('node_2') # To Node

    #     edge_1 = Edge('id_test', fnode, tnode)
    #     # Same id
    #     edge_2 = Edge('id_test', fnode, tnode)

    #     self.graph.nodes.add(fnode)
    #     self.graph.nodes.add(tnode)
    #     self.graph.edges.add(edge_1)

    #     # Try to add again the edge annotation
    #     self.graph.edges.add(edge_2)

    #     assert(len(self.graph.edges) == 1)
Beispiel #16
0
 def test_iter_roots(self):
     node = Node('test_node')
     self.graph.nodes.add(node)
     self.graph.root = node
     assert(list(self.graph.iter_roots()) == [node])
Beispiel #17
0
 def test_set_root_not_in_graph(self):
     node = Node('test_node')
     self.assertRaises(ValueError,
                       lambda: setattr(self.graph, 'root', node))
Beispiel #18
0
 def test_get_root(self):
     node = Node('test_node')
     self.graph.nodes.add(node)
     self.graph.root = node
     self.assertEqual(self.graph.root, node)
Beispiel #19
0
    def test_parents_and_children(self):
        n1 = Node('n1')
        n2 = Node('n2')
        n3 = Node('n3')
        n4 = Node('n4')
        self.graph.nodes.add(n1)
        self.graph.nodes.add(n2)
        self.graph.nodes.add(n3)
        self.graph.nodes.add(n4)
        self.graph.create_edge(n1, n2)
        self.graph.create_edge(n2, n1)
        self.graph.create_edge(n1, n3)
        self.graph.create_edge(n3, n4)

        assert(list(n1.iter_children()) == [n2, n3])
        assert(list(n2.iter_children()) == [n1])
        assert(list(n3.iter_children()) == [n4])
        assert(list(n4.iter_children()) == [])
        assert(list(n1.iter_parents()) == [n2])
        assert(list(n2.iter_parents()) == [n1])
        assert(list(n3.iter_parents()) == [n1])
        assert(list(n4.iter_parents()) == [n3])
Beispiel #20
0
 def test_get_region(self):
     node = Node('test_node')
     self.graph.nodes.add(node)
     assert(self.graph.nodes['test_node'] ==node)