Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bsm/qualify

Match a fact against large number of pre-defined rules in Go
https://github.com/bsm/qualify

adtech golang matching qualifier rule-engine rules

Last synced: 2 months ago
JSON representation

Match a fact against large number of pre-defined rules in Go

Awesome Lists containing this project

README

        

# Qualify!

[![Build Status](https://travis-ci.org/bsm/qualify.png?branch=master)](https://travis-ci.org/bsm/qualify)
[![GoDoc](https://godoc.org/github.com/bsm/qualify?status.png)](http://godoc.org/github.com/bsm/qualify)
[![Go Report Card](https://goreportcard.com/badge/github.com/bsm/qualify)](https://goreportcard.com/report/github.com/bsm/qualify)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

Library for fast rules evaluation for Go. Qualify is able to quickly match a fact against large number of pre-defined rules.

### Example:

```go
import (
"fmt"

"github.com/bsm/qualify"
)

// Fact is an example fact
type Fact struct {
Country string
Browser string
OS string
Attrs []int
}

// Enumeration of our fact features
const (
FieldCountry qualify.Field = iota
FieldBrowser
FieldOS
FieldAttrs
)

// factReader is a wrapper around facts to
// make them comply with qualify.Fact
type factReader struct {
Dict qualify.StrDict
Fact
}

func (r *factReader) AppendFieldValues(x []int, f qualify.Field) []int {
switch f {
case FieldCountry:
return r.Dict.Lookup(x, r.Country)
case FieldBrowser:
return r.Dict.Lookup(x, r.Browser)
case FieldOS:
return r.Dict.Lookup(x, r.OS)
case FieldAttrs:
return append(x, r.Attrs...)
}
return x
}

func main() package qualify_test

import (
"fmt"

"github.com/bsm/qualify"
)

func ExampleQualifier() {
// We can use dictionary encoding to translate
// string values into numerics
dict := qualify.NewStrDict()

// Init a new builder popute with rules
// for each outcome.
builder := qualify.NewBuilder()

// Outcome #34 requires:
// * Country to be GBR or FRA, and
// * Attrs to contain 101 or 102, and
// * Attrs to contain 202 or 203
builder.Require(34, FieldCountry,
qualify.OneOf(dict.Add("GBR"), dict.Add("FRA")))
builder.Require(34, FieldAttrs,
qualify.OneOf(101, 102))
builder.Require(34, FieldAttrs,
qualify.OneOf(202, 203))

// Outcome #35 requires:
// * Country NOT to be NLD nor GER, and
// * Browser NOT to be Safari, and
// * OS to be either Android or iOS
builder.Require(35, FieldCountry,
qualify.NoneOf(dict.Add("NLD"), dict.Add("GER")))
builder.Require(35, FieldBrowser,
qualify.NoneOf(dict.Add("Safari")))
builder.Require(35, FieldOS,
qualify.OneOf(dict.Add("Android"), dict.Add("iOS")))

// Setup the qualifier
qfy := builder.Compile()

// Init result set
var res []int

// Matches outcome #34
res = qfy.Qualify(res[:0], &factReader{Dict: dict, Fact: Fact{Country: "GBR", Attrs: []int{101, 202}}})
fmt.Println(res)

// Matches outcome #35
res = qfy.Qualify(res[:0], &factReader{Dict: dict, Fact: Fact{Country: "IRE", OS: "iOS"}})
fmt.Println(res)

// Matches nothing
res = qfy.Qualify(res[:0], &factReader{Dict: dict, Fact: Fact{Country: "NLD"}})
fmt.Println(res)

}
```