PHP Rules is a rule engine that models formal propositional logic. It allows you to separate conditional logic from source code and database triggers in a reusable package, where explicit rules can be independently defined and managed.
For a detailed description of PHP Rules, please read chapter 12, "Rule archetype pattern" in Enterprise Patterns and MDA: Building Better Software with Archetype Patterns and UML. I cannot recommend this book enough, and my thanks go to its authors — Jim Arlow and Ila Neustadt — for their permission to avail the "Rule Archetype Pattern" to PHP developers. Chapter 12 — "Rule archetype pattern" — is essential for
We won't list the advantages of rule engines here, since we assume you've already figured out why you want a rule engine. Instead, this tutorial provides step-by-step instructions detailing how to:
Rules are simple statements of fact about the state of something that may be used explicitly to trigger actions.
Rules are defined & stored in text files with RUL extension. They define
RuleContexts are facts, stored in text files, databases, etc., that provide the informational context for the execution of Rules. Rules evaluate RuleContexts, returning a Proposition that tells us whether a given set of facts conform to the defined Rule.

Executing a Rule is simple. Suppose we have a very simple rule that checks whether a customer is eligible for a discount. In order to be eligible, the customer simply needs to be a Gold Card holder.
// Create the rule
$rule = new Rule('eligibleForDiscount');
// Add a Proposition, i.e., a statement that has a value of true or false
$rule->addProposition('customerIsGoldCardHolder', TRUE);
// Create a RuleContext, i.e., a "Fact"
$ruleContext = new RuleContext('eligibleForDiscountContext');
// Provide the truth statement as to whether the actual customer
// has a Gold Card
$ruleContext->addProposition('customerIsGoldCardHolder', TRUE);
// Evaluate
$result = $rule->evaluate($ruleContext);
// Print the resulting Proposition
echo $result->toString();
// RETURNS:
// Proposition statement = customerIsGoldCardHolder, value = TRUE
Say you provide a discount to a group of six or more people:
// Create the rule
$rule = new Rule('eligibleForGroupDiscount');
// Declare the minimun number of people required for discount
$rule->addVariable('minNumPeople', 6);
// Declare a "placeholder" variable for the actual number of people
$rule->addVariable('actualNumPeople', 0);
// Compare the two, i.e.,
// minNumPeople >= actualNumPeople
$rule->addOperator('GREATERTHANOREQUALTO');
// Create a RuleContext, i.e., a "Fact"
$ruleContext = new RuleContext('eligibleForGroupDiscountFact');
// Declare the minimun number of people required for discount
$ruleContext->addVariable('minNumPeople', 6);
// How many people are there?
$ruleContext->addVariable('actualNumPeople', 5);
// Evaluate
$result = $rule->evaluate($ruleContext);
// Print the resulting Proposition
echo $result->toString();
// RETURNS:
// Proposition statement =
// ( actualNumPeople >= minNumPeople ), value = FALSE
Comments
Menno (not verified)
Sun, 10/02/2011 - 13:59
Permalink
Online version of the 'Enterprise Patterns' book
Please note that the referenced book is available for free at: http://uml-design.net/Addison.Wesley-Enterprise.Patt/pindex.htmThe chapter about the Rule Archetype is here: http://uml-design.net/Addison.Wesley-Enterprise.Patt/032111230X_ch12.html
Greg Swindle
Thu, 02/23/2012 - 10:04
Permalink
Thanks! I'll update the links
Thanks! I'll update the links.
Claudio (not verified)
Sat, 11/12/2011 - 04:52
Permalink
Mathematical Calculation derivations
Hi there! first of all, congratulations for your work and thanks for sharing it... I was wondering if your Rules Engine can be used to derive mathematical calculations from facts and rule contexts. Cheers,Claudio
Add new comment