npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pyramid-rules-engine

v0.1.0

Published

JSON-based Business Rules Engine

Downloads

8

Readme

pyramid

JSON-based Business Rules Engine

Installing

npm install pyramid-rules-engine

Modules

pyramid contains serveral modules - loader, validator, stores, loggers, operators, rules.

Loader

Loader is responsible for loading data files such as data objects and input json file. If data is not found, the rule engine would stop immediately and throw related errors.

Validator

Validator is responsible for validating the input source loaded by Loader. The scope includes rule properties, rule id and rule conditions.

Rule Properties

Properties such as id, name, when are mandatory.

Rule ID

Rule ID must be unqiue.

Rule Conditions

Rule conditions are objects defined in when. Each object can contain other nested objects.

$AND and $OR

If you want to define a condition with a logical operators such as AND and OR. You have to use an array with the attribute $and and or. Each array could only contain two objects. Nested objects are also supported.

Condition

A condition is an an object with three attributes - fact, operator and value

fact: The target attribute which is being used to evalute. It has to be started with a data object, such as $$bankruptcy.amountOwed. Another example for a data object is inside another object would be $$applicant.$$applicantAddress.zipcode.

``operator`: Please refer to Operators below.

value: The value is used to compare with the one in input file.

A condition will be evaluted and return a boolean indicating if the condition is met with the defined criteria.

Stores

Stores is responsible for centralizing stores such as DATA_OBJECTS_STORE, RULES_STORE, INPUT_FILE_STORE and RESULT_STORE

Loggers

Loggers is responsible for logging. Two loggers are available.

Normal Logger:

logger('Normal Logger');

Error Logger:

errorLogger('Normal Logger');

Operators

Operators is responsible for performing mathematical operations. Six operators are available which are ==, >=, <=, >, < and !=.

Rules

Rules is responsible for initializing the rule engine, process and evalute the defined rules based on the input file and return the result.

Configuration

  • Create a file named pyramid.cfg
  • Define the paths for DATA_OBJ_DIR, RULES_DIR and INPUT_FILE
  • Example:
{
    "DATA_OBJ_DIR": "../example/loan-application/data_objects/",
    "RULES_DIR": "../example/loan-application/rules/",
    "INPUT_FILE": "../example/loan-application/input.json"
}

Define data object

  • Create a file with an extension .data under DATA_OBJ_DIR
  • A data object has to be started with $$
  • Example:
{
    "$$applicant": {
        "type": "object",
        "value": {
            "age": "number",
            "applicantAddress": "$$applicantAddress"
        }
    }
}

Define rule

  • Create a file with an extension .rule under RULES_DIR
  • Example: Sample Rule
{
    "id": 1,
    "name": "checkApplicantAge",
    "description": "Check applicant age ",
    "when": [
        {
            "fact": "$$applicant.age",
            "operator": ">=",
            "value": 25
        }
    ]
}
  • Example 2: Complex Rule
{
    "id": 0,
    "name": "Loan Application",
    "description": "Check if loan application is approved or rejected ",
    "when": [
        {
            "$and": [
                {
                    "fact": "$$applicant.age",
                    "operator": "<=",
                    "value": 25
                },
                {
                    "$or": [
                         {
                            "fact": "$$bankruptcy.amountOwed",
                            "operator": "==",
                            "value": 100000
                        },
                        {
                            "fact": "$$incomeSource.amount",
                            "operator": ">=",
                            "value": 30000
                        }
                    ]
                }
            ]
        },
        {
            "fact": "$$applicant.$$applicantAddress.zipcode",
            "operator": "==",
            "value": "5223"
        },
        {
            "fact": "$$coApplicant.name",
            "operator": "==",
            "value": "Milena Sears"
        }
    ]
}

Define input file

  • Create a file with an extension .json as INPUT_FILE
  • Example:
{
    "applicant": {
        "name": "Zayna Wainwright",
        "age": 24,
        "occupation": "N/A",
        "applicantAddress": {
            "address": "2752  Star Route",
            "zipcode": "60634"
        }
    }, 
    "bankruptcy": {
        "amountOwed": 3000
    },
    "incomeSource": {
        "amount": 35000
    },
    "coApplicant": [
        {
            "name": "Milena Sears",
            "applicantReferralCode": "CA1234"
        },
        {
            "name": "Clement Simpson",
            "applicantReferralCode": "CA2345"
        }
    ]
}

Run

node pyramid.js <PYRAMID_CFG_PATH>

Example

The example can be found in folder example

Example Result

------------------------------------------------------------------------
[INFO] Load Initialization
------------------------------------------------------------------------
[INFO] Loading Configuration file
[INFO] Loading Data Objects: ../example/loan-application/data_objects/
[INFO] Loading data object ../example/loan-application/data_objects/applicant.data
[INFO] Loading data object ../example/loan-application/data_objects/applicantAddress.data
[INFO] Loading data object ../example/loan-application/data_objects/bankruptcy.data
[INFO] Loading data object ../example/loan-application/data_objects/coApplicant.data
[INFO] Loading data object ../example/loan-application/data_objects/coApplicantObject.data
[INFO] Loading data object ../example/loan-application/data_objects/incomeSource.data
[INFO] Loading Rules ../example/loan-application/rules/
[INFO] Loading rule file ../example/loan-application/rules/checkApplicantAge.rule
[INFO] Loading rule file ../example/loan-application/rules/loanApplication.rule
[INFO] Loading Input File ../example/loan-application/input.json
------------------------------------------------------------------------
[INFO] Validator Initialization
------------------------------------------------------------------------
[INFO] Validation Finished. No errors found
------------------------------------------------------------------------
[INFO] Rule Engine Initialization
------------------------------------------------------------------------
[INFO] Processing Rule 1 - Check Applicant Age
[INFO] Processing Rule 0 - Loan Application
------------------------------------------------------------------------
[INFO] Rule Engine Result Summary
------------------------------------------------------------------------
[INFO] Rule 1 (Check Applicant Age) : SUCCESS
[INFO] Rule 0 (Loan Application) : FAILURE