How to dynamically create an attribute definition using strings

Hi,

Here is a code snippet to illuminate my question:
 

//Initialize attribute strings
                                                AttrDef creastr                         //final string
                                                        string aname
                                                        string desc
                                                        string type
                                                        string defval
                                                        string inhrtval
                                                        string exsfr
                                                        string mltval = null
                                                        
                                                        //Construct attribute string
                                                        bool done = false
                                                        itr = 0
                                                        while(!done){
                                                        
                                                                //Name
                                                                if(itr == 0){
 
                                                                        //Set value
                                                                        aname = "attribute " "\"" kval "\""
 
                                                                        //Increment index
                                                                        i++
                                                                }
 
                                                                //Description
                                                                if(itr == 1){
 
                                                                        //Set value
                                                                        desc = kval
 
                                                                        //Increment index
                                                                        i++
                                                                }
 
                                                                //Type
                                                                if(itr == 2){
                                        
                                                                        //Set value
                                                                        type = "type " "\"" kval "\""
 
                                                                        //Increment index
                                                                        i++
                                                                }
 
                                                                //Default value
                                                                if(itr == 3){
 
                                                                        //Set value
                                                                        defval = "(default " "\"" kval "\"" ")"
 
                                                                        //Increment index
                                                                        i++
                                                                }
 
                                                                //Inherit value
                                                                if(itr == 4){
 
                                                                        //Set value
                                                                        if(kval == "Yes")
                                                                                inhrtval = "inherit true" 
                                                                        else
                                                                                inhrtval = "inherit false"
 
                                                                        //Increment index
                                                                        i++
                                                                }
                                                        
                                                                //Exists for
                                                                if(itr == 5){
                                                                        
                                                                        //Set value
                                                                        if(kval == "Module")
                                                                                exsfr = "module"
                                                                        else
                                                                                exsfr = "object"
 
                                                                        //Increment index
                                                                        i++
                                                                }
 
                                                                //Enumerated values
                                                                if(itr == 6){
 
                                                                        //Set flag
                                                                        eflag++
 
                                                                        //Determine value
                                                                        if(kval == "strtEnum"){
 
                                                                                //Set value
                                                                                mltval = "(default " "\""
 
                                                                                //Traverse enumerated values
                                                                                bool finished = false
                                                                                while(!finished){
 
                                                                                        //Do comparison
                                                                                        if(kval == "endEnum"){
 
                                                                                                //Terminate loop
                                                                                                finished = true
 
                                                                                                //Increment index
                                                                                                i++
                                                                                        }
                                                                                        else{
                        
                                                                                                //Check value
                                                                                                if(kval != "strtEnum"){
 
                                                                                                        //Check if last enumerated value
                                                                                                        if((string get(keyArray,i+1,1)) == "endEnum"){
 
                                                                                                                //Concatenate string
                                                                                                                mltval = mltval kval
                                                                                                        }
                                                                                                        else{
 
                                                                                                                //Concatenate string
                                                                                                                mltval = mltval kval "\\n"
                                                                                                        }
                                                                                                }
 
                                                                                                //Increment index
                                                                                                i++
 
                                                                                                //Extract string
                                                                                                kval = (string get(keyArray,i,1))
                                                                                        }
                                                                                }
 
                                                                                //Append closing string
                                                                                mltval = mltval "\"" ") multi true"
                                                                        }
                                                                }
 
                                                                //Increment iterator
                                                                itr++
 
                                                                //Determine if done
                                                                if(itr == 7){
                
                                                                        //Terminate loop
                                                                        done = true
                                                                }
                                                                else{
 
                                                                        //Change string to new attribute
                                                                        kval = (string get(keyArray,i,1))
                                                                }
                                                        }
 
                                                        //Create string
                                                        if(null mltval)
                                                                creastr = exsfr " " type " " defval " " inhrtval " " aname
                                                        else                    
                                                                creastr = exsfr " " type " " mltval " " inhrtval " " aname
 
                                                        //Create the attribute
                                                        create creastr

 


The compiler tells me that I'm incorrectly concatenating the values. What am I doing wrong?

 


meherts - Thu Jun 17 09:28:14 EDT 2010

Re: How to dynamically create an attribute definition using strings
Mathias Mamsch - Thu Jun 17 10:01:18 EDT 2010

Wow that code looks scary. The compiler tells me "kval" is not declared. But even if I declare it there seems to be more broken. Can you post your error message.

What exactly are you trying to do with that code? There might be a more elegant way for whatever you are trying to achieve.

Regards, Mathias

Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: How to dynamically create an attribute definition using strings
llandale - Thu Jun 17 12:22:08 EDT 2010

E-gads. Consider using spaces to indent, instead of tabs.

You seem to be missing several declarations. Anyway, the offending lines seem to be at about lines 160 and 162:
creastr = exsfr " " type " " defval " " inhrtval " " aname
creastr = exsfr " " type " " mltval " " inhrtval " " aname

Since 'creastr' is defined as an AttrDef, it doesn't make sense to concatanate a bunch of strings and assign it to it. That failure will trigger 'if' statement errors. I suspect that should be declared as a string; unless you are really trying to create an attribute. Here is some sample code to do that:

{  current = mod
   noError()
   if (DoModule and DoObject)
     ad    = create(module object                                       //-
         type NameType  history DoHistory       changeBars DoBars       //-
         date DoDate    inherit DoInherit       attribute NameAttr
             )
   elseif (DoModule and !DoObject)
      ad        = create(module                                         //-
         type NameType  history DoHistory       changeBars DoBars       //-
         date DoDate    inherit DoInherit       attribute NameAttr
          )
   elseif (!DoModule and DoObject)
      ad        = create(object                                         //-
          type NameType history DoHistory       changeBars DoBars       //-
          date DoDate   inherit DoInherit       attribute NameAttr
              )
    else{}     // must be either DoModule or DoObject or Both.
    ErrMess = lastError()       // Capture the Create error, if any


The 'Name' attributes are strings; the 'Do' attributes are boolean; 'ad' is an AttrDef. Hmmm, I see the code doesn't deal with default values. I think you do that by inserting "(default DefValue)" in there, after a 'Do' attribute.

 

 

  • Louie

 

 

Re: How to dynamically create an attribute definition using strings
meherts - Thu Jun 17 12:23:43 EDT 2010

Mathias Mamsch - Thu Jun 17 10:01:18 EDT 2010
Wow that code looks scary. The compiler tells me "kval" is not declared. But even if I declare it there seems to be more broken. Can you post your error message.

What exactly are you trying to do with that code? There might be a more elegant way for whatever you are trying to achieve.

Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

The code is scary, even to me!

Here is what my code is trying to acheive: 1)read all attributes from a key module into an array (kval); 2) inspect all modules within DOORS to see if they have these key attributes - if they don't, create them.

I've realized that the error with my code is that I'm trying to create an attribute using a string, instead of an attribute definition variable ... there has to be a more elegant way of acheiving what I'm trying to do.

Re: How to dynamically create an attribute definition using strings
llandale - Thu Jun 17 12:27:50 EDT 2010

One of the fundamenal principles of software engineering is this: understandable software that doesn't work is much more useful than mis-understandable software that does work.

One of the corelaries of that is this: its a lot easier to make understandable software work than mis-understandable software.

Thus, always right code for the benefit of the reader 6 months from now.

  • Louie

Re: How to dynamically create an attribute definition using strings
Mathias Mamsch - Thu Jun 17 12:32:48 EDT 2010

llandale - Thu Jun 17 12:22:08 EDT 2010

E-gads. Consider using spaces to indent, instead of tabs.

You seem to be missing several declarations. Anyway, the offending lines seem to be at about lines 160 and 162:
creastr = exsfr " " type " " defval " " inhrtval " " aname
creastr = exsfr " " type " " mltval " " inhrtval " " aname

Since 'creastr' is defined as an AttrDef, it doesn't make sense to concatanate a bunch of strings and assign it to it. That failure will trigger 'if' statement errors. I suspect that should be declared as a string; unless you are really trying to create an attribute. Here is some sample code to do that:

{  current = mod
   noError()
   if (DoModule and DoObject)
     ad    = create(module object                                       //-
         type NameType  history DoHistory       changeBars DoBars       //-
         date DoDate    inherit DoInherit       attribute NameAttr
             )
   elseif (DoModule and !DoObject)
      ad        = create(module                                         //-
         type NameType  history DoHistory       changeBars DoBars       //-
         date DoDate    inherit DoInherit       attribute NameAttr
          )
   elseif (!DoModule and DoObject)
      ad        = create(object                                         //-
          type NameType history DoHistory       changeBars DoBars       //-
          date DoDate   inherit DoInherit       attribute NameAttr
              )
    else{}     // must be either DoModule or DoObject or Both.
    ErrMess = lastError()       // Capture the Create error, if any


The 'Name' attributes are strings; the 'Do' attributes are boolean; 'ad' is an AttrDef. Hmmm, I see the code doesn't deal with default values. I think you do that by inserting "(default DefValue)" in there, after a 'Do' attribute.

 

 

  • Louie

 

 

Instead of doing all cases with IF statements you can use the AttrDef__ Type to build your create string. Something like this does the trick:
 

AttrDef__ a = type sType date DoDates history DoHistory changeBars DoChangeBars attribute attributeName
        
        if(bModule) a = module a  else a = object a
        if (!null sDefault) a = (default sDefault) a
 
        create a

 


Regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: How to dynamically create an attribute definition using strings
meherts - Thu Jun 17 14:33:25 EDT 2010

Mathias Mamsch - Thu Jun 17 12:32:48 EDT 2010

Instead of doing all cases with IF statements you can use the AttrDef__ Type to build your create string. Something like this does the trick:
 

AttrDef__ a = type sType date DoDates history DoHistory changeBars DoChangeBars attribute attributeName
        
        if(bModule) a = module a  else a = object a
        if (!null sDefault) a = (default sDefault) a
 
        create a

 


Regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Thank you so much!

@Louie ... I'm relatively new to DXL, but I understand where you're coming from ... unfortunately, this was my first attempt at coding this up ... refactoring was definitely going to be necessary anyway ... just trying to wrap my head around this language - thank you for your input as well!

Best regards to all that helped me,
m.

Re: How to dynamically create an attribute definition using strings
meherts - Thu Jun 17 16:14:59 EDT 2010

meherts - Thu Jun 17 14:33:25 EDT 2010
Thank you so much!

@Louie ... I'm relatively new to DXL, but I understand where you're coming from ... unfortunately, this was my first attempt at coding this up ... refactoring was definitely going to be necessary anyway ... just trying to wrap my head around this language - thank you for your input as well!

Best regards to all that helped me,
m.

what if I receive this error?: "no access to create attribute 'Comment'"

I tried the setDef function, but the attribute doesn't actually exist within the module ... so how can I give myself permissions to create?

Re: How to dynamically create an attribute definition using strings
Mathias Mamsch - Thu Jun 17 18:44:23 EDT 2010

meherts - Thu Jun 17 16:14:59 EDT 2010
what if I receive this error?: "no access to create attribute 'Comment'"

I tried the setDef function, but the attribute doesn't actually exist within the module ... so how can I give myself permissions to create?

Excluse Edit Mode helps a lot ... Despite that you need "Create Access" to the Module ... Also take into account that there might be hidden Attributes in the module (especially comment might be a reserved attribute name). So you can maybe try another attribute name. Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: How to dynamically create an attribute definition using strings
meherts - Fri Jun 18 09:15:50 EDT 2010

Mathias Mamsch - Thu Jun 17 18:44:23 EDT 2010
Excluse Edit Mode helps a lot ... Despite that you need "Create Access" to the Module ... Also take into account that there might be hidden Attributes in the module (especially comment might be a reserved attribute name). So you can maybe try another attribute name. Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

This is going to sound silly, but here is what I was missing ...:
 

//Open module for editing
m = edit(fullName(m), true)

 


Once again, thank you so much ...