""" Modelverse operations used by the UI """ import sys sys.path.append("..") import wrappers.modelverse as mv import commons from sketchUI.graphics_node_item import IconType def add_node(model, node_type): """ Adds new node to model "model" with type attribute "node_type" """ node_id = mv.instantiate(model, "Node") mv.attr_assign(model, node_id, "typeID", node_type) return node_id def add_edge(model, from_id, to_id, directed=False): """ Adds an edge to model "model" between the nodes "from_id" and "to_id" """ edge_id = mv.instantiate(model, "Edge", (from_id, to_id)) mv.attr_assign(model, edge_id, "directed", directed) return edge_id def delete_node(model, node_id): mv.delete_element(model, node_id) def delete_edge(model, edge_id): mv.delete_element(model, edge_id) def get_consyn_of(node_type): """ Queries the modelverse for a concrete syntax of elements of type "node_type". Returns the list of model elements by mv.element_list_nice or an empty list if no concrete syntax exists. """ for csm in commons.all_consyn_models(): if csm.split("/")[-1] == node_type: return mv.element_list_nice(csm) return [] def new_concrete_syntax(type_id, icon_type): # type: (str, IconType) -> str """ Add a new concrete syntax model for type "type_id" and representation type "icon_type" to the modelverse """ csm = "models/consyn/" + type_id try: mv.model_add(csm, "formalisms/consynMM") except mv.ModelExists: return "" icon = mv.instantiate(csm, "Icon") if icon_type == IconType.PRIMITIVE: mv.attr_assign(csm, icon, "is_primitive", True) mv.instantiate(csm, "PrimitiveGroup") else: mv.attr_assign(csm, icon, "is_primitive", False) return csm def add_attribute(model, node_id, key, val): """ Adds an attribute to node_id in model with key and value """ attr_id = mv.instantiate(model, "Attribute") mv.attr_assign(model, attr_id, "key", key) mv.attr_assign(model, attr_id, "value", val) mv.instantiate(model, "NodeAttribute", (node_id, attr_id)) def is_attributing_allowed(node_type): """ Check if attributing is allowed for a node of type node_type. Goes through all example models and checks if they have attributes. """ for exm in commons.all_example_models(): if commons.get_all_attributes_of_type(exm, node_type): return True return False def update_attribute_val(model, node_id, key, new_val): """ Update the attribute identified by its key of node node_id in model to new_val """ outgoings = mv.read_outgoing(model, node_id, "NodeAttribute") for link in outgoings: attr = mv.read_association_destination(model, link)[0] attr_key = mv.read_attrs(model, attr)["key"] if attr_key == key: mv.attr_assign(model, attr, "value", new_val) break def delete_attribute_from_node(model, node_id, key): """ Deletes the attribute identified by its key of node node_id in model """ outgoings = mv.read_outgoing(model, node_id, "NodeAttribute") for link in outgoings: attr = mv.read_association_destination(model, link)[0] attr_key = mv.read_attrs(model, attr)["key"] if attr_key == key: mv.delete_element(model, attr) break