Issue creating new functions under other functions

I receive the following error when trying to add system functions under the Root System Function instead of alongside it:
java.lang.UnsupportedOperationException
at org.eclipse.emf.common.util.BasicEList$UnmodifiableEList.add(BasicEList.java:908)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
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 com.sun.proxy.$Proxy30.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)

This is the code which caused the issue, the variable new_FuncNames is just a list of Strings:
model.start_transaction()
try:
for value in new_FuncNames:
# create a SystemFunction
sf = SystemFunction()
#set its name
print("* Create a new system function with name " + value )
sf.set_name(value)
#add the new SystemFunction
OwnedFunc = se.get_system_analysis().get_system_function_pkg().get_owned_system_functions().get(0).get_contained_system_functions()
OwnedFunc.add(sf)
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()

The SystemFunction.containedSystemFunctions is a derived reference (it is computed), so you can’t modify its value. For the moment the API for modifying the Capella model is far from complete, but you can add the following method to the SystemFunction class in the capella.py file:

    def get_owned_functions(self):
        """
        """
        return create_e_list(self.get_java_object().getOwnedFunctions(), Function)

Then you can call it like this:

OwnedFunc = se.get_system_analysis().get_system_function_pkg().get_owned_system_functions().get(0).get_owned_functions()

I don’t think the final API will look like this, but it’s a workaround until we release the corresponding API.

1 Like

Thank you for your help! Is there an easy way to tell what is a derived reference and what is a standard java method for the class?

In the simplified API specification the reference should be marked as read only. If you have the Capella source code in your environment, you can check the comment of the method:

/**
 * Returns the value of the '<em><b>Contained System Functions</b></em>' reference list.
 * The list contents are of type {@link org.polarsys.capella.core.data.ctx.SystemFunction}.

 * <!-- begin-user-doc -->
 * <p>
 * If the meaning of the '<em>Contained System Functions</em>' reference list isn't clear,
 * there really should be more of a description here...
 * </p>
 * <!-- end-user-doc -->
 * @return the value of the '<em>Contained System Functions</em>' reference list.
 * @see org.polarsys.capella.core.data.ctx.CtxPackage#getSystemFunction_ContainedSystemFunctions()
 * @model transient="true" changeable="false" volatile="true" derived="true"
 *        annotation="http://www.polarsys.org/capella/derived viatra.variant='alias' viatra.expression='ownedFunctions'"
 *        annotation="http://www.polarsys.org/capella/semantic feature='ownedFunctions'"
 *        annotation="http://www.polarsys.org/capella/MNoE/CapellaLike/Mapping UML/SysML\040semantic\040equivalences='keyword::none' explanation='Derived and transient' constraints='none'"
 * @generated
 */

In the model tag, you can see derived=“true”.

Also the stack in your first post shows:

java.lang.UnsupportedOperationException at org.eclipse.emf.common.util.BasicEList$UnmodifiableEList.add(BasicEList.java:908)

Which happen only if the reference is read only or is derived.