Add property in class

Hi,
I am trying to create a new class and its properties with Python 4 Capella.
I successfully created the class, but failed to add the properties.

I’ve tried something like this :

new_prop = Property()
new_prop.get_java_object().setName(prop_name)
new_prop.get_java_object.setAbstractType(type)
myclass.get_contained_properties().add(new_prop)
org.polarsys.capella.core.model.helpers.CapellaElementExt.creationService(new_prop.get_java_object())

my first problem was with the “add” method, for which i had the Unsupported Operation Exception, so I tried the “Append” method instead.

But now, I have this one :

java.lang.NullPointerException: Cannot invoke “org.eclipse.emf.ecore.EObject.eContents()” because “container” is null
at org.polarsys.capella.common.helpers.EcoreUtil2.getUniqueName(EcoreUtil2.java:439)
at org.polarsys.capella.common.menu.dynamic.CreationHelper.getNamingCommand(CreationHelper.java:246)
at org.polarsys.capella.common.menu.dynamic.CreationHelper.getAdditionnalCommand(CreationHelper.java:134)
at org.polarsys.capella.common.menu.dynamic.CreationHelper.getAdditionnalCommand(CreationHelper.java:89)
at org.polarsys.capella.core.model.helpers.CapellaElementExt.creationService(CapellaElementExt.java:434)
at jdk.internal.reflect.GeneratedMethodAccessor102.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:282)
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)

Can someone help me with this ?
Is the Get_contained_properties method the good one to do what I want to ?

Thank you.

Problem solved : the method getOwnedFeatures must be used instead of getContainedProperties.

Hi,

When you call the creationService() the passed object should be contained in a Capella model. Here my_class is not contained yet. It won’t happen if the Class was already in the Capella model or if it was added to the model beforehand. Also not that to modify the Capella model, you need to be in a transaction.

You can use something like:

# get the first class from the Capella model
my_class = se.get_all_contents_by_type(Class)[0]

model.start_transaction()
try:
    new_prop = PropertyValue()
    my_class.get_owned_property_values().add(new_prop)
    org.polarsys.capella.core.model.helpers.CapellaElementExt.creationService(new_prop.get_java_object())
    new_prop.set_name("SomeProperty")
except:
    # if something went wrong we rollback the transaction
    model.rollback_transaction()
    raise
else:
    # if everything is ok we commit the transaction
    model.commit_transaction()

# save the Capella model
model.save()