""" 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 from PyQt5.Qt import QRect, QPoint 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_model(type_id, icon_type, overwrite=False): # type: (str, IconType, bool) -> str """ Add a new concrete syntax model for type "type_id" and representation type "icon_type" to the modelverse. If overwrite, an already existing CS model for the same type is deleted. Returns the path of the newly created model. """ csm = "models/consyn/" + type_id try: mv.model_add(csm, "formalisms/consynMM") except mv.ModelExists: if not overwrite: return "" else: mv.model_delete(csm) mv.model_add(csm, "formalisms/consynMM") icon = mv.instantiate(csm, "Icon") mv.attr_assign(csm, icon, "typeID", type_id) if icon_type == IconType.PRIMITIVE: mv.attr_assign(csm, icon, "is_primitive", True) else: mv.attr_assign(csm, icon, "is_primitive", False) return csm def del_concrete_syntax_model(type_id): """ Delete a concrete syntax model. Returns True if success, False otherwise. """ csm = "models/consyn/" + type_id if csm not in commons.all_consyn_models(): return False mv.model_delete(csm) return True def add_rect_to_cs(csm, rect): # type: (str, QRect) -> None rect_id = mv.instantiate(csm, "Rectangle") mv.attr_assign(csm, rect_id, "x", rect.topLeft().x()) mv.attr_assign(csm, rect_id, "y", rect.topLeft().y()) mv.attr_assign(csm, rect_id, "width", rect.width()) mv.attr_assign(csm, rect_id, "height", rect.height()) def add_ellipse_to_cs(csm, rect): # type: (str, QRect) -> None elp_id = mv.instantiate(csm, "Ellipse") mv.attr_assign(csm, elp_id, "x", rect.topLeft().x()) mv.attr_assign(csm, elp_id, "y", rect.topLeft().y()) mv.attr_assign(csm, elp_id, "width", rect.width()) mv.attr_assign(csm, elp_id, "height", rect.height()) def add_line_to_cs(csm, p1, p2): # type: (str, QPoint, QPoint) -> None line_id = mv.instantiate(csm, "Line") mv.attr_assign(csm, line_id, "start_x", p1.x()) mv.attr_assign(csm, line_id, "start_y", p1.y()) mv.attr_assign(csm, line_id, "end_x", p2.x()) mv.attr_assign(csm, line_id, "end_y", p2.y()) 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 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 def delete_instance_model(model): """ Deletes the instance model "model". Returns True on success, False otherwise. """ if model not in commons.all_instance_models(): return False mv.model_delete(model) return True def delete_example_model(model): """ Deletes the example model "model". Should be empty since no evolution handling is done here. Returns True on success, False otherwise. """ if model not in commons.all_example_models(): return False mv.model_delete(model) return True