source: Dev/trunk/rdfapi/sparql/SparqlEngine/ResultRenderer/Default.php @ 12

Last change on this file since 12 was 12, checked in by basvannuland, 14 years ago

Added RAP RDF API
Added RDF reader writer for save and load survey

File size: 8.7 KB
Line 
1<?php
2require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb/ResultRenderer.php';
3
4/**
5*   Default result renderer for SparqlEngine
6*
7*   @author Tobias Gauß <tobias.gauss@web.de>
8*   @author Christian Weiske <cweiske@cweiske.de>
9*   @license http://www.gnu.org/licenses/lgpl.html LGPL
10*
11*   @package sparql
12*/
13class SparqlEngine_ResultRenderer_Default implements SparqlEngine_ResultRenderer
14{
15    /**
16    *   Converts the database results into the output format
17    *   and returns the result.
18    *
19    *   @param array $arVartable    Variable table
20    *   @param Query $query         SPARQL query object
21    *   @param SparqlEngine $engine Sparql Engine to query the database
22    *   @return mixed               Most likely an array
23    */
24    public function convertFromResult($arVartable, Query $query, SparqlEngine $engine)
25    {
26        $this->query   = $query;
27        $this->engine  = $engine;
28        $this->dataset = $engine->getDataset();
29
30        $result = false;
31        $qrf    = $this->query->getResultForm();
32
33        if ($arVartable != null) {
34            switch ($qrf) {
35                case 'ask':
36                    if (count($arVartable) > 0) {
37                        $result = true;
38                    } else {
39                        $result = false;
40                    }
41                    break;
42                case 'count':
43                    $result = count($arVartable);
44                    break;
45                case 'construct':
46                    $result = $this->constructGraph(
47                        $arVartable,
48                        $this->query->getConstructPattern()
49                    );
50                    break;
51                case 'describe':
52                    $result = $this->describeGraph($arVartable);
53                    break;
54                default:
55                    $result = $arVartable;
56                    break;
57            }
58        } else if ($qrf == 'describe'){
59            $result = $this->describeGraph(null);
60        } else if ($qrf == 'construct'){
61            $result = $this->constructGraph(
62                false,
63                $this->query->getConstructPattern()
64            );
65        }
66
67        return $result;
68    }//public function convertFromResult($arVartable, Query $query, SparqlEngine $engine)
69
70
71
72    /**
73    * Constructs a result graph.
74    *
75    * @param  array         $arVartable       A table containing the result vars and their bindings
76    * @param  GraphPattern  $constructPattern The CONSTRUCT pattern
77    * @return MemModel      The result graph which matches the CONSTRUCT pattern
78    */
79    function constructGraph($arVartable, $constructPattern)
80    {
81        $resultGraph = new MemModel();
82
83        if (!$arVartable) {
84            return $resultGraph;
85        }
86
87        $tp = $constructPattern->getTriplePatterns();
88
89        $bnode = 0;
90        foreach ($arVartable as $value) {
91            foreach ($tp as $triple) {
92                $sub  = $triple->getSubject();
93                $pred = $triple->getPredicate();
94                $obj  = $triple->getObject();
95
96                if (is_string($sub)  && $sub{1} == '_') {
97                    $sub  = new BlankNode("_bN".$bnode);
98                }
99                if (is_string($pred) && $pred{1} == '_') {
100                    $pred = new BlankNode("_bN".$bnode);
101                }
102                if (is_string($obj)  && $obj{1} == '_') {
103                    $obj  = new BlankNode("_bN".$bnode);
104                }
105
106
107                if (is_string($sub)) {
108                    $sub  = $value[$sub];
109                }
110                if (is_string($pred)) {
111                    $pred = $value[$pred];
112                }
113                if (is_string($obj)) {
114                    $obj  = $value[$obj];
115                }
116
117                if ($sub !== "" && $pred !== "" && $obj !== "") {
118                    $resultGraph->add(new Statement($sub,$pred,$obj));
119                }
120            }
121            $bnode++;
122        }
123        return $resultGraph;
124    }//function constructGraph($arVartable, $constructPattern)
125
126
127
128    /**
129    * Builds a describing named graph. To define an attribute list for a
130    * several rdf:type look at constants.php
131    *
132    * @param  array      $arVartable
133    * @return MemModel
134    */
135    function describeGraph($arVartable)
136    {
137        // build empty named graph
138        $resultGraph = new MemModel();
139        // if no where clause fill $arVartable
140        $vars = $this->query->getResultVars();
141        if ($arVartable == null) {
142            if ($vars) {
143                $arVartable[0] = array('?x' => new Resource(substr($vars[0],1,-1)));
144                $vars[0] = '?x';
145            }
146        }
147        // fetch attribute list from constants.php
148        global $sparql_describe;
149        // for each resultset
150        foreach ($arVartable as $resultset) {
151            foreach ($vars as $varname) {
152                $varvalue = $resultset[$varname];
153                // try to determine rdf:type of the variable
154                $type = $this->_determineType($varvalue, $resultGraph);
155                // search attribute list defined in constants.php
156                $list = null;
157                if ($type) {
158                    $strLuri = strtolower($type->getUri());
159                    if (isset($sparql_describe[$strLuri])) {
160                        $list = $sparql_describe[$strLuri] ;
161                    }
162                }
163                // search in dataset
164                $this->_getAttributes($list, $resultGraph, $varvalue);
165            }
166        }
167
168        return $resultGraph;
169    }//function describeGraph($arVartable)
170
171
172
173    /**
174    * Tries to determine the rdf:type of the variable.
175    *
176    * @param  Node       $var The variable
177    * @param  MemModel   $resultGraph The result graph which describes the Resource
178    * @return String     Uri of the rdf:type
179    */
180    protected function _determineType($var, $resultGraph)
181    {
182        $type = null;
183        // find in namedGraphs
184        if (!$var instanceof Literal) {
185            $iter = $this->dataset->findInNamedGraphs(
186                null,
187                $var,
188                new Resource(RDF_NAMESPACE_URI.'type'),
189                null,
190                true
191            );
192            while ($iter->valid()) {
193                $statement = $iter->current();
194                $type = $statement->getObject();
195                $resultGraph->add($iter->current());
196                break;
197            }
198        }
199        // if no type information found find in default graph
200        if (!$type) {
201            if (!$var instanceof Literal) {
202                $iter1 = $this->dataset->findInDefaultGraph(
203                    $var,
204                    new Resource(RDF_NAMESPACE_URI.'type'),
205                    null
206                );
207                $type = null;
208                while ($iter1->valid()) {
209                    $statement = $iter1->current();
210                    $type      = $statement->getObject();
211                    $resultGraph->add($iter1->current());
212                    break;
213                }
214            }
215        }
216        return $type;
217    }//protected function _determineType($var, $resultGraph)
218
219
220
221    /**
222    * Search the attributes listed in $list in the dataset.
223    * Modifies $resultGraph
224    *
225    * @param Array      $list List containing the attributes
226    * @param MemModel   $resultGraph The result graph which describes the Resource
227    * @return void
228    */
229    protected function _getAttributes($list, $resultGraph, $varvalue)
230    {
231        if ($list){
232            foreach ($list as $attribute) {
233                if (!$varvalue instanceof Literal) {
234                    $iter2 = $this->dataset->findInNamedGraphs(
235                        null,
236                        $varvalue,
237                        new Resource($attribute),
238                        null,
239                        true
240                    );
241                    while ($iter2->valid()) {
242                        $resultGraph->add($iter2->current());
243                        $iter2->next();
244                    }
245                    $iter3 = $this->dataset->findInDefaultGraph(
246                        $varvalue,
247                        new Resource($attribute),
248                        null
249                    );
250                    while ($iter3->valid()) {
251                        $resultGraph->add($iter3->current());
252                        $iter3->next();
253                    }
254                }
255            }
256        }
257    }//protected function _getAttributes($list, $resultGraph, $varvalue)
258
259
260}//class SparqlEngine_ResultRenderer_Default implements SparqlEngine_ResultRenderer
261
262?>
Note: See TracBrowser for help on using the repository browser.