import re from wrappers import modelverse as mv def all_models(): """ Returns a list of paths of all example- and instance models """ return all_instance_models() + all_example_models() def all_instance_models(): """ Returns a list of paths of all instance models """ try: instance_models = mv.model_list("models/instance") except mv.UnknownLocation: # no instance models return [] instance_models_full = ["models/instance/"+m for m in instance_models] return instance_models_full def all_example_models(): """ Returns a list of paths of all example models """ example_models = mv.model_list("models/example") example_models_full = ["models/example/"+exm for exm in example_models] return example_models_full def all_nodes_with_type(model, typ): """ Returns a list of nodes in model model with type typ """ all_nodes = mv.all_instances(model, "Node") ret = [node for node in all_nodes if mv.read_attrs(model, node)["typeID"] == typ] return ret def get_node_type(model, node): """ Returns the type attribute of node in model as string""" return mv.read_attrs(model, node)["typeID"] def get_associations_between(model, node1, node2): """ Returns a list of association IDs between the nodes node1 and node2 """ edges_n1 = mv.read_outgoing(model, node1, "Edge") edges_n1.update(mv.read_incoming(model, node1, "Edge")) edges_n2 = mv.read_outgoing(model, node2, "Edge") edges_n2.update(mv.read_incoming(model, node2, "Edge")) return list(edges_n1.intersection(edges_n2)) def new_instance_model(): """ Adds a new, empty instance model to the Modelverse. Returns the name of the new model """ existing_models = all_instance_models() idx = 1 nums = [] for model in existing_models: m = model.split("/")[-1] try: idx = int(re.search(r'\d+', m).group()) nums.append(idx) except AttributeError: pass if nums: idx = sorted(nums)[-1] + 1 im = "models/instance/im{}".format(idx) print("Adding new instance model {}".format(im)) mv.model_add(im, "formalisms/graphMM") mid = mv.instantiate(im, "Model") mv.attr_assign(im, mid, "is_example", False) mv.attr_assign(im, mid, "descr", "") return im def new_example_model(): """ Adds a new, empty example model to the Modelverse. Returns the name of the new model """ existing_models = all_example_models() idx = 1 nums = [] for model in existing_models: m = model.split("/")[-1] try: idx = int(re.search(r'\d+', m).group()) nums.append(idx) except AttributeError: pass if nums: idx = sorted(nums)[-1] + 1 exm = "models/instance/ex{}".format(idx) print("Adding new example model {}".format(exm)) mv.model_add(exm, "formalisms/graphMM") mid = mv.instantiate(exm, "Model") mv.attr_assign(exm, mid, "is_example", True) mv.attr_assign(exm, mid, "descr", "") return exm