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

新手想自己写条件判断,类似 "include": "(INT>1)&(CHR>3)&(EVT?[10101])&(TLT?[1028])",怎么用一套代码判断 #346

Open
BlackLeeHandsome opened this issue Oct 20, 2021 · 2 comments

Comments

@BlackLeeHandsome
Copy link

"include": "(INT>1)&(CHR>3)&(EVT?[10101])&(TLT?[1028])",
"include": "(INT>2)&(CHR>4)&(EVT?[10101])",
怎么用一套代码判断类似这样的条件呢?有大佬给个思路吗?

@VickScarlet
Copy link
Owner

写一个复杂一点的做为例子

((INT>1|CHR>3)&(MNY>5|STR>8))|TLT?[1001,1002]|(EVT=10001&(INT>5|CHR>9))

  1. 先将整句条件用 () | & 三种符号拆分成子条件,这些条件放入数组
[
    [
        ["INT>1", "|", "CHR>3"],
        "&",
        ["MNY>5", "|", "STR>8"]
    ],
    "|",
    "TLT?[1001,1002]",
    "|",
    [
        "EVT=10001",
        "&", 
        ["INT>5", "|", "CHR>9"]
    ]
]
  1. 按层判断结果,每一层的偶数位必定是子条件,偶数位必定是逻辑符号,从左到右判断各子句
    先判断第一个子句,作为基准结果
    如果有下一个子句,先判断逻辑符号

    • 逻辑符号为 &,基准结果为假,直接跳过下一个子句判断;
    • 逻辑符号为 |,基准结果为真,直接返回结果为真,流程结束;
    • 剩下的情况,将基准结果更新为下一个子句的结果;
      未提前返回结果则返回最新的基准结果
  2. 子条件的判断
    将整子条件用 > >= < <= = != ? ! 等符号分割为 property symbol condition

  property symbol condition
INT>1 INT > 1
MNY>5 MNY > 5
TLT?[1001,1002] TLT ? [1001,1002]
EVT=10001 EVT = 10001

然后根据 property 取得相应值,进行逻辑判断即可
*?[] 表示判断前值是否在数组中,如果前值为数组,则逐一判断
*![] 表示判断前值是否不数组中,如果前值为数组,则逐一判断

@chengxg
Copy link

chengxg commented Jun 13, 2022

变成 js eval来执行

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants