The code is pretty much the same as yours, I only changed the line ‘main_architectures = [se.get_system_analysis()]’. This is the code:
# Import necessary modules for the Capella modeller API
include('workspace://Python4Capella/simplified_api/capella.py')
if False:
from simplified_api.capella import *
def createDialog():
pass
def createScrolledComposite():
pass
def pushComposite():
pass
def popComposite():
pass
def createView(label_string, uri, targetview, position_wrt_target):
pass
def createListViewer():
pass
def executeUI():
pass
loadModule('/System/UI Builder')
loadModule('/System/UI')
# Define a function to recursively get architecture elements and their hierarchy
def get_architecture_elements(element, level=0):
elements = []
indent = " " * level
# Get the name of the element, or use a default name if not available
element_name = getattr(element, "get_name", lambda: f"Unnamed Element ({type(element).__name__})")()
elements.append(f"{indent}{element_name}")
# Get the children of the element if available
children = element.get_contents() if hasattr(element, "get_all_contents") else []
for child in children:
elements.extend(get_architecture_elements(child, level + 1))
return elements
# Define a function to get the model hierarchy
def get_model_hierarchy():
aird_path = '/In-Flight Entertainment System/In-Flight Entertainment System.aird'
model = CapellaModel()
model.open(aird_path)
# Get the SystemEngineering and print its name
se = model.get_system_engineering()
main_architectures = [se.get_system_analysis()]
unique_elements = set()
hierarchy = []
# Iterate through the main architectures and get their elements
for architecture in main_architectures:
if architecture is not None:
elements = get_architecture_elements(architecture)
for element in elements:
if element not in unique_elements:
unique_elements.add(element)
hierarchy.append(element)
return hierarchy
# Define a class for the model tree dialog
class ModelTreeDialog:
def __init__(self):
self.listViewer = None
self.elements = get_model_hierarchy()
print("ModelTreeDialog instance created")
# Build the UI components for the dialog
def build(self):
print("Building UI components...")
scrolledComposite = createScrolledComposite()
pushComposite(scrolledComposite)
self.listViewer = createListViewer(self.elements)
print("List viewer created:", self.listViewer)
popComposite() # Pop the scrolled composite to return to the main composite
# Create an instance of the ModelTreeDialog class
dialog_instance = ModelTreeDialog()
print("about to create the dialog")
# Create and execute the dialog
javaDialog = createDialog("dialog_instance.build()", "Capella Model Hierarchy", "Browse architectures and their elements")
result = executeUI("javaDialog.open()")
Regardless of the architecture I choose to read elements from, I always get this error:
org.eclipse.ease.ScriptExecutionException: Traceback (most recent call last):
File "workspace://TRY/src/TreeViewer.py", line 83, in <module>
Tries to convert the given value using different strategies.
File "workspace://TRY/src/TreeViewer.py", line 67, in __init__
File "workspace://TRY/src/TreeViewer.py", line 55, in get_model_hierarchy
def _pyease_patch_builtins(name, value):
File "workspace://TRY/src/TreeViewer.py", line 36, in get_architecture_elements
from six import integer_types as _pyease_integer_types
File "workspace://TRY/src/TreeViewer.py", line 36, in get_architecture_elements
from six import integer_types as _pyease_integer_types
File "workspace://TRY/src/TreeViewer.py", line 36, in get_architecture_elements
from six import integer_types as _pyease_integer_types
File "workspace://TRY/src/TreeViewer.py", line 30, in get_architecture_elements
from py4j.java_gateway import JavaClass as _pyease_JavaClass
File "workspace://Python4Capella/simplified_api/capella.py", line 348, in get_name
filtered = {
File "C:\Users\User\Desktop\capella-7.0.0.202407091438-win32-win32-x86_64\capella\plugins\py4j-python_0.10.9.5-bnd-2odeag\src\py4j\java_gateway.py", line 1322, in __call__
answer, self.gateway_client, self.target_id, self.name)
File "C:\Users\User\Desktop\capella-7.0.0.202407091438-win32-win32-x86_64\capella\plugins\py4j-python_0.10.9.5-bnd-2odeag\src\py4j\protocol.py", line 332, in get_return_value
format(target_id, ".", name, value))
py4j.protocol.Py4JError: An error occurred while calling o2492.getName. Trace:
py4j.Py4JException: Method getName([]) does not exist
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
at py4j.Gateway.invoke(Gateway.java:274)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.ClientServerConnection.sendCommand(ClientServerConnection.java:244)
at py4j.CallbackClient.sendCommand(CallbackClient.java:384)
at py4j.CallbackClient.sendCommand(CallbackClient.java:356)
at py4j.reflection.PythonProxyHandler.invoke(PythonProxyHandler.java:106)
at jdk.proxy12/jdk.proxy12.$Proxy28.executeScript(Unknown Source)
at org.eclipse.ease.lang.python.py4j.internal.Py4jScriptEngine.internalExecute(Py4jScriptEngine.java:240)
at org.eclipse.ease.lang.python.py4j.internal.Py4jScriptEngine.execute(Py4jScriptEngine.java:227)
at org.eclipse.ease.AbstractScriptEngine.inject(AbstractScriptEngine.java:189)
at org.eclipse.ease.AbstractScriptEngine.run(AbstractScriptEngine.java:242)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
If I try to read operational analysis from a custom project, it works, but not with system, logical or physical.
If I change the line
element_name = getattr(element, "get_name", lambda: f"Unnamed Element ({type(element).__name__})")()
with
element_name = getattr(element, "getName", lambda: f"Unnamed Element ({type(element).__name__})")()
I don’t get the error, but I get the types and not the names
