blueeon/php-logical-dsl

简单的 WHEN 和 THEN 语法

dev-master 2017-11-29 13:19 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:34:08 UTC


README

简单的 WHEN 和 THEN 语法

安装

composer require blueeon/php-logical-dsl

包管理器页面

https://packagist.org.cn/packages/blueeon/php-logical-dsl

试试看!

特性

语法定义

原型

/**
 *
 *Test rules
 */
rule_name{
    WHEN  req.order.order_from = 11 AND req.order.stock_channel NOT IN( 'cn-order') AND req.order.price >= 1000
    THEN
        (res.mihome = 100 AND res.price = 100 AND WEIGHT=30),
        (res.mihome = 112 AND res.price = 100 AND WEIGHT=70)
    PRIORITY = 1
}
rule_name{
    WHEN  req.order.order_from = 11 AND req.order.stock_channel NOT IN( 'cn-order') AND req.order.price >= 1000
    THEN
        (res.mihome = 100 AND res.price = 100 AND WEIGHT=30),
        (res.mihome = 112 AND res.price = 100 AND WEIGHT=70)
    PRIORITY = 2
}
[RULE NAME]{
    WHEN        [[Condition]...]
    THEN        [[Result]...]
    PRIORITY =  [priority]
}
...

示例

1. 简单

/**
 * 一个简单例子
 */


rule1{
    when    req.order.order_type = 10
    THEN    res.price = 110
}
rule2{
    WHEN    req.order.order_type = 11
    THEN    res.price = 500
}

2. 带优先级

/**
 * 一个带权重的简单例子
 */
rule1{
    when    req.order.order_type = 10
    THEN    res.price = 110
    PRIORITY = 1
}
rule2{
    WHEN    req.order.order_type = 10 AND req.order.order_from = 12
    THEN    res.price = 500
    PRIORITY = 100
}

3. 多个结果

/**
 * 随机按比例返回结果
 */
rule1{
    when    req.order.order_type = 10
    THEN    (res.express = 110 AND WEIGHT = 10),
            (res.express = 120 AND WEIGHT = 90)
}
rule2{
    WHEN    req.order.order_type = 11
    THEN    (res.express = 114 AND WEIGHT = 10),
            (res.express = 118 AND WEIGHT = 90)
}

4. 复杂示例

/**
 * 带权重,多返回值,复杂判断逻辑
 */
rule1{
    WHEN    req.order.order_type = 10
    AND(
            req.order.stock_channel  NOT IN ('cn-tmall')
            OR  req.order.price >= 1000
            AND req.order.order_from IN(1,2,3,12)
    )
    THEN
        (res.mihome = 100 AND res.price = 110 AND WEIGHT=30),
        (res.express = 14  AND res.mihome = 112 AND res.price = 1200 AND WEIGHT=70)
    PRIORITY = 1
}
rule2{
    WHEN    req.order.order_from = 12 
            AND req.order.stock_channel IN( 'cn-order') 
            AND req.order.price >= 1000
    THEN    res.mihome = 100 AND res.price = 1000
    PRIORITY = 10
}


5. 返回函数并执行它

/**
 * 返回一个方法,并且执行这个方法,如果找不到这个方法,则抛一个异常,仅支持静态方法,且需完整的可访问到的命名空间
 */


rule1{
    when    req.order.order_type = 10
    THEN    res.mihome = 100 AND res.mihome = PHPLogicalDSLTests\data\CommonFunction::getMihome(100)
}
rule2{
    WHEN    req.order.order_type = 11
    THEN    res.mihome = 112 AND res.mihome = PHPLogicalDSLTests\data\CommonFunction::getMihome(112)
}

支持的运算符

实用工具