Source for file DatasetMem.php

Documentation is available at DatasetMem.php

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: DatasetMem
  4. // ----------------------------------------------------------------------------------
  5.  
  6. /**
  7. * In-memory implementation of a RDF dataset.
  8. * A RDF dataset set is a collection of named RDF graphs.
  9. *
  10. @version  $Id: fsource_dataset__datasetDatasetMem.php.html,v 1.8 2006/06/26 12:34:12 tgauss Exp $
  11. @author Daniel Westphal (http://www.d-westphal.de)
  12. @author Chris Bizer <chris@bizer.de>
  13. *
  14. @package     dataset
  15. @access    public
  16. ***/
  17.  
  18. class DatasetMem extends Dataset 
  19. {
  20.     
  21.     /**
  22.     * Reference to a Memmodel that holds the default graph.
  23.     *
  24.     * @var        resource Memmodel
  25.     * @access    private
  26.     */
  27.     var $defaultGraph;
  28.     
  29.     
  30.     /**
  31.     * Name of the DatasetMem.
  32.     *
  33.     * @var        string 
  34.     * @access    private
  35.     */
  36.     var $setName;
  37.     
  38.     /**
  39.     * List of all NamedGraphs.
  40.     *
  41.     * @var        array 
  42.     * @access    private
  43.     */
  44.     var $graphs=array();
  45.     
  46.     /**
  47.     * Constructor.
  48.     * You can supply a Dataset name.
  49.     *
  50.     * @param string 
  51.     * @access    public
  52.     */        
  53.     function DatasetMem($datasetName null)
  54.     {
  55.         $this->setDatasetName($datasetName);
  56.         $this->defaultGraph=new MemModel();        
  57.     }
  58.     
  59. //    === Graph level methods ========================
  60.     
  61.         
  62.     /**
  63.     * Sets the Datasets name.
  64.     *
  65.     * @param  string 
  66.     * @access    public
  67.     */    
  68.     function setDatasetName($datasetName)
  69.     {
  70.         $this->setName=$datasetName;
  71.     }
  72.     
  73.     /**
  74.     * Returns the Datasets name.
  75.     *
  76.     * @return string 
  77.     * @access    public
  78.     */
  79.     function getDatasetName()
  80.     {
  81.         return $this->setName;
  82.     }
  83.     
  84.     /**
  85.      * Adds a NamedGraph to the set. Will replace a NamedGraph with the same name that is already in the set.
  86.      *
  87.      * @param NamedGraphMem 
  88.      */
  89.     function addNamedGraph(&$graph)
  90.     {
  91.         $graphNameURI=$graph->getGraphName();
  92.         $this->graphs[$graphNameURI]=&$graph;
  93.     }
  94.     
  95.     /**
  96.      * Overwrites the existting default graph.
  97.      * 
  98.      * @param MemModel 
  99.      */
  100.     function setDefaultGraph(&$graph)
  101.     {
  102.         $this->defaultGraph=&$graph;
  103.     }
  104.     
  105.     /**
  106.      * Returns a reference to the defaultGraph
  107.      * @return Model 
  108.     */
  109.     function &getDefaultGraph()
  110.     {
  111.         return ($this->defaultGraph);
  112.     }
  113.     
  114.     /**
  115.      * Returns true, if an defaultGraph exists. False otherwise    .
  116.      * 
  117.      * @return boolean 
  118.     */    
  119.     function hasDefaultGraph()
  120.     {
  121.         return ($this->defaultGraph!=null);
  122.     }
  123.     
  124.     /**
  125.      * Removes a NamedGraph from the set. Nothing happens
  126.      * if no graph with that name is contained in the set.
  127.      *
  128.      * @param string 
  129.      */
  130.     function removeNamedGraph($graphName)
  131.     {
  132.         unset($this->graphs[$graphName]);
  133.     }
  134.     
  135.     /**
  136.      * Tells wether the Dataset contains a NamedGraph.
  137.      *
  138.      * @param  string 
  139.      * @return boolean 
  140.      */
  141.     function containsNamedGraph($graphName)
  142.     {
  143.         return (isset($this->graphs[$graphName])===true);    
  144.     }
  145.     
  146.     /**
  147.      * Returns the NamedGraph with a specific name from the Dataset.
  148.      * Changes to the graph will be reflected in the set.
  149.      *
  150.      * @param string 
  151.      * @return NamedGraphMem or NULL
  152.      */
  153.     function &getNamedGraph($graphName)
  154.     {
  155.         if (!isset($this->graphs[$graphName])) return NULL;
  156.         return ($this->graphs[$graphName]);
  157.     }
  158.     
  159.     /**
  160.      * Returns the names of the namedGraphs in this set as strings in an array.
  161.      *
  162.      * @return Array 
  163.      */
  164.     function listGraphNames()
  165.     {
  166.         return array_keys($this->graphs);
  167.     }
  168.     
  169.     /**
  170.      * Creates a new NamedGraph and adds it to the set. An existing
  171.      * graph with the same name will be replaced.The name of the NamedGraph to be created ; must be an URI
  172.      *
  173.      * @param  string 
  174.      * @param  string 
  175.      * @return NamedGraphMem 
  176.      */
  177.     function &createGraph($graphName,$baseURI null)
  178.     {
  179.         $this->graphs[$graphName]=new NamedGraphMem($graphName,$baseURI);
  180.         return $this->getNamedGraph($graphName);
  181.     }
  182.     
  183.     /**
  184.      * Deletes all NamedGraphs from the set.
  185.      */
  186.     function clear()
  187.     {
  188.         $this->graphs = array();    
  189.     }
  190.  
  191.     /** 
  192.      * Returns the number of NamedGraphs in the set. Empty graphs
  193.      * are counted.
  194.      *
  195.      * @return int 
  196.      */
  197.     function countGraphs(
  198.     {
  199.         return count($this->graphs);    
  200.     }
  201.     
  202.     /**
  203.      * Returns the NamedGraph with a specific offset in the dataset.
  204.      * Changes to the graph will be reflected in the set.
  205.      *
  206.      * @param int 
  207.      * @return NamedGraphMem or null
  208.      * @access    private
  209.      */
  210.     function &getGraphWithOffset($offset)
  211.     {
  212.         $i=0;
  213.         foreach ($this->graphs as $graph)
  214.         {
  215.             if (($i++)==$offset)
  216.                 return $graph;
  217.         }
  218.         return NULL;
  219.     }
  220.     
  221.     /**
  222.      * Returns an iterator over all {@link NamedGraph}s in the set.
  223.      *
  224.      * @return IteratorAllGraphsMem 
  225.      */
  226.     function &listNamedGraphs()
  227.     {
  228.             return (new IteratorAllGraphsMem($this));
  229.     }
  230.     
  231.     /**
  232.      * Tells wether the set contains any NamedGraphs.
  233.      *
  234.      * @return boolean 
  235.      */
  236.     function isEmpty()
  237.     {
  238.         return ($this->countGraphs()==0);    
  239.     }
  240.     
  241.     /**
  242.      * Adds all named graphs of the other dataset to this dataset.
  243.      *
  244.      * @param Dataset 
  245.      */
  246.     function addAll($otherDataset)
  247.     {
  248.         for($iterator $otherDataset->listNamedGraphs()$iterator->valid()$iterator->next()) 
  249.         {
  250.             $current=$iterator->current();
  251.             $this->graphs[$current->getGraphName()]=$current;
  252.          };
  253.          
  254.          if ($otherDataset->hasDefaultGraph())
  255.          {
  256.              $this->defaultGraph = $this->defaultGraph->unite($otherDataset->getDefaultGraph());    
  257.          }
  258.     }
  259.     
  260. //    === Quad level methods ========================
  261.  
  262.     
  263.     /**
  264.      * Adds a quad to the Dataset. The argument must not contain any
  265.      * wildcards. If the quad is already present, nothing happens. A new
  266.      * named graph will automatically be created if necessary.
  267.      *
  268.      * @param Quad 
  269.      */
  270.     function addQuad(&$quad)
  271.     {
  272.         $graphName=$quad->getGraphName();
  273.         if ($this->containsNamedGraph($graphName->getLabel())===false)
  274.             $this->createGraph($graphName->getLabel());
  275.             
  276.         $this->graphs[$graphName->getLabel()]->add($quad->getStatement());
  277.     }
  278.  
  279.     
  280.     /**
  281.      * Tells wether the Dataset contains a quad or
  282.      * quads matching a pattern.
  283.      * 
  284.      * @param Resource $graphName 
  285.      * @param Resource $subject 
  286.      * @param Resource $predicate 
  287.      * @param Resource $object 
  288.      * @return boolean 
  289.      */
  290.     function containsQuad($graphName,$subject,$predicate,$object)
  291.     {
  292.         if($graphName!=null)
  293.         {
  294.             if ($this->containsNamedGraph($graphName->getLabel())!==true)
  295.                 return false;
  296.                 
  297.             return ($this->graphs[$graphName->getLabel()]->findFirstMatchingStatement($subject,$predicate,$object)!=null);    
  298.         }
  299.         
  300.         foreach ($this->graphs as $graph)
  301.         {
  302.             if ($graph->findFirstMatchingStatement($subject,$predicate,$object)!=null)
  303.                 return true;    
  304.         };
  305.         
  306.         return false;
  307.     
  308.     
  309.     /**
  310.      * Deletes a Quad from the RDF dataset.
  311.      *
  312.      * @param Quad 
  313.      */
  314.     function removeQuad($quad)
  315.     {
  316.             $graphName=$quad->getGraphName();
  317.         
  318.             if($graphName!=null)
  319.             {
  320.                 if ($this->containsNamedGraph($graphName->getLabel())!==true)
  321.                     return;
  322.                     
  323.                 return ($this->graphs[$graphName->getLabel()]->remove($quad->getStatement())!=null);
  324.                     
  325.             }    
  326.             foreach ($this->graphs as $graph)
  327.             {
  328.                 $graph->remove($quad->getStatement());    
  329.             };
  330.     }
  331.     
  332.     /**
  333.      * Counts the Quads in the RDF dataset. Identical Triples in
  334.      * different NamedGraphs are counted individually.
  335.      *
  336.      * @return int 
  337.      */
  338.     function countQuads()
  339.     {
  340.         $count=0;
  341.         foreach ($this->graphs as $graph)
  342.         {
  343.             $count+=$graph->size();    
  344.         }
  345.         return $count;    
  346.     }
  347.     
  348.     /**
  349.      * Finds Statements that match a quad pattern. The argument may contain
  350.      * wildcards.
  351.      *
  352.      * @param Resource or Null
  353.      * @param Resourceor Null
  354.      * @param Resource or Null
  355.      * @param Resource or Null
  356.      * @return Iterator 
  357.      */
  358.     function &findInNamedGraphs($graph,$subject,$predicate,$object,$returnAsTriples false)
  359.     {
  360.  
  361.         if ($graph!=null)
  362.         {
  363.             $findGraph=&$this->getNamedGraph($graph->getLabel());
  364.             if($findGraph==null)
  365.                 $findGraph=new MemModel();
  366.  
  367.             return $findGraph->iterFind($subject,$predicate,$object);    
  368.         }
  369.  
  370.             
  371.         return new IteratorFindQuadsMem($subject,$predicate,$object,$this->listNamedGraphs(),$returnAsTriples);
  372.     }
  373.     
  374.     /**
  375.      * Finds Statements that match a pattern in the default Graph. The argument may contain
  376.      * wildcards.
  377.      *
  378.      * @param Resource or Null
  379.      * @param Resource or Null
  380.      * @param Resource or Null
  381.      * @return Iterator 
  382.      */
  383.     function &findInDefaultGraph($subject,$predicate,$object)
  384.     {
  385.         return $this->defaultGraph->iterFind($subject,$predicate,$object);
  386.     }
  387. }
  388. ?>

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