Object has no attribute '_get_object_id'

Hello,
I am interested in the OLD Requirements elements in Capella, theses ones:
image


I added a method into the Physical Archi Class to be able to COLLECT the > requirements Packages (pkg):

def get_reqPKG3(self):
“”"
“”"
return create_e_list(self.get_java_object().getOwnedRequirementPkgs(), RequirementPkg)

I do obtain a java list containing the requirements packages successfully:

print(se.get_physical_architecture().get_reqPKG3())
<main.JavaList object at 0x000001A011195648>


Then I got interested into getting the requirements pkgs that are INSIDE a requirement package:

  • RequirementsPkg 1
  • RequirementsPkg 2

I used direclty .getOwnedRequirementPkgs function :

print(se.get_physical_architecture().get_reqPKG3().get_java_object().get(j5).getOwnedRequirementPkgs())
[JavaObject id=o480, JavaObject id=o481]

It seems I am obtaining a LIST? Rather than a JavaList?


(get(j5) here gives me one of the packages that are direclty under the phys arch “Physical Requirement”, it’s class (org.eclipse.emf.ecore.impl.EClassImpl@c8d09e4 (name: RequirementsPkg) (instanceClassName: null) (abstract: false, interface: false))):


AS you can expect this does cause a problem, when trying to append / add an element into it and “modify” the model inside a transaction.
I obtain the the error message:

-----------object has no attribute ‘_get_object_id’-------------------

Wether i try to use “append(item)” or ".add(item), i would get that message error, here:
se.get_physical_architecture().get_reqPKG3().get_java_object().get(j5).getOwnedRequirementPkgs().add(j2b) #get_reqPKG_inPkg3
se.get_physical_architecture().get_reqPKG3().get_java_object().get(j5).getOwnedRequirementPkgs().append(j2b)
(j2b here is a requirementsPkg)
==>
Case of using add:

py4j.Py4JException: An exception was raised by the Python Proxy. Return Message: Traceback (most recent call last):
File “D:------\Capella 5.1 Test Python4 capella 4\capella\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/Python4Capella/NewScript/NewScript.py”, line 368, in
File “D:------\Capella 5.1 Test Python4 capella 4\capella\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\java_gateway.py”, line 1313, in call
args_command, temp_args = self._build_args(*args)
File “D:------\Capella 5.1 Test Python4 capella 4\capella\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\java_gateway.py”, line 1283, in _build_args
[get_command_part(arg, self.pool) for arg in new_args])
File “D:------\Capella 5.1 Test Python4 capella 4\capella\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\java_gateway.py”, line 1283, in
[get_command_part(arg, self.pool) for arg in new_args])
File “D:------\Capella 5.1 Test Python4 capella 4\capella\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\protocol.py”, line 298, in get_command_part
command_part = REFERENCE_TYPE + parameter._get_object_id()
AttributeError: ‘RequirementPkg’ object has no attribute ‘_get_object_id’

During handling of the above exception, another exception occurred:
etc…

Case of using append:

py4j.Py4JException: An exception was raised by the Python Proxy. Return Message: Traceback (most recent call last):
File “D:-----\Capella 5.1 Test Python4 capella 4\capella\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/Python4Capella/NewScript/NewScript.py”, line 367, in
_pyease_sys.path.append(path)
File “D:-----\Capella 5.1 Test Python4 capella 4\capella\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\java_collections.py”, line 427, in append
self.add(value)
File “D:-----\Capella 5.1 Test Python4 capella 4\capella\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\java_gateway.py”, line 1313, in call
args_command, temp_args = self._build_args(*args)
File “D:-----\Capella 5.1 Test Python4 capella 4\capella\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\java_gateway.py”, line 1283, in _build_args
[get_command_part(arg, self.pool) for arg in new_args])
File “D:-------\Capella 5.1 Test Python4 capella 4\capella\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\java_gateway.py”, line 1283, in
[get_command_part(arg, self.pool) for arg in new_args])
File “D:-----\Capella 5.1 Test Python4 capella 4\capella\plugins\py4j-python_0.10.9.3-bnd-2odeag\src\py4j\protocol.py”, line 298, in get_command_part
command_part = REFERENCE_TYPE + parameter._get_object_id()
AttributeError: ‘RequirementPkg’ object has no attribute ‘_get_object_id’

During handling of the above exception, another exception occurred:
…etc

(Btw, I did create a class called (RequirementPkg) I don’t know if the problem comes from it =>

class RequirementPkg(CapellaElement):
#class RequirementPkg(EObject):

    def __init__(self, java_object = None):
        if java_object is None:
            JavaObject.__init__(self, create_e_object("http://www.polarsys.org/capella/core/requirement/" + capella_version(), "RequirementsPkg"))
        elif isinstance(java_object, requirements):
            JavaObject.__init__(self, java_object.get_java_object())
        else:
            JavaObject.__init__(self, java_object)
    def get_name(self):
        """
        """
        return self.get_java_object().getName()
    def set_name(self, value):
        """
        """
        self.get_java_object().setName(value)
    def get_reqPKG_inPkg3(self):
        """
        """
        return create_e_list(self.get_java_object().getOwnedRequirementPkgs(), RequirementPkg

Strangely enough, this method (get_reqPKG_inPkg3) cannot be called here:
se.get_physical_architecture().get_reqPKG3().get_java_object().get(j5).get_reqPKG_inPkg3().add(j2b)
The error message states:
py4j.Py4JException: Method get_reqPKG_inPkg3([]) does not exist
So is seems that my get(j5) is the class i have created? I don’t see any other instance where that class was created though… (i checked on the capella.py, and all other py files)

I wonder where the problem could lie? @YvanLussaud Or any expert/competent on Python4Capella please.


Extra Edit:
I just noticed a problem;
The RequirementPkg I obtain with the code “get(j5)” does not provides me a java object, it’s rather get mt the RequirementsPkg in it’s pure form:
I obtain:
(org.polarsys.capella.core.data.requirement.impl.RequirementsPkgImpl@6b48f568 (id: 7c285582-4263-48e6-a6dd-398e558ffdbc, sid: null) (name: Physical Requirements) (visibleInDoc: true, visibleInLM: true, summary: null, review: null) (additionalInformation: null, level: null))
While I am expecting:
<main.RequirementPkg object at 0x0000023075A76748>
I thought i might get it with a “.get_java_object()” method, but all it gets me is an error message :
print(se.get_physical_architecture().get_reqPKG3().get_java_object().get(j5).get_java_object())

-> ...py4j.protocol.Py4JError: An error occurred while calling o471.get_java_object. Trace:
py4j.Py4JException: Method get_java_object([]) does not exist

End of edit.

I made additional Tests, and i am pretty sure the problem come from the function:

def get_reqPKG3(self):
        """
        """
        return create_e_list(self.get_java_object().getOwnedRequirementPkgs(), RequirementPkg)

That i made inside Phys Archi.
Which returns a java list of “PURE” requirementsPkgs (org.polarsys.capella.core.data.requirement.impl.RequirementsPkgImpl@75fd4bc4…)
They should be Java object represeting the Requirement Package.

The fonction should have worked like the one for property value groups, native function:

def get_owned_property_value_groups(self):
        """
        """
        return create_e_list(self.get_java_object().getOwnedPropertyValueGroups(), PropertyValueGroup)

This one would provide a java list of “java objects” represeting the property value groups, such as:
<main.PropertyValueGroup object at 0x000002412FD04288>

So it comes to .getOwnedPropertyValueGroups() vs .getOwnedRequirementPkgs()
The first one provide java list of JAVA OBJETCS, and the second one provides a java list of “NON JAVA” objects.

How Can I fix this?

Python lists cannot be divided into separate lists based on characters that appear in the values of a list. This is unlike strings which values can be separated into a list. The AttributeError is an exception thrown when an object does not have the attribute you tried to access. The ‘list’ object has no attribute ‘split’ and you’re trying to call python split() function on the whole list of lines, and you can’t split a list of strings, only a string. So, you need to split each line, not the whole thing.

To solve the above problem, you need to iterate over the strings in the list to get individual strings; then, you can call the split() function.

1 Like

Thank you @gilbertcane for your reply. in the context of Python4Capella this error can append when one pass a Python object to a Java method. Maybe you need to add a .get_java_object() somewhere. You can also wrap the Java List as a Python JavaList:

myPythonList = JavaList(someJavaList)
...
for element in myPythonList:
    ...

In this case element will be a Python object that implement JavaObject.

Note only Java objects of the Java list that can be wrapped into a Python object will be iterated over. You can have a look at the EObject.get_class() method for more details.

Thanks to you both!
I actually wrote this post when I was beginning at p4C,
It was the “only” post without a response in this subforum (and few announcements). Since then, I found solutions to this, most of the time it was resolved when wraping back a java object into a python object. Which saved me so many times.

1 Like