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.
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:
Rule_Element_Type|Rule_Element_Name|
SQL_Statement|Expected_Return_Data_Type_from_SQL_Query
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|
SQL_Statement|
Expected_Return_Data_Type_from_SQL_Query
a|studentAnnualAverage|
SELECT a.average FROM rules.average a
WHERE a.variable_name = 'studentAnualAverage';|double
s|studentIsParticipatingToContest|
SELECT if(((SELECT s.variable_name
FROM rules.student s
WHERE s.id = ? ) = 1), 1, 0);|boolean
s|studentIsGettingFirstPrize|
SELECT if(((
SELECT s.variable_name
FROM rules.student s
WHERE s.id = ? ) = 1), 1, 0);|boolean
s|studentIsInHighschool|
SELECT if(((
SELECT s.variable_name
FROM rules.student s
WHERE s.id = ? ) = 1), 1, 0);|boolean
s|studentParentsAreAgree|
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:
<?php
class Student extends Controller {
private $appPhysicalPath = null;
function __construct() {
parent::Controller();
$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.
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.