Skip to content

rules engine for go and partially compatible with K-Phoen/rulerz which is written in PHP

License

Notifications You must be signed in to change notification settings

ibllex/go-ruler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-Ruler

Go-Ruler is an rules engine for go and partially compatible with K-Phoen/rulerz which is written in PHP

Introduction

Business rules can be written as text using a dedicated language, very close to SQL, in which case we refer to them as rules or they can be encapsulated in single classes and referred to as specifications.

Once a rule (or a specification) is written, it can be used to check if a single candidate satisfies it or directly to query a datasource.

Quick usage

1. Install

go get -u github.com/ibllex/go-ruler

2. Write a rule

The rule hereafter describes a "high ranked female player" (basically, a female player having more than 9000 points).

highRankFemalesRule := "gender = 'F' and points > 9000"

3. Define a datasource

We have the following datasources:

import "github.com/ibllex/go-ruler"

players := []ruler.T{
	ruler.T{"pseudo": "Joe", "gender": "M", "points": 40},
	ruler.T{"pseudo": "Moe", "gender": "M", "points": 20},
	ruler.T{"pseudo": "Alice", "gender": "F", "points": 60},
	ruler.T{"pseudo": "Birdie", "gender": "F", "points": 60},
}

4. Create ruler

import (
	"github.com/ibllex/go-ruler"
)

r := ruler.New(ruler.O{})

5. Use a rule to query a datasource

retrieving the results is as simple as calling the Filter method:

import (
	"github.com/ibllex/go-ruler"
)

highRankFemales, err := r.Filter(players, highRankFemalesRule, ruler.P{}, ruler.PP{})

Given a candidate, checking if it satisfies a rule boils down to calling the Satisfies method:

import (
	"github.com/ibllex/go-ruler"
)

isHighRankFemale, err := r.Satisfies(players[0], highRankFemalesRule, ruler.P{}, ruler.PP{})

License

This library is under the MIT license.

About

rules engine for go and partially compatible with K-Phoen/rulerz which is written in PHP

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages