Author Topic: Arithmetic / globals in custom code gen template  (Read 1536 times)

Uffe

  • EA Practitioner
  • ***
  • Posts: 1305
  • Karma: +95/-8
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Arithmetic / globals in custom code gen template
« on: July 17, 2007, 05:25:03 pm »
Hey everyone,


I'm setting up code generation templates for some proprietary config / scripting languages we use.

In one of these, I need to be able to
a) Output the number of elements in a class, and
b) Output the (zero-based) element numbers.

According to the help files there are no numeric variables in the code generation language; all variables are strings.

Variables are local to a template and do not retain their values between successive calls. So I can't, say, initialise a (string) variable in the Class Body template, then in Attribute output LENGTH($var) and set $var = $var + "x" to increase the length by one. Which would have been neat.

If you fiddle around with the attribute list in the Attributes dialog for a class you'll find an ID column to the right of the Initial Value column. These IDs are one-based, but if I could subtract 1 from each of them (or write a monster %if statement to output one less than the ID), I'd get what I need. However, the field is not mentioned in the help file's list of Field Substitution Macros, and %attID% is not recognised by the framework (produces no output).

So:

1) Does anyone know of a simple way to access the number of elements in a class, rather than counting them?

2) Does anyone know of a way to do simple arithmetic in the code generation language?

3) Does anyone know of a way to access the Attribute ID field? No, it's not the GUID. Thanks.

4) Does anyone know of a way to create a global variable?

5) Does anyone have any other bright ideas?

I know that with a full-fledged Add-In you've got all the freedom you need, it's just it seems a bit excessive to me to go through that whole process just for x = x + 1.

I'm using EA 6.5.806 on my production machine, 7.0.813 is still in evaluation (by me), but seems to be no different in this regard.

(Adding %attID% and %classNrAttributes% would of course solve my problems too...)


Cheers,


/Uffe
My theories are always correct, just apply them to the right reality.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 6843
  • Karma: +66/-7
    • View Profile
Re: Arithmetic / globals in custom code gen templa
« Reply #1 on: July 17, 2007, 06:13:46 pm »
Basically, the templates don't have any arithmetic, so what you're wanting isn't easy.

1) You can count the number attributes or operations by:
  • creating a custom template for that type that outputs a single character. eg. Attribute__Count
    Code: [Select]
    a
  • Assign the result of that template to a variable. You could even add conditions to this if necessary.  
    Code: [Select]
    $count = %list="Attribute__Count" @separator=""%
  • Get the length of this string.  
    Code: [Select]
    $count = %LENGTH($count)%
Anything else I think you need to resort to an addin.  In which case I wouldn't use the above trick anyway.

If you go the way of the addin, you'll be better off using the Pos property instead of the ID property.
Eve

support@sparxsystems.com

Uffe

  • EA Practitioner
  • ***
  • Posts: 1305
  • Karma: +95/-8
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Arithmetic / globals in custom code gen templa
« Reply #2 on: July 17, 2007, 11:08:41 pm »
Hi again,


Thanks for that. It solved one of my problems, and got me thinking in a different direction which enabled me to hack something together for the numbering as well, and it ain't pretty but it works.

Attribute__Name
Code: [Select]
%attName%

Attribute__Id
Code: [Select]
$attrNames = %list="Attribute__Name" @separator="#"%
$attrName = %attName%
$attrPos = %FIND($attrNames, $attrName)%
$attrNames = %LEFT($attrNames, $attrPos)%
$attrNames = %TO_UPPER($attrNames)%
$attrNames = %REPLACE($attrNames, "A", "")%
$attrNames = %REPLACE($attrNames, "B", "")%
$attrNames = %REPLACE($attrNames, "C", "")%
... (one for each legal attribute name character)
$attrNames = %REPLACE($attrNames, "8", "")%
$attrNames = %REPLACE($attrNames, "9", "")%
%LENGTH($attrNames)%


The above puts all the attribute names in a #-separated string, then finds the current attribute's name in that string and cuts everything from that position. We're then left with only the names of those attributes preceding the current one in the #-separated string.
Deleting all the legal attribute name characters leaves us with a string with just #'s in it, one for each attribute preceding the current one. The length of this string is the zero-based attribute number.

So it can be done. Please everybody, don't use this code. It's not something I'd care to put my name to. :)
« Last Edit: July 17, 2007, 11:12:25 pm by Uffe »
My theories are always correct, just apply them to the right reality.