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


Eliminate Spaghetti Code Using PHP Rules : Page 2

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


Create a RuleContext Text File

To generate a Boolean resulting proposition, you need both a Rule file—in this case the SuitableForScholarShip.rul file, described earlier—and an explicit case, represented by the RuleContext file. For this first example, the RuleContext file is SuitableForScholarShip.txt.con, listed below:

# SuitableForScholarShip.txt.con:
studentAnnualAverage EQUALS 8.0
studentIsParticipatingToContest IS true
studentIsGettingFirstPrize IS true
studentIsInHighschool IS true
studentParentsAreAgree IS true

As you can see, this RuleContext test contains the data for a specific situation. This student's annual average is 8.0; he participated in a national contest and received the first prize; the student is in high school, and the student's parents agree with this scholarship application. Using these facts according to the logic in the Rule file, this student should receive the scholarship: in other words, the resulting proposition value should be TRUE.

Notice that the RuleContext data for the two examples (one that loads and execute the rules from Rule and RuleContext text files, and the other from a database) are completely different. Another document that differs is the .php script. In the PHP Rules examples the custodian.php (/htdocs/rules/system/application/controllers/rules) script contains the loadTxt and loadSql functions; which one gets called depends on the mode. The text file example uses the file http://localhost/rules/index.php/rules/custodian/loadtxt/4; the database example uses http://localhost/rules/index.php/rules/custodian/loadsql/4. The Student.php script shown below calls the loadTxt function:

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 loadTxt($id) {
         'strategy' );
      $data['rule']= $this->loader->loadRule(
         $this->appPhysicalPath . 'data/SuitableForScholarship.rul');
      $data['ruleContext'] = $this->loader->loadRuleContext(
         $this-> appPhysicalPath . 
         'data/SuitableForScholarship.txt.con', $id );
      $data['result'] = $data['rule']-> evaluate($data['ruleContext']);
      $data['ruleText'] = read_file(
      $data['ruleContextText'] = read_file(
      $this->load->view('studentview', $data);

To modify the implicit rules description, save the student.php script in the same directory as the custodian.php script (/htdocs/rules/system/application/controllers/rules).

Again, to determine eligibility, you have to compare each specific student's data with the rule set. Simply modify the ruleview.php page (htdocs/rules/system/application/views) with your own view page (in this example, studentview.php) and save it in the same directory.

To see the expected result, browse to the local URL http://localhost/rules/index.php/rules/student/loadtxt/4. The result should look like Figure 4.

Figure 4. Evaluating Rules: Here's the sample output from evaluating rule data stored in a text file.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date