Integrations (PTC products, 3rd party products and code) > Code integration (Ada, ARINC 653, C, C#, C++, IDL, Java, SQL and VB) > Transformation Development Kit (TDK) > Customizing an ACS Code Generator DLL > Customizing a TDK code generation transformation patterns model - current TDK models > How TDK code generation transformation patterns models define code generation > Working with rule text in TDK code generation transformation patterns models (TDK)
  
Working with rule text in TDK code generation transformation patterns models (TDK)
A 4G Generation Class has a number of Rules that define how code is generated. Rules are defined in the Rule Text property (appears on the Text tab of the Property Pages) of 4G Generation Classes and 4G Business Classes. Each Rule has a name, followed by ->, followed by the Rule Text. The Rule Text is written in Backus-Naur form (BNF) like notation and it specifies how code is generated for the item type.
Creating or opening a code file
To create or open a code file, specify the file name and extension through SDL script within -> and <- identifiers in a Group construct.
The following example uses the FilesysLocation operation to determine the folder and the Modeler item name to determine the file name.
(-> Self.FilesysLocation() & Self.Name() & ".cs" <-)
Generating text
To generate text to a code file through Rule Text, specify the text as a literal within straight quotation marks. For example:
"This text is generated in the code file"
Generating spaces
To generate a space, add " " to the Rule Text.
To generate a space only if the previously generated character is not a space, add _ to the Rule Text.
For example:
" "
internal _ protectedInternal
Generating carriage returns and line feeds
To generate a carriage return or line feed, add CR or LF to the Rule Text.
To add a carriage return or line feed to the text only if the previously generated character was not a carriage return or line feed, add _CR_ or _LF_ to the Rule Text.
In the following example, _CR_ ensures that if the Description operation returns text, that text will start after a carriage return.
_CR_ Description
Generating the values of declared extended properties
To generate the value of a Modeler item extended property extended property, in the Rule Text, add a model object reference to the Attribute that declares that extended property. For more information about declaring extended properties, see Declaring extended properties in a TDK code generation transformation patterns model.
To add a model object reference to an Attribute, drag the Attribute from, for example, the Packages pane to the Rule Text that is shown in the Property Pages.
* 
It is not mandatory to use model object references, because the parser that analyzes the rules attempts name matching. However, it is good practice to do so.
Calling other rules and operations
To call another Rule or Operation, add the name of the other Rule or Operation to the Rule Text.
After a called Rule or Operation has been processed, control returns to the point in the Rule Text immediately after the call.
Rules can call Rules and Operation that are defined on the same Class, and they can also call Rules and Operation on related 4G Business Classes and other 4G Generation Classes. For more information about working with Rules and Operations from other Classes, see:
Definition of how tagged text is reverse engineered - 4G GenClass classes (TDK)
Reuse of rules and operations - 4G business classes (TDK)
Option construct
The Option construct allows you to generate text only when values have been returned by the content. The Option construct is enclosed within square brackets. An Option construct can optionally have a Conditional directive:
If the Option construct has a conditional directive, the Conditional directive determines whether the content of the Option construct is generated or ignored.
If the Option construct does not have a conditional directive, the parts of the Optional construct that return values determine whether the content of the Option construct is generated or ignored:
If no values are returned, no text is generated from the Option construct.
If any values are returned, text is generated from the Option construct.
* 
If the Description operation returns a value, that value and a carriage return are generated and if the Description operation does not return a value, no text is generated, not even the carriage return.
[Description CR]
Group construct
A Group construct serves three purposes:
It allows you to change the precedence of association order among other constructs.
It allows you to create a code file or specify a code file into which code is generated.
It allows you to group alternatives that may or may be generated depending on whether a condition is met.
The Group construct is enclosed within opening and closing parenthesis. A group construct can specify one or many alternatives to generate, with each alternative delimited by a pipe (¦). One (and only one) alternative is used to generate code.
If only one alternative is specified, which may be the case when specifying a code file, that alternative is generated. If two or more alternatives are specified, each alternative (except for the last alternative), requires a conditional construct to determine whether that alternative is used to generate code.
In the following example, Hello is generated to the file named MyFile.h.
(-> "Myfile.h" <- "Hello")
In the following example, the IsArray operation is called by the Conditional directive. If the IsArray operation returns 1, params is generated and if the IsArray operation returns 0, the Mechanism rule is called.
(/? IsArray() :/ "params " | Mechanism)
Loop construct
The loop construct runs a rule against a set of Modeler items. The set of items can be specified in two ways:
Through an automation interface role that returns a set of Modeler items that are linked to the Modeler item through that role.
Through a called Operation that returns a list of Modeler items.
The Loop construct is enclosed within opening and closing braces using the following syntax:
{<operation or role>.<rule>}
The rule that is specified must be a rule that is available to the 4G Generation Classes that are used to generate code for the looped items. For example, if the role or called Operation returns a list of Attributes, the specified rule will be a rule on a 4G Generation Class that is mapped to the Attribute meta class.
In the following example, the GetParameters operation returns a list of Parameters. The loop construct runs the Gen rule against each Parameter in the list. Because the items in the list are Parameters, the Gen rule that is specified is a rule that is available to the 4G Generation Class that is mapped to the Parameter meta class.
{GetParameters.Gen}
The following example shows how an Operation's Parameters are generated with multiple Parameters being delimited by a comma. The first Parameter.Gen element runs the Gen rule against only the Operation's first Parameter (because it is not iterated). The second Parameter.Gen element runs the Gen rule against each of the Operation's remaining Parameters, preceding each one with a comma.
Parameters.Gen {"," Parameters.Gen}
* 
Within the context of a Rule, the second Parameters.Gen does not regenerate the first Parameter again, because TDK knows that the first Parameter has been generated using the Parameters operation already.
Literal negotiator
To generate a literal only if the previously generated text was not that the same as that literal, use the literal negotiator. The syntax is as follows, where the negotiated <literal 2> is not generated if it has the same value as <literal 1>:
"<literal 1>" N("<literal 2>")
In the following example, the negotiated literal matches the previously generated text, so only AB is generated.
"AB" N("AB")
In the following example, the negotiated literal does not match the previously generated text, so 12AB is generated.
"12" N("AB")
Imperative directives (running SDL script)
An imperative directive contains SDL script that is run when the control reaches the directive. To run SDL script within Rule Text, specify the SDL Script within /: and :/ identifiers.
* 
/: %error "Invalid Type" :/
In the SDL script, use Self for the Modeler item that will be worked with through the ACS Code Generator DLL.
Conditional directives
A conditional directive contains SDL script that evaluates to numeric zero (False) or non-zero (True). To run conditional directives within Rule Text, specify the SDL Script within /? and :/ identifiers.
* 
If the Default Value property of the item does not have a value, the conditional directive returns True.
/? Self:"Default Value" == "" :/
Indenting and outdenting generated text
To increase the indentation of generated text by one indent, add +1 to the Rule Text. To increase the indentation by two indents, add +2, and so on.
To decrease the indentation of generated text by one indent, add -1 to the Rule Text. To decrease the indentation by two indents, add -2, and so on.
* 
The code generated by the EnumMembers operation is indented by 2 levels, and then the indentation level is outdented by two levels after the EnumMembers operation is processed:
CR +2 EnumMembers -2 CR
Precedence and associativity
The constructs are processed in the following order:
Grouped alternatives (..¦..¦..)
[], {} and ()
Sequence of a b c.
Commenting out rules
To comment out a rule, start the rule line with //.
* 
The first line is a comment.
// The following rule generates Hello to a file named Myfile.h
(-> "Myfile.h" <- "Hello")