def test_create2(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test2', Node('test3'))) assert [node.name for node in G.dfs()] == ['test1', 'test2', 'test3'] assert not G.verify_edges()
def test_match4alt(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_node(Node('test3')) G.add_node(Node('test4')) G.add_node(Node('test5')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test2', 'test4')) G.add_edge(Edge('test3', 'test4', to_idx=1)) G.add_edge(Edge('test4', 'test5')) fragment = GraphMatcher() m2 = MatchNodeByName("test2") m3 = MatchNodeByName('test3') m4 = MatchNodeByName('test4') e1 = MatchEdgeInputsGroupFactory() fragment.add_edge(e1.get_edge(from_node=m2, to_node=m4)) fragment.add_edge(e1.get_edge(from_node=m3, to_node=m4)) res = fragment.match_graph(G) assert len(res) == 1 assert res[0].num_nodes() == 3 assert res[0].num_edges() == 2
def test_match_fail(): class MatchSuffNode(MatchNode): def __init__(self, name, suff): super().__init__(name) self.suff = suff def _match(self, G, node, edge): return node.name.endswith(self.suff) G = Graph() G.add_node(Node("test1a")) G.add_edge(Edge('test1a', Node('test2a'))) G.add_edge(Edge('test2a', Node('test3b'))) G.add_edge(Edge('test3b', Node('test4a'))) fragment = Graph() fragment.add_node(MatchSuffNode('match1', 'a')) fragment.add_node(MatchSuffNode('match2', 'b')) fragment.add_edge(Edge('match1', 'match2')) res = G.match_fragment(fragment) assert len(res) == 1 assert res[0].num_nodes() == 2 assert res[0].num_edges() == 1 assert [node.name for node in res[0].nodes()] == ['test2a', 'test3b']
def test_reverse_dfs1(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test1', Node('test3'))) expect = ['test1', 'test2', 'test3'] assert [node.name for node in G.dfs()] == expect expect.reverse() assert [node.name for node in G.dfs(reverse=True)] == expect
def test_create1(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test2', Node('test3'))) expect = ['test1', 'test2', 'test3'] assert [node.name for node in G.dfs()] == expect expect.reverse() assert [node.name for node in G.dfs(reverse=True)] == expect assert G.num_in_edges('test1') == 0 assert G.num_out_edges('test1') == 1 assert not G.verify_edges()
def test_create4(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test2', Node('test3'))) assert not G.verify_edges() G.replace_node('test2', Node('test4')) expect = ['test1', 'test4', 'test3'] assert [node.name for node in G.dfs()] == expect expect.reverse() assert [node.name for node in G.dfs(reverse=True)] == expect assert not G.verify_edges()
def test_create5(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test1', Node('test3'))) G.add_node(Node('test4')) G.add_edge(Edge('test2', 'test4')) G.add_edge(Edge('test3', 'test4')) assert G.num_in_edges('test4') == 2 assert G.num_out_edges('test1') == 2 assert [node.name for node in G.dfs()] == ['test1', 'test2', 'test3', 'test4'] assert not G.verify_edges()
def _generate_graph(self): temp_grid = copy.copy(self._grid_with_nodes) for i in range(len(self.cells)): row = self.cells[i] for j in range(len(row)): node = Node('x' + str(i) + 'y' + str(j)) current_node_name = 'x' + str(i) + 'y' + str(j) if i > 0: # not top row node_under_tesing_name = 'x' + str(i - 1) + 'y' + str(j) node, temp_grid = self._decide_if_connection_or_not(\ current_node_name, node_under_tesing_name, node, temp_grid) if i + 1 < self._y_dim: # not bottom row node_under_tesing_name = 'x' + str(i + 1) + 'y' + str(j) node, temp_grid = self._decide_if_connection_or_not(\ current_node_name, node_under_tesing_name, node, temp_grid) if j > 0: # not left most col node_under_tesing_name = 'x' + str(i) + 'y' + str(j - 1) node, temp_grid = self._decide_if_connection_or_not(\ current_node_name, node_under_tesing_name, node, temp_grid) if j + 1 < self._x_dim: # not right most node_under_tesing_name = 'x' + str(i) + 'y' + str(j + 1) node, temp_grid = self._decide_if_connection_or_not(\ current_node_name, node_under_tesing_name, node, temp_grid) self.graph['x' + str(i) + 'y' + str(j)] = node self._grid_with_nodes_and_edges_with_obs = temp_grid
def test_all_predecessors(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test1', Node('test3'))) G.add_node(Node('test4')) G.add_edge(Edge('test2', 'test4')) G.add_edge(Edge('test3', 'test4', to_idx=1)) preds = set() for node in G.all_predecessors('test4'): assert node.name not in preds preds.add(node.name) test_fn = lambda name: set(node.name for node in G.all_predecessors(name)) assert set(['test1', 'test2', 'test3']) == test_fn('test4') assert set(['test1']) == test_fn('test2') assert set(['test1']) == test_fn('test3') assert not test_fn('test1')
def test_match1(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test2', Node('test3'))) G.add_node(Node('test4')) G.add_edge(Edge('test3', 'test4')) assert not G.verify_edges() fragment = GraphMatcher() fragment.add_edge( MatchEdgeByIdx(from_node=MatchNodeByName("test1"), to_node=MatchNodeByName('test2'))) res = fragment.match_graph(G) assert len(res) == 1 assert len(res[0]) == 2 assert res[0].num_edges() == 1
def test_insert(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test1', Node('test3'))) G.add_node(Node('test4')) G.add_edge(Edge('test2', 'test4')) G.add_edge(Edge('test3', 'test4', to_idx=2)) G.insert_node(Node('test5'), 'test2', 'test4') nodes = [node.name for node in G.dfs()] assert nodes == ['test1', 'test2', 'test5', 'test3', 'test4'] suc = G.successor_names('test5') pred = G.predecessor_names('test5') assert len(suc) == 1 and 'test4' in suc assert len(pred) == 1 and 'test2' in pred assert not G.verify_edges()
def test_match1(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test2', Node('test3'))) G.add_node(Node('test4')) G.add_edge(Edge('test3', 'test4')) assert not G.verify_edges() fragment = Graph() fragment.add_node(MatchNameNode("test1")) fragment.add_node(MatchNameNode('test2')) fragment.add_edge(Edge('test1', 'test2')) assert not fragment.verify_edges() res = G.match_fragment(fragment) assert len(res) == 1 assert len(res[0]) == 2 assert res[0].num_edges() == 1
def test_match3(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test1', Node('test3'))) G.add_node(Node('test4')) G.add_edge(Edge('test2', 'test4')) # not the same - look here fragment = Graph() fragment.add_node(MatchNameNode("test1")) fragment.add_node(MatchNameNode('test2')) fragment.add_edge(Edge('test1', 'test2')) fragment.add_edge(Edge('test1', MatchNameNode('test3'))) res = G.match_fragment(fragment) assert len(res) == 1 assert res[0].num_nodes() == 3 assert res[0].num_edges() == 2
def test_remove_edge(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test2', Node('test3'))) assert [node.name for node in G.dfs()] == ['test1', 'test2', 'test3'] assert G.num_edges() == 2 edge12 = G.edge('test1', 'test2') assert edge12.from_node.name == 'test1' and edge12.from_idx == 0 assert edge12.to_node.name == 'test2' and edge12.to_idx == 0 assert not G.verify_edges() G.remove_edge(edge12) assert G.num_edges() == 1 assert G.num_in_edges('test1') == 0 assert G.num_out_edges('test1') == 0 assert G.num_in_edges('test2') == 0 assert G.num_out_edges('test2') == 1 assert G.num_in_edges('test3') == 1 assert G.num_out_edges('test3') == 0 assert not G.verify_edges()
def test_match_and_remove(): G = Graph() G.add_node(Node("test0")) G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test0', 'test1')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test1', Node('test3'))) G.add_node(Node('test4')) G.add_edge(Edge('test2', 'test4')) G.add_edge(Edge('test3', 'test4')) G.add_edge(Edge('test4', Node('test5'))) fragment = Graph() fragment.add_node(MatchNameNode("test1")) fragment.add_node(MatchNameNode('test2')) fragment.add_edge(Edge('test1', 'test2')) fragment.add_edge(Edge('test1', MatchNameNode('test3'))) res = G.match_fragment(fragment) assert len(res) == 1 assert res[0].num_nodes() == 3 assert res[0].num_edges() == 2 G.remove_fragment(res[0]) assert [node.name for node in G.dfs()] == ['test0', 'test4', 'test5'] assert not G.verify_edges()
def test_match3(caplog): caplog.set_level(logging.DEBUG) G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test1', Node('test3'))) G.add_node(Node('test4')) G.add_edge(Edge('test2', 'test4')) # not the same - look here fragment = GraphMatcher() n1 = MatchNodeByName('test1') fragment.add_edge( MatchEdgeByIdx(from_node=n1, to_node=MatchNodeByName('test2'))) fragment.add_edge( MatchEdgeByIdx(from_node=n1, to_node=MatchNodeByName('test3'))) res = fragment.match_graph(G) assert len(res) == 1 assert res[0].num_nodes() == 3 assert res[0].num_edges() == 2
def test_match_fail(caplog): caplog.set_level(logging.DEBUG) class MatchSuffNode(NodeMatch): def __init__(self, suff): self._suff = suff self._has_matched = False def match(self, G, node, state): if self._has_matched: return False if node.name.endswith(self._suff): self._has_matched = True return True return False def commit_match(self, G, node, state): pass def reset_match(self, G, state, node=None, init=False): self._has_matched = False G = Graph() G.add_node(Node("test1a")) G.add_edge(Edge('test1a', Node('test2a'))) G.add_edge(Edge('test2a', Node('test3b'))) G.add_edge(Edge('test3b', Node('test4a'))) fragment = GraphMatcher() fragment.add_edge( MatchEdgeByIdx(from_node=MatchSuffNode('a'), to_node=MatchSuffNode('b'))) res = fragment.match_graph(G) assert len(res) == 1 assert res[0].num_nodes() == 2 assert res[0].num_edges() == 1 assert set([node.name for node in res[0].nodes()]) == set(['test2a', 'test3b'])
def test_match4(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_node(Node('test3')) G.add_node(Node('test4')) G.add_node(Node('test5')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test2', 'test4')) G.add_edge(Edge('test3', 'test4', to_idx=1)) G.add_edge(Edge('test4', 'test5')) fragment = GraphMatcher() m2 = MatchNodeByName("test2") m3 = MatchNodeByName('test3') m4 = MatchNodeByName('test4') fragment.add_edge(MatchEdgeByIdx(from_node=m2, to_node=m4)) fragment.add_edge(MatchEdgeByIdx(from_node=m3, to_node=m4, to_idx=1)) res = fragment.match_graph(G) assert len(res) == 1 assert res[0].num_nodes() == 3 assert res[0].num_edges() == 2
def test_match4(): G = Graph() G.add_node(Node("test1")) G.add_node(Node('test2')) G.add_node(Node('test3')) G.add_node(Node('test4')) G.add_node(Node('test5')) G.add_edge(Edge('test1', 'test2')) G.add_edge(Edge('test2', 'test4')) G.add_edge(Edge('test3', 'test4', to_idx=1)) G.add_edge(Edge('test4', 'test5')) fragment = Graph() fragment.add_node(MatchNameNode("test2")) fragment.add_node(MatchNameNode('test3')) fragment.add_node(MatchNameNode('test4')) fragment.add_edge(Edge('test2', 'test4')) fragment.add_edge(Edge('test3', 'test4', to_idx=1)) res = G.match_fragment(fragment) assert len(res) == 1 assert res[0].num_nodes() == 3 assert res[0].num_edges() == 2