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

Source for file Model.php

Documentation is available at Model.php

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: RDF_Model
  4. // ----------------------------------------------------------------------------------
  5. /**
  6.  * A model is a programming interface to an RDF graph.
  7.  * An RDF graph is a directed labeled graph, as described in http://www.w3.org/TR/rdf-mt/.
  8.  * It can be defined as a set of <S, P, O> triples, where P is a uriref, S is either
  9.  * a uriref or a blank node, and O is either a uriref, a blank node, or a literal.
  10.  *
  11.  * @version V0.7
  12.  * @author Radoslaw Oldakowski <radol@gmx.de>
  13.  * @author Daniel Westphal <mail@d-westphal.de>
  14.  * @package model
  15.  * @access public
  16.  */
  17.  
  18. class RDF_Model extends RDF_Object
  19. {
  20.     /**
  21.      * Base URI of the Model.
  22.      * Affects creating of new resources and serialization syntax.
  23.      *
  24.      * @var string 
  25.      * @access protected
  26.      */
  27.     var $baseURI;
  28.  
  29.     /**
  30.      * Return current baseURI.
  31.      *
  32.      * @return string 
  33.      * @access public
  34.      */
  35.     function getBaseURI()
  36.     {
  37.         return $this->baseURI;
  38.     }
  39.  
  40.     /**
  41.      * Load a model from a file containing RDF, N3 or N-Triples.
  42.      * This function recognizes the suffix of the filename (.n3 or .rdf) and
  43.      * calls a suitable parser, if no $type is given as string ("rdf" "n3" "nt");
  44.      * If the model is not empty, the contents of the file is added to this Model_MDB.
  45.      *
  46.      * @param string $filename 
  47.      * @param string $type 
  48.      * @access public
  49.      */
  50.     function load($filename$type = null)
  51.     {
  52.         if ((isset($type)) && ($type =='n3'|| ($type =='nt')) {
  53.             $parser =new RDF_N3_Parser();
  54.         elseif ((isset($type)) && ($type =='rdf')) {
  55.             $parser =new RDF_Parser();
  56.         else {
  57.             // create a parser according to the suffix of the filename
  58.             // if there is no suffix assume the file to be XML/RDF
  59.             $suffix = array();
  60.             preg_match("/\.([a-zA-Z0-9_]+)$/"$filename$suffix);
  61.             if (isset($suffix[1])
  62.                 && (strtolower($suffix[1]== 'n3'|| (strtolower($suffix[1]== 'nt')
  63.             {
  64.                 $parser =new RDF_N3_Parser();
  65.             else {
  66.                 $parser =new RDF_Parser();
  67.             }
  68.         }
  69.         $temp =$parser->generateModel($filename);
  70.         if (PEAR::isError($temp)) {
  71.             return $temp;
  72.         }
  73.         $result $this->addModel($temp);
  74.         if (PEAR::isError($result)) {
  75.             return $result;
  76.         }
  77.         if ($this->getBaseURI(== null{
  78.             $this->setBaseURI($temp->getBaseURI());
  79.         }
  80.     }
  81.  
  82.     /**
  83.      * Adds a statement from another model to this model.
  84.      * If the statement to be added contains a blankNode with an identifier
  85.      * already existing in this model, a new blankNode is generated.
  86.      *
  87.      * @param RDF_Object Statement   $statement
  88.      * @access protected
  89.      */ 
  90.     function _addStatementFromAnotherModel($statement&$blankNodes_tmp)
  91.     {
  92.         $subject $statement->getSubject();
  93.         $object $statement->getObject();
  94.  
  95.         if (is_a($subject'RDF_BlankNode')) {
  96.             $label $subject->getLabel();
  97.             if (!array_key_exists($label$blankNodes_tmp)) {
  98.                 $res1 $this->findFirstMatchingStatement($subjectnullnull);
  99.                 if (PEAR::isError($res1)) {
  100.                     return $res1;
  101.                 }
  102.                 $res2 $this->findFirstMatchingStatement(nullnull$subject);
  103.                 if (PEAR::isError($res2)) {
  104.                     return $res2;
  105.                 }
  106.                 if ($res1 || $res2{
  107.                     $blankNodes_tmp[$label=RDF_BlankNode::factory($this);
  108.                     if (PEAR::isError($blankNodes_tmp[$label])) {
  109.                         return $blankNodes_tmp[$label];
  110.                     }
  111.                     $statement->subj = $blankNodes_tmp[$label];
  112.                 else {
  113.                     $blankNodes_tmp[$label$subject;
  114.                 }
  115.             else {
  116.                 $statement->subj = $blankNodes_tmp[$label];
  117.             }
  118.         }
  119.  
  120.         if (is_a($object'RDF_BlankNode')) {
  121.             $label $object->getLabel();
  122.             if (!array_key_exists($label$blankNodes_tmp)) {
  123.                 $res1 $this->findFirstMatchingStatement($objectnullnull);
  124.                 if (PEAR::isError($res1)) {
  125.                     return $res1;
  126.                 }
  127.                 $res2 $this->findFirstMatchingStatement(nullnull$object);
  128.                 if (PEAR::isError($res2)) {
  129.                     return $res2;
  130.                 }
  131.                 if ($res1 || $res2{
  132.                     $blankNodes_tmp[$label=RDF_BlankNode::factory($this);
  133.                     if (PEAR::isError($blankNodes_tmp[$label])) {
  134.                         return $blankNodes_tmp[$label];
  135.                     }
  136.                     $statement->obj = $blankNodes_tmp[$label];
  137.                 else {
  138.                     $blankNodes_tmp[$label$object;
  139.                 }
  140.             else {
  141.                 $statement->obj = $blankNodes_tmp[$label];
  142.             }
  143.         }
  144.  
  145.         return $this->add($statement);
  146.     }
  147. // end: Model
  148.  
  149. ?>

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