def test_match_with_one_explicit_labeled_arc_should_succeed(self):
     first = Graph({Red(1, 2)})
     second = Graph({Red('a', 'b')})
     first_variants = to_list(replace_node_by_obj(match(first, second)))
     assert_that(first_variants, equal_to([[(1, 'a'), (2, 'b')]]))
     second_variants = to_list(replace_node_by_obj(match(second, first)))
     assert_that(second_variants, equal_to([[('a', 1), ('b', 2)]]))
 def test_match_with_two_and_three_components_should_succeed(self):
     first = Graph({1, 2})
     second = Graph({'a', 'b', 'c'})
     first_variants = to_list(replace_node_by_obj(match(first, second)))
     assert_that(
         first_variants,
         equal_to([
             [(1, 'a'), (2, 'b')],
             [(1, 'b'), (2, 'a')],
             [(1, 'a'), (2, 'c')],
             [(1, 'c'), (2, 'a')],
             [(1, 'b'), (2, 'c')],
             [(1, 'c'), (2, 'b')],
         ]))
     second_variants = to_list(replace_node_by_obj(match(second, first)))
     assert_that(
         second_variants,
         equal_to([
             [('a', 1), ('b', 2)],
             [('a', 2), ('b', 1)],
             [('a', 1), ('c', 2)],
             [('a', 2), ('c', 1)],
             [('b', 1), ('c', 2)],
             [('b', 2), ('c', 1)],
         ]))
Exemplo n.º 3
0
 def test_with_one_node(self):
     graph = Graph([1])
     assert_that(graph.as_dot().to_string(), equal_to(
         'digraph Model {\n'
         '"1" [shape=box];\n'
         '}\n'
     ))
    def test_match_with_many_components_and_special_equiv_should_succeed(self):
        class Node(object):
            def __init__(self, value, equiv=None):
                self.value = value
                self.equiv = equiv or frozenset()

            def equiv_pattern(self, other):
                return other.value in self.equiv

            def __repr__(self):
                return repr(self.value)

            def __eq__(self, other):
                return (id(self) == id(other)
                        or (self.value == other.value if hasattr(
                            other, 'value') else self.value == other))

            def __hash__(self):
                return hash(self.value)

            def __lt__(self, other):
                return hash(self) < hash(other)

        first = Graph({(Node(1, {'a'}), Node(2, {'b'})), (Node(3), Node(4))})
        second = Graph({(Node('a', {1}), Node('b', {2})),
                        Node('c'),
                        Node('d')})
        first_variants = to_list(replace_node_by_obj(match(first, second)))
        assert_that(len(first_variants), equal_to(1))
        assert_that(first_variants[0], equal_to([(1, 'a'), (2, 'b')]))
        second_variants = to_list(replace_node_by_obj(match(second, first)))
        assert_that(len(second_variants), equal_to(1))
        assert_that(second_variants[0], equal_to([('a', 1), ('b', 2)]))
 def test_match_with_self_connected_nodes_should_succeed(self):
     first = Graph({(1, 1)})
     second = Graph({('a', 'a')})
     first_variants = to_list(replace_node_by_obj(match(first, second)))
     assert_that(first_variants, equal_to([[(1, 'a')]]))
     second_variants = to_list(replace_node_by_obj(match(second, first)))
     assert_that(second_variants, equal_to([[('a', 1)]]))
 def test_match_one_component_in_two_should_succeed(self):
     target = Graph({(1, 2), (3, 4)})
     pattern = Graph({('a', 'b')})
     variants = to_list(replace_node_by_obj(match(target, pattern)))
     assert_that(variants,
                 equal_to([
                     [(1, 'a'), (2, 'b')],
                     [(3, 'a'), (4, 'b')],
                 ]))
 def test_match_chain_of_two_in_chain_of_three_should_succeed(self):
     target = Graph({(1, 2), (2, 3)})
     pattern = Graph({('a', 'b')})
     variants = to_list(replace_node_by_obj(match(target, pattern)))
     assert_that(variants,
                 equal_to([
                     [(1, 'a'), (2, 'b')],
                     [(2, 'a'), (3, 'b')],
                 ]))
Exemplo n.º 8
0
 def test_with_one_labeled_arc(self):
     graph = Graph([Red(1, 2)])
     assert_that(graph.as_dot().to_string(), equal_to(
         'digraph Model {\n'
         '"1" [shape=box];\n'
         '"1" -> "2"  [label=Red];\n'
         '"2" [shape=box];\n'
         '}\n'
     ))
 def test_match_with_one_node_should_succeed(self):
     first = Graph({1})
     second = Graph({'a'})
     first_variants = to_list(replace_node_by_obj(match(first, second)))
     assert_that(first_variants,
                 equal_to([[Isomorphic(target=1, pattern='a')]]))
     second_variants = to_list(replace_node_by_obj(match(second, first)))
     assert_that(second_variants,
                 equal_to([[Isomorphic(target='a', pattern=1)]]))
Exemplo n.º 10
0
 def test_with_two_arcs_between_two_nodes(self):
     graph = Graph([{1, 2}])
     assert_that(graph.as_dot().to_string(), equal_to(
         'digraph Model {\n'
         '"1" [shape=box];\n'
         '"1" -> "2"  [label=tuple];\n'
         '"2" [shape=box];\n'
         '"2" -> "1"  [label=tuple];\n'
         '}\n'
     ))
 def test_match_generated_graphs_should_succeed(self):
     for nodes_count in xrange(2, 5):
         for target, pattern in generate_graphs(nodes_count):
             if target.nodes and pattern.nodes:
                 t = Graph(nodes=target.largest_connected_component)
                 p = Graph(nodes=pattern.largest_connected_component)
                 for i in replace_node_by_obj(match(t, p)):
                     assert_that({x.pattern
                                  for x in i},
                                 equal_to({x.obj
                                           for x in p.nodes}))
    def test_match_complete_graphs_and_should_succeed(self):
        target_nodes = (1, 2, 3, 4)
        pattern_nodes = ('a', 'b', 'c', 'd')
        first = Graph((p[0], p[1]) for p in permutations(target_nodes))
        second = Graph((p[0], p[1]) for p in permutations(pattern_nodes))
        actual_variants = to_list(replace_node_by_obj(match(first, second)))

        def generate_expected_variants():
            for pattern in permutations(pattern_nodes, len(target_nodes)):
                yield zip(target_nodes, pattern)

        expected_variants = list(generate_expected_variants())
        assert_that(actual_variants, contains_inanyorder(*expected_variants))
 def test_two_components_should_succeed(self):
     graph = Graph({(1, 2), (3, 4)})
     nodes = {node.obj: node for node in graph.nodes}
     assert_that(nodes[1].connected_component, {nodes[1], nodes[2]})
     assert_that(nodes[2].connected_component, {nodes[1], nodes[2]})
     assert_that(nodes[3].connected_component, {nodes[3], nodes[4]})
     assert_that(nodes[4].connected_component, {nodes[3], nodes[4]})
def generate_graphs(nodes_count):
    nodes = range(1, nodes_count + 1)
    arcs = list(combinations(nodes, 2))

    def sym(x):
        return chr(ord('a') + int(x) - 1)

    def sym_iter(iterable):
        return type(iterable)(sym(x) for x in iterable)

    for target_mask in product({True, False}, repeat=len(arcs)):
        target = Graph(mask_filter(target_mask, arcs))
        for pattern_mask in product({True, False}, repeat=len(arcs)):
            if Mask(pattern_mask) <= Mask(target_mask):
                pattern = Graph(
                    sym_iter(x) for x in mask_filter(pattern_mask, arcs))
                yield target, pattern
Exemplo n.º 15
0
 def test_make_complex_should_succeed(self):
     graph = Graph([Red(1, 2), (1, 2), Blue(2, 4), Blue(3, 2), (4, 3)])
     assert_that(replace_node_by_obj(graph.nodes), equal_to({1, 2, 3, 4}))
     assert_that(repr(graph), equal_to('[1] ---> 2'    '\n'
                                       '[1] -Red-> 2'  '\n'
                                       '[2] -Blue-> 4' '\n'
                                       '[3] -Blue-> 2' '\n'
                                       '[4] ---> 3'))
     assert_that(graph.connections_types, equal_to({tuple, Red, Blue}))
 def test_check_current_isomorphism_before_add_to_visited(self):
     target = Graph({
         Red(1, 3),
         Red(1, 4),
         Red(2, 3),
         Blue(5, 4),
         Blue(6, 3),
         Blue(2, 4),
     })
     pattern = Graph({
         Red('a', 'b'),
         Red('a', 'c'),
         Blue('d', 'b'),
         Blue('e', 'c'),
     })
     variants = to_list(replace_node_by_obj(match(target, pattern)))
     assert_that(
         variants,
         equal_to([
             [(1, 'a'), (2, 'd'), (3, 'c'), (4, 'b'), (6, 'e')],
             [(1, 'a'), (3, 'c'), (4, 'b'), (5, 'd'), (6, 'e')],
             [(1, 'a'), (2, 'e'), (3, 'b'), (4, 'c'), (6, 'd')],
             [(1, 'a'), (3, 'b'), (4, 'c'), (5, 'e'), (6, 'd')],
         ]))
Exemplo n.º 17
0
 def test_make_one_node_and_one_labeled_arc_should_succeed(self):
     graph = Graph({1, Red(2, 3)})
     assert_that(replace_node_by_obj(graph.nodes), equal_to({1, 2, 3}))
     assert_that(repr(graph), equal_to('[1]' '\n'
                                       '[2] -Red-> 3'))
     assert_that(graph.connections_types, equal_to({Red}))
 def test_three_connected_should_succeed(self):
     graph = Graph({(1, 2), (2, 3)})
     for node in graph.nodes:
         assert_that(node.connected_component, graph.nodes)
 def test_two_double_connected_should_succeed(self):
     graph = Graph(({1, 2}, ))
     for node in graph.nodes:
         assert_that(node.connected_component, graph.nodes)
 def test_two_not_connected_should_succeed(self):
     graph = Graph({1, 2})
     for node in graph.nodes:
         assert_that(node.connected_component, {node})
Exemplo n.º 21
0
 def test_two_twice_labeled_connected_should_succeed(self):
     assert_that(get_connected_components(Graph([Red(1, 2), Blue(1, 2)])),
                 equal_to([{1, 2}]))
Exemplo n.º 22
0
 def test_two_connected_should_succeed(self):
     assert_that(get_connected_components(Graph([(1, 2)])),
                 equal_to([{1, 2}]))
Exemplo n.º 23
0
 def test_empty_should_succeed(self):
     assert_that(get_connected_components(Graph()), empty())
Exemplo n.º 24
0
 def test_make_one_node_and_one_arc_with_duplication_should_succeed(self):
     graph = Graph({1, (1, 2)})
     assert_that(replace_node_by_obj(graph.nodes), equal_to({1, 2}))
     assert_that(repr(graph), equal_to('[1] ---> 2'))
     assert_that(graph.connections_types, equal_to({tuple}))
Exemplo n.º 25
0
 def test_empty(self):
     graph = Graph()
     assert_that(graph.as_dot().to_string(), equal_to(
         'digraph Model {\n'
         '}\n'
     ))
Exemplo n.º 26
0
 def test_make_one_node_and_one_arc_should_succeed(self):
     graph = Graph({1, (2, 3)})
     assert_that(replace_node_by_obj(graph.nodes), equal_to({1, 2, 3}))
     assert_that(repr(graph), equal_to('[1]' '\n'
                                       '[2] ---> 3'))
Exemplo n.º 27
0
 def test_make_self_connected_by_labeled_arc_node_should_succeed(self):
     graph = Graph({Red(1, 1)})
     assert_that(replace_node_by_obj(graph.nodes), equal_to({1}))
     assert_that(repr(graph), equal_to('[1] * Red'))
     assert_that(graph.connections_types, equal_to({Red}))
Exemplo n.º 28
0
 def test_make_with_one_labeled_arc_should_succeed(self):
     graph = Graph({Red(1, 2)})
     assert_that(replace_node_by_obj(graph.nodes), equal_to({1, 2}))
     assert_that(repr(graph), equal_to('[1] -Red-> 2'))
Exemplo n.º 29
0
 def test_make_with_one_node_should_succeed(self):
     graph = Graph({1})
     assert_that(replace_node_by_obj(graph.nodes), equal_to({1}))
     assert_that(repr(graph), equal_to('[1]'))
     assert_that(graph.connections_types, equal_to(frozenset()))
Exemplo n.º 30
0
 def test_make_empty_should_succeed(self):
     graph = Graph()
     assert_that(graph.nodes, equal_to(tuple()))
     assert_that(repr(graph), equal_to(''))
     assert_that(graph.connections_types, equal_to(frozenset()))