Main Content

getOwnershipHierarchy

R2026b

Get elements in ownership hierarchy of element

Since R2026a

    Description

    elements = getOwnershipHierarchy(workspace,element) gets all the elements in the ownership hierarchy of the element specified by element, which must be in the SysML® v2 workspace specified by workspace.

    example

    Examples

    collapse all

    This example shows how to connect to a SysML v2 repository, retrieve projects, and navigate elements using the System Composer™ SysML v2 API.

    This example assumes a SysML v2 project with specific content described by this SysML v2 text.

    package test1 {
        part def myArch;
        part def subArch :> myArch;
        part def myTopArch {
          part A:subArch;
        }
    }

    If you have access to a SysML v2 editor, you can publish SysML v2 text into a repository.

    When published into a repository, the SysML v2 text is translated into SysML v2 elements you can access via the getElements function. First, connect to the server. If your server supports authentication via bearer token, then provide your token as the credentials argument systemcomposer.sysml.Repository.connect(serverUrl,credentials="my-token").

    serverUrl = 'http://sysml-example.home.com/api/v1/sysml/';
    repo = systemcomposer.sysml.Repository.connect(serverUrl)
    repo = 
    
      Repository with properties:
    
        BaseUri: 'http://sysml-example.home.com/api/v1/sysml'

    You can obtain information about all the projects in the repository by calling getProjects. Alternatively, if you know the project ID you want to use, you can retrieve the project directly by calling getProjectById.

    allProjects = repo.getProjects();
    projectToUse = repo.getProjectById('398687a6-cc37-4ef7-8e74-c30dd635181a')
    projectToUse = 
    
      Project with properties:
    
        ResourceIdentifier: 'http://sysml-example.home.com/api/v1/sysml/projects/398687a6-cc37-4ef7-8e74-c30dd635181a'
                        Id: '398687a6-cc37-4ef7-8e74-c30dd635181a'
               Description: 'Connect to Repository and Get Elements example'
                      Name: 'test1_docexample'

    To look at the elements of a project, first create a workspace for a specified commit in the project by calling createWorkspace. If you call createWorkspace with no arguments, the created workspace is anchored to the head commit of the default branch and retrieves all elements. Alternatively, if you know the branch that you are working with, then you can provide the branch ID by calling createWorkspace(branchId="123456").

    myFullWorkspace = projectToUse.createWorkspace()
    myFullWorkspace = 
      Workspace with no properties.

    Calling getElements returns a list of all the elements in the snapshot for the anchored commit for the workspace and retrieves all elements that were not retrieved upon workspace creation. The output shows that this snapshot has 13 elements, which populates a local cache of the snapshot, organized as a graph based on the SysML v2 meta-model.

    allElements = myFullWorkspace.getElements()
    
    allElements =
    
      1×13 Collection array with no properties.

    You can now write code to inspect the elements of the workspace. For example, you can find all the elements that are of type Package. In this example, only one element, test1, has this type.

    theOnlyPackage = allElements.Package;
    theOnlyPackage.declaredName
    ans = 
    
        "test1"

    From any element, you can navigate to related elements using the SysML v2 properties of that element. For example, you can look at one of the members of the package.

    allOwnedMembers = theOnlyPackage.ownedMember;
    aMember = allOwnedMembers(3);
    aMember.declaredName
    ans = 
    
        "myTopArch"

    This member is a part definition that contains a single part. You can find that part and its type. The owner of this type is the original package.

    thePart = aMember.ownedFeature(1);
    thePart.declaredName
    ans = 
    
        "A"
    thePartsType = thePart.type(1);
    thePartsType.declaredName
    ans = 
    
        "subArch"
    backToTheStart = isequal(thePartsType.owner,theOnlyPackage)
    backToTheStart =
    
      logical
    
       1

    Elements are represented using typed MATLAB® classes such as sysml.PartUsage, sysml.PartDefinition, and sysml.Package that reflect the SysML v2 meta-model. Use class to get the type of an element.

    class(thePart)
    ans =
    
        'sysml.PartUsage'
    class(thePartsType)
    ans =
    
        'sysml.PartDefinition'

    The class hierarchy follows the SysML v2 specialization hierarchy, so you can use isa to test for a general type. For example, a part usage is a kind of usage.

    isa(thePart,"sysml.Usage")
    ans =
    
      logical
    
       1

    Each element has the properties that the SysML v2 meta-model defines for its type, including inherited properties. To list them, use properties. To display them with their values, enter the variable name, for example thePart.

    numel(properties(thePart))
    ans =
    
       219

    To learn more about how the elements are connected, get the element relationships around the PartUsage element.

    elemRels = getElementRelationships(myFullWorkspace,thePart)
    elemRels =
    
      1×2 Collection array with no properties.

    Confirm that the element thePart is the only element of type PartUsage. The executeQuery function returns a sysml.Collection object, so index into the result to compare elements.

    queryString = '{"@type":"Query","name":"string","where":{"@type":"PrimitiveConstraint","operator":"=","property":"@type","value":["PartUsage"]}}';
    queryResult = executeQuery(myFullWorkspace,queryString);
    flag = isequal(queryResult(1),thePart)
    flag =
    
      logical
    
       1

    You can also get an element directly from its ID by using getElement.

    sameElement = getElement(myFullWorkspace,thePart.elementId);
    isequal(sameElement,thePart)
    ans =
    
      logical
    
       1

    Writing code is the most flexible approach to finding elements, but you can also get element relationships by looking at the root elements of a project. Root elements have no owner. In this case, the project has only one root element, which has a single member, the package test1.

    root = myFullWorkspace.getRootElements()
    root =
    
      Collection with no properties.
    rootMember = root(1).ownedMember;
    rootMember(1).declaredName
    ans = 
    
        "test1"

    You can also find elements with a given meta type and name. The getElementsByTypeAndName function returns a sysml.Collection object, even when only one element matches.

    theTopArch = myFullWorkspace.getElementsByTypeAndName('PartDefinition','myTopArch');
    theTopArch.declaredName
    ans = 
    
        "myTopArch"

    Once the elements for a given commit are in the cache, you can explore the element graph for the project in MATLAB. You can obtain some collections of elements via a single function call. For example, you can get all the members of a namespace by using getMembershipHierarchy. The first element in the returned vector is the source element. You can also retrieve the ownership hierarchy of an element by calling getOwnershipHierarchy. The first element of the vector is the source element.

    myMembers = myFullWorkspace.getMembershipHierarchy(theOnlyPackage)
    myMembers =
    
      1×5 Collection array with no properties.
    myOwners = myFullWorkspace.getOwnershipHierarchy(thePart)
    myOwners =
    
      1×4 Collection array with no properties.

    You can also easily classify elements in SysML v2. You can find the descendants of a definition by using the getDescendants method. In this case, subArch is the only descendant. Because getElementsByTypeAndName returns a collection, index into it to pass a single element to getDescendants.

    myArch = myFullWorkspace.getElementsByTypeAndName('PartDefinition','myArch');
    mySubDefinitions = myFullWorkspace.getDescendants(myArch(1))
    mySubDefinitions =
    
      1×2 Collection array with no properties.
    mySubDefinitions(end).declaredName
    ans = 
    
        "subArch"

    You can find the ancestors of a definition by calling getAncestors. In this case, myArch is the only ancestor.

    subArch = myFullWorkspace.getElementsByTypeAndName('PartDefinition','subArch');
    mySuperDefinitions = myFullWorkspace.getAncestors(subArch(1))
    mySuperDefinitions =
    
      1×2 Collection array with no properties.
    mySuperDefinitions(end).declaredName
    ans = 
    
        "myArch"

    You can also find all the usages of a given definition by using the getUsages method. In this project, only one definition has any usages, subArch, which has a PartUsage A.

    theOnlyUsage = myFullWorkspace.getUsages(subArch(1));
    theOnlyUsage(1).declaredName
    ans = 
    
        "A"

    Input Arguments

    collapse all

    Workspace, specified as a systemcomposer.sysml.Workspace object.

    Element, specified as a sysml.Element object. Specify a single element. To use an element from a sysml.Collection object, index into the collection, such as elements(1).

    Output Arguments

    collapse all

    Collection of elements, returned as a sysml.Collection object.

    More About

    collapse all

    Version History

    Introduced in R2026a

    expand all