Find and navigate elements

New in v0.11.0

This guide is for a script that has a loaded model and needs specific elements from it: every element of a type, one element by name, or the neighbors of an element it already holds. It uses syside.query, whose calls return plain Python values; you need Syside Automator installed and licensed, and Models as Python objects explains the objects the calls return.

The recipes below run against one small model. Create a file named wheel.sysml and paste it in, or adapt the names to a model of your own:

part wheel{
   part wheel_rim{
       part metal;
       attribute diameter;
       attribute mass = 5;
   }
   part tire{
       part rubber;
       attribute diameter;
       attribute mass = 10;
   }
   attribute mass = wheel_rim.mass + tire.mass;
}

Every script starts by loading the model:

import syside
from syside import query

model, diagnostics = syside.load_model(["wheel.sysml"])

The recipes continue one script: the last two use the tire element the second one finds.

List every element of a type

query.find_elements_of_type returns a list of every element of the given type, standard-library elements excluded:

for part in query.find_elements_of_type(model, syside.PartUsage):
    print(part.name)
wheel
wheel_rim
tire
metal
rubber

Find one element by name

query.find_by_name_and_type returns the element already typed, or None when the model holds no such element, so a script can check the result before using it:

tire = query.find_by_name_and_type(model, "tire", syside.PartUsage)
assert tire is not None
print(tire.name)
tire

List an element’s children

query.contents returns the elements an element owns, as a plain list in declaration order:

for child in query.contents(tire):
    print(child.name)
rubber
diameter
mass

Find the owner

Upward navigation uses .owner on the element classes: the owning element, or None at the root. For the whole chain of owners, query.containers yields each in turn.

assert tire.owner is not None
print(tire.owner.name)
wheel

Every syside.query call is read-only. A script that also edits the model uses the element classes for the writing side, and Rewrite a script with syside.query lists the element-class equivalent of each call used here.