Unable to get Physical Links Ports

Hello,

I am very new to Python and also to Python for Capella…

I am trying to write a script to export the list of Physical Links and associated ports in an Excel file.

I get an error message with the get_connected_physical_ports() method :
It seems that the underlying getConnectedPhysicalPorts() does not exist.

Extract of my source code :

->>>>>>>>>>> Source code extract start >>>>>>>>>>>

writing excel file header

worksheet = workbook.active
worksheet.title = ‘Physical links’
worksheet[“A1”] = ‘Physical link’
worksheet[“B1”] = ‘Origine Port’
worksheet[“C1”] = ‘Destination Port’

i=2

retrieving elements from the model

all_PhysicalLinks = se.get_all_contents_by_type(PhysicalLink)

for PhysicalLink in all_PhysicalLinks:
worksheet[“A” + str(i)] = PhysicalLink.get_name()
ConnectedPhysicalPorts = []
ConnectedPhysicalPorts = PhysicalLink.get_connected_physical_ports()
Port1 = ConnectedPhysicalPorts[0]
Port2 = ConnectedPhysicalPorts[1]
worksheet[“B” + str(i)] = Port1.get_name()
worksheet[“C” + str(i)] = Port2.get_name()
i = i +1

Save the xlsx file

workbook.save(xlsx_file_name)
<<<<<<<<<<<< Source code extract end <<<<<<<<<<<<

Line 82 corresponds to
ConnectedPhysicalPorts = PhysicalLink.get_connected_physical_ports()

The error message in console is

->>>>>>>>>>> Console content start >>>>>>>>>>>

py4j.Py4JException: An exception was raised by the Python Proxy. Return Message: Traceback (most recent call last):
File “C:\SC\PSYS\capella142_py4c\eclipse\plugins\org.eclipse.ease.lang.python.py4j_0.7.0.I201907020902\pysrc\ease_py4j_main.py”, line 186, in runcode
exec(compiled, self.locals)
File “L/PyDev - Essais01/mf_Export_list_of_physical_links_to_xlsx.py”, line 82, in
‘’’
File “L/Python4Capella/simplified_api/capella.py”, line 2455, in get_connected_physical_ports
File “C:\SC\PSYS\capella142_py4c\eclipse\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\java_gateway.py”, line 1322, in call
answer, self.gateway_client, self.target_id, self.name)
File “C:\SC\PSYS\capella142_py4c\eclipse\plugins\py4j-python_0.10.9.3-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 o403.getConnectedPhysicalPorts. Trace:
py4j.Py4JException: Method getConnectedPhysicalPorts([]) 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 com.sun.proxy.$Proxy30.executeScript(Unknown Source)
at org.eclipse.ease.lang.python.py4j.internal.Py4jScriptEngine.internalExecute(Py4jScriptEngine.java:236)

at org.eclipse.ease.lang.python.py4j.internal.Py4jScriptEngine.execute(Py4jScriptEngine.java:226)
at org.eclipse.ease.AbstractScriptEngine.inject(AbstractScriptEngine.java:245)
at org.eclipse.ease.AbstractScriptEngine.run(AbstractScriptEngine.java:309)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\SC\PSYS\capella142_py4c\eclipse\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\clientserver.py”, line 581, in _call_proxy
return_value = getattr(self.pool[obj_id], method)(*params)
File “C:\SC\PSYS\capella142_py4c\eclipse\plugins\org.eclipse.ease.lang.python.py4j_0.7.0.I201907020902\pysrc\ease_py4j_main.py”, line 335, in executeScript
return self.executeCommon(code_text, self.interp.runcode, filename)
File “C:\SC\PSYS\capella142_py4c\eclipse\plugins\org.eclipse.ease.lang.python.py4j_0.7.0.I201907020902\pysrc\ease_py4j_main.py”, line 313, in executeCommon
execution_result = code_exec(code_text, filename)
File “C:\SC\PSYS\capella142_py4c\eclipse\plugins\org.eclipse.ease.lang.python.py4j_0.7.0.I201907020902\pysrc\ease_py4j_main.py”, line 213, in runcode
except Exception:
TypeError: catching classes that do not inherit from BaseException is not allowed

at py4j.Protocol.getReturnValue(Protocol.java:476)
at py4j.reflection.PythonProxyHandler.invoke(PythonProxyHandler.java:108)
at com.sun.proxy.$Proxy30.executeScript(Unknown Source)
at org.eclipse.ease.lang.python.py4j.internal.Py4jScriptEngine.internalExecute(Py4jScriptEngine.java:236)
at org.eclipse.ease.lang.python.py4j.internal.Py4jScriptEngine.execute(Py4jScriptEngine.java:226)
at org.eclipse.ease.AbstractScriptEngine.inject(AbstractScriptEngine.java:245)
at org.eclipse.ease.AbstractScriptEngine.run(AbstractScriptEngine.java:309)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

<<<<<<<<<<<< Console content end <<<<<<<<<<<<

Thank you very much for your help.

I’m not really sure what PhysicalLink.get_connected_physical_ports() should return but I tried an other implementation:

    def get_connected_physical_ports(self):
        """
        """
        res = []
        for link_end in self.get_java_object().getOwnedPhysicalLinkEnds():
            value = link_end.getPort()
            if value is not None:
                e_object_class = getattr(sys.modules["__main__"], "EObject")
                specific_cls = e_object_class.get_class(value)
                res.append(specific_cls(value))
        return res

You can try to replace your implementation with this one in capella.py. Let me know if it works for you. An other implementation could be to return the source and target ports from the PhysicalLink:

PhysicalPort(self.get_java_object().getSourcePhysicalPort())
PhysicalPort(self.get_java_object().getTargetPhysicalPort())

The second Exception can be fixed by renaming Exception by CapellaException in capella.py, see:

Hello Yvan,
Thank you very much for provided help and fast answer.

Solution 1:
Within proposed new implementation, I checked “self” value using print(self.get_name()) and it seems correct.
However, self.get_java_object().getOwnedPhysicalLinkEnds() returns an empty list.

Solution 2:
Woks fine, thanks !

Class exception renaming:
Not tested yet. I will do soon.

Thank you for the feedback.