Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Eliminate Spaghetti Code Using PHP Rules

Discover how to install and use PHP Rules to separate conditional logical statements from your source code and manage them independently.


advertisement

HP Rules is a rule engine that lets you separate conditional logical statement from your source code and from the database, placing the conditional logic in reusable files, where you can define and manage them independently.

This article demonstrates how to:

  • Install PHP Rules and execute rules stored in text files or a database
  • Create your own rules, starting from a particular case
  • Evaluate a rule stored in a text file
  • Evaluate a rule stored in a database

Download and Install PHP Rules

You can download PHP Rules here. At the bottom of that page, you'll see a section titled "Download and install PHP Rules." To install PHP Rules follow these steps:

  1. Download the rules.zip archive.
  2. Unzip the archive in the htdocs directory.
  3. Configure the database from the htdocs/rules/system/application/config path according to your local parameters.
  4. Set the hostname, username, password, etc.
  5. Create a new database named "rules" in MySQL.
  6. Run the index.php script from the rules/index.php path.
  7. To install the two examples select the "install wizard" link (see Figure 1).

  8.  
    Figure 1. Installing PHP Rules: Install PHP Rules by clicking on the install wizard link.

  9. Finally, click the Install the Demo Database button (see Figure 2).

  10.  
    Figure 2. Install the Demo Database: You need the demonstration database to run the examples.

 
Figure 3. Running the Examples: Click on each link to run the two examples.


On the http://localhost/rules/index.php/rules/install/db page you'll find links to the two examples as shown in Figure 3.

Using PHP Rules

As the two examples illustrate, you must first create Rule and RuleContext files to get the resulting proposition. The Rule file contains simple statements of facts written using propositions, operators, and variables, while the RuleContext file contains data you want to test against the rule, stored either as plain text or in a database. The Rule file evaluates the RuleContext file and returns a Boolean value that represents whether the information in the RuleContext conforms to the Rule.

In this article, the Rules files correspond to the examples. You can download all the sample code for this article. The two text files SuitableForScholarship.rul and SuitableForScholarship.txt.con, and the relational database file SuitableForScholarship.sql.con are stored in the rules/data directory. The rule examples determine whether a student is suitable for a scholarship, testing against RuleContext data stored in both text files and the database. The rules that a student must meet to get a scholarship are:

  • The student should have an annual grade point average equal to 9.5.
  • OR the student should have participated in a national contest AND won first prize.
  • The student should be in high school.
  • The parents must agree that the student should apply for the scholarship.

The Rule file that structures the rules described above is SuitableForScholarShip.rul:

# SuitableForScholarShip.rul
# Rule establishing when a student can
# gets an international scholarship
studentAnnualAverage EQUALS 9.5
studentIsParticipatingToContest IS true      
studentIsGettingFirstPrize IS true
AND
OR
studentIsInHighschool IS true
studentParentsAreAgree IS true
AND
AND
Author's Note: Rules are case sensitive; miscapitalizing rule keywords or phrases such as "is TRUE" or "Is True" or "equals" will raise an error.

Notice that the SuitableForScholarShip.rul file is a plain text file with a .rul extension. The rules themselves are written using Reverse Polish Notation (RPN). You can find more about RPN here.

"Translating" these rules from the bottom up you get something like this:

(studentParentsAreAgree AND studentIsInHighschool) AND 
   ((studentIsGettingFirstPrize AND studentIsParticipatingToContest) OR 
     studentAnnualAverage))
Author's Note: Both the examples in this article use the SuitableForScholarShip.rul file, but with different RuleContext files.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap