1. Home
  2. /
  3. Docs
  4. /
  5. Documentation
  6. /
  7. User Guide
  8. /
  9. Rule Engine Overview

Rule Engine Overview

NexAI Rule Engine is a highly customizable and configurable system for complex event processing. With rule engine you are able to filter, enrich and transform incoming messages originated by IoT devices and related assets. You are also able to trigger various actions, for example, notifications or communication with external systems.

Key Concepts

Rule Engine Message

Rule Engine Message is a serializable, immutable data structure that represent various messages in the system. For example:

  • Incoming telemetry, attribute update or RPC call from device;
  • Entity life-cycle event: created, updated, deleted, assigned, unassigned, attributes updated;
  • Device status event: connected, disconnected, active, inactive, etc;
  • Other system events.

Rule Engine Message contains the following information:

  • Message ID: time based, universally unique identifier;
  • Originator of the message: Device, Asset or other Entity identifier;
  • Type of the message: “Post telemetry” or “Inactivity Event”, etc;
  • Payload of the message: JSON body with actual message payload;
  • Metadata: List of key-value pairs with additional data about the message.

Rule Node

Rule Node is a basic component of Rule Engine that process single incoming message at a time and produce one or more outgoing messages. Rule Node is a main logical unit of the Rule Engine. Rule Node can filter, enrich, transform incoming messages, perform action or communicate with external systems.

Rule Node Connection

Rule Nodes may be connected to other rule nodes. Each relation has relation type, a label used to identify logical meaning of the relation. When rule node produces the outgoing message it always specifies the relation type which is used to route message to next nodes.

Typical rule node relations are “Success” and “Failure”. Rule nodes that represent logical operations may use “True” or “False”. Some specific rule nodes may use completely different relation types, for example: “Post Telemetry”, “Attributes Updated”, “Entity Created”, etc.

Some rule nodes support custom connection names. Just type your custom connection name and click the “Create a new one!” link:

All connection names are case-sensitive.

Rule Chain

Rule Chain is a logical group of rule nodes and their relations. For example, the rule chain below will:

  • save all telemetry messages to the database;
  • raise “High Temperature Alarm” if temperature field in the message will be higher then 50 degrees;
  • raise “Low Temperature Alarm” if temperature field in the message will be lower then -40 degrees;
  • log failure to execute the temperature check scripts to console in case of logical or syntax error in the script.

Tenant administrator is able to define one Root Rule Chain and optionally multiple other rule chains. Root rule chain handles all incoming messages and may forward them to other rule chains for additional processing. Other rule chains may also forward messages to different rule chains.

For example, the rule chain below will:

  • raise “High Temperature Alarm” if temperature field in the message will be higher then 50 degrees;
  • clear “High Temperature Alarm” if temperature field in the message will be less then 50 degrees;
  • forward events about “Created” and “Cleared” alarms to external rule chain that handles notifications to corresponding users.

Message Processing Result

There are three possible results of message processing: Success, Failure and Timeout. The message processing attempt is marked as “Success” when the last rule node in the processing chain successfully process the message. The message processing attempt is marked as “Failure” if one of the rule nodes produce “Failure” of message processing, and there is no rule nodes to handle that failure. The message processing attempt is marked as “Timeout” when overall time of processing exceed configurable threshold.

See diagram below and let’s review the possible scenarios:

If the “Transformation” script fails, the message is not marked as “Failed”, because there is a “Save to DB” node connected with “Failure” relation. If the “Transformation” script is successful, it will be pushed to “External System” with the REST API call. If the external system is overloaded, the REST API call may “hang” for some time. Let’s assume the overall timeout for message pack processing is 20 seconds. Let’s ignore Transformation script execution time because it is < 1ms. So, if the “External System” will reply within 20 seconds, the message will be successfully processed. Similar, if “Save to DB” call will succeed, the message will be successfully processed. However, if the external system will not reply within 20 seconds, the message processing attempt will be marked as “timed-out”. Similar, if “Save to DB” call will fail, the message will be marked as failed.

Predefined Message Types

List of the predefined Message Types is presented in the following table.

Message TypeDisplay NameDescriptionMessage metadataMessage payload
POST_ATTRIBUTES_REQUESTPost attributesRequest from device to publish client side attributes deviceName – originator device name,
deviceType – originator device type
key/value json:
{
  "currentState": "IDLE"
}
POST_TELEMETRY_REQUESTPost telemetryRequest from device to publish telemetrydeviceName – originator device name,
deviceType – originator device type,
ts – timestamp (milliseconds)
key/value json:
{
  "temperature": 22.7
}
TO_SERVER_RPC_REQUESTRPC Request from DeviceRPC request from devicedeviceName – originator device name,
deviceType – originator device type,
requestId – RPC request Id provided by client
json containing method and params:
{
  "method": "getTime",
  "params": { "param1": "val1" }
}
RPC_CALL_FROM_SERVER_TO_DEVICERPC Request to DeviceRPC request from server to devicerequestUUID – internal request id used by sustem to identify reply target,
expirationTime – time when request will be expired,
oneway – specifies request type: true – without response, false – with response
json containing method and params:
{
  "method": "getGpioStatus",
  "params": { "param1": "val1" }
}
ACTIVITY_EVENTActivity EventEvent indicating that device becomes activedeviceName – originator device name,
deviceType – originator device type
json containing device activity information:
{
  "active": true,
  "lastConnectTime": 1526979083267,
  "lastActivityTime": 1526979083270,
  "lastDisconnectTime": 1526978493963,
  "lastInactivityAlarmTime": 1526978512339,
  "inactivityTimeout": 10000
}
INACTIVITY_EVENTInactivity EventEvent indicating that device becomes inactivedeviceName – originator device name,
deviceType – originator device type
json containing device activity information, see Activity Event payload
CONNECT_EVENTConnect EventEvent produced when device is connecteddeviceName – originator device name,
deviceType – originator device type
json containing device activity information, see Activity Event payload
DISCONNECT_EVENTDisconnect EventEvent produced when device is disconnecteddeviceName – originator device name,
deviceType – originator device type
json containing device activity information, see Activity Event payload
ENTITY_CREATEDEntity CreatedEvent produced when new entity was created in systemuserName – name of the user who created the entity,
userId – the user Id
json containing created entity details:
{
  "id": {
    "entityType": "DEVICE",
    "id": "efc4b9e0-5d0f-11e8-8559-37a7f8cdca74"
  },
  "createdTime": 1526918366334,
  ...
  "name": "my-device",
  "type": "temp-sensor"
}
ENTITY_UPDATEDEntity UpdatedEvent produced when existing entity was updateduserName – name of the user who updated the entity,
userId – the user Id
json containing updated entity details, see Entity Created payload
ENTITY_DELETEDEntity DeletedEvent produced when existing entity was deleteduserName – name of the user who deleted the entity,
userId – the user Id
json containing deleted entity details, see Entity Created payload
ENTITY_ASSIGNEDEntity AssignedEvent produced when existing entity was assigned to siteuserName – name of the user who performed assignment operation,
userId – the user Id,
assignedSiteName – assigned site name,
assignedSiteId – Id of assigned site
json containing assigned entity details, see Entity Created payload
ENTITY_UNASSIGNEDEntity UnassignedEvent produced when existing entity was unassigned from siteuserName – name of the user who performed unassignment operation,
userId – the user Id,
unassignedSiteName – unassigned site name,
unassignedSiteId – Id of unassigned site
json containing unassigned entity details, see Entity Created payload
ADDED_TO_ENTITY_GROUPAdded to GroupEvent produced when entity was added to Entity Group.userName – name of the user who performed assignment operation,
userId – the user Id,
addedToEntityGroupName – entity group name,
addedToEntityGroupId – Id of entity group
empty json payload
REMOVED_FROM_ENTITY_GROUPRemoved from GroupEvent produced when entity was removed from Entity Group.userName – name of the user who performed unassignment operation,
userId – the user Id,
removedFromEntityGroupName – entity group name,
removedFromEntityGroupId – Id of entity group
empty json payload
ATTRIBUTES_UPDATEDAttributes UpdatedEvent produced when entity attributes update was performeduserName – name of the user who performed attributes update,
userId – the user Id,
scope – updated attributes scope (can be either SERVER_SCOPE or SHARED_SCOPE)
key/value json with updated attributes:
{
  "softwareVersion": "1.2.3"
}
ATTRIBUTES_DELETEDAttributes DeletedEvent produced when some of entity attributes were deleteduserName – name of the user who deleted attributes,
userId – the user Id,
scope – deleted attributes scope (can be either SERVER_SCOPE or SHARED_SCOPE)
json with attributes field containing list of deleted attributes keys:
{
  "attributes": ["modelNumber", "serial"]
}
ALARMAlarm eventEvent produced when an alarm was created, updated or deletedAll fields from original Message Metadata
isNewAlarm – true if a new alram was just created
isExistingAlarm – true if an alarm is existing already
isClearedAlarm – true if an alarm was cleared
json containing created alarm details:
{
  "tenantId": {
     ...
  },
  "type": "High Temperature Alarm",
  "originator": {
     ...
  },
  "severity": "CRITICAL",
  "status": "CLEARED_UNACK",
  "startTs": 1526985698000,
  "endTs": 1526985698000,
  "ackTs": 0,
  "clearTs": 1526985712000,
  "details": {
    "temperature": 70,
    "ts": 1526985696000
  },
  "propagate": true,
  "id": "33cd8999-5dac-11e8-bbab-ad47060c9431",
  "createdTime": 1526985698000,
  "name": "High Temperature Alarm"
}
ALARM_ASSIGNEDAlarm AssignedEvent produced when an alarm was assigned to some userAll fields from original Message Metadata
entityName – name of alarm
entityType – ALARM
userEmail – user email
userFirstName – user first name
userId – user id
userLastName – user last name
userName – user name
json containing alarm details, see Alarm event
ALARM_UNASSIGNEDAlarm UnassignedEvent produced when an alarm was unassigned from userAll fields from original Message Metadata
entityName – name of alarm
entityType – ALARM
userEmail – user email
userFirstName – user first name
userId – user id
userLastName – user last name
userName – user name
json containing alarm details, see Alarm event
COMMENT_CREATEDComment CreatedEvent produced when an alarm comment was createdAll fields from original Message Metadata
userId – user id
userName – user name
userFirstName – first name of user
userLastName – last name of user
userEmail – email of user
comment – json object containing comment details and text of comment
json containing alarm details, see Alarm event
COMMENT_UPDATEDComment UpdatedEvent produced when an alarm comment was updatedAll fields from original Message Metadata
userId – user id
userName – user name
userFirstName – first name of user
userLastName – last name of user
userEmail – email of user
comment – json object containing comment details and text of comment
json containing alarm details, see Alarm event
REST_API_REQUESTREST API Request to Rule EngineEvent produced when user executes REST API callrequestUUID – the unique request id,
expirationTime – the expiration time of the request
json with request payload

Rule Node Types

All available rule nodes are grouped in correspondence with their nature:

  • Filter Nodes are used for message filtering and routing;
  • Enrichment Nodes are used to update meta-data of the incoming Message;
  • Transformation Nodes are used for changing incoming Message fields like Originator, Type, Payload, Metadata;
  • Action Nodes execute various actions based on incoming Message;
  • External Nodes are used to interact with external systems.

Configuration

Each Rule Node may have specific configuration parameters that depend on the Rule Node Implementation. For example, “Filter – script” rule node is configurable via custom JS function that process incoming data. “External – send email” node configuration allows to specify mail server connection parameters.

Rule Node configuration window may be opened by double-clicking on the node in the Rule Chain editor:

Test script functions

Some rule nodes have specific UI feature that allow users to test TBEL/JS functions. Once you click on the Test Filter Function you will see the JS Editor that allows you to substitute input parameters and verify the output of the function.

You can define:

  • Message Type in the top left field.
  • Message payload in the left Message section.
  • Metadata in right Metadata section.
  • Actual NAEL/JS script in Filter section.

After pressing Test output will be returned in right Output section.

Rule Engine Statistics

NexAI Team have prepared the “default” dashboard for Rule Engine statistics. This dashboard is automatically loaded for each tenant. You may access it by navigating to “Api Usage”->”View statistics” (see screen below).

The statistics collection is enabled by default and is controlled via configuration properties.

You may notice insights about errors in processing and what causes them on the dashbaord below:

Debugging

NexAI provides ability to review incoming and outgoing messages for each Rule Node. To enable debug, user need to ensure that “Debug mode” checkbox is selected in the main configuration window.

Once debug is enabled, user is able to see incoming and outgoing messages info as long as corresponding relation types. See image below for a sample debug messages view:

Import/Export

You are able to export your rule chain to JSON format and import it to the same or another NexAI instance.

In order to export rule chain, you should navigate to the Rule Chains page and click on the export button located on the particular rule chain card.

Similar, to import the rule chain you should navigate to the Rules Chains page and click on the big “+” button in the bottom-right part of the screen and then click on the import button.

Still stuck? Contact

How can we help?

Access the CapitalAI Library

Enter your details below to view and download all of the content from our library.

PDF Download Form
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.