Grule前端表单post后端执行grule引擎规则

时间:2024-12-08 16:25:55

Grule前端表单post后端执行grule引擎规则

  • 编写前端表单和后端接口

编写test.go执行grule引擎规则

  • 示例都是 go test 执行的测试代码,所以将里面的测试代码去除
    • 由于之前 NumberExponentExample_test.go 已经验证可运行, 所以将 err 的异常处理去除
package main

import (
	"log"

	"github.com/hyperjumptech/grule-rule-engine/ast"
	"github.com/hyperjumptech/grule-rule-engine/builder"
	"github.com/hyperjumptech/grule-rule-engine/engine"
	"github.com/hyperjumptech/grule-rule-engine/pkg"
)

type ExponentData struct {
	Check float64
	Set   float64
}

const ExponentRule = `
rule  ExponentCheck  "User Related Rule"  salience 10 {
	when 
		ExponentData.Check == 6.67428e-11
	Then
		ExponentData.Set = .12345E+5;
		Retract("ExponentCheck");
}
`

func main() {
	exponent := &ExponentData{
		Check: 6.67428e-11,
		Set:   0,
	}
	dataContext := ast.NewDataContext()
	dataContext.Add("ExponentData", exponent)
	lib := ast.NewKnowledgeLibrary()
	rb := builder.NewRuleBuilder(lib)
	rb.BuildRuleFromResource("TestExponent", "1.0.0", pkg.NewBytesResource([]byte(ExponentRule)))
	eng1 := &engine.GruleEngine{MaxCycle: 5}
	kb, _ := lib.NewKnowledgeBaseInstance("TestExponent", "1.0.0")
	eng1.Execute(dataContext, kb)
	log.Println(exponent)
}
  • 运行结果: 结构体 ExponentDataSet 字段被设置为了 12345
$ go run test.go 
2024/12/05 01:22:59 &{6.67428e-11 12345}

部署gin和加载模板引擎

  • gin中文文档: https://learnku.com/docs/gin-gonic/1.7
  • 下载 gin 模块
go get -u github.com/gin-gonic/gin
  • 编写 main.go , 并引入 模板引擎
package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
    r.LoadHTMLGlob("templates/*")
	r.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.tmpl", gin.H{})
    })
	r.Run()
}
  • 创建 templates 目录, 然后添加 index.tmpl 模板
mkdir -p templates && touch templates/index.tmpl

编写前端表单

  • 编写表单, 往后端 /number/ 接口发送 post 请求
<html>
    <form action="/number/" method="POST" >
        <textarea name="textarea"></textarea>
        <input type="submit" value="Submit">
    </form>
</html>
  • 启动项目访问 localhost:8080/index 会看到一个文本框
go run main.go

在这里插入图片描述

编写后端接口

  • 编写 /number/ 的后端接口, 运行Grule规则
import (
    "github.com/hyperjumptech/grule-rule-engine/ast"
	"github.com/hyperjumptech/grule-rule-engine/builder"
	"github.com/hyperjumptech/grule-rule-engine/engine"
	"github.com/hyperjumptech/grule-rule-engine/pkg"
)

type ExponentData struct {
	Check float64
	Set   float64
}

func main() {
    r.POST("/number/", func(c *gin.Context) {
        textareaContent := c.PostForm("textarea")

        exponent := &ExponentData{
            Check: 6.67428e-11,
            Set:   0,
        }
        dataContext := ast.NewDataContext()
        dataContext.Add("ExponentData", exponent)
        lib := ast.NewKnowledgeLibrary()
        rb := builder.NewRuleBuilder(lib)
        rb.BuildRuleFromResource("TestExponent", "1.0.0", pkg.NewBytesResource([]byte(textareaContent)))
        eng1 := &engine.GruleEngine{MaxCycle: 5}
        kb, _ := lib.NewKnowledgeBaseInstance("TestExponent", "1.0.0")
        eng1.Execute(dataContext, kb)
        c.JSON(http.StatusOK, gin.H{
            "textareaContent": exponent.Set,
        })
    })
}

验证表单传入Grule能否执行规则

  • 重启项目, 访问 localhost:8080/index , 将Grule规则放入文本框提交
rule  ExponentCheck  "User Related Rule"  salience 10 {
	when 
		ExponentData.Check == 6.67428e-11
	Then
		ExponentData.Set = .12345E+5;
		Retract("ExponentCheck");
}

在这里插入图片描述

  • 结果能返回 exponentSet 字段值: 12345
    在这里插入图片描述

完整代码

  • main.go
package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
    "github.com/hyperjumptech/grule-rule-engine/ast"
	"github.com/hyperjumptech/grule-rule-engine/builder"
	"github.com/hyperjumptech/grule-rule-engine/engine"
	"github.com/hyperjumptech/grule-rule-engine/pkg"
)

type ExponentData struct {
	Check float64
	Set   float64
}

func main() {
	r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    r.LoadHTMLGlob("templates/*")
	r.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.tmpl", gin.H{})
    })
    r.POST("/number/", func(c *gin.Context) {
        textareaContent := c.PostForm("textarea")

        exponent := &ExponentData{
            Check: 6.67428e-11,
            Set:   0,
        }
        dataContext := ast.NewDataContext()
        dataContext.Add("ExponentData", exponent)
        lib := ast.NewKnowledgeLibrary()
        rb := builder.NewRuleBuilder(lib)
        rb.BuildRuleFromResource("TestExponent", "1.0.0", pkg.NewBytesResource([]byte(textareaContent)))
        eng1 := &engine.GruleEngine{MaxCycle: 5}
        kb, _ := lib.NewKnowledgeBaseInstance("TestExponent", "1.0.0")
        eng1.Execute(dataContext, kb)
        c.JSON(http.StatusOK, gin.H{
            "textareaContent": exponent.Set,
        })
    })
	r.Run()
}