Modify Enumeration Properties values

Hi there,

I’m working on a script which automatically updates properties with values taken from an excel file. I’m using this line

PVMT.set_p_v_value(element_obj, prop_name, new_value)

which uses this code that I saw in previous topics that I manually added in the API file:

    def set_p_v_value(elem, PVName, value):
        for group in elem.get_java_object().getOwnedPropertyValueGroups():
            for pv in group.getOwnedPropertyValues():
                if PVName == pv.getName():
                    pv.setValue(value)
                    return

It works flawlessy for boolean, integer, float and string properties, as long as new_value is a variable that is compatible with the property. It doesn’t work with Enumeration Properties. In this case, I read from excel the name of the Enumeration Literal to apply to the property as a string. This is likely the problem, because when set_p_v_value tries to apply a string to the Enumeration Property, I get this error: py4j.Py4JException: Method setValue([class java.lang.String]) does not exist. I’m pretty sure that, when the property is an Enumeration Property, I have to ‘process’ new_value in order to make it a compatible variable with the property (an Enumeration Literal?) instead of a string. I’ve already tried this other version of the command:

    def set_p_v_value(elem, PVName, value):
        for group in elem.get_java_object().getOwnedPropertyValueGroups():
            for pv in group.getOwnedPropertyValues():
                if PVName == pv.getName():
                    if isinstance(java_object, JavaObject):
                        pv.setValue(value.get_java_object())
                    else:
                        pv.setValue(value)
                    return

but it doesn’t solve the issue.

Thanks in advance.

The API use the PVMT addon so it suppose you already made your PVMT definition and applied it to your Capella model. Also you seem to be using Python for Capella 1.2.x or less. Improvements has been done to the PVMT API in version 1.3.0 so you should update your Python for Capella to version 1.3.0. Note that there is still this issue in 1.3.0.

I’m fairly sure that I’ve the 1.3.0 version installed. I also have the PVMT addon applied and properly functioning. In the pvmt.py file I see the get_p_v_value definition, but not the set_p_v_value, so I added it manually, like this:

    def set_p_v_value(elem, PVName, value):
        for group in elem.get_java_object().getOwnedPropertyValueGroups():
            for pv in group.getOwnedPropertyValues():
                if PVName == pv.getName():
                    pv.setValue(value)
                return

As I said, this command works perfectly for every kind of property value except Enumeration Property. For the other properties I just have to make sure that If PVName is the name of a boolean, float, integer and string property, value is respectively a boolean, float, integer or string value. But if PVName refers to an Enumeration Property, I think I need to make value an Enumeration Literal or to change the function in order for it to accept value as a string to set the value of an Enumeration Property.

You probably need to re-import the Python4Capella project to have the up to date Python API. The old PVMT API was not applying property value like the PVMT addon. After re-importing the Python4Capella project the pvmt.py file should look like this.

The method set_p_v_value has been replaced by the method get_or_apply_property_value_group:

pvg = PVMT.get_or_apply_property_value_group(myCapellaElement, "pvmt_package_name", "pvmt_group_name")
pv = pvg.get_owned_property_value_by_name("pvmt_property_name")
myEnumerationPropertyLiteral = pv.get_type().get_owned_literal_by_name("literal_name")
pv.set_value(myEnumerationPropertyLiteral)

It works now, thank you so much :slight_smile:

1 Like