""" 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 get_available_types(): """ Returns a list of all types of nodes in all example models """ types = [] for m in commons.all_example_models(): nodes = mv.all_instances(m, "Node") for node in nodes: typ = commons.get_node_type(m, node) types.append(typ) return list(set(types)) def is_edge_supported(from_type, to_type): """ True if an association between from_type and to_type exists in any example model False otherwise """ for m in commons.all_example_models(): nodes_from = commons.all_nodes_with_type(m, from_type) nodes_to = commons.all_nodes_with_type(m, to_type) for nf in nodes_from: for nt in nodes_to: assocs = commons.get_associations_between(m, nf, nt) if assocs: # somewhere in an example model, there is such an association return True return False 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 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