source: Dev/branches/rest-dojo-ui/server/rdfapi/model/Statement.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: 7.7 KB
Line 
1<?php
2
3// ----------------------------------------------------------------------------------
4// Class: Statement
5// ----------------------------------------------------------------------------------
6
7/**
8 * An RDF statement.
9 * In this implementation, a statement is not itself a resource.
10 * If you want to use a a statement as subject or object of other statements,
11 * you have to reify it first.
12 * 
13 * @author Chris Bizer <chris@bizer.de>
14 * @version  $Id: Statement.php 268 2006-05-15 05:28:09Z tgauss $
15 * @package model
16 */
17class Statement extends Object {
18       
19        /**
20        * Subject of the statement
21        *
22        * @var          object resource
23        * @access       private
24        */     
25         var $subj;
26
27        /**
28        * Predicate of the statement
29        *
30        * @var          object resource
31        * @access       private
32        */             
33    var $pred;
34       
35        /**
36        * Object of the statement
37        *
38        * @var          object node     
39        * @access       private
40        */             
41    var $obj;
42
43  /**
44   * The parameters to constructor are instances of classes and not just strings
45   *
46   * @param     object  node $subj
47   * @param     object  node $pred
48   * @param     object  node $obj
49        * @throws       PhpError
50   */
51  function Statement($subj, $pred, $obj) {
52
53    if (!is_a($subj, 'Resource')) {
54                $errmsg = RDFAPI_ERROR .
55                          '(class: Statement; method: new): Resource expected as subject.';
56                trigger_error($errmsg, E_USER_ERROR);
57        }
58        if (!is_a($pred, 'Resource') || is_a($pred, 'BlankNode')) {
59                $errmsg = RDFAPI_ERROR .
60                          '(class: Statement; method: new): Resource expected as predicate, no blank node allowed.';
61                trigger_error($errmsg, E_USER_ERROR);
62        }
63        if (!(is_a($obj, 'Resource') or is_a($obj, 'Literal'))) {
64                $errmsg = RDFAPI_ERROR .
65                          '(class: Statement; method: new): Resource or Literal expected as object.';
66                trigger_error($errmsg, E_USER_ERROR);
67        }
68               
69        $this->pred = $pred;
70    $this->subj = $subj;
71    $this->obj = $obj;
72  }
73
74  /**
75   * Returns the subject of the triple.
76   * @access    public
77   * @return    object node
78   */
79  function getSubject() {
80    return $this->subj;
81  }
82
83  /**
84   * Returns the predicate of the triple.
85   * @access    public
86   * @return    object node
87   */
88    function getPredicate() {
89    return $this->pred;
90  }
91
92  /**
93   * Returns the object of the triple.
94   * @access    public
95   * @return    object node
96   */
97   function getObject() {
98     return $this->obj;
99   }
100 
101  /**
102   * Alias for getSubject()
103   * @access    public
104   * @return    object node
105   */
106  function subject() {
107    return $this->subj;
108  }
109
110  /**
111   * Alias for getPredicate()
112   * @access    public
113   * @return    object node
114   */
115    function predicate() {
116    return $this->pred;
117  }
118
119  /**
120   * Alias for getObject()
121   * @access    public
122   * @return object node
123   */
124   function object() {
125     return $this->obj;
126   }
127    /**
128   * Retruns the hash code of the triple.       
129   * @access    public
130   * @return string
131   */
132   function hashCode()  {
133      return md5($this->subj->getLabel() . $this->pred->getLabel()  . $this->obj->getLabel());
134   }
135
136  /**
137   * Dumps the triple. 
138   * @access    public
139   * @return string
140   */ 
141
142  function toString() {
143    return  'Triple(' . $this->subj->toString() . ', ' . $this->pred->toString() . ', ' . $this->obj->toString() . ')';
144       
145  }
146
147  /**
148   * Returns a toString() serialization of the statements's subject.
149   *
150   * @access    public
151   * @return    string
152   */ 
153   function toStringSubject() {
154       return $this->subj->toString();
155   }
156
157  /**
158   * Returns a toString() serialization of the statements's predicate.
159   *
160   * @access    public
161   * @return    string
162   */ 
163   function toStringPredicate() {
164       return $this->pred->toString();
165   }
166
167  /**
168   * Reurns a toString() serialization of the statements's object.
169   *
170   * @access    public
171   * @return    string
172   */ 
173   function toStringObject() {
174       return $this->obj->toString();
175   }
176
177  /**
178   * Returns the URI or bNode identifier of the statements's subject.
179   *
180   * @access    public
181   * @return    string
182   */ 
183   function getLabelSubject() {
184       return $this->subj->getLabel();
185   }
186
187  /**
188   * Returns the URI of the statements's predicate.
189   *
190   * @access    public
191   * @return    string
192   */ 
193   function getLabelPredicate() {
194       return $this->pred->getLabel();
195   }
196
197  /**
198   * Reurns the URI, text or bNode identifier of the statements's object.
199   *
200   * @access    public
201   * @return    string
202   */ 
203   function getLabelObject() {
204       return $this->obj->getLabel();
205   }
206 
207  /**
208   * Checks if two statements are equal.
209   * Two statements are considered to be equal if they have the
210   * same subject, predicate and object. A statement can only be equal
211   * to another statement object.
212   * @access    public
213   * @param             object  statement $that
214   * @return    boolean
215   */ 
216
217  function equals ($that) {
218   
219            if ($this == $that) {
220              return true;
221            }
222            if ($that == NULL || !(is_a($that, 'Statement'))) {
223              return false;
224            }
225 
226            return
227                $this->subj->equals($that->subject()) &&
228                $this->pred->equals($that->predicate()) &&
229                $this->obj->equals($that->object());
230          }
231
232  /**
233   * Compares two statements and returns integer less than, equal to, or greater than zero.
234   * Can be used for writing sorting function for models or with the PHP function usort().
235   *
236   * @access    public
237   * @param             object  statement &$that
238   * @return    boolean
239   */ 
240
241  function compare(&$that) {
242          return statementsorter($this, $that);
243          // statementsorter function see below
244  }
245     
246         
247  /**
248   * Reifies a statement.
249   * Returns a new MemModel that is the reification of the statement.
250   * For naming the statement's bNode a Model or bNodeID must be passed to the method.   
251   *
252   * @access    public
253   * @param             mixed   &$model_or_bNodeID
254   * @return    object  model
255   */ 
256
257  function & reify(&$model_or_bNodeID) {
258               
259                if (is_a($model_or_bNodeID, 'MemModel')) {
260                        // parameter is model
261                        $statementModel = new MemModel($model_or_bNodeID->getBaseURI());
262                        $thisStatement = new BlankNode($model_or_bNodeID);
263                } else {
264                        // parameter is bNodeID
265                        $statementModel = new MemModel();
266                        $thisStatement = &$model_or_bNodeID;
267                }
268               
269                $RDFstatement = new Resource(RDF_NAMESPACE_URI . RDF_STATEMENT);
270                $RDFtype = new Resource(RDF_NAMESPACE_URI . RDF_TYPE);
271                $RDFsubject = new Resource(RDF_NAMESPACE_URI . RDF_SUBJECT);
272                $RDFpredicate = new Resource(RDF_NAMESPACE_URI . RDF_PREDICATE);
273                $RDFobject = new Resource(RDF_NAMESPACE_URI . RDF_OBJECT);
274               
275                $statementModel->add(new Statement($thisStatement, $RDFtype, $RDFstatement));
276                $statementModel->add(new Statement($thisStatement, $RDFsubject, $this->getSubject()));
277                $statementModel->add(new Statement($thisStatement, $RDFpredicate, $this->getPredicate()));
278                $statementModel->add(new Statement($thisStatement, $RDFobject, $this->Object()));
279               
280                return $statementModel;
281  }
282 
283} // end: Statement
284
285
286/**
287* Comparison function for comparing two statements.
288* statementsorter() is used by the PHP function usort ( array array, callback cmp_function)
289*
290* @access       private
291* @param                object Statement        $a
292* @param                object Statement        $b
293* @return       integer less than, equal to, or greater than zero 
294* @throws phpErrpr
295*/ 
296function statementsorter($a,$b) {
297          //Compare subjects
298          $x=$a->getSubject();
299          $y=$b->getSubject();
300          $r=strcmp($x->getLabel(),$y->getLabel());
301          if ($r!=0) return $r;
302          //Compare predicates
303          $x=$a->getPredicate();
304          $y=$b->getPredicate();
305          $r=strcmp($x->getURI(),$y->getURI());
306          if ($r!=0) return $r;
307          //Final resort, compare objects
308          $x=$a->getObject();
309          $y=$b->getObject();
310          return strcmp($x->toString(),$y->toString());
311}
312       
313?>
Note: See TracBrowser for help on using the repository browser.