Developed with love by KnpLabs Hire us for your project!
0

RulerBundle

by rezzza

A simple stateless production rules engine for Symfony 2

RulerBundle

A simple stateless production rules engine for Symfony 2.

Integration of Hoa\Ruler library.

Roadmap

  • Create dynamically Propositions via a Form and persist them on storage.
  • Dynamics inferences

Configuration

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

Usage

<?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

Serialization

To store rules on a storage, you can serialize it, store it on storage, fetch it from storage, and deserialize it. Context does not stay on storage.


$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);

Add custom functions

Define a service with rezzza.ruler.functions tag.

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

Then the php class:

<?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);
            },
        );
    }
}

That's all folks !

Glossary

  • Inference: A group of proposition
  • Proposition: A rule attached to an inference.

Any idea, suggestion ? Create an issue.

Copyright (C) 2011-2012 by rezZza

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
rezzza_ruler:
property_access: true
context_builder: [] # Required
events:
label: ~ # Required
context_builder: default
inferences: # Required

# Prototype
name:
type: ~ # Required
description: ~
event: []