Streams Studio allows developers to¬†use¬†code templates as¬†an easy way to insert predefined code snippets into an¬†SPL application. These code snippets are defined by the operator developer and cover the most common use cases of the operator. ¬†For example, one use case of¬†the Beacon operator is to use it as a counter generator. ¬†The Beacon operator¬†has defined the code snippet for this use¬†case as a code template, called Beacon as Counter,¬†¬†in the operator model. ¬†Prior to Streams V4.0, you can only use code¬†templates in the SPL Editor. In Streams V4.0, this feature is now¬†available in both the SPL Editor and the SPL Graphical Editor.
To insert¬†code template into your SPL application, press¬†ctrl+space¬†in the SPL Editor to activate content assist and then start typing to narrow down the list of suggestions. Starting to type the word ‘beacon’¬†results in¬†the following list of proposals:
The above image shows the content assist popup window and a preview of the code snippet. ¬†Selecting the Beacon As Counter template would add¬†the following code into your SPL application.
Once a template has been inserted, placeholder values will be highlighted by square boxes surrounding the values. ¬†You will need to replace these placeholder values with proper values for your application to compile. ¬†In this case, the numIterations, period, and outputStream would need to modified.
In Streams v4.0, code templates have been added as child entries of the operator that they belong to in the SPL Graphical Editor palette. ¬†For example, in the diagram shown below, you will find that the Beacon operator has become expandable in the SPL Graphical Editor palette. ¬†If you expand that entry, you will find all code templates defined for the¬†Beacon operator. ¬†Hovering over a template will show the code that the template represents. ¬†To add the code template to your application, simply drag and drop the code template from the palette into the composite operator on the canvas.
You can also find templates quickly using the Find feature in the palette:
Once dropped¬†onto the canvas, hovering over the resulting operator will show¬†the code snippet inserted into your application:
When a template is dropped¬†onto the canvas, any placeholder values in the template will need to be replaced to allow the application to compile successfully. This can be accomplished by using the Properties View of the SPL Graphical Editor, or by editing directly in the SPL Editor. With the Beacon as Counter template, the ‘numIterations‘ and ‘period‘ values in the param clause need to be updated.
You can also define your own code templates for commonly used blocks of code – see Mary Komor’s article on custom code templates.