qfy

package
v0.0.0-...-8f88b0c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2017 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package qfy is a generic implementation of a semi-optimised qualification rule evaluation engine, using basic principles or the RETE algorithm.

The goal is to match a fact, usually attributes or key/value pairs against a list of pre-defined rules. Rules can be simple conditions or complex logical expressions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CRC64

type CRC64 struct {
	// contains filtered or unexported fields
}

CRC64 is used by conditions to calculate unique and consistent CRC64 sum

func NewCRC64

func NewCRC64(sign byte, capacity int) *CRC64

NewCRC64 initializes a new hash generator

func (*CRC64) Add

func (h *CRC64) Add(factors ...uint64)

Add adds factors to the CRC hash

func (*CRC64) Sum64

func (h *CRC64) Sum64() uint64

Sum64 calculates a numeric hash sum

type ConcurrentDict

type ConcurrentDict struct {
	// contains filtered or unexported fields
}

ConcurrentDict is just like Dict, but thread-safe and therefore slightly slower.

func NewConcurrentDict

func NewConcurrentDict() *ConcurrentDict

NewConcurrentDict creates a new dict

func (*ConcurrentDict) Add

func (d *ConcurrentDict) Add(v string) int64

Add adds a value to the dictionary if not already present and returns the associtated ID

func (*ConcurrentDict) AddSlice

func (d *ConcurrentDict) AddSlice(vv ...string) []int64

AddSlice adds a whole slice of values

func (*ConcurrentDict) Get

func (d *ConcurrentDict) Get(v string) int64

Get is a read-only operation, only returns known ID or 0

func (*ConcurrentDict) GetSlice

func (d *ConcurrentDict) GetSlice(vv ...string) []int64

GetSlice is a read-only operation, only returns known IDs

type Condition

type Condition interface {
	// CRC64 must return a globally unique an consistent rules identifier
	CRC64() uint64

	// Match tests if the rule is qualified. Certain conditions can only
	// support certain types and must return a negative response if an
	// unsupported type is given.
	Match(interface{}) bool

	// String returns a human-readable rule description
	String() string
}

Condition is an abstract logic evaluation condition

type Dict

type Dict map[string]int64

Dict is a simple helper to to turn convert strings into integers using dictionary encoding. Dict instances are NOT thread-safe because they are populated sequentially on Qualifier creation and then only read.

If your use case requires rules to call Add operations concurrently please use (the slower but) thread-safe ConcurrentDict

func NewDict

func NewDict() Dict

NewDict creates a new dict

func (Dict) Add

func (d Dict) Add(v string) int64

Add adds a value to the dictionary if not already present and returns the associtated ID

func (Dict) AddSlice

func (d Dict) AddSlice(vv ...string) []int64

AddSlice adds a whole slice of values

func (Dict) Get

func (d Dict) Get(v string) int64

Get is a read-only operation, only returns known ID or 0

func (Dict) GetSlice

func (d Dict) GetSlice(vv ...string) []int64

GetSlice is a read-only operation, only returns known IDs

type Equality

type Equality struct {
	// contains filtered or unexported fields
}

Equality conditions require the fact value to match input.

func EqualTo

func EqualTo(v interface{}) *Equality

EqualTo constructs an Equality condition Supports bool, string, intN, uintN and floatN fact values as inputs.

func (*Equality) CRC64

func (r *Equality) CRC64() uint64

CRC64 returns a unique ID

func (*Equality) Match

func (r *Equality) Match(v interface{}) bool

Match tests if the condition is qualified

func (*Equality) String

func (r *Equality) String() string

String returns a human-readable description

type Exclusion

type Exclusion struct {
	// contains filtered or unexported fields
}

Exclusion conditions require none of the values to be present in the fact

func NoneOf

func NoneOf(vals []int64) *Exclusion

NoneOf constructs an Exclusion

func (*Exclusion) CRC64

func (r *Exclusion) CRC64() uint64

CRC64 returns a unique ID

func (*Exclusion) Match

func (r *Exclusion) Match(v interface{}) bool

Match tests if the condition is qualified Supports only int64 and []int64 fact values as inputs.

func (*Exclusion) String

func (r *Exclusion) String() string

String returns a human-readable description

type Fact

type Fact interface {
	GetQualifiable(FactKey) interface{}
}

Fact is an interface of a fact that may be passed to a qualifier. Each fact must implement a GetQualifiable(FatKey) method which receives a key and must return either a bool, an int64 a float64 or an []int64 slice.

type FactKey

type FactKey uint16

FactKey is a unique identifier of any fact attribute

func (FactKey) MustBe

func (k FactKey) MustBe(cond Condition) Rule

MustBe is syntactic sugar for CheckFact.

Example:

	const (
		AttrA qfy.FactCheck = iota
		AttrB
	)
	ruleA := AttrA.MustBe(qfy.OneOf(1,2,3))
	ruleB := AttrB.MustBe(qfy.OneOf(6,5,4))
 ruleC := qfy.And(ruleA, ruleB)

func (FactKey) MustInclude

func (k FactKey) MustInclude(cond Condition) Rule

MustInclude is an alias for MustBe

func (FactKey) MustNotBe

func (k FactKey) MustNotBe(cond Condition) Rule

MustNotBe is the equivalent of MustBe(Not(...))

type Inclusion

type Inclusion struct {
	// contains filtered or unexported fields
}

Inclusion conditions require at least one value to be present in the fact Supports only int64 and []int64 fact values as inputs.

func OneOf

func OneOf(vals []int64) *Inclusion

OneOf constructs an Inclusion

func (*Inclusion) CRC64

func (r *Inclusion) CRC64() uint64

CRC64 returns a unique ID

func (*Inclusion) Match

func (r *Inclusion) Match(v interface{}) bool

Match tests if the condition is qualified

func (*Inclusion) String

func (r *Inclusion) String() string

String returns a human-readable description

type Ints64

type Ints64 []int64

Ints64 is a sorted and optimised []int64 slice

func SortInts64

func SortInts64(vv ...int64) Ints64

SortInts64 creates a new sorted Ints64

func (Ints64) Exists

func (p Ints64) Exists(v int64) bool

Exists checks the existence

func (Ints64) Inter

func (p Ints64) Inter(q Ints64) bool

Inter checks if intersectable

func (Ints64) Len

func (p Ints64) Len() int

func (Ints64) Less

func (p Ints64) Less(i, j int) bool

func (Ints64) Search

func (p Ints64) Search(x int64) int

Search searches for an item in the slice

func (Ints64) Swap

func (p Ints64) Swap(i, j int)

type Negation

type Negation struct {
	// contains filtered or unexported fields
}

Negation inverts a Condition

func Not

func Not(cond Condition) *Negation

Not creates a negation

func (*Negation) CRC64

func (r *Negation) CRC64() uint64

CRC64 returns a combined unique ID

func (*Negation) Match

func (r *Negation) Match(v interface{}) bool

Match tests if the condition is qualified

func (*Negation) String

func (r *Negation) String() string

String returns a human-readable description

type NumericGreater

type NumericGreater struct {
	// contains filtered or unexported fields
}

NumericGreater conditions require the fact value to match input.

func GreaterThan

func GreaterThan(v float64) *NumericGreater

GreaterThan constructs a NumericGreater condition Supports intN and floatN fact values as inputs.

func (*NumericGreater) CRC64

func (r *NumericGreater) CRC64() uint64

CRC64 returns a unique ID

func (*NumericGreater) Match

func (r *NumericGreater) Match(v interface{}) bool

Match tests if the condition is qualified

func (*NumericGreater) String

func (r *NumericGreater) String() string

String returns a human-readable description

type NumericGreaterOrEqual

type NumericGreaterOrEqual struct {
	// contains filtered or unexported fields
}

NumericGreaterOrEqual conditions require the fact value to match input.

func GreaterOrEqual

func GreaterOrEqual(v float64) *NumericGreaterOrEqual

GreaterOrEqual constructs a NumericGreaterOrEqual condition Supports intN and floatN fact values as inputs.

func (*NumericGreaterOrEqual) CRC64

func (r *NumericGreaterOrEqual) CRC64() uint64

CRC64 returns a unique ID

func (*NumericGreaterOrEqual) Match

func (r *NumericGreaterOrEqual) Match(v interface{}) bool

Match tests if the condition is qualified

func (*NumericGreaterOrEqual) String

func (r *NumericGreaterOrEqual) String() string

String returns a human-readable description

type NumericLess

type NumericLess struct {
	// contains filtered or unexported fields
}

NumericLess conditions require the fact value to match input.

func LessThan

func LessThan(v float64) *NumericLess

LessThan constructs a NumericLess condition Supports intN and floatN fact values as inputs.

func (*NumericLess) CRC64

func (r *NumericLess) CRC64() uint64

CRC64 returns a unique ID

func (*NumericLess) Match

func (r *NumericLess) Match(v interface{}) bool

Match tests if the condition is qualified

func (*NumericLess) String

func (r *NumericLess) String() string

String returns a human-readable description

type NumericLessOrEqual

type NumericLessOrEqual struct {
	// contains filtered or unexported fields
}

NumericLessOrEqual conditions require the fact value to match input.

func LessOrEqual

func LessOrEqual(v float64) *NumericLessOrEqual

LessOrEqual constructs a NumericLessOrEqual condition Supports intN and floatN fact values as inputs.

func (*NumericLessOrEqual) CRC64

func (r *NumericLessOrEqual) CRC64() uint64

CRC64 returns a unique ID

func (*NumericLessOrEqual) Match

func (r *NumericLessOrEqual) Match(v interface{}) bool

Match tests if the condition is qualified

func (*NumericLessOrEqual) String

func (r *NumericLessOrEqual) String() string

String returns a human-readable description

type NumericRange

type NumericRange struct {
	// contains filtered or unexported fields
}

NumericRange conditions require the fact value to match input.

func Between

func Between(min, max float64) *NumericRange

Between constructs a NumericRange condition Supports intN and floatN fact values as inputs.

func (*NumericRange) CRC64

func (r *NumericRange) CRC64() uint64

CRC64 returns a unique ID

func (*NumericRange) Match

func (r *NumericRange) Match(v interface{}) bool

Match tests if the condition is qualified

func (*NumericRange) String

func (r *NumericRange) String() string

String returns a human-readable description

type Qualifier

type Qualifier struct {
	// contains filtered or unexported fields
}

Qualifier represents a rule engine that can match a fact against a list of rule-sets using a fixed set of attributes

func New

func New() *Qualifier

New creates a new qualifier with a list of known/qualifiable attributes

func (*Qualifier) Resolve

func (q *Qualifier) Resolve(rule Rule, id int64)

Resolve registers a rule with a numeric id resolved by that rule

func (*Qualifier) Select

func (q *Qualifier) Select(fact Fact) []int64

Select performs the qualification and matches all known rules against a given fact returning a list of associated identifiers

type Rule

type Rule interface {
	// String returns a human-readable description
	String() string
	// contains filtered or unexported methods
}

Rule is an abstract logic evaluation

func All

func All(rules ...Rule) Rule

All requires all of the rules to match

func Any

func Any(rules ...Rule) Rule

Any requires any of the rules to match

func CheckFact

func CheckFact(key FactKey, cond Condition) Rule

CheckFact constructs a new rule, it accepts a fact key (used to query the fact) and an evaluation condition

type State

type State struct {
	// contains filtered or unexported fields
}

State holds the state of the qualification process

func NewState

func NewState() *State

NewState initiali

func (*State) Reset

func (m *State) Reset()

Reset resets the stateory state

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL