How to convert from Integer to enumeration to assign value to DXL attribute

Hi all,

I'm trying to calculate a new value for a DXL attribute. I thought this was easy but I can't find the solution.

The attribute is of a custom type based on an enumeration. For example, let's assume I have the custom type "ResultType", based on enumeration, looking like this:
(0) -> "Undefined"
(1) -> "Bad"
(2) -> "Moderate"
(3) -> "Good"
(4) -> "Very Good"

I then create a DXL attribute called "MyAttribute" of type "ResultType". Based on two other integer attributes, which I guarentee to be between 0 and 2, I want to calculate the value of the new attribute.

Here's the DXL:
 

int a = obj."A"
int b = obj."B"
int result = a + b 
obj."MyAttribute" = result // Fails here

 


When I run this code, the error message I get is "expected int attribute".

Please note that the resulting attribute is NOT a multi-valued attribute. So I assume that I should be able to do a simple conversion from int to the enumeration.

Thanks in advance for any help!

 


atroq - Thu Oct 14 07:47:09 EDT 2010

Re: How to convert from Integer to enumeration to assign value to DXL attribute
SystemAdmin - Thu Oct 14 08:19:35 EDT 2010

There is no simple conversion from integer to enumeration (or at least I could not think of one ..). An integer attribute is just an integer value, but enumerated attribute is kind of array of values, each of which have a string value and an integer "related number". The code below presumes that your related numbers are as you have described them from 0 to 4 and then display the corresponding string value for type of the DXL attribute.
 

int a = obj."A"
int b = obj."B"
int result = a + b
AttrDef ad = find(current Module, attrDXLName)
AttrType at = ad.type 
obj.attrDXLName = at.strings[result]

Re: How to convert from Integer to enumeration to assign value to DXL attribute
atroq - Thu Oct 14 08:30:30 EDT 2010

SystemAdmin - Thu Oct 14 08:19:35 EDT 2010

There is no simple conversion from integer to enumeration (or at least I could not think of one ..). An integer attribute is just an integer value, but enumerated attribute is kind of array of values, each of which have a string value and an integer "related number". The code below presumes that your related numbers are as you have described them from 0 to 4 and then display the corresponding string value for type of the DXL attribute.
 

int a = obj."A"
int b = obj."B"
int result = a + b
AttrDef ad = find(current Module, attrDXLName)
AttrType at = ad.type 
obj.attrDXLName = at.strings[result]

That did it! Great, thanks a lot!

Re: How to convert from Integer to enumeration to assign value to DXL attribute
SystemAdmin - Thu Oct 14 10:28:09 EDT 2010

In many programming languages, enums are have a literal value and an ordinal value. Both must be unique.

The DOORS model can essentially be modeled as one string array and two int arrays namely:
 

string strings[ NO_OF_ENUM_MEMBERS ]; // required & unique
int    colors [ NO_OF_ENUM_MEMBERS ]; // optional & may overlap
int    values [ NO_OF_ENUM_MEMBERS ]; // optional & may overlap

 


Values is the "associated value" on the DOORS GUI.

You can use values[] but you must manually ensure the unique values.

The typical programming language usage is:

 

 

// C syntax
typedef enum {RED, BLUE, GREEN} ColorType;



Above:
RED has literal value "RED" and ordinal value 0.
BLUE has literal value "BLUE" and ordinal value 1.
GREEN has literal value "GREEN" and ordinal value 2.

In DOORS the same thing is represented by defining an enumeration type called ColorType in the GUI and putting strings "RED", "BLUE", and "GREEN" as members IN THIS ORDER!

In DXL, the attribute ColorType's properties would be as follows:

The .size = 3
The .strings[] member would be:



 

 

strings[0] = "RED";
strings[1] = "BLUE";
strings[2] = "GREEN";



It is a bit of a nuisance to get at the ordinal values of an enum at run time. The following would work however:

Set up a Skip list with the enum literals as Skip keys and the ordinals as Skip data.



 

 

 

put(colorSkip, "RED", 0); 
put(colorSkip, "BLUE", 1); 
put(colorSkip, "GREEN", 2);
 
string someColorLiteral = "BLUE";
int    someColorOrdinal = -1;
 
// The below would find key "BLUE" and populate someColorOrdinal
// with 1.
find(colorSkip, someColorLiteral, someColorOrdinal);
 
int iBLUE = 1;
if (someColorOrdinal != iBLUE) {
 
}



Another tip:
In order for DOORS to honor two enum types as the same (suppose they are in different modules). ALL of the below must be true:
The .size must me identical
Each literal in strings[] must be identical
Each int number in colors[] must be identical
Each int number in values[] must be identical

PLUS:
The ordering of the identical literals in strings must match 100%.

Cheers!