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 test_load_graph_undir(self): __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) filename = os.path.join(__location__, "graph_example.json") a = TypedGraph() a.load(filename) assert_equals(a.nodes(), [1, 2, 3]) assert_edges_undir(a.edges(), [(1, 2), (2, 3), (3, 1)]) assert_equals(a.node[1].type_, "agent") assert_equals(a.node[2].type_, "agent") assert_equals(a.node[3].type_, "action") assert_equals(a.node[1].attrs_, {"u": {1}, "k": {33}}) assert_equals(a.node[2].attrs_, None) assert_equals(a.node[3].attrs_, {"x": {33, 55, 66}})
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 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 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_load_export(self): __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) filename = os.path.join(__location__, "graph_example.json") a = TypedGraph() a.load(filename) out_filename = os.path.join(__location__, "output_graph.json") a.export(out_filename) b = TypedGraph() b.load(out_filename) assert_equals(a.nodes(), b.nodes()) assert_edges_undir(a.edges(), b.edges()) assert_equals(a.node[3].attrs_, b.node[3].attrs_)
def test_init_with_metamodel_undirected(self): meta_meta = TypedGraph() meta_meta.add_node("agent", "node") meta_meta.add_node("action", "node") meta_meta.add_edges_from([ ("agent", "agent"), ("action", "action"), ("action", "agent")]) meta = TypedGraph(meta_meta) meta.add_node("protein", "agent") meta.add_node("region", "agent") meta.add_node("action", "agent") meta.add_edges_from([ ("protein", "protein", {'a': 1}), ("region", "region"), ("action", "action"), ("protein", "region", {'a': 2}), ("action", "region"), ]) assert_equals( meta.edge["protein"]["region"], meta.edge["region"]["protein"]) graph = TypedGraph(meta) graph.add_nodes_from([ (1, "protein"), (2, "region"), (3, "action"), (4, "region"), (5, "protein"), (6, "region"), (7, "protein")]) graph.add_edge(2, 1, {'x': 1}) graph.add_edge(2, 3, {'x': 2}) graph.add_edge(4, 3, {'x': 3}) graph.add_edge(4, 5, {'x': 4}) graph.add_edge(6, 3, {'x': 5}) graph.add_edge(6, 7, {'x': 6}) assert_equals(graph.edge[1][2], graph.edge[2][1])
def test_rule_to_homomorphism(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([(1, "action"), (2, "agent"), (3, "agent")]) LHS.node[2].set_attrs({"u": 0}) LHS.add_edges_from([(1, 2), (2, 3)]) LHS.set_edge(2, 3, {"k": {1, 2}}) instances = rw.find_matching(LHS) rw.add_node_attrs(instances[0], 1, {"u": 55, "x": 0}) h1, h2 = rw.generate_rule( LHS, """delete_node 1. clone 2 as 'clone'. delete_node_attrs 'clone' {'u': 0}. delete_edge 2 3. delete_edge_attrs 'clone' 3 {'k': {1}}. update_edge_attrs 'clone' 3 {'t': 333}. update_node_attrs 2 {'u': {12, 13}}. merge ['clone', 3] as 'merged'. add_node_attrs 'merged' {'m': 1}. add_node 'new_node' type 'region'. add_node_attrs 'new_node' {'x': 1}. add_edge 'new_node' 'merged'. add_edge_attrs 'merged' 'new_node' {'j': 33}.""") RHS_instance = rw.apply_rule(instances[0], h1, h2) plot_instance(rw.graph_, h2.target_, RHS_instance, filename=os.path.join(__location__, "undir_rule_to_hom_RHS.png"))
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 test_undirected_imp_init(self): __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) g = TypedGraph() g.add_node(1, "agent", {"name": "John"}) g.add_node(2, "action") g.add_node(3, "agent", {"name": "Paul"}) g.add_edges_from([(1, 2), (3, 2)]) g.set_edge(1, 2, {"a": 0}) rw = Rewriter(g) LHS = TypedGraph() LHS.add_nodes_from([(1, "agent"), (2, "action")]) LHS.add_edges_from([(1, 2)]) instances = rw.find_matching(LHS) for i, instance in enumerate(instances): plot_instance( rw.graph_, LHS, instance, os.path.join(__location__, "undir_instance_%d.png" % i)) rw.add_node(instances[0], 'region', 'Europe', {"a": 44}) rw.delete_node(instances[0], 1) rw.delete_edge(instances[0], 2, 3) rw.clone(instances[0], 2) cast_node(rw.graph_, "Europe", "action") rw.merge(instances[0], ["Europe", 2]) plot_graph(rw.graph_, filename=os.path.join(__location__, "undir_cloned.png"))
def test_init_with_metamodel_undirected(self): meta_meta = TypedGraph() meta_meta.add_node("agent", "node") meta_meta.add_node("action", "node") meta_meta.add_edges_from([("agent", "agent"), ("action", "action"), ("action", "agent")]) meta = TypedGraph(meta_meta) meta.add_node("protein", "agent") meta.add_node("region", "agent") meta.add_node("action", "agent") meta.add_edges_from([ ("protein", "protein", { 'a': 1 }), ("region", "region"), ("action", "action"), ("protein", "region", { 'a': 2 }), ("action", "region"), ]) assert_equals(meta.edge["protein"]["region"], meta.edge["region"]["protein"]) graph = TypedGraph(meta) graph.add_nodes_from([(1, "protein"), (2, "region"), (3, "action"), (4, "region"), (5, "protein"), (6, "region"), (7, "protein")]) graph.add_edge(2, 1, {'x': 1}) graph.add_edge(2, 3, {'x': 2}) graph.add_edge(4, 3, {'x': 3}) graph.add_edge(4, 5, {'x': 4}) graph.add_edge(6, 3, {'x': 5}) graph.add_edge(6, 7, {'x': 6}) assert_equals(graph.edge[1][2], graph.edge[2][1])
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 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)