コード例 #1
0
 def test_inject_remove_node(self):
     pattern = NXGraph()
     pattern.add_nodes_from([1, 2, 3])
     pattern.add_edges_from([(1, 2), (3, 2)])
     rule = Rule.from_transform(pattern)
     rule.inject_remove_node(2)
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert (2 in rule.lhs.nodes())
     assert (2 not in rule.p.nodes())
     assert (2 not in rule.rhs.nodes())
コード例 #2
0
 def test_inject_clone_node(self):
     pattern = NXGraph()
     pattern.add_nodes_from([1, 2, 3])
     pattern.add_edges_from([(1, 2), (3, 2)])
     rule = Rule.from_transform(pattern)
     new_p_node, new_rhs_node = rule.inject_clone_node(2)
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert (new_p_node in rule.p.nodes())
     assert (new_rhs_node in rule.rhs.nodes())
     assert (rule.p_rhs[new_p_node] == new_rhs_node)
     assert ((1, new_p_node) in rule.p.edges())
     assert ((3, new_p_node) in rule.p.edges())
     assert ((1, new_rhs_node) in rule.rhs.edges())
     assert ((3, new_rhs_node) in rule.rhs.edges())
     new_p_node, new_rhs_node = rule.inject_clone_node(2)
     assert (len(keys_by_value(rule.p_lhs, 2)) == 3)
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     rule.inject_remove_node(3)
     try:
         rule.inject_clone_node(3)
         raise ValueError("Cloning of removed node was not caught")
     except:
         pass
コード例 #3
0
 def test_inject_add_node(self):
     pattern = NXGraph()
     pattern.add_nodes_from([1, 2, 3])
     pattern.add_edges_from([(1, 2), (3, 2)])
     rule = Rule.from_transform(pattern)
     try:
         rule.inject_add_node(3)
         raise ValueError("Node duplication was not caught")
     except RuleError:
         pass
     rule.inject_add_node(4)
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert (4 in rule.rhs.nodes() and 4 not in rule.lhs.nodes()
             and 4 not in rule.p.nodes())
コード例 #4
0
ファイル: rules.py プロジェクト: hastebrot/ReGraph
    def __init__(self, p, lhs, rhs, p_lhs=None, p_rhs=None):
        """Rule initialization.

        A rule is initialized with p, lhs, rhs graphs, and
        p -> lhs & p -> rhs homomorphisms, these homomorphisms
        are checked to be valid ones (edge and attribute preserving).
        By default the homomorphisms p -> lhs & p -> rhs are None,
        in this case they are initialized as the identity homomorphism
        (id(p)).

        Parameters
        ----------
        p : networkx.(Di)Graph
            Preserved part of the rule
        lhs : networkx.(Di)Graph
            Left-hand side (pattern) of the rule
        rhs : networkx.(Di)Graph
            Right-hand side of the rule
        p_lhs : dict
            Homomorphism between `p` and `lhs` given by
            a dictionary with keys -- nodes of `p`,
            values -- nodes of `lhs`
        p_rhs : dict
            Homomorphism between `p` and `rhs` given by
            a dictionary with keys -- nodes of `p`,
            values -- nodes of `rhs`
        """
        if not p_lhs:
            self.p_lhs = identity(p, lhs)
        else:
            check_homomorphism(p, lhs, p_lhs)
            self.p_lhs = copy.deepcopy(p_lhs)

        if not p_rhs:
            self.p_rhs = identity(p, rhs)
        else:
            check_homomorphism(p, rhs, p_rhs)
            self.p_rhs = copy.deepcopy(p_rhs)

        self.p = copy.deepcopy(p)
        self.lhs = copy.deepcopy(lhs)
        self.rhs = copy.deepcopy(rhs)

        return
コード例 #5
0
 def test_inject_add_node_attrs(self):
     pattern = NXGraph()
     prim.add_nodes_from(pattern, [1, 2, 3])
     prim.add_edges_from(pattern, [(1, 2), (3, 2)])
     rule = Rule.from_transform(pattern)
     clone_name_p, clone_name_rhs = rule.inject_clone_node(2)
     rule.inject_add_node(4)
     merge = rule.inject_merge_nodes([1, 3])
     rule.inject_add_node_attrs(2, {"a": {True}})
     assert ("a" in rule.rhs.get_node(2))
     rule.inject_add_node_attrs(clone_name_p, {"b": {True}})
     assert ("b" in rule.rhs.get_node(clone_name_rhs))
     assert ("b" not in rule.rhs.get_node(2))
     rule.inject_add_node_attrs(4, {"c": {True}})
     assert ("c" in rule.rhs.get_node(4))
     rule.inject_add_node_attrs(merge, {"d": {True}})
     assert ("d" in rule.rhs.get_node(merge))
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
コード例 #6
0
ファイル: test_rules.py プロジェクト: hastebrot/ReGraph
    def test_inject_add_edge_attrs(self):
        pattern = nx.DiGraph()
        prim.add_nodes_from(pattern, [0, 1, 2, 3])
        prim.add_edges_from(pattern, [(0, 1), (0, 2), (1, 2), (3, 2)])
        rule = Rule.from_transform(pattern)
        clone_name_p, clone_name_rhs = rule.inject_clone_node(2)
        rule.inject_add_node(4)
        rule.inject_add_edge(4, 3)
        merge = rule.inject_merge_nodes([1, 3])

        rule.inject_add_edge_attrs(0, 1, {"a": {True}})
        assert ("a" in rule.rhs.edge[0][merge])
        rule.inject_add_edge_attrs(0, clone_name_p, {"b": {True}})
        assert ("b" in rule.rhs.edge[0][clone_name_rhs])
        rule.inject_add_edge_attrs(merge, clone_name_p, {"c": {True}})
        assert ("c" in rule.rhs.edge[merge][clone_name_rhs])
        assert ("c" not in rule.rhs.edge[merge][2])
        rule.inject_add_edge_attrs(4, merge, {"d": {True}})
        assert ("d" in rule.rhs.edge[4][merge])
        check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
        check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
コード例 #7
0
 def test_inject_remove_edge(self):
     pattern = NXGraph()
     pattern.add_nodes_from([1, 2, 3])
     pattern.add_edges_from([(1, 2), (3, 2)])
     rule = Rule.from_transform(pattern)
     rule.inject_remove_edge(3, 2)
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert ((3, 2) not in rule.p.nodes())
     new_name, _ = rule.inject_clone_node(2)
     rule.inject_remove_edge(1, new_name)
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert ((1, new_name) not in rule.p.edges())
     assert ((1, 2) in rule.p.edges())
コード例 #8
0
 def test_inject_add_edge(self):
     pattern = NXGraph()
     pattern.add_nodes_from([1, 2, 3])
     pattern.add_edges_from([(1, 2), (3, 2)])
     rule = Rule.from_transform(pattern)
     rule.inject_add_node(4)
     rule.inject_add_edge(1, 4)
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert ((1, 4) in rule.rhs.edges())
     merge_node = rule.inject_merge_nodes([1, 2])
     rule.inject_add_edge(merge_node, 3)
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert ((merge_node, 3) in rule.rhs.edges())
     new_p_node, new_rhs_node = rule.inject_clone_node(2)
コード例 #9
0
def _check_instance(hierarchy, graph_id, pattern, instance, pattern_typing):
    check_homomorphism(pattern,
                       hierarchy.node[graph_id].graph,
                       instance,
                       total=True)
    # check that instance typing and lhs typing coincide
    for node in pattern.nodes():
        if pattern_typing:
            for typing_graph, typing in pattern_typing.items():
                try:
                    instance_typing = hierarchy.compose_path_typing(
                        nx.shortest_path(hierarchy, graph_id, typing_graph))
                    if node in pattern_typing.keys() and\
                       instance[node] in instance_typing.keys():
                        if typing[node] != instance_typing[instance[node]]:
                            raise RewritingError(
                                "Typing of the instance of LHS does not " +
                                " coincide with typing of LHS!")
                except NetworkXNoPath:
                    raise RewritingError(
                        "Graph '%s' is not typed by '%s' specified "
                        "as a typing graph of the lhs of the rule." %
                        (graph_id, typing_graph))
コード例 #10
0
 def test_inject_remove_node_attrs(self):
     pattern = NXGraph()
     pattern.add_nodes_from([1, (2, {"a2": {True}}), (3, {"a3": {False}})])
     pattern.add_edges_from([(1, 2), (3, 2)])
     rule = Rule.from_transform(pattern)
     rule.inject_remove_node_attrs(3, {"a3": {False}})
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert ("a3" not in rule.p.get_node(3))
     assert ("a3" in rule.lhs.get_node(3))
     new_p_node, new_rhs_node = rule.inject_clone_node(2)
     rule.inject_remove_node_attrs(new_p_node, {"a2": {True}})
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert ("a2" not in rule.p.get_node(new_p_node))
     assert ("a2" in rule.p.get_node(2))
     assert ("a2" not in rule.rhs.get_node(new_rhs_node))
     assert ("a2" in rule.rhs.get_node(2))
コード例 #11
0
ファイル: test_rules.py プロジェクト: hastebrot/ReGraph
 def test_inject_remove_node_attrs(self):
     pattern = nx.DiGraph()
     prim.add_nodes_from(pattern,
                         [1, (2, {
                             "a2": {True}
                         }), (3, {
                             "a3": {False}
                         })])
     prim.add_edges_from(pattern, [(1, 2), (3, 2)])
     rule = Rule.from_transform(pattern)
     rule.inject_remove_node_attrs(3, {"a3": {False}})
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert ("a3" not in rule.p.node[3])
     assert ("a3" in rule.lhs.node[3])
     new_p_node, new_rhs_node = rule.inject_clone_node(2)
     rule.inject_remove_node_attrs(new_p_node, {"a2": {True}})
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert ("a2" not in rule.p.node[new_p_node])
     assert ("a2" in rule.p.node[2])
     assert ("a2" not in rule.rhs.node[new_rhs_node])
     assert ("a2" in rule.rhs.node[2])
コード例 #12
0
 def test_inject_merge_nodes(self):
     pattern = NXGraph()
     prim.add_nodes_from(pattern, [1, 2, 3])
     prim.add_edges_from(pattern, [(1, 2), (3, 2)])
     rule = Rule.from_transform(pattern)
     new_name = rule.inject_merge_nodes([1, 2])
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert ((new_name, new_name) in rule.rhs.edges())
     assert ((3, new_name) in rule.rhs.edges())
     new_p_name, new_rhs_name = rule.inject_clone_node(2)
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     new_name = rule.inject_merge_nodes([2, 3])
     check_homomorphism(rule.p, rule.lhs, rule.p_lhs)
     check_homomorphism(rule.p, rule.rhs, rule.p_rhs)
     assert (new_rhs_name in rule.rhs.nodes())