Access Capella model metadata (from Capella Library)

Hi,

I’m trying to retrieve the valued elements of a Property Value Group (PVG), which is used as metadata. In my script, I used PVG.get_valued_elements(), but it doesn’t return all the elements associated with the PVG.

Example: When I view the “Valued Elements” of the PVG in the semantic browser, I can see both the PVG it’s attached to and several Component Ports that are referenced from another model. However, when I print the output to the console, I only get the PVGs from the library, not the Component Ports.

I have two projects: one is the main model, and the other is a library that the main model references. The Component Ports visible under the PVG in the library are linked to Component Ports in the main model. I also tried using capella_query_by_name(pvg, "Valued Elements"), but I still couldn’t retrieve the Component Ports.

Is there another way to fetch these referenced Component Ports?

Thanks,

If you have the result you are looking for in the semantic browser view, you can call the corresponding query by its name (in bold in the semantic view). For instance:

myCapellaElement.get_query_result("Allocated Interactions")

You will need to replace the query name.

Hi,

Thanks for your response. It seems that get_query_result() provides the same output as capella_query_by_name(). Below are a few snippets from the Capella library I’m working with.

When checking the valued elements of PVG 1 in the Semantic Browser, I can see CP1, CP2, and PropertyValueGroup 1.
However, when using scripting, myCapellaElement.get_query_result("Valued Elements") only returns PropertyValueGroup 1 in the console.

Is there any other way to fetch all the valued elements? For reference, CP1 and CP2 in the snippet refer to elements in the IFE model.

Thanks.

Did you open the library project directly with Python for Capella or did you open the project that reference this library and then navigate to it ? In the first case it should work, in the second one the library might be loaded in a way that is not expected by Capella.

I think proxy EObject (when a reference can’t be resolved) could be filtered out by Python for Capella. A test you can do to see if it’s the case:

for eObj in getSBQuery(pvg.get_java_object(), "Valued Elements"):
    print(eObj.toString())

And look for the number of output and if there is a proxyURI defined. You can past the output here so I can have a look. You should not need to import Capella_API.py in your script but just in case the function getSBQuery() is defined there.

I’ve noticed that when the library project is opened directly with Python for Capella, it doesn’t show the referenced elements (e.g., CP1, CP2); it only displays PropertyValueGroup 1. However, when I open the library project through the main project (IFE model), I can see both the references and PropertyValueGroup 1. Is it because the library hasn’t been referenced correctly?

I’m currently running the script on the library project, and I suspect that’s why I’m only retrieving PropertyValueGroup1 and not the referenced elements.

I tried the getSBQuery() method and received the following output:

org.polarsys.capella.core.data.capellacore.impl.PropertyValueGroupImpl@2ca0e36e 
(id: 28f7739a-1200-ee754g6a20p, sid: null) (name: PropertyValueGroup1) 
(visibleInDoc: true, visibleInLM: true, summary: null, review: null)

The main goal of fetching these references from the library is to check whether the main project has any libraries attached to it and if attached, access the library and fetch some Capella elements from it. If there’s a better way through scripting to identify whether the main project (e.g., IFE) depends on a Capella library, I would appreciate your suggestions.

Thanks,

From your results the library is loaded properly. My guess is referenced elements are referenced when the library is referenced in a given project.

You can list all libraries referenced by a Capella project:

myCapellaModel.get_referenced_libraries()

Note that accessing the library this way may have some limits like no access to diagrams from the library. Also if the library is loaded with Python for Capella from the library project elements wont by equals in memory. You will need to use object ids for instance.

Thanks. I’ve used get_referenced_libraries() in the past, but encountered errors. I wasn’t sure whether the issue was with the API or the model, as I wasn’t able to retrieve all the referenced libraries. Below are the code snippet and the error I got. I am using Capella 1.4.2 version.

Are the errors related to the internal API methods?

Thanks,

Looking a the code I don’t think it can work… you can try to fix it like that in Capella_API.py (wrapping in an EObject):

def get_libraries(system_engineering):
    """Gets the list of libraries for the given system engineering"""
    res = []
    
    if system_engineering is not None:
        lib_cls = getattr(sys.modules["__main__"], "CapellaLibrary")
        e_object_cls = getattr(sys.modules["__main__"], "EObject")
        for value in getLibraries(system_engineering.get_java_object()):
            lib = lib_cls()
            lib.open(e_object_cls(value))
            res.append(lib)
        
    return res

or you can try using the Java API directly:

for value in getLibraries(system_engineering.get_java_object()):
    print(value.getName())

The Java code behind that make me think the library loading should work fine. We should not have some of the issues I described above.

Let me know if the first fix works, I’ll patch the code.

I tried using the Java API directly and was able to fetch the attached referenced libraries. I also attempted to modify the get_libraries() function in Capella_API.py based on your code changes (used myModel.get_referenced_libraries()), but it didn’t work for me—I encountered the error shown below.

Thanks for your input.

1 Like

Thank you for the feedback… I’ll look into this, I tracked this discussion on the corresponding issue.

Hi,

Just a follow-up query to my earlier question — is there a way to identify all Capella models that reference a particular Capella library? For example, I have two models, Model 1 and Model 2, and both reference the same Capella library. If I have access to the .aird file or the systems engineering attribute of the library, can I programmatically determine which models reference it?

Alternatively, is there a way to programmatically access all the projects/models listed in the Project Explorer? If so, I could iterate through each model and use getLibraries() to identify the referenced libraries.

Thanks,

Hi,

Is there a way to fetch references (from a Capella model) to a Capella library using AQL queries—whether by selecting the .aird file, the Systems Engineering model, or another entry point in the library? If so, I’d like to try converting it into a script programmatically.

Any input would be appreciated.

Thanks.

You will need to load all Capella projects in the workspace and for each list referenced libraries.

You can try to list all `.aird` files with the EASE resource module:

loadModule('/System/Resources')

for aird_file in findFiles("*.aird"):
    # try to open the CapellaModel
    # some .aird files might not be in a Capella project