Source for file Query.php

Documentation is available at Query.php

  1. <?php
  2. require_once RDFAPI_INCLUDE_DIR 'sparql/GraphPattern.php';
  3.  
  4. /**
  5. * The Class Query represents a SPARQL query.
  6. *
  7. *
  8. @author   Tobias Gauss <tobias.gauss@web.de>
  9. @version     $Id: fsource_sparql__sparqlQuery.php.html 443 2007-06-01 16:25:38Z cax $
  10. *
  11. @package sparql
  12. */
  13. class Query extends Object
  14. {
  15.     /**
  16.     * The BASE part of the SPARQL query.
  17.     * @var string 
  18.     */
  19.     protected $base;
  20.  
  21.     /**
  22.     * Array that contains used prefixes and namespaces.
  23.     * Key is the prefix, value the namespace.
  24.     *
  25.     * @example
  26.     *  array(8) {
  27.     *   ["rdf"] => string(43) "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  28.     *   ["rdfs"]=> string(37) "http://www.w3.org/2000/01/rdf-schema#"
  29.     *  }
  30.     *
  31.     * @var array 
  32.     */
  33.     public $prefixes = array();
  34.  
  35.     /**
  36.     * Array of result variables that shall be returned.
  37.     * E.g. "?name", "?mbox"
  38.     *
  39.     * @var array 
  40.     */
  41.     protected $resultVars = array();
  42.  
  43.     /**
  44.     * What form/type of result should be returned.
  45.     *
  46.     * One of:
  47.     * - "ask"
  48.     * - "count"
  49.     * - "construct"
  50.     * - "describe"
  51.     * - "select"
  52.     * - "select distinct"
  53.     *
  54.     * @var string 
  55.     * @see http://www.w3.org/TR/rdf-sparql-query/#QueryForms
  56.     */
  57.     protected $resultForm;
  58.  
  59.     /**
  60.     * Contains the result part of the SPARQL query.
  61.     * Array of GraphPattern
  62.     *
  63.     * @var array 
  64.     */
  65.     protected $resultPart;
  66.  
  67.     /**
  68.     *  Contains the FROM part of the SPARQL query.
  69.     * @var array 
  70.     */
  71.     protected $fromPart = array();
  72.  
  73.     /**
  74.     * Contains the FROM NAMED part of the SPARQL query.
  75.     * @var array 
  76.     */
  77.     protected $fromNamedPart = array();
  78.  
  79.     /**
  80.     * Optional solution modifier of the query.
  81.     * Array with three keys:
  82.     *  "order by", "limit", "offset"
  83.     * If they are not set, their value is null
  84.     *
  85.     * "order by" can be an array with subarrays, each of those
  86.     *  subarrays having two keys: "val" and "type".
  87.     *  "val" determines the variable ("?mbox"), "type" is
  88.     *  "asc" or "desc"
  89.     *
  90.     * @var array 
  91.     */
  92.     protected $solutionModifier = array();
  93.  
  94.     /**
  95.     *  Blanknode counter.
  96.     * How many blank nodes are in $resultPart
  97.     *
  98.     * @var int 
  99.     */
  100.     protected $bnodeCounter;
  101.  
  102.     /**
  103.     * GraphPattern counter.
  104.     * How many GraphPattern are in $resultPart
  105.     *
  106.     * @var int 
  107.     */
  108.     public $graphPatternCounter;
  109.  
  110.     /**
  111.     * List of all vars used in the query.
  112.     * Key is the variable (e.g. "?x"), value
  113.     * is boolean true
  114.     *
  115.     * @var array 
  116.     */
  117.     public $usedVars;
  118.  
  119.     /**
  120.     * If the query type is CONSTRUCT this variable contains the
  121.     * CONSTRUCT graph pattern.
  122.     */
  123.     protected $constructPattern;
  124.  
  125.     /**
  126.     * TRUE if the query is empty FALSE if not.
  127.     *
  128.     * @var boolean 
  129.     */
  130.     public $isEmpty;
  131.  
  132.     /**
  133.     *   Language of variables. NULL if the variable has no
  134.     *   language tag (e.g. @en) set.
  135.     *   $varname => $language tag
  136.     *   @var array 
  137.     */
  138.     public $varLanguages = array();
  139.  
  140.     /**
  141.     *   Datatype of variables. NULL if the variable has no
  142.     *   data type (e.g. ^^xsd::integer) set.
  143.     *   $varname => $datatype
  144.     *   @var array 
  145.     */
  146.     public $varDatatypes = array();
  147.  
  148.  
  149.  
  150.     /**
  151.     * Constructor
  152.     */
  153.     public function Query(){
  154.         $this->resultForm = false;
  155.         $this->solutionModifier['order by'null;
  156.         $this->solutionModifier['limit']    null;
  157.         $this->solutionModifier['offset']   null;
  158.         $this->bnodeCounter = 0;
  159.         $this->graphPatternCounter = 0;
  160.  
  161.     }
  162.  
  163.     /**
  164.     * Returns the BASE part of the query.
  165.     *
  166.     * @return String 
  167.     */
  168.     public function getBase(){
  169.         return $this->base;
  170.     }
  171.  
  172.     /**
  173.     * Returns the prefix map of the query.
  174.     *
  175.     * @return Array 
  176.     */
  177.     public function getPrefixes(){
  178.         return $this->prefixes;
  179.     }
  180.  
  181.     /**
  182.     * Returns a list containing the result vars.
  183.     *
  184.     * @return Array 
  185.     */
  186.     public function getResultVars(){
  187.         return $this->resultVars;
  188.     }
  189.  
  190.     /**
  191.     * Returns the type the result shall have.
  192.     * E.g. "select", "select distinct", "ask", ...
  193.     *
  194.     * @see $resultForm
  195.     *
  196.     * @return string 
  197.     */
  198.     public function getResultForm(){
  199.         return $this->resultForm;
  200.     }
  201.  
  202.     /**
  203.     * Returns a list containing the graph patterns of the query.
  204.     *
  205.     * @return Array 
  206.     */
  207.     public function getResultPart(){
  208.         return $this->resultPart;
  209.     }
  210.  
  211.     /**
  212.     * Returns the FROM clause of the query.
  213.     *
  214.     * @return String 
  215.     */
  216.     public function getFromPart(){
  217.         return $this->fromPart;
  218.     }
  219.  
  220.     /**
  221.     * Returns the FROM NAMED clause of the query.
  222.     *
  223.     * @return Array 
  224.     */
  225.     public function getFromNamedPart(){
  226.         return $this->fromNamedPart;
  227.     }
  228.  
  229.     /**
  230.     * Returns $isEmpty variable
  231.     * @return boolean 
  232.     */
  233.     public function isEmpty({
  234.         return $this->isEmpty;
  235.     }
  236.  
  237.     /**
  238.     * Returns an unused Bnode label.
  239.     *
  240.     * @return String 
  241.     */
  242.     public function getBlanknodeLabel(){
  243.         return "_:bN".$this->bnodeCounter++;
  244.     }
  245.  
  246.  
  247.     /**
  248.     * Sets the base part.
  249.     *
  250.     * @param String $base 
  251.     * @return void 
  252.     */
  253.     public function setBase($base){
  254.         $this->base = $base;
  255.     }
  256.  
  257.  
  258.     /**
  259.     * Adds a prefix to the list of prefixes.
  260.     *
  261.     * @param  String $prefix 
  262.     * @param  String $label 
  263.     * @return void 
  264.     */
  265.     public function addPrefix($prefix$label){
  266.         $this->prefixes[$prefix]$label;
  267.     }
  268.  
  269.     /**
  270.     * Adds a variable to the list of result variables.
  271.     *
  272.     * @param  String $var 
  273.     * @return void 
  274.     */
  275.     public function addVariable($var){
  276.         $this->resultVars[]$var;
  277.         $this->varLanguages[$varself::getLanguageTag($var);
  278.         $this->varDatatypes[$var$this->getDatatype($var);
  279.     }
  280.  
  281.  
  282.     /**
  283.     * Sets the result form.
  284.     *
  285.     * @param  String $form 
  286.     * @return void 
  287.     */
  288.     public function setResultForm($form){
  289.         $this->resultForm = strtolower($form);
  290.     }
  291.  
  292.     /**
  293.     * Adds a graph pattern to the result part.
  294.     *
  295.     * @param  GraphPattern $pattern 
  296.     * @return void 
  297.     */
  298.     public function addGraphPattern($pattern){
  299.         $pattern->setId($this->graphPatternCounter);
  300.         $this->resultPart[$pattern;
  301.         $this->graphPatternCounter++;
  302.     }
  303.  
  304.     /**
  305.     * Adds a construct graph pattern to the query.
  306.     *
  307.     * @param  GraphPattern $pattern 
  308.     * @return void 
  309.     */
  310.     public function addConstructGraphPattern($pattern){
  311.         $this->constructPattern = $pattern;
  312.     }
  313.  
  314.  
  315.     /**
  316.     * Adds a graphuri to the from part.
  317.     *
  318.     * @param  String $graphURI 
  319.     * @return void 
  320.     */
  321.     public function addFrom($graphURI){
  322.         $this->fromPart = $graphURI;
  323.     }
  324.  
  325.     /**
  326.     * Adds a graphuri to the from named part.
  327.     *
  328.     * @param  String $graphURI 
  329.     * @return void 
  330.     */
  331.     public function addFromNamed($graphURI){
  332.         $this->fromNamedPart[$graphURI;
  333.     }
  334.  
  335.     /**
  336.     * Sets a solution modifier.
  337.     *
  338.     * @param  String $name 
  339.     * @param  Value  $value 
  340.     * @return void 
  341.     */
  342.     public function setSolutionModifier($name$value){
  343.         $this->solutionModifier[$name$value;
  344.     }
  345.  
  346.  
  347.     /**
  348.     * Generates a new GraphPattern. If it is a CONSTRUCT graph pattern
  349.     * $constr has to set to TRUE, FALSE if not.
  350.     *
  351.     * @param  boolean $constr 
  352.     * @return GraphPattern 
  353.     */
  354.     public function getNewPattern($constr false){
  355.         $pattern new GraphPattern();
  356.         if ($constr{
  357.             $this->addConstructGraphPattern($pattern);
  358.         else {
  359.             $this->addGraphPattern($pattern);
  360.         }
  361.         return $pattern;
  362.     }
  363.  
  364.     /**
  365.     * Adds a new variable to the variable list.
  366.     *
  367.     * @param  String $var 
  368.     * @return void 
  369.     */
  370.     public function addVar($var){
  371.         $this->usedVars[$var]=true;
  372.     }
  373.  
  374.     /**
  375.     * Returns a list with all used variables.
  376.     *
  377.     * @return Array 
  378.     */
  379.     public function getAllVars(){
  380.         return array_keys($this->usedVars);
  381.     }
  382.  
  383.     /**
  384.     * Gets the solution modifiers of the query.
  385.     * $solutionModifier['order by'] = value
  386.     *                  ['limit']    = vlaue
  387.     *                  ['offset']   = value
  388.     *
  389.     *
  390.     * @return Array 
  391.     */
  392.     public function getSolutionModifier(){
  393.         return $this->solutionModifier;
  394.     }
  395.  
  396.  
  397.     /**
  398.     * Returns the construvtGraphPattern of the query if there is one.
  399.     *
  400.     * @return GraphPattern 
  401.     */
  402.     public function getConstructPattern(){
  403.         return $this->constructPattern;
  404.     }
  405.  
  406.  
  407.  
  408.     /**
  409.     *   Returns a list of variables used in the construct patterns.
  410.     *
  411.     *   @return array Array of variable names, unique.
  412.     */
  413.     public function getConstructPatternVariables()
  414.     {
  415.         $arVars array();
  416. /*
  417.         if (count($this->resultPart) > 0) {
  418.             foreach ($this->resultPart as $pattern) {
  419.                 $arVars = array_merge($arVars, $pattern->getVariables());
  420.             }
  421.         }
  422. */
  423.         if ($this->constructPattern{
  424.             $arVars array_merge($arVars$this->constructPattern->getVariables());
  425.         }
  426.  
  427.         return array_unique($arVars);
  428.     }//public function getConstructPatternVariables()
  429.  
  430.  
  431.  
  432.     /**
  433.     *   Returns the language of a variable if the tag is set (e.g. @en)
  434.     *   Returns NULL if no language is set.
  435.     *
  436.     *   @param string Sparql variable name
  437.     *   @return mixed NULL or language tag
  438.     */
  439.     public static function getLanguageTag($var)
  440.     {
  441.         $nAt strpos($var'@');
  442.         if ($nAt === false{
  443.             return null;
  444.         }
  445.         //in case @ and ^^ are combined
  446.         $nHatHat strpos($var'^^'$nAt 1);
  447.         if ($nHatHat === false{
  448.             $tag substr($var$nAt 1);
  449.         else {
  450.             $tag substr($var$nAt 1$nHatHat $nAt 1);
  451.         }
  452.         return $tag;
  453.     }//public static function getLanguageTag($var)
  454.  
  455.  
  456.  
  457.     /**
  458.     *   Returns the datatype of a variable if it is set.
  459.     *
  460.     *   @param string Sparql variable name
  461.     *   @return mixed NULL or datatype
  462.     */
  463.     public function getDatatype($var)
  464.     {
  465.         $nHatHat strpos($var'^^');
  466.         if ($nHatHat === false{
  467.             return null;
  468.         }
  469.         $nAt strpos($var'@'$nHatHat 2);
  470.         if ($nAt === false{
  471.             $type substr($var$nHatHat 2);
  472.         else {
  473.             $type substr($var$nHatHat 2$nAt $nHatHat 2);
  474.         }
  475.  
  476.         $fullUri $this->getFullUri($type);
  477.         if ($fullUri === false{
  478.             $fullUri $type;
  479.             if ($fullUri[0== '<' && substr($fullUri-1== '>'{
  480.                 $fullUri substr($fullUri1-1);
  481.             }
  482.         }
  483.  
  484.         return $fullUri;
  485.     }//public function getDatatype($var)
  486.  
  487.  
  488.  
  489.     /**
  490.     * Gets the full URI of a qname token.
  491.     *
  492.     * @param  string $token 
  493.     * @return string The complete URI of a given token, false if $token is not
  494.     *                 a qname or the prefix is not defined
  495.     */
  496.     public function getFullUri($token)
  497.     {
  498.         $pattern="/^([^:]*):([^:]*)$/";
  499.         if (preg_match($pattern$token$hits0{
  500.  
  501.             if (isset($this->prefixes{$hits{1}})) {
  502.                 return substr($this->base1-1)
  503.                      . $this->prefixes{$hits{1}}
  504.                      . $hits{2};
  505.             }
  506.             if ($hits{1}=='_'{
  507.                 return "_".$hits{2};
  508.             }
  509.         }
  510.  
  511.         return false;
  512.     }
  513.  
  514. }
  515. // end class: Query.php
  516.  
  517. ?>

Documentation generated on Fri, 1 Jun 2007 16:50:27 +0200 by phpDocumentor 1.3.2