Hello, I read the example:
...
type User struct {
Name string
Age int64
Male bool
}
func (u *User)GetNum(i int64) int64 {
return i
}
func (u *User)Print(s string){
fmt.Println(s)
}
func (u *User)Say(){
fmt.Println("hello world")
}
const (
base_rule = ...
func exe(user *User){
dataContext := context.NewDataContext()
//inject struct
dataContext.Add("User", user)
...
//init rule engine
knowledgeContext := base.NewKnowledgeContext()
ruleBuilder := builder.NewRuleBuilder(knowledgeContext, dataContext)
//读取规则
start1 := time.Now().UnixNano()
err := ruleBuilder.BuildRuleFromString(base_rule)
end1 := time.Now().UnixNano()
logrus.Infof("rules num:%d, load rules cost time:%d ns", len(knowledgeContext.RuleEntities), end1-start1 )
if err != nil{
logrus.Errorf("err:%s ", err)
}else{
eng := engine.NewGengine()
start := time.Now().UnixNano()
// true: means when there are many rules, if one rule execute error,continue to execute rules after the occur error rule
err := eng.Execute(ruleBuilder, true)
...
}
}
func Test_Base(t *testing.T){
user := &User{
Name: "Calo",
Age: 0,
Male: true,
}
exe(user) // HERE: If I have a list of user, for example I have 10 user object, how to run exe(user) efficiently?
}
My questions is: if I have a list of user, with 10 user objects. How to run exe(user) 10 times but more efficiently.
Because in the exe it will execute the functions in sequence as below:
NewDataContext, then NewKnowledgeContext, NewRuleBuilder, NewGengine and eng.Execute(ruleBuilder, true). I think those methods are just duplicated running for 10 times, how can I inject each of the user object in the list by the function dataContext.Add("User",user), but do not execute other function each time I inject a new user object?
Hello, I read the example:
...
type User struct {
Name string
Age int64
Male bool
}
func (u *User)GetNum(i int64) int64 {
return i
}
func (u *User)Print(s string){
fmt.Println(s)
}
func (u *User)Say(){
fmt.Println("hello world")
}
const (
base_rule =
...func exe(user *User){
dataContext := context.NewDataContext()
//inject struct
dataContext.Add("User", user)
...
//init rule engine
knowledgeContext := base.NewKnowledgeContext()
ruleBuilder := builder.NewRuleBuilder(knowledgeContext, dataContext)
...
}
}
func Test_Base(t *testing.T){
user := &User{
Name: "Calo",
Age: 0,
Male: true,
}
exe(user) // HERE: If I have a list of user, for example I have 10 user object, how to run exe(user) efficiently?
}
My questions is: if I have a list of user, with 10 user objects. How to run exe(user) 10 times but more efficiently.
Because in the exe it will execute the functions in sequence as below:
NewDataContext, then NewKnowledgeContext, NewRuleBuilder, NewGengine and eng.Execute(ruleBuilder, true). I think those methods are just duplicated running for 10 times, how can I inject each of the user object in the list by the function dataContext.Add("User",user), but do not execute other function each time I inject a new user object?