labs4capella / python4capella

Python for Capella
Eclipse Public License 2.0
52 stars 10 forks source link

Using Python4Capella to extract Actors, Activities and Related Diagrams/ Scenarios #207

Closed mm-capella closed 6 months ago

mm-capella commented 6 months ago

Hi, I have been building a model in the Operational Analysis layer and mainly created scenarios in the OAB space and consequently created diagrams in the OES space. I have been using Python4Capella to extract data through the whole model and have easily collected the actors and their allocated activities, however, I am struggling to find a code to see which diagrams/ scenarios they have been used in. The semantic browser shows that they have been allocated so there should be some data to connect them, however when I check the 'Python4Capella Simplified Metamodel' word document, there seems to be no function that can obtain the scenario/ related diagram from the Operational Analysis class. Alternatively, I tried working from the Scenario class but all I can call on are the State Fragments (attached to Operational Activities) and Sequence Message (Interactions). This would be close, however the StateFragment is unnamed so it returns 'None' for the name.

I was wondering if anyone has worked on extracting the data from different classes or anything similar.

ylussaud commented 6 months ago

You can use the following code that call the semantic browser query:

# include needed for the Capella modeller API
include('workspace://Python4Capella/simplified_api/capella.py')
if False:
    from simplified_api.capella import *

aird_path = '/In-Flight Entertainment System/In-Flight Entertainment System.aird'
model = CapellaModel()
model.open(aird_path)

# table = model.get_diagrams('System Functions - Operational Activities').get(0).get_java_object().getRepresentation()
# path = org.eclipse.core.runtime.Path.fromOSString('/tmp/file.csv')
# csv_format = org.eclipse.sirius.ui.business.api.dialect.ExportFormat(org.eclipse.sirius.ui.business.api.dialect.ExportFormat.ExportDocumentFormat.CSV, None)
# progress_monitor = org.eclipse.core.runtime.NullProgressMonitor()
# org.eclipse.sirius.ui.business.api.dialect.DialectUIManager.INSTANCE.export(table, None, path, csv_format, progress_monitor, False)

myActor = model.get_system_engineering().get_all_contents_by_type(OperationalActor)[2]

print(myActor.get_name())
for diagram in capella_query_by_name(myActor, "All Related Diagrams", Diagram):
    if diagram.get_java_object().getDescription().getName() == 'Operational Interaction Scenario':
        print( - diagram.get_name())

There is a bug with the implementation of Java_List, you will need to apply the following patch to Capella_API.py.

mm-capella commented 6 months ago

Hi @ylussaud, Really appreciate the help, I was only using the capella.py to navigate. I am not sure how to patch Capella_API.py but tried replacing the code in there with yours, however I am still coming across an error 'AttributeError: 'EObject' object has no attribute 'get_name''

ylussaud commented 6 months ago

This error message comes from the diagram being wrapped to the wrong Python Class EObject instead of Diagram. You can try to wrap it manually as a workaround until the next release of Python4Cpaella:

for eobj in capella_query_by_name(myActor, "All Related Diagrams", Diagram):
    diagram = Diagram(eobj.get_java_object())
    if diagram.get_java_object().getDescription().getName() == 'Operational Interaction Scenario':
        print( - diagram.get_name())

Let me know if it resolve your issue.

mm-capella commented 6 months ago

Thanks. Am I replacing the: 'for diagram in capella_query_by_name(myActor, "All Related Diagrams", Diagram): if diagram.get_java_object().getDescription().getName() == 'Operational Interaction Scenario': print( - diagram.get_name())' part of code with this?

I come up with a new error 'TypeError: bad operand type for unary -: 'str'

ylussaud commented 6 months ago

Sorry my bad:

print(' - ' + diagram.get_name())
mm-capella commented 6 months ago

Thanks @ylussaud, managed to adjust the code to obtain the data I was looking to get.