Getting started with PHP Rules

Share this page

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.

The origin of PHP Rules: the Rule Archetype Pattern

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

  • Developers incorporating a rule engine into their PHP project(s)
  • Business analysts who act as liaisons between "the business," i.e., customers, and develpers.

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:

  1. Download and install PHP Rules
  2. Create rules of increasing complexity
  3. Integrate PHP Rules into your software project

Overview of PHP Rules

What are Rules?

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

  • Propositions: statements with Boolean truth values
  • Variables: symbols that represent the value of something
  • Operators: Boolean and quantifier operators

RuleContexts and Rule execution

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.

Rule Package Class Diagram


Example 1: Is this customer eligible for a discount?

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

Example 2: Group discount for six or more people

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

Greg Swindle's picture

Thanks! I'll update the links.

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or class="OPTIONS" [title="the title"].
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
Prove your humanity! (Text is case insensitive.)