决策引擎是一种计算机程序,用于计算一个系统的决策。

目标

  1. 提供规则集、决策表、交叉决策表(决策矩阵)、决策树、评分卡、复杂评分卡、规则流等八种类型的业务规则设计工具
  2. 并发执行规则

数据库设计

规则

  • 表名称: rules
字段名称 字段类型 描述
id string primary key
created int 创建时间
updated int 更新时间
merchant string 商户
code string 规则编码
type string(enum) 规则类型
information json(dict) 规则信息
name string 规则名称
description string 规则描述

type enum列表

  1. raw - 原始脚本
  2. ruleset - 规则集
  3. normal_score_card - 普通评分卡
  4. complex_score_card - 复杂评分卡
  5. decision_tree - 决策树
  6. decision_table - 决策表
  7. decision_matrix - 决策矩阵

information json字段介绍

type == raw 时的information
1
2
3
{
    "script": "string, 脚本字符串"
}
type == ruleset 时的information
 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
{
    "rules": [
        {
            "name": "string, 子规则名称",
            "description": "string, 子规则描述",
            "rule_group": {
                // 树形结构
                // type: string(enum), 规则条件类型, enum: expression, logic_expression
                "type": "logic_expression",
                "op": "string(enum), 规则操作符, or, and",
                "conditions": [
                    {
                        "type": "expression",
                        "operator": "string(enum), 表达式操作符, eq, neq, gt, gte, lt, lte, in, nin",
                        "left_value": {
                            "type": "string(enum), 左值类型, variable, constant, function",
                            "value": "string, 左值值"
                        },
                        "right_value": {
                            "type": "string(enum), 左值类型, variable, constant, function",
                            "value": "string, 左值值"
                        }
                    },
                    {
                        "type": "logic_expression",
                        "op": "string(enum), 规则操作符, or, and",
                        "conditions": []
                    }
                ]
            }
        }
    ]
}

规则模型

type == decision_table 时的information
 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
{
    "table_conditions": [
        {
            // 树形结构
            // type: string(enum), 规则条件类型, enum: expression, logic_expression
            "type": "logic_expression",
            "op": "string(enum), 规则操作符, or, and",
            "conditions": [
                {
                    "type": "expression",
                    "operator": "string(enum), 表达式操作符, eq, neq, gt, gte, lt, lte, in, nin",
                    "left_value": {
                        "type": "string(enum), 左值类型, variable, constant, function",
                        "value": "string, 左值值"
                    },
                    "right_value": {
                        "type": "string(enum), 左值类型, variable, constant, function",
                        "value": "string, 左值值"
                    }
                },
                {
                    "type": "logic_expression",
                    "op": "string(enum), 规则操作符, or, and",
                    "conditions": []
                }
            ]
        }
    ],
    "table_actions": [
        // type: string(enum), 动作类型, assignment, function
        {
            "type": "assignment",
            "field": "string, 字段路径",
            "value": "string, 写入值"
        },
        {
            "type": "function",
            "function_name": "string, 函数名",
            "arguments": [
                {
                    "type": "string(enum), 参数类型, variable, constant",
                    "value": "string, 参数值"
                }
            ]
        }
    ]
}

决策表模型

type == decision_matrix 时的information
 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
{
    // 赋值动作
    "action": {
        "field": "string, 字段路径"
    },
    // 表信息
    "matrix": [
        [
            {
                "crosswise_conditions": [
                    {
                        "type": "expression",
                        "operator": "string(enum), 表达式操作符, eq, neq, gt, gte, lt, lte, in, nin",
                        "left_value": {
                            "type": "string(enum), 左值类型, variable, constant, function",
                            "value": "string, 左值值"
                        },
                        "right_value": {
                            "type": "string(enum), 左值类型, variable, constant, function",
                            "value": "string, 左值值"
                        }
                    },
                    {
                        "type": "logic_expression",
                        "op": "string(enum), 规则操作符, or, and",
                        "conditions": []
                    }
                ],
                "lengthwise_conditions": [
                    {
                        "type": "expression",
                        "operator": "string(enum), 表达式操作符, eq, neq, gt, gte, lt, lte, in, nin",
                        "left_value": {
                            "type": "string(enum), 左值类型, variable, constant, function",
                            "value": "string, 左值值"
                        },
                        "right_value": {
                            "type": "string(enum), 左值类型, variable, constant, function",
                            "value": "string, 左值值"
                        }
                    },
                    {
                        "type": "logic_expression",
                        "op": "string(enum), 规则操作符, or, and",
                        "conditions": []
                    }
                ],
                "value": "string, 值"
            }
        ]
    ]
}

决策矩阵模型

type == normal_score_card 时的information
 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
{
    "is_weight": "boolean, 权重",
    "action": { // 求和方式
        "type": "function",
        "function_name": "string, 函数名称"
    },
    "score_card_table": {
        "string, 字段路径": {
            "weight": "int, 权重",
            "conditions_and_score": [
                {
                    "conditions": [
                        {
                            "type": "expression",
                            "operator": "string(enum), 表达式操作符, eq, neq, gt, gte, lt, lte, in, nin",
                            "left_value": {
                                "type": "string(enum), 左值类型, variable, constant, function",
                                "value": "string, 左值值"
                            },
                            "right_value": {
                                "type": "string(enum), 左值类型, variable, constant, function",
                                "value": "string, 左值值"
                            }
                        },
                        {
                            "type": "logic_expression",
                            "op": "string(enum), 规则操作符, or, and",
                            "conditions": []
                        }
                    ],
                    "score": "int, 分数"
                }
            ]
        }
    }
}

普通评分卡模型

type == complex_score_card 时的information
 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
{
    "action": { // 求和方式
        "type": "function",
        "function_name": "string, 函数名称"
    },
    "score_card_table": [
        [
            {
                "string, 字段路径": {
                    "weight": "int, 权重",
                    "conditions_and_score": [
                        {
                            "conditions": [
                                {
                                    "type": "expression",
                                    "operator": "string(enum), 表达式操作符, eq, neq, gt, gte, lt, lte, in, nin",
                                    "left_value": {
                                        "type": "string(enum), 左值类型, variable, constant, function",
                                        "value": "string, 左值值"
                                    },
                                    "right_value": {
                                        "type": "string(enum), 左值类型, variable, constant, function",
                                        "value": "string, 左值值"
                                    }
                                },
                                {
                                    "type": "logic_expression",
                                    "op": "string(enum), 规则操作符, or, and",
                                    "conditions": []
                                }
                            ],
                            "score": "int, 分数"
                        }
                    ]
                }
            }
        ]
    ]
}

复杂评分卡模型

type == decision_tree 时的information
 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{
    "field": "string, 字段路径",
    "nodes": [
        {
            "conditions": [ // 需要执行的条件
                {
                    "type": "expression",
                    "operator": "string(enum), 表达式操作符, eq, neq, gt, gte, lt, lte, in, nin",
                    "left_value": {
                        "type": "string(enum), 左值类型, variable, constant, function",
                        "value": "string, 左值值"
                    },
                    "right_value": {
                        "type": "string(enum), 左值类型, variable, constant, function",
                        "value": "string, 左值值"
                    }
                },
                {
                    "type": "logic_expression",
                    "op": "string(enum), 规则操作符, or, and",
                    "conditions": []
                }
            ],
            "alise": {
                // type: string(enum), enum: leaf_node(叶子节点), non_leaf_node(非叶子节点)
                "type": "string(enum), enum: leaf_node",
                "action": [
                    // type: string(enum), 动作类型, assignment, function
                    {
                        "type": "assignment",
                        "field": "string, 字段路径",
                        "value": "string, 写入值"
                    },
                    {
                        "type": "function",
                        "function_name": "string, 函数名",
                        "arguments": [
                            {
                                "type": "string(enum), 参数类型, variable, constant",
                                "value": "string, 参数值"
                            }
                        ]
                    }
                ]
            }
        },
        {
            "conditions": [ // 需要执行的条件
                {
                    "type": "expression",
                    "operator": "string(enum), 表达式操作符, eq, neq, gt, gte, lt, lte, in, nin",
                    "left_value": {
                        "type": "string(enum), 左值类型, variable, constant, function",
                        "value": "string, 左值值"
                    },
                    "right_value": {
                        "type": "string(enum), 左值类型, variable, constant, function",
                        "value": "string, 左值值"
                    }
                },
                {
                    "type": "logic_expression",
                    "op": "string(enum), 规则操作符, or, and",
                    "conditions": []
                }
            ],
            "alise": {
                // type: string(enum), enum: leaf_node(叶子节点), non_leaf_node(非叶子节点)
                "type": "string(enum), enum: non_leaf_node",
                "field": "string, 字段路径",
                "nodes": []
            }
        }
    ]
}

决策树模型

决策流

  • 表名称: decision_flow
字段名称 字段类型 描述
id string primary key
created int 创建时间
updated int 更新时间
merchant string 商户
code string 决策流编码
name string 决策流名称
description string 决策流描述

决策流-规则节点

  • 表名称: decision_flow_node
字段名称 字段类型 描述
id string primary key
created int 创建时间
updated int 更新时间
merchant string 商户
decision_flow_code string 决策流编码
rule_code string 规则编码
next_node list[string] 下一步规则节点编码列表
name string 规则节点名称
description string 规则节点描述

数据库图

数据库图

接口文档

规则

创建

Request
  • Method: POST
  • URL: /v1/rule
  • Headers: Content-Type: application/json
Body
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
    "data_list": [
        {
            "merchant": "string, 商户号",
            "code": "string, 规则编码",
            "type": "string(enum), 规则类型",
            "information": "json(dict), 规则信息, 根据type进行变化",
            "name": "string, 规则名称",
            "description": "string, 规则描述"
        }
    ]
}
Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
    "data": [
        {
            "data": {
                "id": "string, 主键",
                "created": "int, 创建时间",
                "updated": "int, 更新时间"
                // 其余全量数据
            },
            "msg": "success",
            "code": 0
        }
    ],
    "msg": "success",
    "code": 0
}
描述

查询

Request
  • Method: GET
  • URL: /v1/rule
  • Headers: Content-Type: application/json
  • Parameters
    • merchant/merchant_eq - 商户号
    • merchant_in - 批量查询商户
    • code/code_eq - 规则编码
    • code_in - 批量规则编码
    • type/type_eq - 规则类型
    • type_in - 批量规则类型
Body

Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
    "data": {
        "results": [
            {
                "id": "string, 主键",
                "created": "int, 创建时间",
                "updated": "int, 更新时间"
                // 其余全量数据
            }
        ],
        "total": "int, 总数",
        "page": "int, 页码",
        "page_size": "int, 页数"
    },
    "msg": "success",
    "code": 0
}
描述

更新

Request
  • Method: PUT
  • URL: /v1/rule
  • Headers: Content-Type: application/json
Body
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
    "data_list": [
        {
            "id": "string, 主键",
            "information": "json(dict), 规则信息, 根据type进行变化",
            "name": "string, 规则名称",
            "description": "string, 规则描述"
        }
    ]
}
Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
    "data": [
        {
            "data": {
                "id": "string, 主键",
                "created": "int, 创建时间",
                "updated": "int, 更新时间"
                // 其余全量数据
            },
            "msg": "success",
            "code": 0
        }
    ],
    "msg": "success",
    "code": 0
}
描述

只允许更新information,name,description

删除

Request
  • Method: DELETE
  • URL: /v1/rule
  • Headers: Content-Type: application/json
Body
1
2
3
{
    "data_list": ["string, id"]
}
Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
    "data": [
        {
            "data": {
                "id": "string, 主键"
            },
            "msg": "success",
            "code": 0
        }
    ],
    "msg": "success",
    "code": 0
}
描述

决策流-规则节点

创建

Request
  • Method: POST
  • URL: /v1/decision-flow-node
  • Headers: Content-Type: application/json
Body
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
    "data_list": [
        {
            "merchant": "string, 商户号",
            "decision_flow_code": "string, 决策流编码",
            "rule_code": "string, 规则编码",
            "next_node": [
                "string, 规则节点名称"
            ],
            "name": "string, 规则节点名称",
            "description": "string, 规则节点描述"
        }
    ]
}
Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
    "data": [
        {
            "data": {
                "id": "string, 主键",
                "created": "int, 创建时间",
                "updated": "int, 更新时间"
                // 其余全量数据
            },
            "msg": "success",
            "code": 0
        }
    ],
    "msg": "success",
    "code": 0
}
描述

获取

Request
  • Method: GET
  • URL: /v1/decision-flow-node
  • Headers: Content-Type: application/json
  • Parameters
    • merchant/merchant_eq - 商户号
    • merchant_in - 批量查询商户
    • rule_code/rule_code_eq - 规则编码
    • rule_code_in - 批量规则编码
    • decision_flow_code/decision_flow_code_eq - 规则编码
    • decision_flow_code_in - 批量规则编码
Body

Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
    "data": {
        "results": [
            {
                "id": "string, 主键",
                "created": "int, 创建时间",
                "updated": "int, 更新时间"
                // 其余全量数据
            }
        ],
        "total": "int, 总数",
        "page": "int, 页码",
        "page_size": "int, 页数"
    },
    "msg": "success",
    "code": 0
}
描述

更新

Request
  • Method: PUT
  • URL: /v1/decision-flow-node
  • Headers: Content-Type: application/json
Body
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
    "data_list": [
        {
            "id": "string, 主键",
            "next_node": [
                "string, 规则节点名称"
            ],
            "name": "string, 规则节点名称",
            "description": "string, 规则节点描述"
        }
    ]
}
Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
    "data": {
        "results": [
            {
                "id": "string, 主键",
                "created": "int, 创建时间",
                "updated": "int, 更新时间"
                // 其余全量数据
            }
        ],
        "total": "int, 总数",
        "page": "int, 页码",
        "page_size": "int, 页数"
    },
    "msg": "success",
    "code": 0
}
描述

只允许修改next_node,name,description

删除

Request
  • Method: DELETE
  • URL: /v1/decision-flow-node
  • Headers: Content-Type: application/json
Body
1
2
3
{
    "data_list": ["string, id"]
}
Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
    "data": [
        {
            "data": {
                "id": "string, 主键"
            },
            "msg": "success",
            "code": 0
        }
    ],
    "msg": "success",
    "code": 0
}
描述

决策流

创建

Request
  • Method: POST
  • URL: /v1/decison-flow
  • Headers: Content-Type: application/json
Body
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
    "data_list": [
        {
            "merchant": "string, 商户号",
            "code": "string, 决策流编码",
            "name": "string, 规则节点名称",
            "description": "string, 规则节点描述"
        }
    ]
}
Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
    "data": [
        {
            "data": {
                "id": "string, 主键",
                "created": "int, 创建时间",
                "updated": "int, 更新时间"
                // 其余全量数据
            },
            "msg": "success",
            "code": 0
        }
    ],
    "msg": "success",
    "code": 0
}
描述

获取

Request
  • Method: GET
  • URL: /v1/decision-flow
  • Headers: Content-Type: application/json
  • Parameters
    • merchant/merchant_eq - 商户号
    • merchant_in - 批量查询商户
    • code/code_eq - 决策流编码
    • code_in - 批量决策流编码
Body

Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
    "data": {
        "results": [
            {
                "id": "string, 主键",
                "created": "int, 创建时间",
                "updated": "int, 更新时间"
                // 其余全量数据
            }
        ],
        "total": "int, 总数",
        "page": "int, 页码",
        "page_size": "int, 页数"
    },
    "msg": "success",
    "code": 0
}
描述

更新

Request
  • Method: PUT
  • URL: /v1/decision-flow
  • Headers: Content-Type: application/json
Body
1
2
3
4
5
6
7
8
9
{
    "data_list": [
        {
            "id": "string, 主键",
            "name": "string, 规则节点名称",
            "description": "string, 规则节点描述"
        }
    ]
}
Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
    "data": [
        {
            "data": {
                "id": "string, 主键",
                "created": "int, 创建时间",
                "updated": "int, 更新时间"
                // 其余全量数据
            },
            "msg": "success",
            "code": 0
        }
    ],
    "msg": "success",
    "code": 0
}
描述

删除

Request
  • Method: DELETE
  • URL: /v1/decision-flow
  • Headers: Content-Type: application/json
Body
1
2
3
{
    "data_list": ["string, id"]
}
Response
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
    "data": [
        {
            "data": {
                "id": "string, 主键"
            },
            "msg": "success",
            "code": 0
        }
    ],
    "msg": "success",
    "code": 0
}
描述

关键流程图

单个规则执行

单个规则执行

决策流执行

决策流执行

参考文章