-
Notifications
You must be signed in to change notification settings - Fork 278
核心API
rencalo770 edited this page Feb 9, 2021
·
3 revisions
- gengine 只有四个关键api,分别是gengine的
dataContext
,ruleBuilder
,engine
和GenginePool
- 代码位置: https://github.com/bilibili/gengine/blob/main/context/data_context.go
-
dataContext
允许用户注入将要在规则代码中使用的API - 举例:
dataContext := context.NewDataContext()
//用户注入需要在规则代码中使用的API
dataContext.Add("println",fmt.Println)
- 代码位置: https://github.com/bilibili/gengine/blob/main/builder/rule_builder.go
- ruleBuilder接受一个dataContext实例作为参数,并将用户传入的字符串构建出可执行的代码(规则)
- 举例:
const rule = `
rule "1" "rule-des" salience 10
begin
println("hello world, gengine!")
end
`
dataContext := context.NewDataContext()
//用户注入需要在规则代码中使用的API
dataContext.Add("println",fmt.Println)
ruleBuilder := builder.NewRuleBuilder(dataContext)
err := ruleBuilder.BuildRuleFromString(rule)
- 代码位置:https://github.com/bilibili/gengine/blob/main/engine/gengine.go
-
engine
允许用户构建gengine实例,并传入实例化好的ruleBuilder
使用不同的模式执行规则代码 - 举例:
const rule = `
rule "1" "rule-des" salience 10
begin
println("hello world, gengine!")
end
`
dataContext := context.NewDataContext()
//用户注入需要在规则代码中使用的API
dataContext.Add("println",fmt.Println)
ruleBuilder := builder.NewRuleBuilder(dataContext)
err := ruleBuilder.BuildRuleFromString(rule)
eng := engine.NewGengine()
err := eng.Execute(ruleBuilder, true)
- gengine实例池,提供给用户在高QPS下使用,为了解决高并发和线程安全问题的API,具体用法在本文档的"引擎池"章节有详细叙述
- 代码 https://github.com/bilibili/gengine/blob/main/engine/gengine_pool.go
- internal文件夹中的代码是gengine的核心,用户可以不必关心其实现