Source for file Model.php

Documentation is available at Model.php

  1. <?php
  2.  
  3. // ----------------------------------------------------------------------------------
  4. // Class: Model
  5. // ----------------------------------------------------------------------------------
  6.  
  7. /**
  8.  * Abstract superclass of MemModel and DbModel. A model is a programming interface to an RDF graph.
  9.  * An RDF graph is a directed labeled graph, as described in http://www.w3.org/TR/rdf-mt/.
  10.  * It can be defined as a set of <S, P, O> triples, where P is a uriref, S is either
  11.  * a uriref or a blank node, and O is either a uriref, a blank node, or a literal.
  12.  *
  13.  *
  14.  * @version  $Id: fsource_model__modelModel.php.html,v 1.10 2006/06/26 12:34:12 tgauss Exp $
  15.  * @author Radoslaw Oldakowski <radol@gmx.de>
  16.  * @author Daniel Westphal <mail@d-westphal.de>
  17.  *
  18.  * @package model
  19.  * @access    public
  20.  */
  21.  
  22. Class Model extends Object {
  23.  
  24.  
  25. /**
  26.  * Base URI of the Model.
  27.  * Affects creating of new resources and serialization syntax.
  28.  *
  29.  * @var     string 
  30.  * @access    private
  31.  */
  32.  var $baseURI;
  33.  
  34. /**
  35.  * Number of the last assigned bNode.
  36.  *
  37.  *
  38.  * @var     integer 
  39.  * @access    private
  40.  */
  41.  var $bNodeCount;
  42.  
  43.  
  44.  
  45. /**
  46.  * Notice for people who are used to work with older versions of RAP.
  47.  *
  48.  * @throws  PHPError
  49.  * @access    public
  50.  */
  51.  function Model({
  52.  
  53.    $errmsg  'Since RAP 0.6 the class for manipulating memory models has been renamed to MemModel.';
  54.    $errmsg .= '<br>Sorry for this inconvenience.<br>';
  55.  
  56.    trigger_error($errmsgE_USER_ERROR);
  57.  }
  58.  
  59.  
  60. /**
  61.  * Return current baseURI.
  62.  *
  63.  * @return  string 
  64.  * @access    public
  65.  */
  66.  function getBaseURI()  {
  67.  
  68.    return $this->baseURI;
  69.  }
  70.  
  71.  
  72. /**
  73.  * Load a model from a file containing RDF, N3, N-Triples or a xhtml document containing RDF.
  74.  * This function recognizes the suffix of the filename (.n3 or .rdf) and
  75.  * calls a suitable parser, if no $type is given as string ("rdf" "n3" "nt");
  76.  * If the model is not empty, the contents of the file is added to this DbModel.
  77.  *
  78.  * @param     string     $filename 
  79.  * @param     string     $type 
  80.  * @param   boolean $stream 
  81.  * @access    public
  82.  */
  83.  function load($filename$type NULL$stream=false{
  84.  
  85.   if ((isset($type)) && ($type =='n3'OR ($type =='nt')) {
  86.   // Import Package Syntax
  87.   include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_N3);
  88.       $parser new N3Parser();
  89.   }elseif ((isset($type)) && ($type =='rdf')) {
  90.   // Import Package Syntax
  91.   include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_RDF);
  92.       $parser new RdfParser();
  93.   }elseif ((isset($type)) && ($type =='grddl')) {
  94.   // Import Package Syntax
  95.   include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_GRDDL);
  96.       $parser new GRDDLParser();
  97.   }elseif ((isset($type)) && ($type =='rss')) {
  98.   // Import Package Syntax
  99.   include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_RSS);
  100.       $parser new RssParser();
  101.   }else {
  102.    // create a parser according to the suffix of the filename
  103.    // if there is no suffix assume the file to be XML/RDF
  104.    preg_match("/\.([a-zA-Z0-9_]+)$/"$filename$suffix);
  105.    if (isset($suffix[1]&& (strtolower($suffix[1]== 'n3' OR strtolower($suffix[1]== 'nt')){
  106.       // Import Package Syntax
  107.       include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_N3);
  108.          $parser new N3Parser();
  109.    }elseif (isset($suffix[1]&& (strtolower($suffix[1]== 'htm' OR strtolower($suffix[1]== 'html' OR strtolower($suffix[1]== 'xhtml')){
  110.            include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_GRDDL);
  111.           $parser new GRDDLParser();
  112.    }else{
  113.       // Import Package Syntax
  114.       include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_RDF);
  115.       $parser new RdfParser();}
  116.    };
  117.  
  118.    if(($stream && $type=='rdf')||($stream && $type=='n3'))
  119.            $temp=&$parser->generateModel($filename,false,$this);
  120.    else{
  121.            $temp=&$parser->generateModel($filename);
  122.    }
  123.    $this->addModel($temp);
  124.    if($this->getBaseURI()== null)
  125.    $this->setBaseURI($temp->getBaseURI());
  126.  
  127.  }
  128.  
  129.  
  130.  
  131.  
  132. /**
  133.  * Adds a statement from another model to this model.
  134.  * If the statement to be added contains a blankNode with an identifier
  135.  * already existing in this model, a new blankNode is generated.
  136.  *
  137.  * @param     Object Statement   $statement
  138.  * @access    private
  139.  */
  140.  function _addStatementFromAnotherModel($statement&$blankNodes_tmp{
  141.  
  142.    $subject $statement->getSubject();
  143.    $object $statement->getObject();
  144.  
  145.    if (is_a($subject"BlankNode")) {
  146.       $label $subject->getLabel();
  147.          if (!array_key_exists($label$blankNodes_tmp))
  148.       {
  149.         if ($this->findFirstMatchingStatement($subjectNULLNULL)
  150.            || $this->findFirstMatchingStatement(NULLNULL$subject))
  151.         {
  152.               $blankNodes_tmp[$labelnew BlankNode($this);
  153.               $statement->subj $blankNodes_tmp[$label];
  154.         else {
  155.            $blankNodes_tmp[$label$subject;
  156.         }
  157.       else
  158.            $statement->subj $blankNodes_tmp[$label];
  159.    }
  160.  
  161.    if (is_a($object"BlankNode")) {
  162.       $label $object->getLabel();
  163.       if (!array_key_exists($label$blankNodes_tmp))
  164.       {
  165.         if ($this->findFirstMatchingStatement($objectNULLNULL)
  166.            || $this->findFirstMatchingStatement(NULLNULL$object))
  167.         {
  168.               $blankNodes_tmp[$labelnew BlankNode($this);
  169.               $statement->obj $blankNodes_tmp[$label];
  170.         else {
  171.            $blankNodes_tmp[$label$object;
  172.         }
  173.       else
  174.              $statement->obj $blankNodes_tmp[$label];
  175.    }
  176.    $this->add($statement);
  177.  }
  178.  
  179.  
  180.  
  181.  /**
  182.  * Internal method, that returns a resource URI that is unique for the Model.
  183.  * URIs are generated using the base_uri of the DbModel, the prefix and a unique number.
  184.  * If no prefix is defined, the bNode prefix, defined in constants.php, is used.
  185.  *
  186.  * @param    string    $prefix 
  187.  * @return    string 
  188.  * @access    private
  189.  */
  190.  function getUniqueResourceURI($prefix false)
  191.  {
  192.      static $bNodeCount;
  193.      if(!$bNodeCount)
  194.          $bNodeCount 0;
  195.  
  196.      if(!$prefix)
  197.          $prefix=BNODE_PREFIX;
  198.  
  199.     return $prefix.++$bNodeCount;
  200.  }
  201.  
  202.  /**
  203.  * Returns a ResModel with this model as baseModel. This is the same as
  204.  * ModelFactory::getResModelForBaseModel($this).
  205.  *
  206.  * @return    object    ResModel 
  207.  * @access    public
  208.  */
  209.  function getResModel()
  210.  {
  211.      return ModelFactory::getResModelForBaseModel($this);
  212.  }
  213.  
  214.  /**
  215.  * Returns an OntModel with this model as baseModel.
  216.  * $vocabulary has to be one of the following constants (currently only one is supported):
  217.  * RDFS_VOCABULARY to select a RDFS Vocabulary.
  218.  *
  219.  * This is the same as ModelFactory::getOntModelForBaseModel($this, $vocabulary).
  220.  *
  221.  * @param   constant  $vocabulary 
  222.  * @return    object    OntModel 
  223.  * @access    public
  224.  */
  225.  function getOntModel($vocabulary)
  226.  {
  227.     return ModelFactory::getOntModelForBaseModel($this$vocabulary);
  228.  }
  229.  
  230.  
  231.  /**
  232.  * Searches for triples using find() and tracks forward blank nodes
  233.  * until the final objects in the retrieved subgraphs are all named resources.
  234.  * The method calls itself recursivly until the result is complete.
  235.  * NULL input for subject, predicate or object will match anything.
  236.  * Inputparameters are ignored for recursivly found statements.
  237.  * Returns a new MemModel or adds (without checking for duplicates)
  238.  * the found statements to a given MemModel.
  239.  * Returns an empty MemModel, if nothing is found.
  240.  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  241.  * WARNING: This method can be slow with large models.
  242.  * NOTE:    Blank nodes are not renamed, they keep the same nodeIDs
  243.  *          as in the queried model!
  244.  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  245.  *
  246.  * @author   Anton Köstlbacher <anton1@koestlbacher.de>
  247.  * @param    object Node     $subject 
  248.  * @param    object Node     $predicate 
  249.  * @param    object Node     $object 
  250.  * @param    object MemModel $object 
  251.  * @return   object MemModel 
  252.  * @access   public
  253.  * @throws   PhpError
  254.  */
  255.  
  256.  function findForward($subject$predicate$object$newModel NULL)
  257.  {
  258.      if (!is_a($newModel"MemModel"))
  259.      {
  260.          $newModel New MemModel;
  261.      }
  262.  
  263.      if (is_a($this"DbModel"))
  264.      {
  265.          $model $this;
  266.          $res   $model->find($subject$predicate$object);
  267.          $it    $res->getStatementIterator();
  268.      }
  269.      elseif (is_a($this"MemModel")) {
  270.          $model $this;
  271.          $it    $model->findAsIterator($subject$predicate$object);
  272.      }
  273.      elseif (is_a($this"ResModel")) {
  274.          $model $this->model;
  275.          $it    $model->findAsIterator($subject$predicate$object);
  276.      }
  277.  
  278.      while ($it->hasNext())
  279.      {
  280.          $statement $it->next();
  281.          $newModel->add($statement);
  282.          if (is_a($statement->object(),'BlankNode'))
  283.          {
  284.              $model->findForward($statement->object()NULLNULL,&$newModel);
  285.          }
  286.      }
  287.      return $newModel;
  288.  }
  289.  
  290.  
  291. /**
  292.  * Perform an RDQL query on this Model. Should work with all types of models.
  293.  * This method returns a MemModel containing the result statements.
  294.  * If $closure is set to TRUE, the result will additionally contain
  295.  * statements found by the findForward-method for blank nodes.
  296.  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  297.  * WARNING: If called with $closure = TRUE this method
  298.  *          can be slow with large models.
  299.  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  300.  *
  301.  * @author   Anton Köstlbacher <anton1@koestlbacher.de>
  302.  * @author   code snippets taken from the RAP Netapi by Phil Dawes and Chris Bizer
  303.  * @access   public
  304.  * @param    string $queryString 
  305.  * @param    boolean $closure 
  306.  * @return   object MemModel 
  307.  *
  308.  */
  309.  
  310.  function getMemModelByRDQL($queryString$closure FALSE)
  311.  {
  312.      require_once(RDFAPI_INCLUDE_DIR.PACKAGE_RDQL);
  313.      $parser new RdqlParser();
  314.      $parsedQuery =$parser->parseQuery($queryString);
  315.  
  316.      // If there are variables used in the pattern but not
  317.      // in the select clause, add them to the select clause
  318.      foreach ($parsedQuery['patterns'as $n => $pattern)
  319.      {
  320.          foreach ($pattern as $key => $val_1)
  321.          {
  322.              if ($val_1['value']{0}=='?')
  323.              {
  324.                  if (!in_array($val_1['value'],$parsedQuery['selectVars']))
  325.                  {
  326.                  array_push($parsedQuery['selectVars'],$val_1['value']);
  327.                  }
  328.              }
  329.          }
  330.      }
  331.  
  332.      if (is_a($this"DbModel"))
  333.      {
  334.          $engine new RdqlDbEngine();
  335.          $model  $this;
  336.      }
  337.      elseif (is_a($this"MemModel"))
  338.      {
  339.          $engine new RdqlMemEngine();
  340.          $model  $this;
  341.      }
  342.      elseif (is_a($this"ResModel"))
  343.      {
  344.          $engine new RdqlMemEngine();
  345.          $model  $this->model;
  346.      }
  347.  
  348.      $res $engine->queryModel($model,$parsedQuery,TRUE);
  349.      $rdqlIter new RdqlResultIterator($res);
  350.      $newModel new MemModel();
  351.  
  352.      // Build statements from RdqlResultIterator
  353.     while ($rdqlIter->hasNext()) {
  354.         $result $rdqlIter->next();
  355.         foreach ($parsedQuery['patterns'as $n => $pattern)
  356.         {
  357.             if (substr($pattern['subject']['value']01== '?')
  358.             {
  359.                 $subj $result[$pattern['subject']['value']];
  360.             }
  361.             else
  362.             {
  363.                 $subj new Resource($pattern['subject']['value']);
  364.             }
  365.             if (substr($pattern['predicate']['value']01== '?')
  366.             {
  367.                 $pred $result[$pattern['predicate']['value']];
  368.             }
  369.             else
  370.             {
  371.                 $pred new Resource($pattern['predicate']['value']);
  372.             }
  373.  
  374.             if (substr($pattern['object']['value']01== '?')
  375.             {
  376.                 $obj $result[$pattern['object']['value']];
  377.             }
  378.             else
  379.             {
  380.                  if (isset($pattern['object']['is_literal']))
  381.                  {
  382.                      $obj new Literal($pattern['object']['value']);
  383.                      $obj->setDatatype($pattern['object']['l_dtype']);
  384.                      $obj->setLanguage($pattern['object']['l_lang']);
  385.                  }
  386.                  else
  387.                  {
  388.                      $obj new Resource($pattern['object']['value']);
  389.                  }
  390.             }
  391.  
  392.             $statement new Statement($subj,$pred,$obj);
  393.             $newModel->add($statement);
  394.  
  395.             // findForward() Statements containing an eventually given blank node
  396.             // and add them to the result, if closure = true
  397.             if (is_a($statement->object(),'BlankNode'&& $closure == True)
  398.             {
  399.                 $newModel $model->findForward($statement->object(),NULL,NULL,&$newModel);
  400.             }
  401.             if (is_a($statement->subject(),'BlankNode'&& $closure == True)
  402.             {
  403.                 $newModel $model->findForward($statement->subject(),NULL,NULL,&$newModel);
  404.             }
  405.         }
  406.     }
  407.     return $newModel;
  408.  }
  409.  
  410.  /**
  411.  * Alias for RDFUtil::visualiseGraph(&$model, $format, $short_prefix)
  412.  *
  413.  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  414.  * Note: See RDFUtil for further Information.
  415.  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  416.  *
  417.  * @author   Anton Köstlbacher <anton1@koestlbacher.de>
  418.  * @param    string  $format 
  419.  * @param    boolean $short_prefix 
  420.  * @return   string, binary
  421.  * @access   public
  422.  * @throws   PhpError
  423.  */
  424.  
  425.  function visualize($format "dot"$short_prefix TRUE)
  426.  {
  427.      return RDFUtil::visualizeGraph($this$format$short_prefix);
  428.  }
  429.  
  430.  
  431.   /**
  432.  * Performs a SPARQL query against a model. The model is converted to
  433.  * an RDF Dataset. The result can be retrived in SPARQL Query Results XML Format or
  434.  * as an array containing the variables an their bindings.
  435.  *
  436.  * @param  String $query      the sparql query string
  437.  * @param  String $resultform the result form ('xml' for SPARQL Query Results XML Format)
  438.  * @return String/array 
  439.  */
  440.  function sparqlQuery($query,$resultform false){
  441.      include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SPARQL);
  442.      include_once(RDFAPI_INCLUDE_DIR.PACKAGE_DATASET);     
  443.      $dataset new DatasetMem();
  444.      $dataset->setDefaultGraph($this);
  445.      $parser new SparqlParser();
  446.      $q $parser->parse($query);
  447.      
  448.      $eng new SparqlEngine();
  449.      return $eng->queryModel($dataset,$q,$resultform);
  450.  }
  451.  
  452.   
  453.  
  454. // end: Model
  455.  
  456. ?>

Documentation generated on Mon, 26 Jun 2006 14:25:38 +0200 by phpDocumentor 1.3.0RC6