RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Eliminate Spaghetti Code Using PHP Rules : Page 3

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


Create and Evaluate a Rule from a Database

To create a RuleContext file for this case (we established above that we will use the same Rule file, SuitableForScholarShip.rul, listed in the above section), first we should do is to create two tables in the MySQL database, rules, created at installation of the PHP Rules. Figure 5 shows the tables used in this section: average and student, while Figure 6 and Figure 7 show the columns defined for the two tables.

Figure 5. Rules Database: The figure shows the tables and structure of the rules database.
Figure 6. Average Table: The average table contains a student ID and that student's academic average value.
Figure 7. Student Table: The student table contains a list of variables and their values for each student.

After creating the rules database and its structure, it's time to create the RuleContext file. The format of this file is slightly more complex than the previous example:


The Rule file for the database example is identical to the text file example, but the RuleContext is different; in this case:

  • The student's annual average is 9.5.
  • The student participated in a national contest, but didn’t get first prize.
  • The student is in high school.
  • The student's parents have agreed that the student should apply for the scholarship.

In this case, according to the Rule file (SuitableForScholarShip.rul) the student should not receive a scholarship; in other words, the resulting proposition should have value=FALSE.

With the rules clearly established and the table structures shown in Figure 6 and Figure 7, the SQL statements are very intuitive. The RuleContext file for this case looks like this:

# Format of this file is:
# Rule_Element_Type|Rule_Element_Name|
   SELECT a.average FROM rules.average a 
   WHERE a.variable_name = 'studentAnualAverage';|double
   SELECT if(((SELECT s.variable_name 
   FROM rules.student s 
   WHERE s.id = ? ) = 1), 1, 0);|boolean
   SELECT if(((
     SELECT s.variable_name 
     FROM rules.student s 
     WHERE s.id = ? ) = 1), 1, 0);|boolean
   SELECT if(((
      SELECT s.variable_name 
      FROM rules.student s 
      WHERE s.id = ? ) = 1), 1, 0);|boolean
   SELECT if(((
      SELECT s.variable_name 
         FROM rules.student s 
         WHERE s.id = ? ) = 1), 1, 0);|boolean  

Here's the student.php script that calls the loadSql function with the appropriate arguments:

class Student extends Controller {
   private $appPhysicalPath = null;
      function __construct() {
      $this->load->model( 'rule/RuleLoader', 'loader' );
      // FCPATH is defined in the /index.php file.
      $this->appPhysicalPath = str_replace( 'index.php', '', str_replace( '\\', 
           '/',FCPATH ) );
      // See whether we can do the same thing with this helper:
      // $this->load->helper( 'file' );
   public function loadSql( $id ) {
      $this->load->model( 'rule/strategy/SqlFileLoaderStrategy', 'strategy' );
      $this->loader->setStrategy( $this->strategy );
      $data[ 'rule' ]= $this->loader->loadRule( $this->appPhysicalPath . 
            'data/SuitableForScholarShip.rul' );
      $data[ 'ruleContext' ] = $this->loader->loadRuleContext( $this-> 
            appPhysicalPath . 'data/SuitableForScholarShip.sql.con', $id );
      $data[ 'result' ]      = $data[ 'rule' ]-> 
            evaluate( $data[ 'ruleContext' ] );
      $data[ 'ruleText' ] = read_file( 'data/SuitableForScholarShip.rul' );
      $data[ 'ruleContextText' ] = 
            read_file( 'data/SuitableForScholarShip.sql.con' );
      $this->load->view( 'ruleview', $data );

To see the expected result, browse to the URL http://localhost/rules/index.php/rules/student/loadsql/1. The result should look like Figure 8.

Figure 8. Evaluate a RuleContext from a Database: Here's the output of evaluating a rule against data stored in the rules database.

At this point, you've seen how to install PHP Rules and how to execute RuleContext data stored in both text files and a database. You've also seen how to create your own rules and how to call them from a PHP program. PHP Rules is well-suited for situations where you have a large number of test cases that you need to test against a set of rules that may change. By separating the rules and the data from your program code, you can evaluate rules even when you don't know in advance exactly what those rules are going to be.

Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies—XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date