Business Rules

Smart contracts are immutable programs on the blockchain. They are automatically executed based on pre-defined rules. However, complex and inferencing rules could be extremely difficult to program, test, and validate, using standard programming languages like Solidity. The long sequence of highly nested IF / THEN statements is fragile and error-prone. The problem is compounded by the need to frequently change rules based on business requirements.

Past enterprise software vendors have long relied on Business Rules Engines (BREs) to solve this problem. BREs allow business analysts to write transactional (state changing) programs in a specialized programming language called formal rules language, which can be generated by graphical user interfaces. BREs automatically evaluate, re-evaluate, and execute those rules. They have been proven successful in traditional financial services.

The Lity language supports a Business Rules Engine, similar to Drools, inside smart contracts. It not only improves the security and reliability of smart contracts, but also allows business rules to be executed with certainty and a high level of trust brought by blockchains.

An example rule-based contract is as follows. You can try it on our Europa online IDE. It allows a budget and personal profiles to be added to the contract. When the pay function is called, the rules engine figures out whom to pay pension to and whether there is sufficient budget. It executes the pension payment when possible.

pragma lity ^1.2.6;

contract AgePension {
    struct Person {
        int age;
        bool eligible;
        address addr;
    }

    struct Budget {
        int amount;
    }

    mapping (address => uint256) addr2idx;
    Person[] ps;
    Budget budget;

    constructor () public {
        factInsert budget;
        budget.amount = 100;
    }

    function addPerson(int age) public {
        ps.push(Person(age, true, msg.sender));
        addr2idx[msg.sender] = factInsert ps[ps.length-1];
    }

    function deletePerson() public {
        factDelete addr2idx[msg.sender];
    }

    function pay() public {
        fireAllRules;
    }

    function () public payable { }

    rule "payPension" when {
        p: Person(age >= 65, eligible == true);
        b: Budget(amount >= 10);
    } then {
        p.addr.transfer(10);
        p.eligible = false;
        b.amount -= 10;
    }
}

You can learn more about the Lity rules engine on our documentation site.