RuleGo RuleGo
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • Quick Start

    • Introduction
    • Use Cases
    • Quick Start
      • Installation
      • Usage
      • Others
      • Summary
    • Dynamic Refresh Rule Chain
  • Rule Chain

  • Standard Components

  • Extension Components

  • Custom Components

  • Components marketplace

  • Visualization

  • AOP

  • Trigger

  • Advanced Topic

  • RuleGo-Server

  • FAQ

  • Endpoint Module

  • Support

目录

Quick Start

# Installation

Use the go get command to install RuleGo:

go get github.com/rulego/rulego
1

# 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:

  1. 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))
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
  1. 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)
1
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))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# Others

  1. 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.
  2. You can also easily develop your own components. Refer to custom components .
  3. 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:

    1. Use Json format rule chain to define business logic, and initialize the rule engine according to the rule chain.
    1. 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......

Edit this page on GitHub (opens new window)
Last Updated: 2025/03/31, 01:52:11
Use Cases
Dynamic Refresh Rule Chain

← Use Cases Dynamic Refresh Rule Chain→

Theme by Vdoing | Copyright © 2023-2025 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式