source: Dev/branches/rest-dojo-ui/server/rdfapi/infModel/InfRule.php @ 289

Last change on this file since 289 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 10.0 KB
Line 
1<?php
2// ----------------------------------------------------------------------------------
3// Class: InfRule
4// ----------------------------------------------------------------------------------
5
6/**
7 * This class represents a single rule in a RDFS inference model.
8 * It primary constists of a trigger and an entailment.
9 * In the forward-chaining mode (RDFSFModel) a statement is checked,
10 * if it satisfies the trigger. If it does, a new statement is returned.
11 * In the backward-chaining mode (RDFSBModel) a find-query is checked
12 * with the entailment. If this entailment could satify the find-query,
13 * a new find-query is returned, that searches for statements that
14 * satisfy the trigger of this rule.
15 *
16 * @version  $Id: InfRule.php 290 2006-06-22 12:23:24Z tgauss $
17 * @author Daniel Westphal <mail at d-westphal dot de>
18
19 *
20 * @package infModel
21 * @access      public
22 **/
23 
24class InfRule
25{
26       
27        /**
28        * Array, that hold the trigger subject in key ['s'], the trigger
29        * predicate in ['p'], and the trigger object in ['o'].
30        * The array values can be NULL to match anything or be a node that
31        * has to be matched.
32        *
33        * @var          array
34        * @access       private
35        */
36        var $trigger;
37
38        /**
39        * Array, that hold the entailment subject in key ['s'], the
40        * entailment predicate in ['p'], and the entailment object in ['o'].
41        * The array values can be a node that will be inserted in the
42        * returning statement, or '<s>' to insert the subject,'<p>' to insert
43        * the predicate, or '<o>' to insert the object of the checked statement
44        * to this position in the new returned statement.
45        *
46        * @var          array
47        * @access       private
48        */     
49        var $entailment;
50       
51       
52   /**
53    * Constructor
54        *
55    *
56        * @access       public
57    */         
58        function infRule()
59        {
60                //initialising vars
61                $this->trigger=array();
62                $this->entailment=array();
63        }
64       
65        /**
66        * Sets the trigger of this rule
67        * The values can be NULL to match anything or be a node that has to
68        * be matched.
69        *
70        * @param        object Node OR NULL     $subject
71        * @param        object Node OR NULL     $predicate
72        * @param        object Node OR NULL     $object
73        * @access       public
74        * @throws       PhpError
75        */     
76        function setTrigger ($subject, $predicate, $object)
77        {
78                //throw an error if subject, predicate, or object are neither
79                //node, nor null.
80                if(!is_a($subject,'Node') && $subject != null)
81                        trigger_error(RDFAPI_ERROR . '(class: Infrule; method:
82                                setTrigger): $subject has to be null or of class Node'
83                                , E_USER_ERROR);
84                if(!is_a($predicate,'Node') && $predicate != null)
85                        trigger_error(RDFAPI_ERROR . '(class: Infrule; method:
86                                setTrigger): $predicate has to be null or of class Node'
87                                , E_USER_ERROR);
88                if(!is_a($object,'Node') && $object != null)
89                        trigger_error(RDFAPI_ERROR . '(class: Infrule; method:
90                                setTrigger): $object has to be null or of class Node'
91                                , E_USER_ERROR);
92                       
93                //set the trigger
94                $this->trigger['s']=$subject;
95                $this->trigger['p']=$predicate;
96                $this->trigger['o']=$object;           
97        }
98
99        /**
100        * Sets the entailment of this rule
101        * The values can be NULL to match anything or be a node that has to
102        * be matched.
103        *
104        * @param        object Node OR NULL     $subject
105        * @param        object Node OR NULL     $predicate
106        * @param        object Node OR NULL     $object
107        * @access       public
108        * @throws       PhpError
109        */             
110        function setEntailment($subject,$predicate,$object)
111        {
112                //throw an error if subject, predicate, or object are neither node,
113                //nor <s>,<p>,<o>.
114                if(!is_a($subject,'Node') && !ereg('<[spo]>', $subject))
115                        trigger_error(RDFAPI_ERROR . '(class: Infrule; method: 
116                                setEntailment): $subject has to be <s>,<p>,or <o> or of class Node'
117                                , E_USER_ERROR);
118                if(!is_a($predicate,'Node') && !ereg('<[spo]>', $predicate))
119                        trigger_error(RDFAPI_ERROR . '(class: Infrule; method:
120                                setEntailment): $predicate has to be <s>,<p>,or <o> or of class Node'
121                                , E_USER_ERROR);
122                if(!is_a($object,'Node') && !ereg('<[spo]>', $object))
123                        trigger_error(RDFAPI_ERROR . '(class: Infrule; method:
124                                setEntailment): $object has to be <s>,<p>,or <o> or of class Node'
125                                , E_USER_ERROR);
126               
127                $this->entailment['s']=$subject;       
128                $this->entailment['p']=$predicate;
129                $this->entailment['o']=$object;
130        }
131 
132        /**
133        * Checks, if the statement satisfies the trigger.
134        *
135        * @param        object Statement
136        * @return       boolean
137        * @access       public
138        * @throws       PhpError
139        */
140        function checkTrigger(& $statement)
141        {       
142                //is true, if the trigger is null to match anything
143                //or equals the statement's subject
144                $shouldFireS =  $this->trigger['s'] ==  null ||
145                                                $this->trigger['s']->equals($statement->getSubject());
146                                               
147                //is true, if the trigger is null to match anything
148                //or equals the statement's predicate
149                $shouldFireP =  $this->trigger['p'] ==  null ||
150                                                $this->trigger['p']->equals($statement->getPredicate());
151
152                //is true, if the trigger is null to match anything
153                //or equals the statement's object
154                $shouldFireO =  $this->trigger['o'] ==  null ||
155                                                $this->trigger['o']->equals($statement->getObject());
156
157                //returns true, if ALL are true
158                return $shouldFireS && $shouldFireP && $shouldFireO;
159        }
160 
161        /**
162        * Checks, if this rule could entail a statement that matches
163        * a find of $subject,$predicate,$object.
164        *
165        * @param        object Statement
166        * @return       boolean
167        * @access       public
168        * @throws       PhpError
169        */     
170        function checkEntailment ($subject, $predicate, $object)
171        {
172                //true, if $subject is null, the entailment's subject matches
173                //anything, or the $subject equals the entailment-subject.
174                $matchesS=      $subject ==  null ||
175                                        !is_a($this->entailment['s'],'Node') ||
176                                        $this->entailment['s']->equals($subject);
177
178                //true, if $predicate is null, the entailment's predicate matches
179                //anything, or the $predicate equals the entailment-predicate.                                 
180                 $matchesP=     $predicate ==  null ||
181                                        !is_a($this->entailment['p'],'Node') ||
182                                        $this->entailment['p']->equals($predicate);
183
184                //true, if $object is null, the entailment's object matches
185                //anything, or the $object equals the entailment-object.                                                                               
186                $matchesO=      $object ==  null ||
187                                        !is_a($this->entailment['o'],'Node') ||
188                                        $this->entailment['o']->equals($object);
189               
190                //returns true, if ALL are true
191                return $matchesS && $matchesP && $matchesO;
192        }
193       
194        /**
195        * Returns a infered InfStatement by evaluating the statement with
196        * the entailment rule.
197        *
198        * @param        object Statement
199        * @return       object InfStatement
200        * @access       public
201        * @throws       PhpError
202        */     
203        function entail(& $statement)
204        {
205                //if the entailment's subject is <s>,<p>,or <o>, put the statements
206                //subject,predicate,or object into the subject of the
207                //entailed statement. If the entailment's subject is a node,
208                //add that node to the statement.       
209                switch ($this->entailment['s'])
210                {
211                case '<s>':
212                        $entailedSubject=$statement->getSubject();
213                break;
214                case '<p>':
215                        $entailedSubject=$statement->getPredicate();
216                break; 
217                case '<o>':
218                        $entailedSubject=$statement->getObject();
219                break;
220                default:
221                        $entailedSubject=$this->entailment['s'];
222                };
223               
224                //if the entailment's predicate is <s>,<p>,or <o>, put the
225                //statements subject,predicate,or object into the predicate of
226                //the entailed statement. If the entailment's predicate is a node,
227                //add that node to the statement.                       
228                switch ($this->entailment['p'])
229                {
230                case '<s>':
231                        $entailedPredicate=$statement->getSubject();
232                break;
233                case '<p>':
234                        $entailedPredicate=$statement->getPredicate();
235                break; 
236                case '<o>':
237                        $entailedPredicate=$statement->getObject();
238                break;
239                default:
240                        $entailedPredicate=$this->entailment['p'];
241                };
242               
243                //if the entailment's object is <s>,<p>,or <o>, put the
244                //statements subject,predicate,or object into the object of
245                //the entailed statement. If the entailment's object is a node,
246                //add that node to the statement.                       
247                switch ($this->entailment['o'])
248                {
249                case '<s>':
250                        $entailedObject=$statement->getSubject();
251                break;
252                case '<p>':
253                        $entailedObject=$statement->getPredicate();
254                break; 
255                case '<o>':
256                        $entailedObject=$statement->getObject();
257                break;
258                default:
259                        $entailedObject=$this->entailment['o'];
260                };
261               
262                //return the infered statement
263                return (new InfStatement($entailedSubject,$entailedPredicate,$entailedObject));
264        }
265       
266        /**
267        * Returns a find-query that matches statements, whose entailed
268        * statements would match the supplied find query.
269        *
270        * @param        Node OR null $subject
271        * @param        Node OR null $predicate
272        * @param        Node OR null $object
273        * @return       array
274        * @access       public
275        * @throws       PhpError
276        */             
277        function getModifiedFind( $subject, $predicate, $object)
278        {                       
279                $findSubject=$this->trigger['s'];
280                $findPredicate=$this->trigger['p'];
281                $findObject=$this->trigger['o'];
282               
283                switch ($this->entailment['s'])
284                        {
285                        case '<s>':
286                                $findSubject=$subject;
287                        break;
288                        case '<p>':
289                                $findPredicate=$subject;
290                        break; 
291                        case '<o>':
292                                $findObject=$subject;
293                        break;
294                        };
295                       
296                switch ($this->entailment['p'])
297                        {
298                        case '<s>':
299                                $findSubject=$predicate;
300                        break;
301                        case '<p>':
302                                $findPredicate=$predicate;
303                        break; 
304                        case '<o>':
305                                $findObject=$predicate;
306                        break;
307                        };
308                       
309                switch ($this->entailment['o'])
310                        {
311                        case '<s>':
312                                $findSubject=$object;
313                        break;
314                        case '<p>':
315                                $findPredicate=$object;
316                        break; 
317                        case '<o>':
318                                $findObject=$object;
319                        break;
320                        };
321       
322                return array('s' => $findSubject,
323                                     'p' => $findPredicate,
324                                         'o' => $findObject ); 
325        }
326       
327        function getTrigger()
328        {
329                return array    (       's' => $this->trigger['s'],
330                                                        'p' => $this->trigger['p'],     
331                                                        'o' => $this->trigger['o'],
332                                                );
333        }
334       
335        function getEntailment()
336        {
337                return array    (       's' => $this->entailment['s'],
338                                                        'p' => $this->entailment['p'], 
339                                                        'o' => $this->entailment['o'],
340                                                );
341        }
342}
343?>
Note: See TracBrowser for help on using the repository browser.