Using enumerations in XML file with Connector for Python

11 posts / 0 new
Last post
Offline
Last seen: 6 years 8 months ago
Joined: 01/03/2018
Posts: 4
Using enumerations in XML file with Connector for Python

I have installed the rticonnextdds-connector-master for Python script support and successfully ran the simple ShapeExample demo.  I then tried to modify the XML based on some IDL->XML generated code from rtiddsgen that we are using, as follows:

<enum name="TestEnum">
      <enumerator name="FIRST_THING"/>
      <enumerator name="SECOND_THING"/>
 </enum>
         

<struct name= "TestOnly" nested="true">
      <member name="string_member" type="string" stringMaxLength="TestEnum::SECOND_THING"/>
</struct>

The python call to rti.Connector fails with the message:

DDS_XMLTypeCode_get_const_value:constant 'TestEnum::SECOND_THING' not found

Any help in how to use enumerations here would be appreciated.

 

Organization:
isabel's picture
Offline
Last seen: 5 years 1 week ago
Joined: 02/13/2017
Posts: 11

Hi sourcebug,

In the GitHub of connector you can find a type with an enum:

https://github.com/rticommunity/rticonnextdds-connector/blob/master/examples/python/ShapeExample.xml

I think that the issue you are facing is related to using "TestEnum::SECOND_THING" as stringMaxLength.
Could you please change this length to the following?

<member name="string_member" type="string" stringMaxLength="TestEnum"/>; 


Let us know if this helps.

Regards,
Isabel

Offline
Last seen: 6 years 8 months ago
Joined: 01/03/2018
Posts: 4

Thank you for your response.  It does quiet the interpreter.  Two issues:

1.  Using the name of the enum "TestEnum" does not set a value for stringMaxLength.

2.  When generating with rtiddsgen, the IDL->XML transformation produceds the name::value construct.  Using IDL:

enum MainStatusIndex
{
    MAIN_STATUS_INDEX_NONE = 0xFF,
    MAIN_STATUS_INDEX_1 = 0,
    MAIN_STATUS_INDEX_2,
    MAIN_STATUS_INDEX_3,
    MAIN_STATUS_INDEX_4,
    MAIN_STATUS_INDEX_COUNT
};

enum SecondStatusIndex
{
    SECOND_STATUS_INDEX_NONE = 0xFF,
    SECOND_STATUS_INDEX_1 = 0,
    SECOND_STATUS_INDEX_2,
    SECOND_STATUS_INDEX_COUNT
};

struct AllThingsState
{
    unsigned short main_statuses[MAIN_STATUS_INDEX_COUNT];
    unsigned short second_statuses[SECOND_STATUS_INDEX_COUNT];
};

Produces XML:
<struct name= "AllThingsState">
  <member name="main_statuses" type="unsignedShort" arrayDimensions="(MainStatusIndex::MAIN_STATUS_INDEX_COUNT)"/>
  <member name="second_statuses" type="unsignedShort" arrayDimensions="(SecondStatusIndex::SECOND_STATUS_INDEX_COUNT)"/>
</struct>

which is not parsed by the rti.Connector call.

isabel's picture
Offline
Last seen: 5 years 1 week ago
Joined: 02/13/2017
Posts: 11

Hi sourcebug,

Using enums as a size of an array is not currently supported.
Right now you need to use a constant int for this, either explicitly or creating a constant in your IDL.

Regards,
Isabel

Offline
Last seen: 6 years 8 months ago
Joined: 01/03/2018
Posts: 4

Another enum question.  The example doesn't show how you write an enum in python.  Can you tell me whether to use setString or setNumber and show the correct syntax for doing this?

Thanks!

Offline
Last seen: 6 years 8 months ago
Joined: 01/03/2018
Posts: 4

Again, please show an example of using an enumerate to set a value of an element.  Thanks.

gianpiero's picture
Offline
Last seen: 8 months 3 weeks ago
Joined: 06/02/2010
Posts: 177

Hello,

I created a simple example on how to set an enum using setNumber. You can access the python code and the relative xml here: https://gist.github.com/gianpiero/4933e0ab3bf3a2f9fb2f395fcf345671

Best,
  Gianpiero

File Attachments: 
Offline
Last seen: 4 years 8 months ago
Joined: 01/29/2020
Posts: 1

Check this one..Python enum

Offline
Last seen: 4 years 3 months ago
Joined: 05/28/2020
Posts: 4

Hi Gianpiero,

I realize this example is old, but can you answer a secondary question in this case?  You're defining in your python code:

COLORS = {"RED":0, "BLUE":1, "YELLOW":2}

The definition in the IDL/XML is defined already as:

<enum name="COLOR">
<enumerator name="RED" value="0"/>
<enumerator name="BLUE" value="1"/>
<enumerator name="YELLOW" value="2"/>
</enum>

 

So my question is, can you bypass the redundant dictionary definition in Python and access the enums from your IDL/XML directly through your Connector?  To clarify, I'd mean something more like this, even though this is essentially fake code:  

 outputDDS.instance.setNumber("myColor", ModuleName::EnumNamePlaceholder::RED)

Let's say I have literally dozens of idl defined enums, so the python dictionary definitions are lengthy and redundant.   Is there any way to achieve this usage using the connector in any way?  This is normally pretty simple in the C++ implementation.

 

Offline
Last seen: 4 years 3 months ago
Joined: 05/28/2020
Posts: 4

Anyone have ideas on this?

Offline
Last seen: 2 months 3 weeks ago
Joined: 10/22/2018
Posts: 91

Hey scrowe,

Unfortunately that is not currently possible. So maintaining a dictionary of the enum is required. The setters for enums only accept integer values currently. I have created an RFE CON-204 which addresses the behaviour you are requesting.
Until then, maybe there is a way you can parse the XML in Python yourself to automatically generate that dictionary?

Sam