RuleContextテキストファイルを作成する
Boolean型の命題結果を生成するには、Ruleファイル(この場合は、前出のSuitableForScholarShip.rulファイル)と、具体的なケースを記述したRuleContextファイルの両方が必要です。この1つめの例では、RuleContextファイルは、以下のSuitableForScholarShip.txt.conとなります。
# SuitableForScholarShip.txt.con: studentAnnualAverage EQUALS 8.0 studentIsParticipatingToContest IS true studentIsGettingFirstPrize IS true studentIsInHighschool IS true studentParentsAreAgree IS true
このファイルからわかるように、RuleContextには、具体的なデータが含まれます。この学生の年間平均成績は8.0で、全国コンテストに出場して優勝した経験があり、高校生であり、学生の両親は奨学金の申請に同意しています。この事実をRuleファイルのロジックに照らし合わせると、この学生には奨学金受給資格があることになります。つまり、命題結果はTRUEとなります。
2つの例(RuleおよびRuleContextテキストファイルのルールをロードして実行するものと、データベースからのルールをロードして実行するもの)におけるRuleContextデータは、まったく異なることに注意してください。もう1つ異なるのは、.phpスクリプトです。このPHP Rulesの例では、スクリプトcustodian.php(/htdocs/rules/system/application/controllers/rules)に、関数loadTxtとloadSqlが含まれています。どちらが呼び出されるかは、モードによって異なります。テキストファイルの例では、ファイル「http://localhost/rules/index.php/rules/custodian/loadtxt/4」を使用し、データベースの例では、「http://localhost/rules/index.php/rules/custodian/loadsql/4」を使用します。以下に示すスクリプトstudent.phpでは、関数loadTxtを呼び出しています。
<?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 loadTxt($id) {
$this->load->model(
'rule/strategy/TxtFileLoaderStrategy',
'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.txt.con', $id );
$data['result'] = $data['rule']-> evaluate($data['ruleContext']);
$data['ruleText'] = read_file(
'data/SuitableForScholarship.rul');
$data['ruleContextText'] = read_file(
'data/SuitableForScholarship.txt.con');
$this->load->view('studentview', $data);
}
}
?>
暗黙のルール記述を変更するには、スクリプトstudent.phpを、custodian.phpと同じディレクトリ(/htdocs/rules/system/application/controllers/rules)に保存します。
奨学金受給資格を判定するには、学生の具体的なデータの1つ1つをルール集合と比較する必要があります。ruleview.phpページ(htdocs/rules/system/application/views)を自分のビューページ(この例ではstudentview.php)に置き換え、同じディレクトリに保存します。
結果を参照するには、ローカルのURLであるhttp://localhost/rules/index.php/rules/student/loadtxt/4を表示します。結果は図4のようになるはずです。

