Exporting EnumerationValue Attribute of a VP Requirement with P4C

Hi,
Many thanks for Python4Capella Fabulous addon!!

following the Exporting Values with Python4Capella - #10 by StephaneLacrampe item:
Extracts Requirements Attributes and Values
if req.get_java_object().getOwnedAttributes() != None:
for att in req.get_java_object().getOwnedAttributes():
print("- Attribute: “+att.getDefinition().getReqIFLongName()+”, value: "+str(att.getValue()))

I would like to export an enumeration value attribute like this (rather than a string value attribute):
Capture

could you help me to script it?
thanks

1 Like

This API has been improved in the last release. You can now use:

for att in req.get_owned_attributes():
    print("- Attribute: “+att.get_definition().getReqIFLongName()+”, value: "+att.get_value())

attr.get_value() will return the value as its primitive type of the Java object corresponding to the EnumerationValueAttribute value. You can then use value.getName()

Your loop could become something like this:

for att in req.get_owned_attributes():
    if attr.get_java_object().eClass().getName() == "EnumerationValueAttribute":
         print("- Attribute: “+att.get_definition().getReqIFLongName()+”, value: "+att.get_value().getName())
    else:
         print("- Attribute: “+att.get_definition().getReqIFLongName()+”, value: "+att.get_value())

You can also create new Attribute by passing the type of attribute you want:

Attribute(None, "StringValueAttribute")
Attribute(None, "IntegerValueAttribute")
Attribute(None, "EnumerationValueAttribute")
Attribute(None, "BooleanValueAttribute")
Attribute(None, "RealValueAttribute")

You can then set the value with:

attr.set_value(value)

Where value needs to be the primitive type of the attribute or the Java enumeration value:

str_attr.setValue("some string")

enum_attr.set_value(other_enum.attr.get_value())

Hi,
I try to implement your script but it fails. I can extract StringValueAttribute, Boolean Value Attribute but it fails on a EnumerationValueAttribute (ProgressStatus has 8 values).

Sorry to insist but it is relevant for my needs. Any Idea?
dvd

Don’t worry, this kind of feedback also helps a lot to improve Python4Capella.

And this is indeed a bug on Attribute.get_value() for EnumerationValueAttribute.

You will need to add some methods to the ReqIFElement class and add a EnumValue class:

class ReqIFElement(EObject):
    """
    """
    def __init__(self, java_object = None):
        if java_object is None:
            JavaObject.__init__(self, create_e_object("http://www.polarsys.org/kitalpha/requirements", "ReqIFElement"))
        elif isinstance(java_object, ReqIFElement):
            JavaObject.__init__(self, java_object.get_java_object())
        elif get_e_classifier("http://www.polarsys.org/kitalpha/requirements", "ReqIFElement").isInstance(java_object):
            JavaObject.__init__(self, java_object)
        else:
            raise AttributeError("Passed object is not compatible with " + self.__class__.__name__ + ": " + str(java_object))
    def get_id(self):
        """
        """
        return self.get_java_object().getReqIFIdentifier()
    def set_id(self, value):
        """
        """
        self.get_java_object().setReqIFIdentifier(value)
    def get_long_name(self):
        """
        """
        return self.get_java_object().getReqIFLongName()
    def set_long_name(self, value):
        """
        """
        self.get_java_object().setReqIFLongName(value)
    def get_description(self):
        """
        """
        return self.get_java_object().getReqIFDescription()
    def set_description(self, value):
        """
        """
        self.get_java_object().setReqIFDescription(value)

class EnumValue(ReqIFElement):
    """
    """
    def __init__(self, java_object = None):
        if java_object is None:
            JavaObject.__init__(self, create_e_object("http://www.polarsys.org/kitalpha/requirements", "EnumValue"))
        elif isinstance(java_object, ReqIFElement):
            JavaObject.__init__(self, java_object.get_java_object())
        elif get_e_classifier("http://www.polarsys.org/kitalpha/requirements", "EnumValue").isInstance(java_object):
            JavaObject.__init__(self, java_object)
        else:
            raise AttributeError("Passed object is not compatible with " + self.__class__.__name__ + ": " + str(java_object))

Then you can replace the Attribute.get_value():

    def get_value(self):
        """
        """
        if self.get_java_object().eClass().getName() == "BooleanValueAttribute":
            return self.get_java_object().isValue()
        elif self.get_java_object().eClass().getName() == "EnumerationValueAttribute":
            return create_e_list(self.get_java_object().getValues(), EnumValue)
        else:
            return self.get_java_object().getValue()

This will return a list of EnumValue when called on a EnumerationValueAttribute.

Let me know if it helps. I also opened this issue:

Hi,
I’m sure that it is the last issue :slight_smile:
get_value() seems to work for ‘EnumerationValueAttribute’

but getName() No:

last help?
merci!

dvd

On a EnumerationValueAttribute get_value() returns a list of EnumValue so you should write something like this:

for enum_value in attr.get_value():
    print(enum_value.get_long_name())

Wunderbar!

I should have found the solution by myself…
Thanks for all
dvd

1 Like

@YvanLussaud

This is just an FYI. I was still having issues running Attribute.get_value() for an EnumerationValueAttribute. It looks like the issue you raised was marked as resolved, but I think it might not be quite complete. What I discovered was that the ReqIFElement and EnumValue classes were updated in requirement.py per your recommendations, but the Attribute.get_value() method in my sample_scripts was not updated with the following lines.

elif self.get_java_object().eClass().getName() == “EnumerationValueAttribute”:
return create_e_list(self.get_java_object().getValues(), EnumValue)

I made that update to the Attribute.get_value() method and it seems to be working as expected now!

1 Like

An issue has been opened to track the problem:

1 Like

Thanks @YvanLussaud! I have limited experience with Github, is something like opening issues limited to people who are involved with the actual development, or is it something I should get familiar with? I’m more than happy to contribute to issue reports as I encounter them rather than bothering you!

1 Like

You can open new issues on your own via this page. The fisrt step should be to search for an already existing issue. But I prefer to filter out duplicates than miss something. When you open an issue there is a template to guide you through important information, if needed you can also read the github documentation.

And don’t worry that doesn’t bother me. Discussing issues here or on github is important for the good health of Python4Capella.

1 Like