Templates are defined on a particular metaclass in a language definition file or extension and are inherited by and available to the children of the metaclass. For example, a template defined on the Classifier metaclass is available to templates or generated files defined on the Class and Interface metaclasses.
GTL supports the following OO concepts as part of inheritance:
Polymorphism - The choice of the template to be evaluated is made at translation-time. A template defined on a classifier can access templates defined on its children (class, interface). In the following example, the content of %definition% depends on whether a class or an interface is being processed:
Template overriding - A template defined on a given metaclass can be overridden by a template of the same name defined on a child class. In the following example the template defined on the Classifier metaclass is overridden by the one defined on the Class metaclass:
You can view the overridden parent by right-clicking the child template and selecting Go to Super-Definition. You can specify the use of the parent template by prefixing the template call with the :: qualifying operator. For example: %Classifier::isAbstract%.
Template overloading - You can overload your template definitions and test for different conditions. Templates can also be defined under criteria (see Criteria (Profile)) or stereotypes (see Stereotypes (Profile)), and the corresponding conditions are combined. At translation-time, each condition is evaluated and the appropriate template (or, in the event of no match, the default template) is applied. For example:
You can define the same template multiple times in the hierarchy of a language definition file and extensions files, and PowerDesigner will resolve it using inheritance rules. For example, the myLang OOM language definition file and the myExtension extension file each contain a template %t% defined on each of the Classifier and Class metaclasses:
myLang Language Definition File | myExtension Extension File |
---|---|
|
|
The Class and Interface metaclasses both inherit from the Classifier metaclass, and each will generate a myFile and a myOtherFile.
Template Call in myFile | Template Called |
---|---|
%t% or %myLang::t% |
myLang/Class/t |
%Classifier::t% or %myLang::Classifier::t% |
myLang/Classifier/t |
Template Call in myOtherFile | Template Called |
---|---|
%t% or %myExtension::t% |
myExtension/Class/t |
%Classifier::t% or %myExtension::Classifier::t% |
myExtension/Classifier/t |
%myLang::t% or %myLang::Class::t% |
myLang/Class/t |
%myLang::Classifier::t% |
myLang/Classifier/t |