{"id":5221,"date":"2024-09-24T11:01:03","date_gmt":"2024-09-24T03:01:03","guid":{"rendered":""},"modified":"2024-09-24T11:01:03","modified_gmt":"2024-09-24T03:01:03","slug":"golang\u89c4\u5219\u5f15\u64cesql_\u89c4\u5219\u5f15\u64ce drools","status":"publish","type":"post","link":"https:\/\/mushiming.com\/5221.html","title":{"rendered":"golang\u89c4\u5219\u5f15\u64cesql_\u89c4\u5219\u5f15\u64ce drools"},"content":{"rendered":"

\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u89c4\u5219\u5f15\u64ce\u5728 golang \u4e2d\u7684\u4f7f\u7528\uff0c\u5c06\u9996\u5148\u4ecb\u7ecd golang \u4e2d\u4e3b\u8981\u7684\u89c4\u5219\u5f15\u64ce\u6846\u67b6\uff0c\u7136\u540e\u5229\u7528 golang \u539f\u751f\u7684 parser \u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u89c4\u5219\u5f15\u64ce\u5b9e\u73b0\u57fa\u672c\u7684 bool \u8868\u8fbe\u5f0f\u89e3\u6790\u5de5\u4f5c\u3002<\/p>\n

\u80cc\u666f<\/h3>\n

\u968f\u7740\u4e1a\u52a1\u4ee3\u7801\u7684\u4e0d\u65ad\u8fed\u4ee3\uff0c\u8bde\u751f\u51fa\u4e86\u8d8a\u6765\u8d8a\u591a\u7684 if-else\uff0c\u5e76\u4e14 if-else \u4e2d\u7684\u903b\u8f91\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u5bfc\u81f4\u4ee3\u7801\u903b\u8f91\u590d\u6742\u3001\u7ef4\u62a4\u6027\u5dee\u3001\u53ef\u8bfb\u6027\u5dee\u3001\u4fee\u6539\u98ce\u9669\u9ad8\u7b49\u7f3a\u9677\u3002<\/p>\n

\u590d\u6742\u7684 if-else \u903b\u8f91\u5176\u5b9e\u5bf9\u5e94\u7684\u662f\u4e00\u6761\u6761\u7684\u89c4\u5219\uff0c\u6ee1\u8db3\u5bf9\u5e94\u7684\u89c4\u5219\u5728\u6267\u884c\u5bf9\u5e94\u7684\u64cd\u4f5c\uff0c\u5373 if-else \u4e2d\u7684\u6761\u4ef6\u5c31\u662f\u4e00\u4e2a\u5bf9\u5e94\u7684 bool \u8868\u8fbe\u5f0f\uff1a<\/p>\n

 |--bool \u8868\u8fbe\u5f0f--| if a == 1 && b == 2 { \/\/ do some business } <\/code><\/pre>\n

\u4e00\u4e2a\u590d\u6742\u7684\u903b\u8f91\u8868\u793a\u4e00\u6761\u5bf9\u5e94\u7684\u89c4\u5219\uff0c\u5c06\u8fd9\u4e9b\u89c4\u5219\u7528 bool \u8868\u8fbe\u5f0f\u8868\u793a\u4e4b\u540e\uff0c\u4fbf\u53ef\u4ee5\u6309\u7167\u5bf9\u5e94\u7684\u89c4\u5219\u6267\u884c\u64cd\u4f5c\uff0c\u5927\u5927\u51cf\u5c11 if-else \u7684\u5e94\u7528\uff1a<\/p>\n

if \u89c4\u5219 { \/\/ do some business } <\/code><\/pre>\n

\u800c\u5982\u4f55\u89e3\u6790\u8fd9\u4e9b bool \u8868\u8fbe\u5f0f\u4fbf\u662f\u89c4\u5219\u5f15\u64ce\u6240\u9700\u8981\u5b8c\u6210\u7684\u4efb\u52a1\u4e86\u3002<\/p>\n

\u89c4\u5219\u5f15\u64ce\u4ecb\u7ecd<\/h3>\n

\u89c4\u5219\u5f15\u64ce\u7531\u662f\u4e00\u79cd\u5d4c\u5165\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u7ec4\u4ef6\uff0c\u5b9e\u73b0\u4e86\u5c06\u4e1a\u52a1\u51b3\u7b56\u4ece\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u5206\u79bb\u51fa\u6765\uff0c\u5e76\u4f7f\u7528\u9884\u5b9a\u4e49\u7684\u8bed\u4e49\u6a21\u5757\u7f16\u5199\u4e1a\u52a1\u51b3\u7b56\u3002<\/p>\n

Golang\u8bed\u8a00\u5b9e\u73b0\u7684\u4e3b\u8981\u89c4\u5219\u5f15\u64ce\u6846\u67b6\uff1a<\/p>\n\n\n\n\n\n\n\n\n\n\n\n\n
\u540d\u79f0<\/th>\n\u5730\u5740<\/th>\n\u89c4\u5219\u63cf\u8ff0\u8bed\u8a00<\/th>\n\u4f7f\u7528\u573a\u666f<\/th>\n\u4f7f\u7528\u590d\u6742\u6027<\/th>\n<\/tr>\n<\/thead>\n
YQL(Yet another-Query-Language)<\/td>\nhttps:\/\/github.com\/caibirdme\/yql<\/td>\n\u7c7bSQL<\/td>\n\u8868\u8fbe\u5f0f\u89e3\u6790<\/td>\n\u4f4e<\/td>\n<\/tr>\n
govaluate<\/td>\nhttps:\/\/github.com\/Knetic\/govaluate<\/td>\n\u7c7bGolang<\/td>\n\u8868\u8fbe\u5f0f\u89e3\u6790<\/td>\n\u4f4e<\/td>\n<\/tr>\n
Gval<\/td>\nhttps:\/\/github.com\/PaesslerAG\/gval<\/td>\n\u7c7bGolang<\/td>\n\u8868\u8fbe\u5f0f\u89e3\u6790<\/td>\n\u4f4e<\/td>\n<\/tr>\n
Grule-Rule-Engine<\/td>\nhttps:\/\/github.com\/hyperjumptech\/grule-rule-engine<\/td>\n\u81ea\u5b9a\u4e49DSL\uff08Domain-Specific Language\uff09<\/td>\n\u89c4\u5219\u6267\u884c<\/td>\n\u4e2d<\/td>\n<\/tr>\n
Gengine<\/td>\nhttps:\/\/github.com\/bilibili\/gengine<\/td>\n\u81ea\u5b9a\u4e49DSL\uff08Domain-Specific Language\uff09<\/td>\n\u89c4\u5219\u6267\u884c<\/td>\n\u4e2d<\/td>\n<\/tr>\n
Common Expression Language<\/td>\nhttps:\/\/github.com\/google\/cel-go#evaluate<\/td>\n\u7c7bC<\/td>\n\u901a\u7528\u8868\u8fbe\u5f0f\u8bed\u8a00<\/td>\n\u4e2d<\/td>\n<\/tr>\n
goja<\/td>\nhttps:\/\/github.com\/dop251\/goja<\/td>\nJavaScript<\/td>\n\u89c4\u5219\u89e3\u6790<\/td>\n\u4e2d<\/td>\n<\/tr>\n
GopherLua: VM and compiler for Lua in Go.<\/td>\nhttps:\/\/github.com\/yuin\/gopher-lua<\/td>\nlua<\/td>\n\u89c4\u5219\u89e3\u6790<\/td>\n\u9ad8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u53ef\u4ee5\u770b\u5230\u65e0\u6570\u7684\u4eba\u5728\u524d\u4ec6\u540e\u7ee7\u5730\u9020\u89c4\u5219\u5f15\u64ce\uff0c\u4f46\u662f\u8fd9\u4e9b\u89c4\u5219\u5f15\u64ce\u7531\u4e8e\u529f\u80fd\u5f3a\u5927\uff0c\u56e0\u6b64\u5bf9\u4e8e\u4e00\u4e9b\u6bd4\u8f83\u7b80\u5355\u7684\u903b\u8f91\u8868\u8fbe\u5f0f\u7684\u89e3\u6790\u4efb\u52a1\u6765\u8bf4\u5c31\u663e\u5f97\u6709\u70b9\u91cd\u4e86\u3002<\/p>\n

\u6bd4\u5982\u60f3\u4f7f\u7528\u89c4\u5219\u5f15\u64ce\u5b9e\u73b0\u5982\u4e0b\u7684\u89c4\u5219\uff0c\u4f8b\u5982\u5982\u4e0a\u7684\u8fd9\u4e9b\u6846\u67b6\u6765\u5b9e\u73b0\u89e3\u6790\u7684\u8bdd\u4f1a\u5927\u91cf\u6d88\u8017 CPU \u7684\u8d44\u6e90\uff0c\u5728\u8bf7\u6c42\u91cf\u8f83\u5927\u7684\u7cfb\u7edf\u5f53\u4e2d\u5c31\u6709\u53ef\u80fd\u6210\u4e3a\u7cfb\u7edf\u7684\u6027\u80fd\u5c4f\u969c\u3002<\/p>\n

if type == 1 && product_id = 3{ \/\/... } <\/code><\/pre>\n

\u56e0\u6b64\u9700\u8981\u4e00\u4e2a\u7b80\u5355\u8f7b\u4fbf\u6027\u80fd\u8f83\u597d\u7684\u89c4\u5219\u5f15\u64ce\u3002<\/p>\n

\u57fa\u4e8eGo parser\u5e93\u6253\u9020\u89c4\u5219\u5f15\u64ce<\/h3>\n

parser \u5e93\u4ecb\u7ecd<\/h4>\n

Go \u5185\u7f6e\u7684 parser \u5e93\u63d0\u4f9b\u4e86 golang \u5e95\u5c42\u8bed\u6cd5\u5206\u6790\u7684\u76f8\u5173\u64cd\u4f5c\uff0c\u5e76\u4e14\u5176\u76f8\u5173\u7684 api \u5411\u7528\u6237\u5f00\u653e\uff0c\u90a3\u4e48\u4fbf\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 Go \u7684\u5185\u7f6e parser \u5e93 \u5b8c\u6210\u4e0a\u9762\u4e00\u4e2a\u57fa\u672c\u89c4\u5219\u5f15\u64ce\u7684\u6846\u67b6\u3002<\/p>\n

\u9488\u5bf9\u5982\u4e0b\u7684\u89c4\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528go\u539f\u751f\u7684parser\u8fdb\u884c\u89e3\u6790\uff08\u89c4\u5219\u4e2d\u4e0d\u80fd\u4f7f\u7528 type \u5173\u952e\u5b57\uff09\uff1a<\/p>\n

\/\/ \u4f7f\u7528go\u8bed\u6cd5\u8868\u793a\u7684bool\u8868\u8fbe\u5f0f\uff0cin_array\u4e3a\u51fd\u6570\u8c03\u7528 expr := `product_id == \"3\" && order_type == \"0\" && in_array(capacity_level, []string{\"900\",\"1100\"}) && carpool_type == \"0\"` \/\/ \u4f7f\u7528go parser\u89e3\u6790\u4e0a\u8ff0\u8868\u8fbe\u5f0f\uff0c\u8fd4\u56de\u7ed3\u679c\u4e3a\u4e00\u9897ast parseResult, err := parser.ParseExpr(expr) if err != nil { fmt.Println(err) return } \/\/ \u6253\u5370\u8be5ast ast.Print(nil, parseResult) <\/code><\/pre>\n

\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u7684\u7ed3\u679c\uff08\u4e00\u9897\u4e8c\u53c9\u6811\uff09\uff1a<\/p>\n

0 *ast.BinaryExpr { 1 . X: *ast.BinaryExpr { 2 . . X: *ast.BinaryExpr { 3 . . . X: *ast.BinaryExpr { 4 . . . . X: *ast.Ident { 5 . . . . . NamePos: 1 6 . . . . . Name: \"product_id\" 7 . . . . } 8 . . . . OpPos: 12 9 . . . . Op: == 10 . . . . Y: *ast.BasicLit { 11 . . . . . ValuePos: 15 12 . . . . . Kind: STRING 13 . . . . . Value: \"\\\"3\\\"\" 14 . . . . } 15 . . . } 16 . . . OpPos: 19 17 . . . Op: && 18 . . . Y: *ast.BinaryExpr { 19 . . . . X: *ast.Ident { 20 . . . . . NamePos: 22 21 . . . . . Name: \"order_type\" 22 . . . . } 23 . . . . OpPos: 33 24 . . . . Op: == 25 . . . . Y: *ast.BasicLit { 26 . . . . . ValuePos: 36 27 . . . . . Kind: STRING 28 . . . . . Value: \"\\\"0\\\"\" 29 . . . . } 30 . . . } 31 . . } 32 . . OpPos: 40 33 . . Op: && 34 . . Y: *ast.CallExpr { 35 . . . Fun: *ast.Ident { 36 . . . . NamePos: 43 37 . . . . Name: \"in_array\" 38 . . . } 39 . . . Lparen: 51 40 . . . Args: []ast.Expr (len = 2) { 41 . . . . 0: *ast.Ident { 42 . . . . . NamePos: 52 43 . . . . . Name: \"capacity_level\" 44 . . . . } 45 . . . . 1: *ast.CompositeLit { 46 . . . . . Type: *ast.ArrayType { 47 . . . . . . Lbrack: 68 48 . . . . . . Elt: *ast.Ident { 49 . . . . . . . NamePos: 70 50 . . . . . . . Name: \"string\" 51 . . . . . . } 52 . . . . . } 53 . . . . . Lbrace: 76 54 . . . . . Elts: []ast.Expr (len = 2) { 55 . . . . . . 0: *ast.BasicLit { 56 . . . . . . . ValuePos: 77 57 . . . . . . . Kind: STRING 58 . . . . . . . Value: \"\\\"900\\\"\" 59 . . . . . . } 60 . . . . . . 1: *ast.BasicLit { 61 . . . . . . . ValuePos: 83 62 . . . . . . . Kind: STRING 63 . . . . . . . Value: \"\\\"1100\\\"\" 64 . . . . . . } 65 . . . . . } 66 . . . . . Rbrace: 89 67 . . . . . Incomplete: false 68 . . . . } 69 . . . } 70 . . . Ellipsis: 0 71 . . . Rparen: 90 72 . . } 73 . } 74 . OpPos: 92 75 . Op: && 76 . Y: *ast.BinaryExpr { 77 . . X: *ast.Ident { 78 . . . NamePos: 95 79 . . . Name: \"carpool_type\" 80 . . } 81 . . OpPos: 108 82 . . Op: == 83 . . Y: *ast.BasicLit { 84 . . . ValuePos: 111 85 . . . Kind: STRING 86 . . . Value: \"\\\"0\\\"\" 87 . . } 88 . } 89 } <\/code><\/pre>\n

\u6253\u9020\u57fa\u4e8eparser\u5e93\u7684\u89c4\u5219\u5f15\u64ce<\/h4>\n

\u5c06 parser \u89e3\u6790\u51fa\u6765\u7684\u8fd9\u9897\u4e8c\u53c9\u6811\u753b\u51fa\u6765\uff1a<\/p>\n

\"golang\u89c4\u5219\u5f15\u64cesql_\u89c4\u5219\u5f15\u64ce<\/p>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u6709\u4e86 Golang \u539f\u751f\u7684\u8bed\u6cd5\u89e3\u6790\u5668\uff0c\u6211\u4eec\u53ea\u9700\u8981\u540e\u5e8f\u904d\u5386\u8fd9\u68f5\u4e8c\u53c9\u6811\uff0c\u7136\u540e\u5b9e\u73b0\u4e00\u5957 AST \u4e0e\u5bf9\u5e94\u6570\u636emap\u7684\u6620\u5c04\u5173\u7cfb\u5373\u53ef\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u89c4\u5219\u5f15\u64ce\u3002<\/p>\n

\u5176\u4e2d\uff0cAST \u4e0e\u5bf9\u5e94\u6570\u636emap\u7684\u6620\u5c04\u5173\u7cfb\u7684\u5b9e\u73b0\u4ee3\u7801\u7684\u4e3b\u8981\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n

func eval(expr ast.Expr, data map[string]interface{}) interface{} { switch expr := expr.(type) { case *ast.BasicLit: \/\/ \u5339\u914d\u5230\u6570\u636e return getlitValue(expr) case *ast.BinaryExpr: \/\/ \u5339\u914d\u5230\u5b50\u6811 \/\/ \u540e\u5e8f\u904d\u5386 x := eval(expr.X, data) \/\/ \u5de6\u5b50\u6811\u7ed3\u679c y := eval(expr.Y, data) \/\/ \u53f3\u5b50\u6811\u7ed3\u679c if x == nil || y == nil { return errors.New(fmt.Sprintf(\"%+v, %+v is nil\", x, y)) } op := expr.Op \/\/ \u8fd0\u7b97\u7b26 \/\/ \u6309\u7167\u4e0d\u540c\u7c7b\u578b\u6267\u884c\u8fd0\u7b97 switch x.(type) { case int64: return calculateForInt(x, y, op) case bool: return calculateForBool(x, y, op) case string: return calculateForString(x, y, op) case error: return errors.New(fmt.Sprintf(\"%+v %+v %+v eval failed\", x, op, y)) default: return errors.New(fmt.Sprintf(\"%+v op is not support\", op)) } case *ast.CallExpr: \/\/ \u5339\u914d\u5230\u51fd\u6570 return calculateForFunc(expr.Fun.(*ast.Ident).Name, expr.Args, data) case *ast.ParenExpr: \/\/ \u5339\u914d\u5230\u62ec\u53f7 return eval(expr.X, data) case *ast.Ident: \/\/ \u5339\u914d\u5230\u53d8\u91cf return data[expr.Name] default: return errors.New(fmt.Sprintf(\"%x type is not support\", expr)) } } <\/code><\/pre>\n

\u5b8c\u6574\u7684\u5b9e\u73b0\u4ee3\u7801\u5728\u8fd9\u91cc\uff1ago_parser<\/p>\n

\u6027\u80fd\u5bf9\u6bd4<\/h4>\n

\u4f7f\u7528\u57fa\u4e8e go parser \u5b9e\u73b0\u7684\u89c4\u5219\u5f15\u64ce\u5bf9\u6bd4\u5176\u4ed6\u5e38\u89c1\u7684\u89c4\u5219\u5f15\u64ce\uff08YQL\u3001govaluate\u3001gval\uff09\u7684\u6027\u80fd\uff1a<\/p>\n

BenchmarkGoParser_Match-8  8912 ns\/op \/\/ \u57fa\u4e8e go parser \u5b9e\u73b0\u7684\u89c4\u5219\u5f15\u64ce BenchmarkGval_Match-8 63584 18358 ns\/op \/\/ gval BenchmarkGovaluateParser_Match-8 13628 86955 ns\/op \/\/ govaluate BenchmarkYqlParser_Match-8 10364  ns\/op \/\/ yql <\/code><\/pre>\n

\u603b\u7ed3<\/h4>\n

\u53ef\u4ee5\u770b\u5230\u5728\u4f7f\u7528\u539f\u751f\u7684 parser \u5b9e\u73b0\u7684\u89c4\u5219\u5f15\u64ce\u5728\u6027\u80fd\u4e0a\u5177\u6709\u8f83\u5927\u7684\u4f18\u52bf\uff0c\u4f46\u7f3a\u70b9\u5728\u4e8e\u9700\u8981\u81ea\u5df1\u5b9e\u73b0\u4e00\u5957 AST \u4e0e\u5bf9\u5e94\u6570\u636emap\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u5e76\u4e14\u53d7\u9650\u4e8e go \u539f\u751f parser \u5e93\u7684\u9650\u5236\u5bfc\u81f4\u89c4\u5219\u7684\u5b9a\u4e49\u8bed\u8a00\u6bd4\u8f83\u7e41\u7410\uff0c\u8fd9\u4e9b\u4e5f\u90fd\u662f\u4e3a\u4ec0\u4e48\u4f1a\u6709\u5176\u4ed6\u89c4\u5219\u5f15\u64ce\u6846\u67b6\u8bde\u751f\u7684\u539f\u56e0\uff0c\u4f46\u4e0d\u53ef\u5426\u8ba4\u57fa\u4e8e\u539f\u751f parser \u5e93\u6253\u9020\u7684\u89c4\u5219\u5f15\u64ce\u7684\u6027\u80fd\u8fd8\u662f\u8db3\u591f\u4f18\u79c0\u7684\uff0c\u56e0\u6b64\u5728\u4e00\u4e9b\u6bd4\u8f83\u7b80\u5355\u7684\u89c4\u5219\u5339\u914d\u573a\u666f\u4e2d\u8fd8\u662f\u4f18\u5148\u8003\u8651\u4f7f\u7528\u539f\u751f parser\uff0c\u53ef\u4ee5\u6700\u5927\u6548\u7387\u7684\u5b9e\u73b0\u964d\u672c\u589e\u6548\u7684\u6548\u679c\u3002<\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"golang\u89c4\u5219\u5f15\u64cesql_\u89c4\u5219\u5f15\u64ce drools\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u89c4\u5219\u5f15\u64ce\u5728golang\u4e2d\u7684\u4f7f\u7528\uff0c\u5c06\u9996\u5148\u4ecb\u7ecdgolang\u4e2d\u4e3b\u8981\u7684\u89c4\u5219\u5f15\u64ce\u6846\u67b6\uff0c\u7136\u540e\u5229\u7528...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5221"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=5221"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5221\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=5221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=5221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=5221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}