arishem

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OpLogicAnd = "&&"
	OpLogicOr  = "||"
	OpLogicNot = "!"
)
View Source
const (
	ExprTypeCondition = iota
	ExprTypeAim
)

Variables

This section is empty.

Functions

func ApplyExecuteOptions added in v1.0.5

func ApplyExecuteOptions(rv typedef.RuleVisitor, dc typedef.DataCtx, opts ...ExecuteOption)

func ApplyOptions added in v1.0.5

func ApplyOptions(c *Configuration, opts ...Option)

func DataContext

func DataContext(ctx context.Context, json string) (dc typedef.DataCtx, err error)

DataContext will create a new DataContext by fact meta json.

func DecodeFeatureHash added in v1.0.4

func DecodeFeatureHash(featureHash string) (featureName string, builtinParamHash string)

func FeatureFetcherFactory

func FeatureFetcherFactory() typedef.FeatureFetcher

FeatureFetcherFactory provides the ability to create a feature fetcher.

func GenBuiltinParamHash added in v1.0.4

func GenBuiltinParamHash(builtinParam typedef.MetaType) string

func Initialize

func Initialize(cfg *Configuration, opts ...Option)

Initialize arishem, this func should be called only once in your init function.

func JudgeCondition

func JudgeCondition(ctx context.Context, condition string, opts ...ExecuteOption) (pass bool, err error)

JudgeCondition will judge the condition string, pass will be set true if condition passed.

func JudgeConditionWithFactMeta

func JudgeConditionWithFactMeta(ctx context.Context, condition, factMeta string, opts ...ExecuteOption) (pass bool, err error)

JudgeConditionWithFactMeta will judge the condition string with fact meta, pass will be set true if condition passed.

func NewDefaultFeatureParamWithBuiltinParam added in v1.0.4

func NewDefaultFeatureParamWithBuiltinParam(featureName string, builtinParam typedef.MetaType) typedef.FeatureParam

func NewDefaultFeatureParamWithNoBuiltinParam added in v1.0.4

func NewDefaultFeatureParamWithNoBuiltinParam(featureName string) typedef.FeatureParam

func WalkAim added in v1.0.10

func WalkAim(aimExpr string, dc typedef.DataCtx, opts ...ExecuteOption) (aim typedef.Aim, err error)

WalkAim will try to parse the aimExpr into the antlr parse tree and visit it to get the result, err will not be null when parse aim expression failed.

func WalkAimTree added in v1.0.10

func WalkAimTree(aimTree antlr.ParseTree, dc typedef.DataCtx, opts ...ExecuteOption) (aim typedef.Aim)

WalkAimTree will visit the aimTree to get the result

Types

type ActionAim

type ActionAim struct {
	ActionName string           `json:"ActionName"`
	ParamMap   map[string]*Expr `json:"ParamMap,omitempty"`
	ParamList  []*Expr          `json:"ParamList,omitempty"`
	// contains filtered or unexported fields
}

func NewParamListAction

func NewParamListAction(name string, param []*Expr) *ActionAim

NewParamListAction create a new action aim with param list.

func NewParamMapAction

func NewParamMapAction(name string, param map[string]*Expr) *ActionAim

NewParamMapAction create a new action aim with param map.

func (*ActionAim) AddKeyParam

func (a *ActionAim) AddKeyParam(key string, val *Expr)

func (*ActionAim) AddParam

func (a *ActionAim) AddParam(val *Expr)

func (*ActionAim) Build

func (a *ActionAim) Build() (string, error)

type Builder

type Builder interface {
	Build() (string, error)
}

type Condition

type Condition struct {
	Operator string `json:"Operator"`
	Lhs      *Expr  `json:"Lhs,omitempty"`
	Rhs      *Expr  `json:"Rhs,omitempty"`
}

func NewCondition

func NewCondition(operator string, not ...Not) *Condition

type ConditionGroupBuilder

type ConditionGroupBuilder struct {
	OpLogic         string                   `json:"OpLogic"`
	ConditionGroups []*ConditionGroupBuilder `json:"ConditionGroups,omitempty"`
	Conditions      []*Condition             `json:"Conditions,omitempty"`
	// contains filtered or unexported fields
}

func NewCondGroupsCondGroup

func NewCondGroupsCondGroup(opLogic string) *ConditionGroupBuilder

NewCondGroupsCondGroup create a new condition group with condition groups.

func NewConditionsCondGroup

func NewConditionsCondGroup(opLogic string) *ConditionGroupBuilder

NewConditionsCondGroup create a new condition group with conditions.

func (*ConditionGroupBuilder) AddConditionGroups

func (c *ConditionGroupBuilder) AddConditionGroups(condGroups ...*ConditionGroupBuilder)

func (*ConditionGroupBuilder) AddConditions

func (c *ConditionGroupBuilder) AddConditions(cond ...*Condition)

func (*ConditionGroupBuilder) Build

func (c *ConditionGroupBuilder) Build() (expr string, err error)

type Configuration added in v1.0.5

type Configuration struct {
	Granularity func(int, ExecuteMode) int
	// prefetch feature
	Prefetch bool
	TCache   TreeCache
	// max parallel computation
	MaxParallel        int
	RuleComputePool    typedef.ConcurrentPool
	FeatureFetchPool   typedef.ConcurrentPool
	FeatFetcherFactory func() typedef.FeatureFetcher
	FeatVisitCache     typedef.SharedVisitCache
}

func DefaultConfiguration

func DefaultConfiguration() *Configuration

type Const

type Const struct {
	BoolConst *bool    `json:"BoolConst,omitempty"`
	NumConst  *float64 `json:"NumConst,omitempty"`
	StrConst  *string  `json:"StrConst,omitempty"`
}

func NewBoolConst

func NewBoolConst(b bool) *Const

func NewNumConst

func NewNumConst(f float64) *Const

func NewStrConst

func NewStrConst(s string) *Const

type ExecuteMode added in v1.0.5

type ExecuteMode int32
const (
	ExecuteModePriority ExecuteMode = iota
	ExecuteModeNoPriority
)

type ExecuteOption added in v1.0.5

type ExecuteOption func(rv typedef.RuleVisitor, dc typedef.DataCtx)

func WithFeatureFetchObserver

func WithFeatureFetchObserver(obs ...typedef.FeatureFetchObserver) ExecuteOption

WithFeatureFetchObserver can add feature fetch observers to feature fetcher.

func WithVisitObserver

func WithVisitObserver(obs ...typedef.VisitObserver) ExecuteOption

WithVisitObserver can add visit observers to rule visitor.

type Expr

type Expr struct {
	ListExpr    []*Expr          `json:"ListExpr,omitempty"`
	MapExpr     map[string]*Expr `json:"MapExpr,omitempty"`
	Const       *Const           `json:"Const,omitempty"`
	ConstList   []*Const         `json:"ConstList,omitempty"`
	MathExpr    *MathExpr        `json:"MathExpr,omitempty"`
	FuncExpr    *FuncExpr        `json:"FuncExpr,omitempty"`
	VarExpr     *VarExpr         `json:"VarExpr,omitempty"`
	FeatureExpr *FeatureExpr     `json:"FeatureExpr,omitempty"`
}

func NewConstExpr

func NewConstExpr(c *Const) *Expr

func NewConstListExpr

func NewConstListExpr(cs []*Const) *Expr

func NewFeatureExpr

func NewFeatureExpr(ft *FeatureExpr) *Expr

func NewFuncExpr

func NewFuncExpr(f *FuncExpr) *Expr

func NewListExpr

func NewListExpr(list []*Expr) *Expr

func NewMapExpr

func NewMapExpr(m map[string]*Expr) *Expr

func NewMathExpr

func NewMathExpr(mt *MathExpr) *Expr

func NewVarExpr

func NewVarExpr(v *VarExpr) *Expr

type ExprType added in v1.0.10

type ExprType int

type FeatureExpr

type FeatureExpr struct {
	FeaturePath  string           `json:"FeaturePath"`
	BuiltinParam map[string]*Expr `json:"BuiltinParam,omitempty"`
}

func NewFeature

func NewFeature(featurePath string) *FeatureExpr

func NewFeatureWithBuiltinParam

func NewFeatureWithBuiltinParam(featurePath string, param map[string]*Expr) *FeatureExpr

type FuncExpr

type FuncExpr struct {
	FuncName  string           `json:"FuncName"`
	ParamMap  map[string]*Expr `json:"ParamMap,omitempty"`
	ParamList []*Expr          `json:"ParamList,omitempty"`
}

func NewFunc

func NewFunc(funcName string) *FuncExpr

func NewParamListFunc

func NewParamListFunc(funcName string, param []*Expr) *FuncExpr

func NewParamMapFunc

func NewParamMapFunc(funcName string, param map[string]*Expr) *FuncExpr

type ListParamFnPair added in v1.0.2

type ListParamFnPair struct {
	Name string
	Fn   func(ctx context.Context, params []interface{}) (interface{}, error)
}

type MapParamFnPair added in v1.0.2

type MapParamFnPair struct {
	Name string
	Fn   func(ctx context.Context, params map[string]interface{}) (interface{}, error)
}

type MathExpr

type MathExpr struct {
	OpMath    string  `json:"OpMath"`
	Lhs       *Expr   `json:"Lhs,omitempty"`
	Rhs       *Expr   `json:"Rhs,omitempty"`
	ParamList []*Expr `json:"ParamList,omitempty"`
}

func NewLRMath

func NewLRMath(opMath string, lhs, rhs *Expr) *MathExpr

func NewParamListMath

func NewParamListMath(opMath string, param []*Expr) *MathExpr

type NoParamFnPair added in v1.0.2

type NoParamFnPair struct {
	Name string
	Fn   func(ctx context.Context) (interface{}, error)
}

type NoPriorityRule

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

func (*NoPriorityRule) AimFeatParams

func (n *NoPriorityRule) AimFeatParams() []typedef.FeatureParam

func (*NoPriorityRule) AimPTree

func (n *NoPriorityRule) AimPTree() antlr.ParseTree

func (*NoPriorityRule) Compare

func (n *NoPriorityRule) Compare(other typedef.Comparable) int

func (*NoPriorityRule) CondFeatParams

func (n *NoPriorityRule) CondFeatParams() []typedef.FeatureParam

func (*NoPriorityRule) ConditionPTree

func (n *NoPriorityRule) ConditionPTree() antlr.ParseTree

func (*NoPriorityRule) Identifier

func (n *NoPriorityRule) Identifier() string

type Not

type Not string
const NOT Not = "!"

type Option added in v1.0.5

type Option func(cfg *Configuration)

func WithCustomListParamFuncs added in v1.0.2

func WithCustomListParamFuncs(funcPairs ...ListParamFnPair) Option

func WithCustomMapParamFuncs added in v1.0.2

func WithCustomMapParamFuncs(funcPairs ...MapParamFnPair) Option

func WithCustomNoParamFuncs added in v1.0.2

func WithCustomNoParamFuncs(funcPairs ...NoParamFnPair) Option

func WithDefFeatFetcherFactory

func WithDefFeatFetcherFactory() Option

func WithDefFeatVisitCache

func WithDefFeatVisitCache() Option

func WithDefFeatureFetchPool

func WithDefFeatureFetchPool() Option

func WithDefGranularity

func WithDefGranularity() Option

func WithDefMaxParallels

func WithDefMaxParallels() Option

func WithDefRuleComputePool

func WithDefRuleComputePool() Option

func WithDefTreeCache

func WithDefTreeCache() Option

func WithFeatureFetcherFactory

func WithFeatureFetcherFactory(factory func() typedef.FeatureFetcher) Option

type PriorityRule

type PriorityRule struct {
	*NoPriorityRule
	// contains filtered or unexported fields
}

func (*PriorityRule) Compare

func (p *PriorityRule) Compare(other typedef.Comparable) int

type RuleResult added in v1.0.6

type RuleResult interface {
	typedef.VisitTarget

	Passed() bool
	Aim() typedef.Aim
}

func ExecuteRules

func ExecuteRules(rules []RuleTarget, dc typedef.DataCtx, opts ...ExecuteOption) (rrs []RuleResult)

ExecuteRules will execute all rule target, and returns rule results which passed by condition.

func ExecuteSingleRule

func ExecuteSingleRule(rule RuleTarget, dc typedef.DataCtx, opts ...ExecuteOption) (rr RuleResult)

ExecuteSingleRule will execute a single rule, if condition pass, rr.Aim() will not be null.

type RuleTarget added in v1.0.6

type RuleTarget interface {
	typedef.Comparable
	typedef.VisitTarget

	ConditionPTree() antlr.ParseTree
	AimPTree() antlr.ParseTree

	CondFeatParams() []typedef.FeatureParam
	AimFeatParams() []typedef.FeatureParam
}

func NewNoPriorityRule

func NewNoPriorityRule(name string, cdtExpr, aimExpr string) (npr RuleTarget, err error)

NewNoPriorityRule will create a new rule with no priority.

func NewPriorityRule

func NewPriorityRule(name string, priority int, cdtExpr, aimExpr string) (pr RuleTarget, err error)

NewPriorityRule will create a new rule with priority.

type RuleTree added in v1.0.5

type RuleTree struct {
	Tree       antlr.ParseTree
	FeatParams []typedef.FeatureParam
}

func ParseAim added in v1.0.10

func ParseAim(aim string) (tree *RuleTree, err error)

ParseAim will parse an aim string and return the rule tree with feature parameter pre-parsed.

func ParseCondition

func ParseCondition(condition string) (tree *RuleTree, err error)

ParseCondition will parse a condition string and return the rule tree with feature parameter pre-parsed.

func ParseRuleTree added in v1.0.10

func ParseRuleTree(expr string, exprType ExprType) (exprTree *RuleTree, err error)

ParseRuleTree will try parsing expr to antlr parse tree, err will not be null when failed.

type TreeCache added in v1.0.5

type TreeCache interface {
	Put(expr string, tc *RuleTree)
	Get(expr string) (tc *RuleTree, ok bool)
}

type VarExpr

type VarExpr string

Jump to

Keyboard shortcuts

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