Skip to content

gphat/rule-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NAME
    Rule::Engine - A Rule Engine

SYNOPSIS
        use Rule::Engine::Filter;
        use Rule::Engine::Rule;
        use Rule::Engine::RuleSet;
        use Rule::Engine::Session;

        my $sess = Rule::Engine::Session->new;
        $sess->set_environment('temperature', 65);

        # Make a ruleset
        my $rs = Rule::Engine::RuleSet->new(
            name => 'some-rule',
            filter => Rule::Engine::Filter->new(
                condition => sub {
                    # Check something here.  Any object that returns true will
                    # be kept.
                    shift->happy ? 1 : 0
                }
            )
        );

        # Make a rule to add to the set.  This rule's condition will be executed
        # for each object.  If it returns a true value then the action will be
        # executed for each object.
        my $rule = Rule::Engine::Rule->new(
            name => 'temperature',
            action => sub {
                my ($env, $obj) = @_;
                $obj->happy(1);
            },
            condition => sub {
                my ($env, $obj) = @_;
                return $foo->favorite_temp == $env->get_environment('temperature');
            }
        );

        # Add the rule
        $rs->add_rule($rule);

        # Add the ruleset to the session
        $sess->add_ruleset($rs->name, $rs);

        # Execute the rule, getting back an arrayref of objects that passed the
        # filter after running through all the rules whose conditions were met
        my $results = $sess->execute('some-rule', \@list_of_objects);

AUTHOR
    Cory G Watson, "<gphat at cpan.org>"

ACKNOWLEDGEMENTS
COPYRIGHT & LICENSE
    Copyright 2010 Cory G Watson.

    This program is free software; you can redistribute it and/or modify it
    under the terms of either: the GNU General Public License as published
    by the Free Software Foundation; or the Artistic License.

    See http://dev.perl.org/licenses/ for more information.

About

Rule::Engine

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages