Skip to content
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
83 changes: 83 additions & 0 deletions apps/application/workflow/compare/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: __init__.py.py
@date:2024/6/7 14:43
@desc:
"""
from typing import List

from .contain_compare import ContainCompare
from .end_with import EndWithCompare
from .equal_compare import EqualCompare
from .ge_compare import GECompare
from .gt_compare import GTCompare
from .is_not_null_compare import IsNotNullCompare
from .is_not_true import IsNotTrueCompare
from .is_null_compare import IsNullCompare
from .is_true import IsTrueCompare
from .le_compare import LECompare
from .len_equal_compare import LenEqualCompare
from .len_ge_compare import LenGECompare
from .len_gt_compare import LenGTCompare
from .len_le_compare import LenLECompare
from .len_lt_compare import LenLTCompare
from .lt_compare import LTCompare
from .not_contain_compare import NotContainCompare
from .not_equal_compare import NotEqualCompare
from .regex_compare import RegexCompare
from .start_with import StartWithCompare
from .wildcard_compare import WildcardCompare

_compare_handler_dict = {
'is_null': IsNullCompare(),
'is_not_null': IsNotNullCompare(),
'contain': ContainCompare(),
'not_contain': NotContainCompare(),
'eq': EqualCompare(),
'not_eq': NotEqualCompare(),
'ge': GECompare(),
'gt': GTCompare(),
'le': LECompare(),
'lt': LTCompare(),
'len_eq': LenEqualCompare(),
'len_ge': LenGECompare(),
'len_gt': LenGTCompare(),
'len_le': LenLECompare(),
'len_lt': LenLTCompare(),
'is_true': IsTrueCompare(),
'is_not_true': IsNotTrueCompare(),
'start_with': StartWithCompare(),
'end_with': EndWithCompare(),
'regex': RegexCompare(),
'wildcard': WildcardCompare(),
}


def _compare(source_value, compare, target_value):
compare_handler = _compare_handler_dict.get(compare)
if compare_handler is None:
raise RuntimeError(f"Unknown compare handler '{compare}'")
return compare_handler.compare(source_value, compare, target_value)


def _assertion(workflow_manage, field_list: List[str], compare: str, value):
try:
value = workflow_manage.generate_prompt(value)
except Exception:
pass
field_value = None
try:
field_value = workflow_manage.get_reference_field(field_list[0], field_list[1:])
except Exception:
pass
return _compare(field_value, compare, value)


def do_assertion(workflow_manage, condition, condition_list):
b = False if condition == 'and' else True
for row in condition_list:
if _assertion(workflow_manage, row.get('field'), row.get('compare'), row.get('value')) is b:
return b
return not b
15 changes: 15 additions & 0 deletions apps/application/workflow/compare/compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: compare.py
@date:2024/6/7 14:37
@desc:
"""
from abc import abstractmethod

class Compare:

@abstractmethod
def compare(self, source_value, compare, target_value):
pass
25 changes: 25 additions & 0 deletions apps/application/workflow/compare/contain_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: contain_compare.py
@date:2024/6/11 10:02
@desc:
"""
from .compare import Compare


class ContainCompare(Compare):

def compare(self, source_value, compare, target_value):
target_value = str(target_value)

if isinstance(source_value, str):
return target_value in source_value
elif isinstance(source_value, list):
for item in source_value:
if str(item) == target_value:
return True
return False
else:
return target_value in str(source_value)
16 changes: 16 additions & 0 deletions apps/application/workflow/compare/end_with.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# coding=utf-8
"""
@project: MaxKB
@Author:虎虎
@file: start_with.py
@date:2025/10/20 10:37
@desc:
"""
from .compare import Compare


class EndWithCompare(Compare):

def compare(self, source_value, compare, target_value):
source_value = str(source_value)
return source_value.endswith(str(target_value))
15 changes: 15 additions & 0 deletions apps/application/workflow/compare/equal_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: equal_compare.py
@date:2024/6/7 14:44
@desc:
"""
from .compare import Compare


class EqualCompare(Compare):

def compare(self, source_value, compare, target_value):
return str(source_value) == str(target_value)
25 changes: 25 additions & 0 deletions apps/application/workflow/compare/ge_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: lt_compare.py
@date:2024/6/11 9:52
@desc: 大于比较器
"""
from .compare import Compare


class GECompare(Compare):

def compare(self, source_value, compare, target_value):
if source_value is None:
return target_value is None

try:
return float(source_value) >= float(target_value)
except Exception:
try:
return str(source_value) >= str(target_value)
except Exception:
pass
return False
25 changes: 25 additions & 0 deletions apps/application/workflow/compare/gt_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: lt_compare.py
@date:2024/6/11 9:52
@desc: 大于比较器
"""
from .compare import Compare


class GTCompare(Compare):

def compare(self, source_value, compare, target_value):
if source_value is None:
return False

try:
return float(source_value) > float(target_value)
except Exception:
try:
return str(source_value) > str(target_value)
except Exception:
pass
return False
18 changes: 18 additions & 0 deletions apps/application/workflow/compare/is_not_null_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: is_not_null_compare.py
@date:2024/6/28 10:45
@desc:
"""
from .compare import Compare


class IsNotNullCompare(Compare):

def compare(self, source_value, compare, target_value):
try:
return source_value is not None and len(source_value) > 0
except Exception:
return True
18 changes: 18 additions & 0 deletions apps/application/workflow/compare/is_not_true.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
"""
@project: MaxKB
@Author:虎
@file: is_not_true.py
@date:2025/4/7 13:44
@desc:
"""
from .compare import Compare


class IsNotTrueCompare(Compare):

def compare(self, source_value, compare, target_value):
try:
return source_value is False
except Exception:
return False
18 changes: 18 additions & 0 deletions apps/application/workflow/compare/is_null_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: is_null_compare.py
@date:2024/6/28 10:45
@desc:
"""
from .compare import Compare


class IsNullCompare(Compare):

def compare(self, source_value, compare, target_value):
try:
return source_value is None or len(source_value) == 0
except Exception:
return False
18 changes: 18 additions & 0 deletions apps/application/workflow/compare/is_true.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
"""
@project: MaxKB
@Author:虎
@file: IsTrue.py
@date:2025/4/7 13:38
@desc:
"""
from .compare import Compare


class IsTrueCompare(Compare):

def compare(self, source_value, compare, target_value):
try:
return source_value is True
except Exception:
return False
25 changes: 25 additions & 0 deletions apps/application/workflow/compare/le_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: lt_compare.py
@date:2024/6/11 9:52
@desc: 小于比较器
"""
from .compare import Compare


class LECompare(Compare):

def compare(self, source_value, compare, target_value):
if source_value is None:
return target_value is None

try:
return float(source_value) <= float(target_value)
except Exception:
try:
return str(source_value) <= str(target_value)
except Exception:
pass
return False
18 changes: 18 additions & 0 deletions apps/application/workflow/compare/len_equal_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: equal_compare.py
@date:2024/6/7 14:44
@desc:
"""
from .compare import Compare


class LenEqualCompare(Compare):

def compare(self, source_value, compare, target_value):
try:
return len(source_value) == int(target_value)
except Exception as e:
return False
18 changes: 18 additions & 0 deletions apps/application/workflow/compare/len_ge_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: lt_compare.py
@date:2024/6/11 9:52
@desc: 大于比较器
"""
from .compare import Compare


class LenGECompare(Compare):

def compare(self, source_value, compare, target_value):
try:
return len(source_value) >= int(target_value)
except Exception:
return False
18 changes: 18 additions & 0 deletions apps/application/workflow/compare/len_gt_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: lt_compare.py
@date:2024/6/11 9:52
@desc: 大于比较器
"""
from .compare import Compare


class LenGTCompare(Compare):

def compare(self, source_value, compare, target_value):
try:
return len(source_value) > int(target_value)
except Exception:
return False
18 changes: 18 additions & 0 deletions apps/application/workflow/compare/len_le_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: lt_compare.py
@date:2024/6/11 9:52
@desc: 小于比较器
"""
from .compare import Compare


class LenLECompare(Compare):

def compare(self, source_value, compare, target_value):
try:
return len(source_value) <= int(target_value)
except Exception:
return False
Loading
Loading