Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimization: 规则配置支持单行跨列规则 #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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