2017 © Pedro Peláez
 

symfony-bundle ruler-bundle

A simple stateless production rules engine for Symfony 2.

image

rezzza/ruler-bundle

A simple stateless production rules engine for Symfony 2.

  • Tuesday, December 15, 2015
  • by steph_py
  • Repository
  • 5 Watchers
  • 25 Stars
  • 12,668 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

RulerBundle

A simple stateless production rules engine for Symfony 2., (*1)

Integration of Hoa\Ruler library., (*2)

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., (*3)


$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., (*4)

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

Then the php class:, (*5)

<?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 !, (*6)

Glossary

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

Any idea, suggestion ? Create an issue., (*7)

The Versions

15/12 2015

dev-master

9999999-dev https://github.com/rezzza/RulerBundle

A simple stateless production rules engine for Symfony 2.

  Sources   Download

MIT

The Requires

 

bundle symfony ruler