Domain-Specific Templates

Last revised 05/03/2011 by A. W. Crapo

The SADL language grammar and the SADL-IDE are implemented using the Xtext language development suite. Xtext supports language templates and SADL comes with a number of predefined templates for common SADL constructs such as declaring a new class. A template appears, when the specified conditions are met based on the cursor location within , when the user asks for content assistance by holding down the Control key and pressing the Space Bar. The template is then inserted into the model file at the cursor location and the user fills in the template variables, using tab to move to the next variable.

Xtext also provides the user of the IDE with the ability to define new templates on-the-fly. These templates can be domain-specific for the purpose of assisting subject matter experts in creating new model content within a particular domain. This document illustrates how this is done.

Suppose that we have a simple model that includes the class Person and properties gender and age, as shown below.

Gender is a class, must be one of {Male, Female}.
is a class, described by gender with a single value of type Gender,
described by age with a single value of type int,
described by birthdate with a single value of type date.

We might wish to provide a template for creating new instances of the Person class. To do so we open the SADL templates dialog: Window -> Preferences ->Sadl -> Templates.

This dialog shows the predefined templates and allows them to be disabled or edited. In this case we wish to add a new template so click the New button to bring a dialog like the one below.

In addition to a list of pre-defined variables, a template "variable" has two syntaxes of interest here.

Variable Syntax Explanation
${name} "name" will be selected in the template insertion allowing the user to type a new value
${name:ResourceName('TYPE')} Content assist will be invoked to get a list of possible choices for this variable based on the value of "TYPE". If there is a single item in the resulting list it will be inserted. Otherwise the user may choose the value from the list

The supported values of the "TYPE" argument are as follows.

Value of "TYPE" Explanation
ONTCLASS will generate a list of known classes
OBJECTPROPERTY will generate a list of known object properties
DATATYPEPROPERTY will generate a list of known datatype properties
INDIVIDUAL will generate a list of known instances
INDIVIDUAL:<class> will generate a list of known instances of the specified class
ONTCLASS:<class> will generate a list of known subclasses of the specified class
OBJECTPROPERTY:<property> will generate a list of known subproperties of the specified object property
DATATYPEPROPERTY:<property> will generate a list of known subproperties of the specified datatype property

In our example we want our new template to be active in the context Statement (whenever a Statement can be inserted into the model at the current cursor location). The template itself will be as follows:

${Name} is a Person, gender ${var2:ResourceName('INDIVIDUAL:Gender")}, age ${xmldate} . ${cursor}

The first variable will allow the user to type in the instance name of the new instance. Tabbing to the second variable, the user will be able to choose from a list of the instances of the class Gender. The desired list item may be selected by typing the first character, e.g., "M" for "Male" or by clicking on the menu item. Tabbing to the third variable, a system-defined variable returning the current date, the user may overwrite the selected date. Here the current date serves as an example of the required XML date syntax. After a final tab the cursor is placed at the location identified by the "cursor" variable. This template adds a space after the period and before the new cursor location to ensure that the statement is valid. (Statements in SADL must have white space after the last period in the file.)