Giter Site home page Giter Site logo

quickshore / rulestone Goto Github PK

View Code? Open in Web Editor NEW

This project forked from atlasgurus/rulestone

0.0 0.0 0.0 182 KB

Wickedly fast and scalable: Rulestone is a rule engine library designed for modern demands, handling thousands of rules effortlessly

License: MIT License

Go 100.00%

rulestone's Introduction

Rulestone

Go Tests Go Report Card License: MIT

Lightweight and fast rule engine written in Go, with API for other languages:

With Rulestone you can define thousands of rules and then process tens of thousands events/objects per second getting the matching rules for each object.

Installation

Install the package:

go get github.com/atlasgurus/rulestone

Usage

Go

The following Go example shows how to load a rule from file and match an object against this rule:

    package main

    import (
        "fmt"
        "github.com/atlasgurus/rulestone/utils"
        "github.com/atlasgurus/rulestone/engine"
    )

    func Match() {
        repo := engine.NewRuleEngineRepo()
        _, err := repo.RegisterRuleFromFile("rule.json")
        if err != nil {
            return
        }

        ruleEngine, err := engine.NewRuleEngine(repo)
        if err != nil {
            return
        }

        event, err := utils.ReadEvent("object.json")
        if err != nil {
            return
        }

        matches := ruleEngine.MatchEvent(event)

        for _, ruleId := range matches {
			// Optionally get matching rules metadata
            ruleDefinition := ruleEngine.GetRuleDefinition(ruleId)
            if ruleIdStr, ok := ruleDefinition.Metadata["rule_id"].(string); ok {
                fmt.Println("Rule matched: ", ruleIdStr)
            }
        }
        // Report all the errors if any
        if repo.GetAppCtx().NumErrors() > 0 {
            repo.GetAppCtx().PrintErrors()
        }
    }

    func main() {
        Match()
    }

The example assumes rule contains the metadata field called rule_id. See for more Go usage examples in tests/rule_api_test.go.

Rules

Simple rule definition

metadata:
  created: "2023-03-29"
  priority: 10
  rule_id: "BUSINESS_RULE_1"
expression: name == "Frank" && age == 20

The rule match if the JSON object has a name field with value Frank and an age field with value 20. The metadata section may store any information linked to the rule, including the rule ID. The condition section contains the expression that will be evaluated against the JSON object.

See examples/rules for more rules examples.

Operators and functions

Rulestone expressions supports:

  • Comparison and negation operators like ==, >, >=, <, <=

  • Arithmetic operations: +, -, *, /

  • Logical operators: &&, ||, !

  • Parentheses: (, )

  • String literals: "string"

  • Numeric literals: 1, 2.3

  • Field access: field1, field1.field2

  • Functions: hasValue, isEqualToAny, regexpMatch, date

  • Date literals: date("11/29/1968")

  • Date comparison operators: <, <=, >, >=, ==

  • Date arithmetic operations: +, -

  • hasValue - check that object has specified field, for example hasValue(field1)

  • isEqualToAny - check that object field is equal to any specified value, for example isEqualToAny(field1, 1, 2, 3, '4')

  • regexpMatch - match the Go regexp, for example regexpMatch("^\\d{4}/\\d{2}/\\d{2}$", child.dob)

Dates

Rulestone handles dates and comparison operators on them, but since JSON doesn't provide field type information, need to use date() function. The function can parse date string in different formats:

metadata:
  created: "2023-03-29"
  priority: 10
expression: 'name == "Frank" && date(dob) < date(child.dob) && date("11/29/1968") > date(dob) && date(dob) == date("11/28/1968")'

Contributing

We love contributions! If you have any suggestions, bug reports, or feature requests, please open an issue in our tracker.

License

This project is licensed under the MIT License - see the LICENSE file for details.

rulestone's People

Contributors

atlasgurus avatar quickshore 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.