GithubHelp home page GithubHelp logo

easyrulesgo's Introduction

EasyRulesGO

A port of EasyRules to Golang https://github.com/j-easy/easy-rules

Please read the EasyRules link for a quick intro to the concepts and workflows.

Build Status

  • Implements default rules engine
  • Allows for composite rule (all or nothing)

Authors/Contributors

package easyrulesgo_test

import (
	"fmt"
	core "github.com/CrowdStrike/easyrulesgo/core"
)

// example taken from https://github.com/j-easy/easy-rules/wiki/shop

type person struct {
	name string
	age int
	adult bool
}


/*-------- AGE RULE --------*/
type ageRule struct {
	core.BasicRule
	person *person
}

func (a *ageRule) Evaluate() bool {
	return a.person.age > a.adultAge()
}

func (a *ageRule) Execute() error {
	a.person.adult = true
	fmt.Printf("person %s has been marked as adult", a.person.name)
	return nil
}

func (a *ageRule) adultAge() int {
	return 18
}


func newAgeRule(p *person) *ageRule {
	ar := &ageRule{}
	ar.person = p
	return ar
}


/*-------- ALCOHOL RULE --------*/

type alcoholRule struct {
	core.BasicRule
	person *person
}

func (a *alcoholRule) Evaluate() bool {
	return !a.person.adult
}

func (a *alcoholRule) Execute() error {
	fmt.Printf("Shop: Sorry %s you are not allowed to buy alcohol", a.person.name)
	return nil
}

func newAlcoholRule(p *person) *alcoholRule {
	ar := &alcoholRule{}
	ar.person = p
	return ar
}


func Example() {

	p := &person{name: "Tom", age: 14}
	ar := newAgeRule(p)
	alcr := newAlcoholRule(p)

	re := core.NewDefaultRulesEngine()
	re.AddRule(ar)
	re.AddRule(alcr)

	err := re.FireRules()
	if err != nil {
		fmt.Println("rules failed!")
	}
}

Future ToDos

  • continuous firing with tickers
  • additional rule patterns and DAGs

easyrulesgo's People

Contributors

jiminoc avatar fmbenhassine avatar shawndwells avatar isimluk avatar

Stargazers

Classic Values 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.