Analytics Builder Block SDK
Tags: analytics, sdk, apama, block
Published: 2020-02-04 15:15:22

---
title: Analytics Builder Block SDK
published: true
date: 2020-01-22 09:41:00 UTC
tags: #analytics, #sdk,  #apama, #block
canonical_url: http://techcommunity.softwareag.com/techniques-blog/-/blogs/analytics-builder-block-sdk
---

# Block SDK describes how to write, test and deploy custom analytics builder blocks

_By Kusuma Kumari, QA Specialist IoT & Analytics and Mounika Konathala, Senior Software Engineer IoT & Analytics, Software AG_

## Prerequisites

- Apama installation 10.5.0 or above
- [Apama Analytics Builder Block SDK](https://github.com/SoftwareAG/apama-analytics-builder-block-sdk/)
- A Cumulocity cloud tenant (with admin user), or a Cumulocity Edge installation (both 10.5.0 or later)

Note: Custom block, and hence the Block SDK, are not supported with Apama Starter, you need a full version.

## Introduction

Analytics Builder allows you to build analytic models using blocks that are capable of processing data in real time. Blocks are small bits of logic wired together to form models in model manager. Analytics Builder provides a set of pre-built blocks like finding the average, or the difference between inputs, etc. ([Pre-built blocks](https://documentation.softwareag.com/onlinehelp/Rohan/Analytics_Builder/pab10-5/apama-pab-webhelp/index.html#page/apamaanalyticsbuilder-webhelp%2Fre-AnaBui_overview_of_blocks.html%23)). In the case where you want to have your own custom logic to be implemented and use them in models, this article gives you detailed information on how to add custom blocks to Analytics Builder using the Block Software Development Kit, which is called the Block SDK. The Block SDK 10.5.0 is introduced in Analytics Builder 10.5.0. You will need to git clone the Block SDK from [github](https://github.com/SoftwareAG/apama-analytics-builder-block-sdk).

In this article let's use one of the samples provided with the Block SDK and upload it to a tenant that hosts Analytics Builder.

Blocks can be tested using the PySys test framework that is included in an Apama installation. To develop, test and package blocks, you will need a full installation of [Apama](http://www.apamacommunity.com/downloads/).

Blocks are implemented in Apama's Event Processing Language (EPL). This guide assumes a working knowledge of EPL. Refer to the [Apama documentation on developing EPL applications](http://www.apamacommunity.com/documents/10.5.0.2/apama_10.5.0.2_webhelp/apama-webhelp/#page/apama-webhelp%2Fco-DevApaAppInEpl_how_this_book_is_organized.html%23) and the [API Reference for EPL (ApamaDoc)](http://www.apamacommunity.com/documents/10.5.0.2/apama_10.5.0.2_webhelp/ApamaDoc/). This guide also assumes a working knowledge of the Analytics Builder data model. Refer to the [Apama Analytics Builder documentation](https://documentation.softwareag.com/onlinehelp/Rohan/Analytics_Builder/pab10-5/apama-pab-webhelp/index.html#page/apamaanalyticsbuilder-webhelp%2Fto-AnaBui_getting_started_with_apama_analytics_builder.html%23).

## Using Software AG Designer

While blocks may be written in any text editor, Software AG Designer provides syntax highlighting, completion proposals and error reporting. For the Designer to provide completion proposals, it needs to be configured with the location of the block SDK. You can do this with the _analytics\_builder configure designer_ command line script, which is included in the Block SDK. Open an Apama Command Prompt window, change to the directory where you have cloned the Block SDK repository, and run the command line:

```
analytics_builder configure designer
```

After the above command has run, restart Software AG Designer. Create an Apama project using Designer and add the bundles Analytics Builder and Cumulocity Block Helpers to the project.

![](http://techcommunity.softwareag.com/documents/10157/11220621/blockSDK_1.png/f9e1766b-2ced-4d10-9548-de6ebba22ba8?t=1579690969267)

_**Figure 1:** ‘Add Bundle’ dialog showing Analytics Builder bundles after configuring with Block SDK_

Create an EPL file Offset.mon under the Apama project as shown below:

![](http://techcommunity.softwareag.com/documents/10157/11220621/blockSDK_2.png/75c71711-26ed-47ad-a4ca-c005fd73f8ac?t=1579690972215)

_**Figure 2:** Right-click on the project and select EPL Event Definition menu item_

![](http://techcommunity.softwareag.com/documents/10157/11220621/blockSDK_3.png/8fe7233d-57d7-42b3-94c5-fba46a999384?t=1579690975118)

_**Figure 3:** New EPL Event Definition dialog with default option selected_

![](http://techcommunity.softwareag.com/documents/10157/11220621/blockSDK_4.png/e07bb6f8-8fce-48d3-87f4-cbf739ba6d2b?t=1579690977998)

_**Figure 4:** Creation of Offset block with package name_

![](http://techcommunity.softwareag.com/documents/10157/11220621/blockSDK_5.png/8f9fd5a9-9e13-467a-8f9f-ed463349a851?t=1579690980757)

_**Figure 5:** EPL editor_

## Writing a block

Let's look at one sample block, Offset, that adds a fixed offset of 100 to the supplied value. This block is available in the 'samples' directory of the Block SDK.

```
package apamax.analyticsbuilder.samples;using apama.analyticsbuilder.BlockBase;using apama.analyticsbuilder.Activation; /\*\* \* Offset. \* \* Add a fixed offset of 100 to the supplied value. \* \* @$blockCategory Calculations \*/event Offset { /\*\*BlockBase object. \* \*/ BlockBase $base; /\*\* \* This action receives the input value and contains the logic of the block. \* \* @$inputName value Value \*/ action $process(Activation $activation, float $input\_value) { $setOutput\_output($activation, $input\_value + 100.0 ); } /\*\* \* Output. \* \* The output value. \*/ action<activation float> $setOutput_output;
}
```

**$base** is a field of type apama.analyticsbuilder.BlockBase that provides contextual information about the model the block is within. All blocks must have a BlockBase $base field.

**$process** in an action is where the main processing of the block will take place.

**$activation** is a parameter to that action to provide contextual information required when generating a block output.

Block inputs are specified as parameters whose name is prefixed with **$input\_** and the **$process** action may contain multiple input parameters.

Block outputs are each specified as separate action variables whose name is prefixed with **$setOutput\_** , and again a block may contain multiple outputs.

When a model containing this block is activated, an instance of the block is created, initializing **$base** and **$setOutput\_\*** and calling the **$process** action when the block receives input.

If you create a model containing this block and the 'value' input wired to a source block and then activate the model, the model will call the $process action of our sample block every time a new value is available on the 'value' input.

Any calls this block makes to **$setOutput\_output** will generate a new value on the block's output.

Adding the ApamaDoc tag **@$blockCategory** followed by a category name shows a block under the category in Analytics Builder's model editor.

For more detailed information refer to the "[Basic Blocks](https://github.com/SoftwareAG/apama-analytics-builder-block-sdk/blob/master/doc/010-BasicBlocks.md)" section of the Block SDK documentation.

# Testing a block

You can test your custom blocks using PySys testing framework shipped with an Apama installation. You also need to set an environment variable, ANALYTICS\_BUILDER\_SDK, to the location of the Block SDK.

Below is a sample PySys test for the Offset block (run.py):

```
from pysys.constants import \*from apamax.analyticsbuilder.basetest import AnalyticsBuilderBaseTest class PySysTest(AnalyticsBuilderBaseTest): def execute(self): correlator = self.startAnalyticsBuilderCorrelator( blockSourceDir=f'{self.project.SOURCE}/blocks/') modelId = self.createTestModel('apamax.analyticsbuilder.samples.Offset') self.sendEventStrings(correlator, self.timestamp(1), self.inputEvent('value', 100.75, id=modelId), self.timestamp(2), self.inputEvent('value', 10.50, id=modelId), self.timestamp(5), ) def validate(self): self.assertGrep('output.evt', expr=self.outputExpr('output', 200.75)) self.assertGrep('output.evt', expr=self.outputExpr('output', 110.50))
```

Each PySys test should contain pysystest.xml which describes the test purpose and run.py which checks the block's functionality.

To elaborate, run.py starts the correlator and injects necessary EPL files into the correlator. It then creates a test model that connects all inputs and outputs of the block under test, so you can send inputs to the block and see the block's outputs. Note that testing of the block is done in the local Apama installation, not within Analytics Builder running inside Cumulocity.

For more detailed information, refer to the "[Testing blocks](https://github.com/SoftwareAG/apama-analytics-builder-block-sdk/blob/master/doc/035-Testing.md)" section of the Block SDK documentation.

For more examples of tests, refer to [Apama Analytics Builder Block SDK github repository](https://github.com/SoftwareAG/apama-analytics-builder-block-sdk/tree/master/samples/tests).

## Uploading a block to Analytics Builder

Once blocks are written and tested, you can upload by packaging them into "extensions" (which are .zip files).

From an Apama Command Prompt, run the below command to upload a new extension named "Offset". Note that extension names must be unique.

```
analytics_builder build extension --input <location of samples> --cumulocity_url <cumulocityurl> --username <user> --password <password> --name <extensionName> --restart
```

e.g.

```
analytics_builder build extension --input apama-analytics-builder-block-sdk-master\samples\blocks\temp --cumulocity_url https://tenant.c8y.io --username testUser --password testPassword --name Offset --restart
```

Note: --input should be a valid path to a directory containing extension files.

If you are using Cumulocity Edge then username should be specified along with the tenant ID, for example --username &lttenantid user&gt

This command will build and upload this custom block as an extension to a Cumulocity tenant. These extensions are only read when the apama-ctrl microservice inside Cumulocity is started, so the service must be restarted to make use of this new block (note the --restart in the above command). The web application must also be reloaded (as a simple browser reload) to reload the block catalog after uploading an extension.

Open Analytics Builder by clicking on Analytics Builder in the app switcher and then when opening/creating a model in the editor you can see the newly uploaded block, Offset, under the Calculation category in the left panel.

![](http://techcommunity.softwareag.com/documents/10157/11220621/blockSDK_6.png/469a9219-13b6-46e6-bef5-9621f039b39a?t=1579690983743)

_**Figure 6:** Shows Apama Analytics Builder pane before uploading block and after uploading Offset block_

![](http://techcommunity.softwareag.com/documents/10157/11220621/blockSDK_7.png/22dd3585-a3af-4e44-9401-a46a261a909a?t=1579690986710)

_**Figure 7:** Offset block being used in the model and its documentation_

The above picture depicts our custom block, Offset, being used in the model connecting input and output blocks and you can see its inline ApamaDoc in the right-side documentation pane of the model editor part of Analytics Builder.

For more examples, refer to [Apama Analytics Builder Block SDK github repository](https://github.com/SoftwareAG/apama-analytics-builder-block-sdk/tree/master/samples).

## Delete a block from Analytics Builder

Similarly, if you do not want to use any custom block in Analytics Builder, you can delete the specified block by running the below command followed by restarting Apama Controller (--restart in the command sends restart request of Apama Controller).

```
analytics_builder build extension --delete <extensionName> --cumulocity_url <cumulocityurl> --username <user> --password <password> --restart
```

e.g

```
analytics_builder build extension --delete Offset --cumulocity_url https://tenant.c8y.io --username testUser --password
```

## Adding parameters to a block

The Offset block adds a fixed value of 100 to the input value. You can also pass this value as a parameter to the block by adding a **<BlockName>\_$Parameters** event with a member for each parameter and add a member to the block of this type named $parameters. Supplied parameters can be validated by implementing **$validate** action on either the <BlockName>\_$Parameters event or the block itself.

```
/\*\* \* Parameters for Offset block. \*/event Offset\_$Parameters { /\*\* \* Offset. \* \* The offset as a float. \* \*/ float offset; action $validate() { if offset < 0.0 or not offset.isFinite() { throw Exception("block\_msg\_apamax.sampleblock.Offset\_outOfRange", offset.toString()); } }} /\*\* \* Offset. \* \* Adds offset to the supplied value. \* \* @$blockCategory Calculations \*/event Offset { BlockBase $base; Offset\_$Parameters $parameters; action $process(Activation $activation, float $input\_value) { $setOutput\_output($activation, $input\_value + $parameters.offset); } action<activation float> $setOutput_output;
}
```

ApamaDoc comments in the snippet will be shown in model editor's documentation pane.

![](http://techcommunity.softwareag.com/documents/10157/11220621/blockSDK_8.png/de05befd-73d2-4c26-acb0-dc8eab4c654f?t=1579690990018)

_**Figure 8:** Offset block after modification showing parameters and its documentation_

Note: Re-uploading the same extension name will replace the old extension. You must not have two extensions with different names but the same block package and type name.

## Summary

Block SDK allows us to create, test and deploy custom Analytics Builder Blocks and you have seen creating a simple Offset block and adding parameters to it. You can create even more sophisticated blocks with timers, blocks with state, listeners that listen to or sends to external events, etc. For more details, refer to [Block SDK Documentation](https://github.com/SoftwareAG/apama-analytics-builder-block-sdk/blob/master/doc/000-contents.md).

To learn more, visit:

[Block SDK Documentation](https://github.com/SoftwareAG/apama-analytics-builder-block-sdk/blob/master/doc/000-contents.md)

[API Reference for Analytics Builder (ApamaDoc)](https://softwareag.github.io/apama-analytics-builder-block-sdk/doc/apamadoc/index.html)

[API Reference for EPL (ApamaDoc)](http://www.apamacommunity.com/documents/10.5.0.2/apama_10.5.0.2_webhelp/ApamaDoc/)

[Developing Apama Applications in EPL](http://www.apamacommunity.com/documents/10.5.0.2/apama_10.5.0.2_webhelp/apama-webhelp/#page/apama-webhelp%2Fco-DevApaAppInEpl_how_this_book_is_organized.html)

[API Reference for Python (Pydoc)](http://www.apamacommunity.com/documents/10.5.0.2/apama_10.5.0.2_webhelp/pydoc)