In the last weeks, I started working on a new project that uses mainly Azure-based technologies. The core of the project is written in Microsoft LogicApps, but as it turns out, not all requirements could be covered with this technology. Microsoft LogicApps are excellent for orchestrating calls to different systems and ETL style operations that are mainly iterative but have some shortcomings when it comes to hierarchical data structures which require recursive processing, and they also do not have a "native" rule engine. There are ways to integrate business rules engines with Microsoft LogicApps because LogicApps are really very flexible and extensible.

Our customer was interested in the integration of a JSON based rules engine with Microsoft LogicApps, and so we started to research how this could be achieved.  At Onepoint we do have quite some experience with Drools (by the way our colleague Allan Schweitz wrote a great blog about Drools), but we wanted to explore libraries that are somehow more lightweight and can express rules and data in JSON format.

After a bit of research, we found four possible candidate libraries - which we could eventually integrate with Microsoft LogicApps - all of them written in Javascript and some also using Typescript:

NameDescriptionStars on Github
json-rules-engine"json-rules-engine is a powerful, lightweight rules engine. Rules are composed of simple json structures, making them human-readable and easy to persist."This rules engine stores the rules as JSON and also can process data directly in JSON format. This rule engine seems to support fact caching using the Almanac. This rule engine supports nested rules using ALL (AND) and ANY (OR) logical operators. Boolean operators include:
  • equal
  • notEqual
  • lessThan
  • lessThanInclusive
  • greaterThan
  • greaterThanInclusive
  • in
  • notIn
  • contains
  • doesNotContain
922
kenoloA lightweight JSON-based business rules engine. The rules are expressed as JSON, and it also processes JSON. It supports also nested AND and OR clauses in rules. Boolean operators include:
  • eq
  • neq
  • weq (week equals)
  • nweq (not week equals) 
  • in
  • nin (not in)
  • gt (greater than)
  • lt (less than)
  • lte (less than equals)
  • sw (starts with an array of values)
  • nsw (does not start with an array of values)
  • ew (ends with an array of values)
  • new (not ends with an array of values)
  • inc (includes an array of values)
  • sl (sounds like an array of values)
  • re (regular expression in an array of values)
  • some (some are included in an array of values)
  • every (all are included in an array of values)
This library has not many stars and also no forks, but some very interesting operators
4
json-logic-jsAnother rule engine which allows saving of the rules and data as JSON objects. This rule engine can run on the browser and on the server too. The operators are also expressed as signs. like e.g: "==" or ">=". This library allows also adding new operators, so it is the most extensible. This is the list of operators:Logic and Boolean Operations
  • if
  • ==
  • ===
  • !-
  • !==
  • !
  • !!
  • or
  • and

Numeric Operations

  • >, >=, <, and <=
  • Between
  • max and min
  • Arithmetic, + - * /
  • %

Array Operations

  • map, reduce, and filter
  • all, none, and some
  • merge
  • in

String Operations

  • in
  • cat (concatenate)
  • substr (substring)
538
business-rules-engine"Business rules engine is a lightweight JavaScript library for easy business rules definition of the product, the contract, the form". This specific library seems to not have been updated for a while (5 years). It seems to be targeting mainly the UI. It supports JSON schema-based validation for rule definition, annotated JSON, and you can also define the rules using Javascript objects.86

Azure Functions

Azure Functions are "server-less" functions that run on the cloud. It is a Software as a Service offering by Azure. These functions can be implemented in different languages, including JavaScript, C#, Python and PHP. 

OK, we want to integrate Microsoft LogicApps with a rule engine, but how can this be achieved? We can run Javascript snippets in Microsoft LogicApps, but managing extra libraries seems to be impossible inside of these snippets.

A much better approach would be to call an Azure Function from Microsoft LogicApps. Azure Functions can properly run a node application and support proper dependency management of a node library like the ones of the rules engines:

The LogicApp prepares the data, sends the data to the Azure function, which processes this data with the rules engine. The Azure function gets then the output of the rules engine and sends the output back to the LogicApp. The LogicApp then processes the output of the Azure function and continues its execution.

Code Walk-through

During our evaluation, we have used the json-rules-engine engine. We have decided to use Typescript to import the classes of this engine, mainly because this engine is written using ES6. When we first started writing the code, we had problems with ES6 imports and export in the Azure function, so we just opted for Typescript, and all these problems disappeared.

These are the dependencies of our Azure function:

"dependencies": {

    "json-rules-engine": "^5.0.3"

  }

In our Azure function we then import the engine with:

import { Engine } from 'json-rules-engine'

After importing the engine, it gets initialized:

const engine = new Engine()

And we feed whatever rules we have into it. In the json-rules-engine you need always to specify a set of conditions and an event associated to it:

Then in the actual Azure function implementation, we read the JSON body and process it with the engine:

The code of the Azure Function we created to test the rules engine integration can be found here:

https://gist.github.com/onepointconsulting/9f64447eead7d564a5daeadfd889512f

Testing the Azure Function

Integration testing of this Azure function can be done via Postman. You just need a POST http request which sends the facts expected by the rules engine. Then you get the output of the rules engine.

Here is a screenshot of a simple test session:

Conclusion

If you want to include a simple rule engine in your Microsoft LogicApps workflow or if you're going to create a REST service which exposes a rules engine you can definitely use Azure Functions. With Azure Functions, you can use libraries of the rich npm eco-system and so you can extend the functionality of your Microsoft LogicApps conveniently.