def test_merging(self): g = TypedDiGraph() g.add_node(10, "agent", {"a": 0}) g.add_node(20, "agent", {"b": 0}) g.add_node(30, "action", {"c": 0}) g.add_edges_from([(10, 30), (20, 30)]) g.set_edge(10, 30, {"x": 0}) g.set_edge(20, 30, {"y": 0}) LHS = TypedDiGraph() LHS.add_node(1, "agent") LHS.add_node(2, "agent") P = TypedDiGraph() P.add_node("a", "agent") P.add_node("b", "agent") RHS = TypedDiGraph() RHS.add_node("x", "agent") h1 = Homomorphism(P, LHS, {"a": 1, "b": 2}) h2 = Homomorphism(P, RHS, {"a": "x", "b": "x"}) rw = Rewriter(g) instances = rw.find_matching(LHS) RHS_instance = rw.apply_rule(instances[0], h1, h2)
def test_homomorphism(self): new_pattern = TypedDiGraph() new_pattern.add_node(34, "agent") new_pattern.add_node(35, "agent") new_pattern.add_node(36, "action") new_pattern.add_edges_from([(34, 36), (35, 36)]) mapping = {34: 5, 35: 5, 36: 6} h = Homomorphism(new_pattern, self.graph_, mapping) assert_equals(h.is_monic(), False)
def test_undirected_dec_init(self): __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) g = TypedGraph() g.add_node(1, "action") g.add_node(2, "agent", {"u": {0, 1}}) g.add_node(3, "agent", {"u": {4}, "name": "Paul"}) g.add_node(4, "action") g.add_node(5, "agent", {"u": {0}}) g.add_node(6, "agent", {"u": {7}}) g.add_node(7, "agent", {"u": {4}}) g.add_edges_from([(1, 2), (3, 2), (1, 5), (5, 4), (5, 6)]) g.set_edge(1, 2, {"a": {0}}) g.set_edge(2, 3, {"k": {1, 2, 3}}) rw = Rewriter(g) LHS = TypedGraph() LHS.add_nodes_from([(10, "action"), (20, "agent"), (30, "agent")]) LHS.node[20].attrs_ = {"u": {0}} LHS.add_edges_from([(10, 20), (20, 30)]) LHS.set_edge(20, 30, {"k": {1, 2}}) P = TypedGraph() P.add_node(100, "agent") P.add_node(200, "agent", {"u": {0}}) P.add_node(300, "agent") P.add_edges_from([(300, 100), (200, 300)]) P.set_edge(100, 300, {"k": {1, 2}}) P.set_edge(200, 300, {"k": set()}) RHS = TypedGraph() RHS.add_node(1000, "region") RHS.add_node(2000, "agent", {"u": {3}}) RHS.add_node(3000, "agent", {"u": {0, 2}}) RHS.add_edges_from([(1000, 3000), (2000, 3000), (3000, 3000)]) RHS.set_edge(3000, 3000, {"k": {5, 6}}) RHS.set_edge(2000, 3000, {"k": {1, 2, 10}}) RHS.set_edge(1000, 3000, {"a": {12}}) instances = rw.find_matching(LHS) for i, instance in enumerate(instances): plot_instance( rw.graph_, LHS, instance, os.path.join(__location__, "undir_dec_instance_%d.png" % i)) left_h = Homomorphism(P, LHS, {100: 20, 200: 20, 300: 30}) righ_h = Homomorphism(P, RHS, {100: 2000, 200: 3000, 300: 3000}) RHS_instance = rw.apply_rule(instances[0], left_h, righ_h) plot_instance(rw.graph_, RHS, RHS_instance, filename=os.path.join(__location__, "undir_dec_RHS.png"))
def pullback(h1, h2): """ Given h1 : B -> D; h2 : C -> D returns A, rh1, rh2 with rh1 : A -> B; rh2 : A -> C """ if h1.target_ != h2.target_: raise ValueError( "Homomorphisms don't have the same codomain, can't do pullback") if type(h1.target_) == TypedGraph: res_graph = TypedGraph() else: res_graph = TypedDiGraph() hom1 = {} hom2 = {} for n1 in h1.source_.nodes(): for n2 in h2.source_.nodes(): if not h1.mapping_[n1] in res_graph.nodes(): if h1.mapping_[n1] == h2.mapping_[n2]: res_graph.add_node( h1.mapping_[n1], h1.target_.node[h1.mapping_[n1]].type_, merge_attributes(h1.source_.node[n1].attrs_, h2.source_.node[n2].attrs_, 'intersection')) hom1[h1.mapping_[n1]] = n1 hom2[h2.mapping_[n2]] = n2 for n1 in res_graph.nodes(): for n2 in res_graph.nodes(): if res_graph.is_directed(): if (hom1[n1], hom1[n2]) in h1.source_.edges(): if (hom2[n1], hom2[n2]) in h2.source_.edges(): res_graph.add_edge(n1, n2) res_graph.set_edge( n1, n2, merge_attributes( h1.source_.get_edge(hom1[n1], hom1[n2]), h2.source_.get_edge(hom2[n1], hom2[n2]), 'intersection')) else: if (hom1[n1], hom1[n2]) in h1.source_.edges() or ( hom1[n2], hom1[n1]) in h1.source_.edges(): if (hom2[n1], hom2[n2]) in h2.source_.edges() or ( hom2[n2], hom2[n1]) in h2.source_.edges(): res_graph.add_edge(n1, n2) res_graph.set_edge( n1, n2, merge_attributes( h1.source_.get_edge(hom1[n1], hom1[n2]), h2.source_.get_edge(hom2[n1], hom2[n2]), 'intersection')) res_h1 = Homomorphism(res_graph, h1.source_, hom1) res_h2 = Homomorphism(res_graph, h2.source_, hom2) return res_graph, res_h1, res_h2
def test_cloning_cases(self): g = TypedDiGraph() g.add_node(10, "agent", {"a": 0}) g.add_node(20, "agent", {"b": 0}) g.add_node(30, "action", {"c": 0}) g.add_edges_from([(10, 30), (20, 30)]) g.set_edge(10, 30, {"x": 0}) g.set_edge(20, 30, {"y": 0}) LHS = TypedDiGraph() LHS.add_node(1, "agent") rw = Rewriter(g) instances = rw.find_matching(LHS) # simple clone P1 = TypedDiGraph() P1.add_node("a", "agent") P1.add_node("b", "agent") RHS1 = TypedDiGraph() RHS1.add_node("x", "agent") RHS1.add_node("y", "agent") h1 = Homomorphism(P1, LHS, {"a": 1, "b": 1}) h2 = Homomorphism(P1, RHS1, {"a": "x", "b": "y"}) RHS_instance = rw.apply_rule(instances[0], h1, h2) # clone merge on the same nodes RHS2 = TypedGraph() RHS2.add_node("x", "agent") h2 = Homomorphism(P1, RHS2, {"a": "x", "b": "x"}) RHS_instance = rw.apply_rule(instances[0], h1, h2) # clone and merge one with other node LHS.add_node(2, "agent") # update matching instances = rw.find_matching(LHS) P3 = TypedDiGraph() P3.add_node("a", "agent") P3.add_node("b", "agent") P3.add_node("c", "agent") RHS3 = TypedDiGraph() RHS3.add_node("x", "agent") RHS3.add_node("y", "agent") h1 = Homomorphism(P3, LHS, {"a": 1, "b": 1, "c": 2}) h2 = Homomorphism(P3, RHS3, {"a": "x", "b": "y", "c": "y"}) RHS_instance = rw.apply_rule(instances[0], h1, h2)
def final_PBC(h1, h2): if h1.target_ != h2.source_: raise ValueError( "Codomain of homomorphism 1 and domain of homomorphism 2 " + "don't match, can't do pullback complement") if not h2.is_monic(): raise ValueError( "Second homomorphism is not monic, cannot find final pullback complement" ) if type(h1.target_) == TypedGraph: res_graph = TypedGraph() else: res_graph = TypedDiGraph() hom1 = {} hom2 = {} for node in h2.target_.nodes(): B_node = keys_by_value(h2.mapping_, node) if len(B_node) > 0: mapped_A_nodes = keys_by_value(h1.mapping_, B_node[0]) print(mapped_A_nodes) for A_node in mapped_A_nodes: res_graph.add_node( str(A_node) + "_" + str(node), h2.target_.node[h2.mapping_[h1.mapping_[A_node]]].type_, merge_attributes( h1.source_.node[A_node].attrs_, h2.target_.node[ h2.mapping_[h1.mapping_[A_node]]].attrs_, "intersection")) hom1[A_node] = str(A_node) + "_" + str(node) hom2[str(A_node) + "_" + str(node)] = h2.mapping_[h1.mapping_[A_node]] else: res_graph.add_node( str(node) + "_", h2.target_.node[node].type_, h2.target_.node[node].attrs_) hom2[str(node) + "_"] = node for s, t in h2.target_.edges(): B_s = keys_by_value(h2.mapping_, s) B_t = keys_by_value(h2.mapping_, t) if len(B_s) > 0 and len(B_t) > 0: mapped_A_ss = keys_by_value(h1.mapping_, B_s[0]) mapped_A_ts = keys_by_value(h1.mapping_, B_t[0]) for A_s in mapped_A_ss: for A_t in mapped_A_ts: if res_graph.is_directed(): if hom1[A_s] == hom1[A_t] and ( A_s, A_t) not in h1.source_.edges(): res_graph.add_edge( hom1[A_s], hom1[A_t], h2.target_.get_edge( h2.mapping_[h1.mapping_[A_s]], h2.mapping_[h1.mapping_[A_t]])) else: res_graph.add_edge( hom1[A_s], hom1[A_t], merge_attributes( h1.source_.get_edge(A_s, A_t), h2.target_.get_edge( h2.mapping_[h1.mapping_[A_s]], h2.mapping_[h1.mapping_[A_t]]), "intersection")) else: if hom1[A_s] == hom1[A_t] and ( A_s, A_t) not in h1.source_.edges() and ( A_t, A_s) not in h1.source_.edges(): res_graph.add_edge( hom1[A_s], hom1[A_t], h2.target_.get_edge( h2.mapping_[h1.mapping_[A_s]], h2.mapping_[h1.mapping_[A_t]])) pass else: res_graph.add_edge( hom1[A_s], hom1[A_t], merge_attributes( h1.source_.get_edge(A_s, A_t), h2.target_.get_edge( h2.mapping_[h1.mapping_[A_s]], h2.mapping_[h1.mapping_[A_t]]), "intersection")) else: if len(B_s) == 0: sources_to_add = [str(s) + "_"] else: mapped_A_ss = keys_by_value(h1.mapping_, B_s[0]) sources_to_add = [hom1[A_s] for A_s in mapped_A_ss] if len(B_t) == 0: targets_to_add = [str(t) + "_"] else: mapped_A_ts = keys_by_value(h1.mapping_, B_t[0]) targets_to_add = [hom1[A_t] for A_t in mapped_A_ts] for new_s in sources_to_add: for new_t in targets_to_add: res_graph.add_edge(new_s, new_t, h2.target_.edge[s][t]) res_h1 = Homomorphism(h1.source_, res_graph, hom1) res_h2 = Homomorphism(res_graph, h2.target_, hom2) return (res_graph, res_h1, res_h2)
def pushout(h1, h2): if h1.source_ != h2.source_: raise ValueError( "Domain of homomorphism 1 and domain of homomorphism 2 " + "don't match, can't do pushout") hom1 = {} hom2 = {} if type(h1.target_) == TypedGraph: res_graph = TypedGraph() else: res_graph = TypedDiGraph() for node in h1.source_.nodes(): res_graph.add_node( str(h1.mapping_[node]) + "_" + str(h2.mapping_[node]), h1.source_.node[node].type_, merge_attributes(h1.target_.node[h1.mapping_[node]].attrs_, h2.target_.node[h2.mapping_[node]].attrs_, "union")) hom1[h1.mapping_[node]] =\ str(h1.mapping_[node]) + "_" + str(h2.mapping_[node]) hom2[h2.mapping_[node]] =\ str(h1.mapping_[node]) + "_" + str(h2.mapping_[node]) for s, t in h1.source_.edges(): res_graph.add_edge( str(h1.mapping_[s]) + "_" + str(h2.mapping_[s]), str(h1.mapping_[t]) + "_" + str(h2.mapping_[t]), merge_attributes( h1.target_.get_edge(h1.mapping_[s], h1.mapping_[t]), h2.target_.get_edge(h2.mapping_[s], h2.mapping_[t]), "union")) for node in h1.target_.nodes(): if node not in h1.mapping_.values(): res_graph.add_node( str(node) + "_", h1.target_.node[node].type_, h1.target_.node[node].attrs_) hom1[node] = str(node) + "_" for node in h2.target_.nodes(): if node not in h2.mapping_.values(): res_graph.add_node( str(node) + "_", h2.target_.node[node].type_, h2.target_.node[node].attrs_) hom2[node] = str(node) + "_" for s, t in h1.target_.edges(): if s not in h1.mapping_.values() or t not in h1.mapping_.values(): res_graph.add_edge(hom1[s], hom1[t], h1.target_.get_edge(s, t)) if res_graph.is_directed(): if (hom1[s], hom1[t]) not in res_graph.edges(): res_graph.add_edge(hom1[s], hom1[t], h1.target_.get_edge(s, t)) else: if (hom1[s], hom1[t]) not in res_graph.edges() and ( hom1[t], hom1[s]) not in res_graph.edges(): res_graph.add_edge(hom1[s], hom1[t], h1.target_.get_edge(s, t)) for s, t in h2.target_.edges(): if s not in h2.mapping_.values() or t not in h2.mapping_.values(): res_graph.add_edge(hom2[s], hom2[t], h2.target_.get_edge(s, t)) if res_graph.is_directed(): if (hom2[s], hom2[t]) not in res_graph.edges(): res_graph.add_edge(hom2[s], hom2[t], h2.target_.get_edge(s, t)) else: if (hom2[s], hom2[t]) not in res_graph.edges() and ( hom2[t], hom2[s]) not in res_graph.edges(): res_graph.add_edge(hom2[s], hom2[t], h2.target_.get_edge(s, t)) res_h1 = Homomorphism(h1.target_, res_graph, hom1) res_h2 = Homomorphism(h2.target_, res_graph, hom2) return (res_graph, res_h1, res_h2)
def test_homomorphism_attributes_mismatch(self): mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7} self.LHS_.node[1].attrs_.update({'new_attr': 0}) Homomorphism(self.LHS_, self.graph_, mapping)
def test_homomorphism_type_mismatch(self): mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7} cast_node(self.LHS_, 1, 'other_type') Homomorphism(self.LHS_, self.graph_, mapping)
def test_homomorphism_not_covered(self): mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6} Homomorphism(self.LHS_, self.graph_, mapping)
def test_homorphism_init(self): # Test homomorphisms functionality mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7} Homomorphism(self.LHS_, self.graph_, mapping)
def test_homomorphism_edge_attributes_mismatch(self): mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7} self.LHS_.edge[5][6].update({'new_attr': 0}) Homomorphism(self.LHS_, self.graph_, mapping)
def test_homomorphism_connectivity_fails(self): mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7} remove_edge(self.graph_, 4, 5) Homomorphism(self.LHS_, self.graph_, mapping)
def generate_rule(self, LHS, commands): """Cast sequence of commands to homomorphisms.""" command_strings = [c for c in commands.splitlines() if len(c) > 0] actions = [] for command in command_strings: try: parsed = parser.parseString(command).asDict() actions.append(parsed) except: raise ValueError("Cannot parse command '%s'" % command) P = LHS.copy() RHS = LHS.copy() pl_mapping = dict(zip(LHS.nodes(), LHS.nodes())) pr_mapping = dict(zip(LHS.nodes(), LHS.nodes())) # We modify P, RHS and respective mapping # in the course of command parsing for action in actions: if action["keyword"] == "clone": node_name = None if "node_name" in action.keys(): node_name = action["node_name"] cloned_node = clone_node(P, action["node"], node_name) pl_mapping[action["node"]] = action["node"] pl_mapping.update({cloned_node: action["node"]}) clone_node(RHS, action["node"], node_name) elif action["keyword"] == "merge": method = None node_name = None edges_method = None if "method" in action.keys(): method = action["method"] if "node_name" in action.keys(): node_name = action["node_name"] if "edges_method" in action.keys(): edges_method = action["edges_method"] merged_node = merge_nodes( RHS, action["nodes"], method, node_name, edges_method) for node in action["nodes"]: pr_mapping.update({node: merged_node}) elif action["keyword"] == "add_node": name = None node_type = None attrs = {} if "node" in action.keys(): name = action["node"] if "type" in action.keys(): node_type = action["type"] if "attrubutes" in action.keys(): attrs = action["attrubutes"] add_node(RHS, node_type, name, attrs) elif action["keyword"] == "delete_node": remove_node(P, action["node"]) del pl_mapping[action["node"]] del pr_mapping[action["node"]] remove_node(RHS, action["node"]) elif action["keyword"] == "add_edge": attrs = {} if "attrubutes" in action.keys(): attrs = action["attrubutes"] add_edge( RHS, action["node_1"], action["node_2"], attrs) elif action["keyword"] == "delete_edge": remove_edge( P, action["node_1"], action["node_2"]) remove_edge( RHS, action["node_1"], action["node_2"]) elif action["keyword"] == "add_node_attrs": add_node_attrs( RHS, action["node"], action["attributes"]) elif action["keyword"] == "add_edge_attrs": add_edge_attrs( RHS, action["node_1"], action["node_2"], action["attributes"]) elif action["keyword"] == "delete_node_attrs": remove_node_attrs( P, action["node"], action["attributes"]) remove_node_attrs( RHS, action["node"], action["attributes"]) elif action["keyword"] == "delete_edge_attrs": remove_edge_attrs( P, action["node_1"], action["node_2"], action["attributes"]) remove_edge_attrs( RHS, action["node_1"], action["node_2"], action["attributes"]) elif action["keyword"] == "update_node_attrs": remove_node_attrs( P, action["node"], P.node[action["node"]].attrs_) update_node_attrs( RHS, action["node"], action["attributes"]) elif action["keyword"] == "update_edge_attrs": remove_edge_attrs( P, action["node_1"], action["node_2"], P.edge[action["node_1"]][action["node_2"]]) update_edge_attrs( RHS, action["node_1"], action["node_2"], action["attributes"]) else: raise ValueError("Unknown command") h_p_lhs = Homomorphism(P, LHS, pl_mapping) h_p_rhs = Homomorphism(P, RHS, pr_mapping) return (h_p_lhs, h_p_rhs)