Filtering Add-on and Python4Capella

Hi everyone, I am trying to acess Filtering information from the model and add new filtering criteria or results.

this is what I did, considering my filtering criteria organized in Pkgs:

se = Sirius.get_system_engineering(model.session)  
# Get Filtering Criterion
FilteringModel = e_all_contents(se)[0]
print(FilteringModel.get_java_object().getName())
for pkg in FilteringModel.get_java_object().getOwnedFilteringCriterionPkgs():
    print("  " + pkg.getName())
    for filter_criteria in pkg.getOwnedFilteringCriteria():
        print("    " + filter_criteria.getName())
        
# Add one Filtering Criterion
model.start_transaction()     
try:
    filtering_criteria_e_class = get_e_classifier("http://www.polarsys.org/capella/filtering/" + capella_version(), "FilteringCriterion")  
    myFilterCriteria = create_e_object_from_e_classifier(filtering_criteria_e_class)
    FilterPkg = FilteringModel.get_java_object().getOwnedFilteringCriterionPkgs().get(0)
    FilterPkg.getOwnedFilteringCriteria().add(myFilterCriteria)
    
except Exception as e:
    # Rollback the transaction if something goes wrong
    print("An error occurred: ", e)
    model.rollback_transaction()
    raise
else:
    # Commit the transaction if everything is successful
    model.commit_transaction()

I don’t know if is the smartest way to do it for two main reasons:

  • I don’t understand why it works only if se is retrieved with Sirius
  • Filtering Model is retrieved as first element of a list, not because it is a Filtering Model

Plus, I don’t know how to add a filtering criterion to a model elements.

Do you have any experience or advice for doing this or in general with Filtering add-on for Pythoon4Capella?

There is no specific API in Python for Capella for the filtering addon. But you can use the Java API, you can have a look at the meta-model (.ecore file) and the code. For specific questions about the filtering addon you might want to ask them in the main forum.

There is an experimental Acceleo generator (the code) to generate the Python for Capella API from an .ecore file.

I attached the result of the generation for the filtering.ecore on the master branch.

filtering_header.py (1.6 KB)
filtering.py (23.9 KB)

I plan to make the generator available in the next verison of Python for Capella.

1 Like

Thank you so much. This is very useful!

1 Like