Using custom code templates

If you see a pattern in how you write the SPL code for a certain element, i.e. you tend to repeatedly write the same block of code to achieve a certain functionality, you can create a code template to help you write that block of code faster. You can then use content assist in the SPL Text Editor to drop in the code template where you want the code pattern to occur. For example, if you always invoke an operator in a certain way, you can create a code template for that operator with some of the common code filled in. When you invoke content assist and drop in that code template, the operator invocation should be mostly complete. You can insert variables in the code template where you can provide context appropriate information after the template has been inserted.

For example, for debugging purposes, if you want to print out each tuple received, you can create a template for a Custom operator that has an onTuple section to print the tuple.

To create the template, open the InfoSphere Streams > SPL > Templates preference page.

template_newtemplate

  1. Click the New… button to create a new template.
  2. Specify a Name for the template. For example, PrintTuple.
  3. Change the Context to Operator Invocation. This means that the template will be available in content assist where an operator invocation is appropriate.
  4. Specify the following block of code as the Pattern. The ${inputStream} is a variable placeholder where you can specify context appropriate value after you have dropped the template in. Because the three variables are the same, changing one will update the rest.
    () as PrintTuple = Custom(${inputStream})  
    { 
         logic                                 
              onTuple ${inputStream}: 
              {
                   println(${inputStream});
              } 
    }
  5. Click OK to save the template.

Now try out the template in the SPL Text Editor. Place the cursor where you would start writing the code for an operator invocation. Then use Ctrl+Space to invoke content assist. Start typing part of the template name (PrintTuple) and select the appropriate entry.

template_invoke

The template is dropped into the file. The cursor is placed at the first variable to allow you to provide context appropriate information.

template_droppedIn

In this case, we want to specify an input stream. You can invoke content assist using Ctrl+Space to get the list of streams.

template_contentAssist

Select the PreVwap stream. Notice that the other occurrences of the variable are updated to use the same value.

template_completed

If the template had more variables, the cursor will be placed at the next variable.

Join The Discussion