## General

The Easy Rules Engine Service supports execution of a centralized set of rules (stored as files or provided within the service configuration) against a provided set of data called facts. It supports both the RulesEngineProvider and RulesEngineService interfaces, allowing callers to send facts to the service to process against a centralized rules engine, or allowing them to retrieve an instance of a rules engine to process facts locally. Upon execution, the rules engine will determine what rules have been met and return a list of actions that should be executed based on the conditions defined within the rules.

Rules can be implemented in any of the following formats:

- NiFi Rules Format - This is a rules file which follows the NiFi style for rules definition, which supports MVEL (MVFLEX) Expression language for conditions (default format).
- Easy Rules Format with MVEL Support - This is a rules file which follows the Easy Rules style for rules definition that supports MVEL (MVFLEX) Expression language for conditions and actions
- Easy Rules Format with SPEL Support - This is a rules file which follows the Easy Rules style for rules definition that supports Spring Expression language for conditions and actions

All rules formats can be structured as JSON or YAML (with JSON serving as default type). Rules can be stored as a file or provided in the Rules Body setting in the service's configuration settings.

NiFi Rules Format consist of the following elements:

Name | The name of the rule.
Description | A description of the rule.
Priority | Unique number for order in which rule should be executed in the rules engine.
Condition | The condition that will be checked given a set of fact data. For NiFi Format the MVEL expression language is supported.
Action(s) | The required actions that should be performed if the condition checked returns true. This includes the type of action and any attributes required to execute that action. Action types and attributes are string values that can be defined as needed. The expectation is the types and attributes are understood by the caller/action executor.
Fact(s) | The name of fact item(s) that is required to execute the rule.

NiFi rules format is very similar to Easy Rules file format except the Action includes details for the type of action that should be performed along with attributes for performing the action. This provides a more flexible way to configure external handlers that will execute returned actions. When Easy Rules formats are provided, easy rule actions are translated into an "EXPRESSION" NiFi action type where the attributes contains the command that should be executed directly along with the supported expression language type (via a "command" and "type" attributes respectively).

The following are examples of NiFi Rules Format as YAML and JSON file types. In both cases a single rule is shown however multiple rule entries can be provided (following the appropriate multiple entry syntax for the given file type):

NiFi Rules YAML File: This file shows an example of a single rule which checks if predictedQueuedCount value (provided in facts) is greater than 50. If the condition is met an action with the type of "LOG" is defined, which denotes the action that should be taken by the caller. Attributes for the "LOG" action type can be provided as demonstrated below with the "logLevel and "message" attributes. The caller can use these or other provided attributes in order to configure and execute the "LOG" action.

    name: "Queue Size"
    description: "Queue size check greater than 50"
    priority: 1
    condition: "predictedQueuedCount > 50"
    actions:
      - type: "LOG"
        attributes:
          logLevel: "DEBUG"
          message: "Queue Size Over 50 is detected!"
    facts:
      - "predictedQueuedCount"

NiFi Rules JSON File: This file shows an example of a single rule which checks the size of a flowFile (with a flowFile being available via facts). If the condition is met two actions are defined for execution (with types that should be recognized by the caller).

    [
    {
    "name": "Large Flow File",
    "description": "Flow File Size Larger Than Expected",
    "priority": 1,
    "condition": "flowFile.size > 500",
    "actions": [
    {
    "type": "LOG",
    "attributes": {
    "logLevel": "WARN",
    "message": "Unexpected file size!"
    },
    {
    "type": "SEND",
    "attributes": {
    "sendZeroResults": "false"
    }
    }
    ],
    "facts": ["flowFile"]
    }
    ]