rezzza/ruler-bundle

适用于Symfony 2的简单无状态生产规则引擎。

安装数: 12,843

依赖者: 0

建议者: 0

安全性: 0

星标: 26

关注者: 6

分支: 3

开放问题: 0

类型:symfony-bundle

dev-master / 1.0.x-dev 2015-12-15 22:02 UTC

This package is not auto-updated.

Last update: 2024-09-11 10:19:48 UTC


README

适用于Symfony 2的简单无状态生产规则引擎。

集成Hoa\Ruler库。

路线图

  • 通过表单动态创建命题并在存储中持久化。
  • 动态推理

配置

rezzza_ruler:
    context_builder: service.id
    # or
    #context_builder:
    #    default: service.id
    events:
        event.cart.paid: 'Cart paid'
        # or
        event.cart.paid:
            label: 'Cart paid'
            context_builder: default
    inferences:
        cart.price_total:
            # This will show this inferences only when event event.cart.paid
            # will be selected on UI.
            # This is optional.
            event:       [event.cart.paid]
            type:        decimal
            description: Cart total price is
        cart.created_at:
            type:        date
            description: Cart was created at
        cart.contain_product:
            type:        product
            # Your own type
            # You'll return a list of product as array.
            # Not yet implemented, we'll have to finish ui via forms.
            description: Cart contain product

用法

<?php

$rb = $container->get('rezzza.ruler.rule_builder');

$rule = $rb->and(
    $rb->{'>='}($rb->variable('cart.price_total'), 100),
    $rb->{'>='}($rb->context('cart.created_at'), '2011-06-10')
);

$context = $container->get('rezzza.ruler.context_builder')->createContext('default');
$context = $container->get('rezzza.ruler.context_builder')->createContextFromEvent('event.cart.paid');

echo $container->get('rezzza.ruler')->assert($rule, $context); // true or false

序列化

要将规则存储在存储中,您可以对其进行序列化,将其存储在存储中,从存储中检索它,然后反序列化它。上下文不会留在存储中。

$rb = $container->get('rezzza.ruler.rule_builder');

$rule = $rb->and(
    $rb->{'>='}($rb->context('cart.price_total'), 100),
    $rb->{'>='}($rb->context('cart.created_at'), '2011-06-10')
);

$string = (string) $rule; // cart.price.total >= 100 AND cart.created_at >= 2011-06-10

$rule = $container->get('rezzza.ruler')->interprete($string);

添加自定义函数

使用rezzza.ruler.functions标签定义服务。

<service id="acme.ruler.functions" class="Acme\Ruler\Functions">
    <tag name="rezzza.ruler.functions" />
</service>

然后是php类

<?php

namespace Acme\Ruler\Functions;

use Rezzza\RulerBundle\Ruler\FunctionCollectionInterface;

class FunctionCollection implements FunctionCollectionInterface
{
    public function getFunctions()
    {
        return array(
            'version_compare' => function($left, $comparator, $right) {
                return version_compare($left, $comparator, $right);
            },
        );
    }
}

就这样了!

术语表

  • 推理:一组命题
  • 命题:与推理关联的规则。

有任何想法或建议? 创建问题