Quick Start
# Installation
Use the go get
command to install RuleGo
:
go get github.com/rulego/rulego
# Usage
First, use Json format to define the rule chain. The rule chain definition does not require learning any specific rule syntax or DSL, just configure the components and connect them with a certain relationship, you can achieve your functional requirements. Reference rule chain
Using RuleGo is extremely simple and lightweight. Just 2 steps:
- Import the
RuleGo
package and use the rule chain definition to create a rule engine instance:
The following example is a simple rule chain definition that includes a filter, a transformer, and a data push component. It filters out device data with a device ID of aa
, transforms the data, and then pushes the data of the device with ID aa
to a remote server.
import "github.com/rulego/rulego"
var ruleFile=`{
{
"ruleChain": {
"id":"chain_call_rest_api",
"name": "Test Rule Chain",
"root": true
},
"metadata": {
"nodes": [
{
"id": "s1",
"type": "jsFilter",
"name": "Filter",
"debugMode": true,
"configuration": {
"jsScript": "return msg.deviceId=='aa';"
}
},
{
"id": "s2",
"type": "jsTransform",
"name": "Transform",
"debugMode": true,
"configuration": {
"jsScript": "msg.temperature=msg.temperature/10; return {'msg':msg,'metadata':metadata,'msgType':msgType};"
}
},
{
"id": "s3",
"type": "restApiCall",
"name": "Push Data",
"debugMode": true,
"configuration": {
"restEndpointUrlPattern": "http://192.168.1.1:9099/api/msg ",
"requestMethod": "POST",
"maxParallelRequestsCount": 200
}
}
],
"connections": [
{
"fromId": "s1",
"toId": "s2",
"type": "True"
},
{
"fromId": "s2",
"toId": "s3",
"type": "Success"
}
]
}
}
}
`
// Create a rule engine instance using the rule chain definition
ruleEngine, err := rulego.New("rule01", []byte(ruleFile))
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
- Hand over the message to the rule engine instance for processing (the message includes: message payload, message type, message metadata, etc.), and then the rule engine will process the message according to the definition of the rule chain.
//Define message metadata
metaData := types.NewMetadata()
metaData.PutValue("productType", "test01")
//Define message payload and message type
msg := types.NewMsg(0, "telemetry_msg", types.JSON, metaData, "{\"deviceId\":\"aa\",\"temperature\":290}")
//Pass the message to the rule engine for processing
ruleEngine.OnMsg(msg)
// Continue processing the message and obtain the rule chain processing result through a callback function.
msg = types.NewMsg(0, "telemetry_msg", types.JSON, metaData, "{\"deviceId\":\"bb\",\"temperature\":310}", types.WithOnEnd(
func(ctx types.RuleContext, msg types.RuleMsg, err error, relationType string) {
fmt.Println(msg.Data) // Get the processing result
}))
ruleEngine.OnMsg(msg)
//Process the alarm message
msg = types.NewMsg(0, "device_alarm", types.JSON, metaData, "{\"deviceId\":\"bb\",\"alarm\":1}")
ruleEngine.OnMsg(msg)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Rule Chain Supports Dynamic Hot Update:
// Obtain the rule engine instance that has been created by the rule chain ID.
ruleEngine, ok := rulego.Get("rule01")
// Update the rule chain, which allows modifying existing node configurations, adding or removing nodes, and changing their connections.
updateRuleChainFile := `{
//... Other configurations remain unchanged
// Modify node s2 to add 5 to the temperature
{
"id": "s2",
"type": "jsTransform",
"name": "Transformation",
"debugMode": true,
"configuration": {
"jsScript": "msg.temperature = msg.temperature / 10 + 5; return {'msg': msg, 'metadata': metadata, 'msgType': msgType};"
}
}
//... Other configurations and connections remain unchanged
}`
// Hot update the rule engine instance.
_ = ruleEngine.ReloadSelf([]byte(updateRuleChainFile))
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Others
- We have built in a large number of standard components and extended components . You only need to combine these components in a configuration - based way to meet your functional requirements.
- You can also easily develop your own components. Refer to custom components .
- We also provide a lightweight Rule Engine Service that can be independently deployed. It enables you to meet your needs by writing rule chains without writing any code. Supports executing rule chains via HTTP interfaces.
# Summary
Using RuleGo is extremely simple and lightweight. Just 2 steps:
- Use Json format rule chain to define business logic, and initialize the rule engine according to the rule chain.
- Then pass the message or event to the rule engine instance, and each message or event will be processed according to the logic defined by the rule chain. The rule chain supports hot update.
RuleGo is light but powerful, let's continue to explore the following chapters......