Skip to content

Commit

Permalink
optimization: 规则配置支持单行跨列规则
Browse files Browse the repository at this point in the history
  • Loading branch information
normal-wls committed Nov 29, 2023
1 parent 4c25a41 commit 6362f17
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ decision_table = {
"rows": [ # 行对应的值
["[0..70]", '"boy"'],
["(70..80]", '"boy"'],
["(80..90]", '"boy"'],
["(80..90]", '"boy"'], # 对于实际场景中,如果希望一行表示多列表达式,则直接用字符串类型进行描述,形如 'score in (80..90] and sex="boy"'
["(90..100]", '"boy"'],
["[0..60]", '"girl"'],
["(60..70]", '"girl"'],
Expand Down
2 changes: 1 addition & 1 deletion bkflow_dmn/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

__version__ = "0.1.0"
__version__ = "0.1.1"
11 changes: 7 additions & 4 deletions bkflow_dmn/data_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from enum import Enum
from typing import List
from typing import List, Union

from pydantic import BaseModel, root_validator

Expand Down Expand Up @@ -31,12 +31,12 @@ class DataTableField(BaseModel):

class DataTable(BaseModel):
cols: List[DataTableField]
rows: List[List[str]]
rows: List[Union[List[str], str]]

@root_validator(skip_on_failure=True)
def validate_length(cls, values: dict):
for row in values["rows"]:
if len(row) != len(values["cols"]):
if isinstance(row, list) and len(row) != len(values["cols"]):
raise ValueError("the length of row should be the same as the length of cols")
return values

Expand All @@ -58,7 +58,7 @@ def decide(self, facts: dict, strict_mode=True):
:param strict_mode: 是否采用严格模式,当为 True 时,决策逻辑会进行一些校验并在失败时抛出异常
:return: 决策结果
"""
feel_inputs: List[List[str]] = self.feel_exp_of_inputs
feel_inputs: List[Union[List[str], str]] = self.feel_exp_of_inputs
feel_outputs: List[str] = self.outputs.rows
parsed_inputs = [
all([parse_expression(feel_input, facts) for feel_input in feel_input_row])
Expand Down Expand Up @@ -104,6 +104,9 @@ def feel_exp_of_inputs(self):

result = []
for row in self.inputs.rows:
if isinstance(row, str):
result.append([row])
continue
row_exps = []
for idx, unit in enumerate(row):
pured_unit = unit.strip()
Expand Down
3 changes: 3 additions & 0 deletions release.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Release Notes

# 0.1.1
- 规则配置支持单行跨列规则

# 0.1.0
- 支持单决策表解析 & 判断
39 changes: 39 additions & 0 deletions tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,52 @@
},
}


UNIQUE_SCORE_MULTI_ROW_FORMAT_TEST_TABLE = {
"title": "testing",
"hit_policy": "Unique",
"inputs": {
"cols": [{"id": "score"}, {"id": "sex"}],
"rows": [
["[0..70]", '"boy"'],
["(70..80]", '"boy"'],
["(80..90]", '"boy"'],
'score in (90..100] and sex="boy"',
'score in [0..60] and sex="girl"',
["(60..70]", '"girl"'],
["(70..80]", '"girl"'],
["(80..100]", '"girl"'],
],
},
"outputs": {
"cols": [{"id": "level"}],
"rows": [
['"bad"'],
['"good"'],
['"good+"'],
['"good++"'],
['"bad"'],
['"good"'],
['"good+"'],
['"good++"'],
],
},
}


UNIQUE_TEST_DATA = [
(UNIQUE_SCORE_TEST_TABLE, {"score": 10, "sex": "boy"}, [{"level": "bad"}]),
(UNIQUE_SCORE_TEST_TABLE, {"score": 80, "sex": "boy"}, [{"level": "good"}]),
(UNIQUE_SCORE_TEST_TABLE, {"score": 95, "sex": "boy"}, [{"level": "good++"}]),
(UNIQUE_SCORE_TEST_TABLE, {"score": 65, "sex": "girl"}, [{"level": "good"}]),
(UNIQUE_SCORE_TEST_TABLE, {"score": 75, "sex": "girl"}, [{"level": "good+"}]),
(UNIQUE_SCORE_TEST_TABLE, {"score": 84, "sex": "girl"}, [{"level": "good++"}]),
(UNIQUE_SCORE_MULTI_ROW_FORMAT_TEST_TABLE, {"score": 10, "sex": "boy"}, [{"level": "bad"}]),
(UNIQUE_SCORE_MULTI_ROW_FORMAT_TEST_TABLE, {"score": 80, "sex": "boy"}, [{"level": "good"}]),
(UNIQUE_SCORE_MULTI_ROW_FORMAT_TEST_TABLE, {"score": 95, "sex": "boy"}, [{"level": "good++"}]),
(UNIQUE_SCORE_MULTI_ROW_FORMAT_TEST_TABLE, {"score": 65, "sex": "girl"}, [{"level": "good"}]),
(UNIQUE_SCORE_MULTI_ROW_FORMAT_TEST_TABLE, {"score": 75, "sex": "girl"}, [{"level": "good+"}]),
(UNIQUE_SCORE_MULTI_ROW_FORMAT_TEST_TABLE, {"score": 84, "sex": "girl"}, [{"level": "good++"}]),
]


Expand Down

0 comments on commit 6362f17

Please sign in to comment.