Business Rules Engines and Declarative Programming

Business Rules Engines and Declarative Programming

Reports, reports everywhere! Organizations have demand for flexible reporting capabilities more than ever. Standard data warehousing techniques solve data aggregation and summary type of reports. But there is another type of reports that can’t be generated this way. Consider a report that consists of N input KPI variables. Based on these N (50 for example) input variables, the algorithm shall produce M output variables (500 for example). The variables shall be calculated gradually using intermediate results.

The input consists of N = 4 variables; the output consists of M = 8 variables. Someone can create the following procedural logic for sure:

E = A + B
F = C + D
G = E + F
J = G + E

But everyone that creates reports knows that calculation rules change. Very often! When variable inter-dependencies change, the sequential algorithm must change its shape and sequence of operations. Not very convenient!

Business Rules Engines and declarative programming solve the problem in a very elegant way. The calculation rules writer shall not consider the sequence of the rules defined. The inter-dependencies will be resolved automatically (there are cases where salience and rule execution order must be controlled, but in most cases that is not needed).

Drools is the facto standard as open source library in the Java world:

https://www.drools.org/

Drools Expert has efficient Rete network implementation that allows fast rule evaluation and execution logic (Alpha and Beta memory nodes, but that is a different story).

There is a similar Business Rules Engine in the Node.js world. Guess the name :-)? This Node.js library is called Nools:

 https://github.com/C2FO/nools

The implementation of this library is also Rete Network based.

So, equipped with Nools, we can solve the above problem as:

calculations.nools

define Variable {
    name: '',
    value: '',
    constructor : function(name, value){
        this.name = name;
        this.value = value;
    }
}
 
 
rule 'Calculate J' {
    when {
        a : Variable a.name == 'A';
        g : Variable g.name == 'G';
    }
    then {
        var j = new Variable('J', g.value +'#'+ a.value);
        assert(j);
    }
}
 
rule 'Calculate E' {
    when {
        a : Variable a.name == 'A';
        b : Variable b.name == 'B';
    }
    then {
        var e = new Variable('E', a.value + '#' + b.value);
        assert(e);
    }
}
 
rule 'Calculate F' {
    when {
        c : Variable c.name == 'C';
        d : Variable d.name == 'D';
    }
    then {
        var f = new Variable('F', c.value + '#' + d.value);
        assert(f);
    }
}
 
rule 'Calculate G' {
    when {
        e : Variable e.name == 'E';
        f : Variable f.name == 'F';
    }
    then {
        var g = new Variable('G',  e.value + '#' + f.value);
        assert(g);
    }
}

The JavaScript that invokes the rules above is:

calculations.js

'use strict';
 
const nools = require('nools');
 
const flow = nools.compile("./calculations.nools");
 
const Variable = flow.getDefined("Variable");
 
const session = flow.getSession();
 
const A = new Variable('A', 'A');
const B = new Variable('B', 'B');
const C = new Variable('C', 'C');
const D = new Variable('D', 'D');
 
session.assert(A);
session.assert(B);
session.assert(C);
session.assert(D);
 
session.match().then(
    function() {
        session.getFacts().forEach((fact) => {
            console.log("%s: %s", fact.name, fact.value);
        });
        session.dispose();
    },
    function(err) {
        session.dispose();
        console.error(err.stack);
    });

If we execute the script above, the output result is:

node calculations.js

A: A
B: B
C: C
D: D
F: C#D
E: A#B
G: A#B#C#D
J: A#B#C#D#A

Fast and efficient!

To view or add a comment, sign in

More articles by Marjan Sterjev

  • Decoding SQL Injection Exfiltrated Data from Network Traffic Dump

    Sequel Dump task was hard level task included into the latest Hackfinity Battle Room. Players had network traffic dump…

  • Secure Multi-Party Computation: Idea and Example

    We are seeing massive AI adoption these days. At least everyone is talking about it.

    1 Comment
  • Theseus TryHackMe Room Writeup

    Theseus is an old room, almost 5 years old. Its difficulty has been specified as Insane.

  • Breaking weak Captcha: Tesseract OCR

    Capture Returns TryHackMe room has been recently added. Sometimes CTF scenarios could be artificial.

  • Behaviour Analysis: Outlier Sequence Detection

    Each production system deals with a lot of signals and sequence of events. Some of the sequences are unusual and…

    6 Comments
  • RSA Optimizations: Think Twice

    I will start here straight ahead with the summary: Resist your temptations and do not implement your customised and…

    2 Comments
  • RSA & Chinese Remainder Theorem MEGA Exploits

    There is a lot of data we are dealing with today. Most of us have multiple digital devices.

    4 Comments
  • A word or two about the parallelisation

    This article will be a short one. It talks about parallelisation and efficiency, so it shall waste as less as possible…

  • Covert Tcp - Scapy Version

    Covert Tcp is one of the tools for covert communication mentioned in the white hacking courses. Instead of sending…

  • Network Traffic Anomaly Detection

    It is well known today that anomaly detection helps us spot interesting data, events, malicious behaviour, potential…

Others also viewed

Explore content categories