def typed_rule_to_json(hie, g_id, parent): rule = hie.node[g_id].rule json_data = {} json_data["name"] = hie.node[g_id].attrs["name"].to_json() json_data["L"] = graph_to_json(rule.lhs) json_data["P"] = graph_to_json(rule.p) json_data["R"] = graph_to_json(rule.rhs) # json_data["PR"] = [{"left": k, "right": v} # for (k, v) in rule.p_rhs.items()] # json_data["PL"] = [{"left": k, "right": v} # for (k, v) in rule.p_lhs.items()] json_data["PR"] = rule.p_rhs json_data["PL"] = rule.p_lhs if parent is not None: lhs_mapping = hie.edge[g_id][parent].lhs_mapping rhs_mapping = hie.edge[g_id][parent].rhs_mapping _type_json_nodes(json_data["R"]["nodes"], rhs_mapping) p_mapping = compose_homomorphisms(rhs_mapping, rule.p_rhs) _type_json_nodes(json_data["P"]["nodes"], p_mapping) lhs_mapping.update({rule.p_lhs[n]: p_mapping[n] for n in rule.p if rule.p_lhs[n] not in lhs_mapping.keys() and n in p_mapping.keys()}) _type_json_nodes(json_data["L"]["nodes"], lhs_mapping) return json_data
def typed_rule_to_json(hie, g_id, parent): rule = hie.node[g_id].rule json_data = {} json_data["name"] = hie.node[g_id].attrs["name"].to_json() json_data["L"] = graph_to_json(rule.lhs) json_data["P"] = graph_to_json(rule.p) json_data["R"] = graph_to_json(rule.rhs) # json_data["PR"] = [{"left": k, "right": v} # for (k, v) in rule.p_rhs.items()] # json_data["PL"] = [{"left": k, "right": v} # for (k, v) in rule.p_lhs.items()] json_data["PR"] = rule.p_rhs json_data["PL"] = rule.p_lhs if parent is not None: lhs_mapping = hie.edge[g_id][parent].lhs_mapping rhs_mapping = hie.edge[g_id][parent].rhs_mapping _type_json_nodes(json_data["R"]["nodes"], rhs_mapping) p_mapping = compose_homomorphisms(rhs_mapping, rule.p_rhs) _type_json_nodes(json_data["P"]["nodes"], p_mapping) lhs_mapping.update({ rule.p_lhs[n]: p_mapping[n] for n in rule.p if rule.p_lhs[n] not in lhs_mapping.keys() and n in p_mapping.keys() }) _type_json_nodes(json_data["L"]["nodes"], lhs_mapping) return json_data
def to_json(self): """Convert the rule to JSON repr.""" json_data = {} json_data["lhs"] = primitives.graph_to_json(self.lhs) json_data["p"] = primitives.graph_to_json(self.p) json_data["rhs"] = primitives.graph_to_json(self.rhs) json_data["p_lhs"] = self.p_lhs json_data["p_rhs"] = self.p_rhs return json_data
def replace_hierachy2(path_to_graph=""): hierarchy = request.json print("hie", hierarchy) for g in hierarchy["graphs"]: if "attrs" not in g.keys(): g["attrs"] = g["graph"]["attrs"] kami_json = {} kami_json["graph"] = graph_to_json(untypedkami) kami_json["id"] = "kami" kami_json["attrs"] = {"name": "kami"} hierarchy["graphs"].append(kami_json) new_typings = [] to_ag_functions = {} for mapping in hierarchy["typing"]: if mapping["to"] == "action_graph": to_ag_functions[mapping["from"]] = mapping["mapping"] else: new_typings.append(mapping) hierarchy["typing"] = new_typings new_hie = ServerHierarchy.from_json(SERVER._hie.__class__, hierarchy) new_hie.add_graph("kami_base", untyped_base_kami, {"name": "kami_base"}) new_hie.add_typing("kami_base", "/", {}, total=False) new_hie.add_typing("kami", "kami_base", kami_basekami, total=True) new_action_graph(new_hie, to_ag_functions) SERVER._hie = new_hie return ("hierarchy replaced", 200)
def typed_graph_to_json(hie, g_id, parent): if hie.node[g_id].graph is None: return None json_data = graph_to_json(hie.node[g_id].graph) generated_list = [] for k, v in hie.node[g_id].attrs.items(): try: generated_list.append((k, v.to_json())) except: generated_list.append((k, v)) json_data["attributes"] = (generated_list) if parent is not None: typing = hie.edge[g_id][parent].mapping _type_json_nodes(json_data["nodes"], typing) return json_data
def to_json(self): """Create a JSON representation of the object.""" return { "graph": graph_to_json(self.graph), "attrs": self.attrs_to_json() }