/
types.go
81 lines (64 loc) · 2.42 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package model
import (
"context"
)
// RuleContext associated with every rule
type RuleContext interface{}
//Rule ... a Rule interface
type Rule interface {
GetName() string
GetIdentifiers() []TupleType
GetConditions() []Condition
GetActionFn() ActionFunction
String() string
GetPriority() int
GetDeps() map[TupleType]map[string]bool
GetContext() RuleContext
}
//MutableRule interface has methods to add conditions and actions
type MutableRule interface {
Rule
AddCondition(conditionName string, idrs []string, cFn ConditionEvaluator, ctx RuleContext) (err error)
SetAction(actionFn ActionFunction)
SetPriority(priority int)
SetContext(ctx RuleContext)
}
//Condition interface to maintain/get various condition properties
type Condition interface {
GetName() string
GetEvaluator() ConditionEvaluator
GetRule() Rule
GetIdentifiers() []TupleType
GetContext() RuleContext
String() string
}
// RuleSession to maintain rules and assert tuples against those rules
type RuleSession interface {
GetName() string
AddRule(rule Rule) (err error)
DeleteRule(ruleName string)
GetRules() []Rule
Assert(ctx context.Context, tuple Tuple) (err error)
Retract(ctx context.Context, tuple Tuple)
ScheduleAssert(ctx context.Context, delayInMillis uint64, key interface{}, tuple Tuple)
CancelScheduledAssert(ctx context.Context, key interface{})
Unregister()
//Optional, called before asserting a tuple but after adding all rules
SetStartupFunction(startupFn StartupRSFunction)
GetStartupFunction() (startupFn StartupRSFunction)
//To be called when the rule session is ready to start accepting tuples
//This will invoke the StartupFunction
Start(startupCtx map[string]interface{}) (err error)
}
//ConditionEvaluator is a function pointer for handling condition evaluations on the server side
//i.e, part of the server side API
type ConditionEvaluator func(string, string, map[TupleType]Tuple, RuleContext) bool
//ActionFunction is a function pointer for handling action callbacks on the server side
//i.e part of the server side API
type ActionFunction func(context.Context, RuleSession, string, map[TupleType]Tuple, RuleContext)
//StartupRSFunction is called once after creation of a RuleSession
type StartupRSFunction func(ctx context.Context, rs RuleSession, sessionCtx map[string]interface{}) (err error)
// ValueChangeListener to pickup and process tuple value changes
type ValueChangeListener interface {
OnValueChange(tuple Tuple, prop string)
}