evolution.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import wrappers.modelverse as mv
  2. def _get_nodes_with_type(model, typ):
  3. """ Returns a list of nodes in model model with type typ """
  4. all_nodes = mv.all_instances(model, "Node")
  5. ret = [node for node in all_nodes if mv.read_attrs(model, node)["type"] == typ]
  6. return ret
  7. def _get_all_models():
  8. """ Returns a list of paths of all example- and instance models """
  9. example_models = mv.model_list("models/example")
  10. example_models_full = ["models/example/"+exm for exm in example_models]
  11. instance_models = mv.model_list("models/instance")
  12. instance_models_full = ["models/instance/"+m for m in instance_models]
  13. return example_models_full + instance_models_full
  14. def retype_node(model, node, new_type, local):
  15. """
  16. Retype the node in model to new_type.
  17. If local is true, the change only affects the single node.
  18. If local is false, the change affects all nodes in all models.
  19. """
  20. if local:
  21. mv.attr_assign(model, node, "type", new_type)
  22. else:
  23. node_type_old = mv.read_attrs(model, node)["type"]
  24. all_models = _get_all_models()
  25. for m in all_models:
  26. nodes = _get_nodes_with_type(m, node_type_old)
  27. for node in nodes:
  28. mv.attr_assign(m, node, "type", new_type)
  29. def add_node(model, node_type, local):
  30. """
  31. Add a new node with ID node and type node_type to model model.
  32. If local is true, the node is only added to the passed model.
  33. If local is false, the node will be added to all models.
  34. """
  35. if local:
  36. ID = mv.instantiate(model, "Node")
  37. mv.attr_assign(model, ID, "type", node_type)
  38. else:
  39. all_models = _get_all_models()
  40. for m in all_models:
  41. ID = mv.instantiate(model, "Node")
  42. mv.attr_assign(model, ID, "type", node_type)