Git Product home page Git Product logo

rule-evaluator's Introduction

rule-evaluator

This project provides a rule engine for evaluating dynamic rules. It can be used to uncover the specific conditions that drive actions in complex rule-based systems.

Features

Define rules using conditions like:

  • Get description of conditions that drove a particular action
  • Built in conditions like GreaterThan (>), LessThan (<), EqualTo (==), In (for slices, Nil checks
  • Combine conditions using AND and OR
  • Write custom functions for rules if you have more complex conditions
  • Specify actions to be executed when rules are met
  • All conditions and actions are just usual Go functions

Example Usage

Say you would like to label a user session as risky based on some conditions that you have stored in some configuration. And the conditions look like this:

IF:

  • user.money > 2000 AND user.employment = nil

OR

  • one of the user's friends is "John"

Then:

  • Mark the session category as risky

Here is how you would go about doing it (see complete code in examples/risk_rules.go):


import (
	"fmt"
	r "github.com/Bureau-Inc/rule-evaluator/rule-evaluator"
)


func AnalyseUserRisk() error {

	userData := UserData{
		Name:       "Paul",
		Age:        29,
		Money:      500000,
		Employment: nil,
		Friends:    []string{"Saul"},
	}

    // Step 1: Create a RuleEngine.
	re := r.RuleEngine{}

    // Step2: Define the above mentioned rules and the corresponding action
	highRiskRule := re.DefineRule(
        // conditions
		re.OR(
			re.AND(re.GreaterThan("Money", 20000), re.IsNil("Employment")),
			re.ListContainsStr("Friends", "John"),
		),

        // action function (UpdateRiskScore returns a function)
		UpdateRiskScore("highRiskCategory"),

        // action description
		fmt.Sprintf("risk level: highRiskCategory"),
	)

	// add as many rules as you want
	re.AddRules(highRiskRule)

    // Step 3: fire the rules by passing the input data and results initializer function
	results, err := re.FireRules(userData, InitializeSessionResultData)

	fmt.Printf("risk category: %s \n", results["riskScore"])


    // Step 4: Inspect the results
	fmt.Printf("%s", re.InspectLastSession())
	return nil

}

Output:

[AND: Money > 20000.00; Employment == nil
]
 Action-> risk level: highRiskCategory

Getting Started

Install the package: go get github.com/Bureau-Inc/rule-evaluator/rule-evaluator

Refer to the example for basic usage. Extend the engine with custom conditions and actions.

rule-evaluator's People

Contributors

sorumehta avatar nandeesh-id avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.