model
[ class tree: model ] [ index: model ] [ all elements ]

Source for file Statement.php

Documentation is available at Statement.php

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: RDF_Statement
  4. // ----------------------------------------------------------------------------------
  5. /**
  6.  * An RDF statement.
  7.  * In this implementation, a statement is not itself a resource.
  8.  * If you want to use a a statement as subject or object of other statements,
  9.  * you have to reify it first.
  10.  *
  11.  * @author Chris Bizer <chris@bizer.de>
  12.  * @version V0.7
  13.  * @package model
  14.  */
  15. class RDF_Statement extends RDF_Object
  16. {
  17.     /**
  18.      * Subject of the statement
  19.      *
  20.      * @var object resource 
  21.      * @access protected
  22.      */
  23.     var $subj;
  24.  
  25.     /**
  26.      * Predicate of the statement
  27.      *
  28.      * @var object resource 
  29.      * @access protected
  30.      */
  31.     var $pred;
  32.  
  33.     /**
  34.      * Object of the statement
  35.      *
  36.      * @var object node 
  37.      * @access protected
  38.      */
  39.     var $obj;
  40.  
  41.     /**
  42.      * The parameters are instances of classes and not just strings
  43.      *
  44.      * @param object node $subj 
  45.      * @param object node $pred 
  46.      * @param object node $obj 
  47.      * @throws PhpError
  48.      */
  49.     function factory($subj$pred$obj)
  50.     {
  51.         $statement =new RDF_Statement;
  52.  
  53.         $return $statement->setSubject($subj);
  54.         if (PEAR::isError($return)) {
  55.             return $return;
  56.         }
  57.         $return $statement->setPredicate($pred);
  58.         if (PEAR::isError($return)) {
  59.             return $return;
  60.         }
  61.         $return $statement->setObject($obj);
  62.         if (PEAR::isError($return)) {
  63.             return $return;
  64.         }
  65.  
  66.         return $statement;
  67.     }
  68.  
  69.     /**
  70.      * Set the subject of the triple.
  71.      *
  72.      * @access public
  73.      * @return object node 
  74.      */
  75.     function setSubject($subj)
  76.     {
  77.         if (!is_a($subj'RDF_Resource')) {
  78.             $errmsg 'Resource expected as subject, got unexpected: '.
  79.                 (is_object($subjget_class($subjgettype($subj));
  80.             return RDF::raiseError(RDF_ERROR_UNEXPECTEDnullnull$errmsg);
  81.         }
  82.         $this->subj = $subj;
  83.     }
  84.  
  85.     /**
  86.      * Returns the subject of the triple.
  87.      *
  88.      * @access public
  89.      * @return object node 
  90.      */
  91.     function getSubject()
  92.     {
  93.         return $this->subj;
  94.     }
  95.  
  96.     /**
  97.      * Set the predicate of the triple.
  98.      *
  99.      * @access public
  100.      * @return object node 
  101.      */
  102.     function setPredicate($pred)
  103.     {
  104.         if (!is_a($pred'RDF_Resource'|| is_a($pred'RDF_BlankNode')) {
  105.             $errmsg 'Resource expected as predicate, no blank node allowed, got unexpected: '.
  106.                 (is_object($predget_class($predgettype($pred));
  107.             return RDF::raiseError(RDF_ERROR_UNEXPECTEDnullnull$errmsg);
  108.         }
  109.         $this->pred = $pred;
  110.     }
  111.  
  112.     /**
  113.      * Returns the predicate of the triple.
  114.      *
  115.      * @access public
  116.      * @return object node 
  117.      */
  118.     function getPredicate()
  119.     {
  120.         return $this->pred;
  121.     }
  122.  
  123.     /**
  124.      * Set the object of the triple.
  125.      *
  126.      * @access public
  127.      * @return object node 
  128.      */
  129.     function setObject($obj)
  130.     {
  131.         if (!(is_a($obj'RDF_Resource'or is_a($obj'RDF_Literal'))) {
  132.            $errmsg 'Resource or Literal expected as object, got unexpected: '.
  133.                 (is_object($objget_class($objgettype($obj));
  134.             return RDF::raiseError(RDF_ERROR_UNEXPECTEDnullnull$errmsg);
  135.         }
  136.         $this->obj = $obj;
  137.     }
  138.  
  139.     /**
  140.      * Returns the object of the triple.
  141.      *
  142.      * @access public
  143.      * @return object node 
  144.      */
  145.     function getObject()
  146.     {
  147.         return $this->obj;
  148.     }
  149.  
  150.     /**
  151.      * Retruns the hash code of the triple.
  152.      *
  153.      * @access public
  154.      * @return string 
  155.      */
  156.     function hashCode()
  157.     {
  158.         return md5($this->subj->getLabel($this->pred->getLabel($this->obj->getLabel());
  159.     }
  160.  
  161.     /**
  162.      * Dumps the triple.
  163.      *
  164.      * @access public
  165.      * @return string 
  166.      */
  167.  
  168.     function toString()
  169.     {
  170.         return 'Triple(' $this->subj->toString(', ' $this->pred->toString(', ' $this->obj->toString(')';
  171.     }
  172.  
  173.     /**
  174.      * Returns a toString() serialization of the statements's subject.
  175.      *
  176.      * @access public
  177.      * @return string 
  178.      */
  179.     function toStringSubject()
  180.     {
  181.         return $this->subj->toString();
  182.     }
  183.  
  184.     /**
  185.      * Returns a toString() serialization of the statements's predicate.
  186.      *
  187.      * @access public
  188.      * @return string 
  189.      */
  190.     function toStringPredicate()
  191.     {
  192.         return $this->pred->toString();
  193.     }
  194.  
  195.     /**
  196.      * Reurns a toString() serialization of the statements's object.
  197.      *
  198.      * @access public
  199.      * @return string 
  200.      */
  201.     function toStringObject()
  202.     {
  203.         return $this->obj->toString();
  204.     }
  205.  
  206.     /**
  207.      * Returns the URI or bNode identifier of the statements's subject.
  208.      *
  209.      * @access public
  210.      * @return string 
  211.      */
  212.     function getLabelSubject()
  213.     {
  214.         return $this->subj->getLabel();
  215.     }
  216.  
  217.     /**
  218.      * Returns the URI of the statements's predicate.
  219.      *
  220.      * @access public
  221.      * @return string 
  222.      */
  223.     function getLabelPredicate()
  224.     {
  225.         return $this->pred->getLabel();
  226.     }
  227.  
  228.     /**
  229.      * Reurns the URI, text or bNode identifier of the statements's object.
  230.      *
  231.      * @access public
  232.      * @return string 
  233.      */
  234.     function getLabelObject()
  235.     {
  236.         return $this->obj->getLabel();
  237.     }
  238.  
  239.     /**
  240.      * Checks if two statements are equal.
  241.      * Two statements are considered to be equal if they have the
  242.      * same subject, predicate and object. A statement can only be equal
  243.      * to another statement object.
  244.      *
  245.      * @access public
  246.      * @param object statement $that 
  247.      * @return boolean 
  248.      */
  249.     function equals($that)
  250.     {
  251.         if ($this == $that{
  252.             return true;
  253.         }
  254.  
  255.         if ($that == null || !(is_a($that'RDF_Statement'))) {
  256.             return false;
  257.         }
  258.  
  259.         $result $this->subj->equals($that->getSubject());
  260.         if (PEAR::isError($result)) {
  261.             return $result;
  262.         }
  263.         if (!$result{
  264.             return false;
  265.         }
  266.  
  267.         $result $this->pred->equals($that->getPredicate());
  268.         if (PEAR::isError($result)) {
  269.             return $result;
  270.         }
  271.         if (!$result{
  272.             return false;
  273.         }
  274.  
  275.         $result $this->obj->equals($that->getObject());
  276.         if (PEAR::isError($result)) {
  277.             return $result;
  278.         }
  279.         if (!$result{
  280.             return false;
  281.         }
  282.  
  283.         return true;
  284.     }
  285.  
  286.     /**
  287.      * Compares two statements and returns integer less than, equal to, or greater than zero.
  288.      * Can be used for writing sorting function for models or with the PHP function usort().
  289.      *
  290.      * @access public
  291.      * @param object statement &$compare_with 
  292.      * @return boolean 
  293.      */
  294.  
  295.     function compare(&$compare_with)
  296.     {
  297.         return RDF_statementsorter($this$compare_with);
  298.         // statementsorter function see below
  299.     }
  300.  
  301.     /**
  302.      * Reifies a statement.
  303.      * Returns a new Model_Memory that is the reification of the statement.
  304.      * For naming the statement's bNode a Model or bNodeID must be passed to the method.
  305.      *
  306.      * @access public
  307.      * @param mixed &$model_or_bNodeID 
  308.      * @return object model 
  309.      */
  310.     function &reify(&$model_or_bNodeID)
  311.     {
  312.         if (is_a($model_or_bNodeID'RDF_Model_Memory')) {
  313.             // parameter is model
  314.             $statementModel =new RDF_Model_Memory($model_or_bNodeID->getBaseURI());
  315.             $thisStatement =RDF_BlankNode::factory($model_or_bNodeID);
  316.         else {
  317.             // parameter is bNodeID
  318.             $statementModel =new RDF_Model_Memory();
  319.             $thisStatement =RDF_BlankNode::factory($model_or_bNodeID);
  320.         }
  321.         if (PEAR::isError($thisStatement)) {
  322.             return $thisStatement;
  323.         }
  324.  
  325.         $RDFstatement =RDF_Resource::factory(RDF_NAMESPACE_URI . RDF_STATEMENT);
  326.         if (PEAR::isError($RDFstatement)) {
  327.             return $RDFstatement;
  328.         }
  329.         $RDFtype =RDF_Resource::factory(RDF_NAMESPACE_URI . RDF_TYPE);
  330.         if (PEAR::isError($RDFtype)) {
  331.             return $RDFtype;
  332.         }
  333.         $RDFsubject =RDF_Resource::factory(RDF_NAMESPACE_URI . RDF_SUBJECT);
  334.         if (PEAR::isError($RDFsubject)) {
  335.             return $RDFsubject;
  336.         }
  337.         $RDFpredicate =RDF_Resource::factory(RDF_NAMESPACE_URI . RDF_PREDICATE);
  338.         if (PEAR::isError($RDFpredicate)) {
  339.             return $RDFpredicate;
  340.         }
  341.         $RDFobject =RDF_Resource::factory(RDF_NAMESPACE_URI . RDF_OBJECT);
  342.         if (PEAR::isError($RDFobject )) {
  343.             return $RDFobject ;
  344.         }
  345.  
  346.         $statement =RDF_Statement::factory($thisStatement$RDFtype$RDFstatement);
  347.         if (PEAR::isError($statement)) {
  348.             return $statement;
  349.         }
  350.         $result $statementModel->add($statement);
  351.         if (PEAR::isError($result)) {
  352.             return $result;
  353.         }
  354.         $statement =RDF_Statement::factory($thisStatement$RDFsubject$this->getSubject());
  355.         if (PEAR::isError($statement)) {
  356.             return $statement;
  357.         }
  358.         $result $statementModel->add($statement);
  359.         if (PEAR::isError($result)) {
  360.             return $result;
  361.         }
  362.         $statement =RDF_Statement::factory($thisStatement$RDFpredicate$this->getPredicate());
  363.         if (PEAR::isError($statement)) {
  364.             return $statement;
  365.         }
  366.         $result $statementModel->add($statement);
  367.         if (PEAR::isError($result)) {
  368.             return $result;
  369.         }
  370.         $statement =RDF_Statement::factory($thisStatement$RDFobject$this->getObject());
  371.         if (PEAR::isError($statement)) {
  372.             return $statement;
  373.         }
  374.         $result $statementModel->add($statement);
  375.         if (PEAR::isError($result)) {
  376.             return $result;
  377.         }
  378.  
  379.         return $statementModel;
  380.     }
  381. // end: Statement
  382.  
  383. /**
  384.  * Comparison function for comparing two statements.
  385.  * RDF_statementsorter() is used by the PHP function usort ( array array, callback cmp_function)
  386.  *
  387.  * @access protected
  388.  * @param object Statement    $a 
  389.  * @param object Statement    $b 
  390.  * @return integer less than, equal to, or greater than zero
  391.  * @throws phpErrpr
  392.  */
  393. function RDF_statementsorter($a$b)
  394. {
  395.     // Compare subjects
  396.     $x $a->getSubject();
  397.     $y $b->getSubject();
  398.     $r strcmp($x->getLabel()$y->getLabel());
  399.     if ($r != 0{
  400.         return $r;
  401.     }
  402.  
  403.     // Compare predicates
  404.     $x $a->getPredicate();
  405.     $y $b->getPredicate();
  406.     $r strcmp($x->getURI()$y->getURI());
  407.     if ($r != 0{
  408.         return $r;
  409.     }
  410.  
  411.     // Final resort, compare objects
  412.     $x $a->getObject();
  413.     $y $b->getObject();
  414.  
  415.     return strcmp($x->toString()$y->toString());
  416. }
  417. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.