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