source: Dev/trunk/rdfapi/sparql/SparqlEngineDb/ResultRenderer/JSON.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: 5.4 KB
Line 
1<?php
2require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb/ResultRenderer.php';
3
4/**
5*   Sparql DB JSON Renderer. Roughly follows http://www.w3.org/2001/sw/DataAccess/json-sparql/;
6*
7*   @author Christoph Rieß
8*   @license http://www.gnu.org/licenses/lgpl.html LGPL
9*
10*   @package sparql
11*/
12class SparqlEngineDb_ResultRenderer_JSON implements SparqlEngineDb_ResultRenderer
13{
14
15    /**
16    *   Converts the database results into JSON Format
17    *
18    *   @param array $arRecordSets  Array of (possibly several) SQL query results.
19    *   @param Query $query     SPARQL query object
20    *   @param SparqlEngineDb $engine   Sparql Engine to query the database
21    *   @return mixed   HTML code
22    */
23    public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine) {
24       
25        $this->query = $query;
26        $this->sg    = $engine->getSqlGenerator();
27        $strResultForm = $query->getResultForm();
28       
29        foreach ($this->getResultVars() as $var)
30                $ResultVarsTemp[] = substr($var,1);
31       
32        switch ($strResultForm) {
33            case 'select':
34            case 'select distinct':
35                $results = $this->createFromRecords($arRecordSets, $strResultForm);
36                                $strCode = json_encode(array(
37                                'head' => array('vars'=>$ResultVarsTemp),
38                                'results'=>array('bindings'=>$results))
39                                );
40                                //$strCode = str_replace(',{',','.PHP_EOL.'{',$strCode);
41                break;
42                        case 'construct':
43            case 'describe':
44                throw new Exception(
45                    'Construct and describe are not supported by the'
46                    . ' JSON renderer'
47                );
48
49            case 'count':
50            case 'ask':
51                if (count($arRecordSets) > 1) {
52                    throw new Exception(
53                        'More than one result set for a '
54                        . $strResultForm . ' query not supported by JSON Renderer'
55                    );
56                }
57
58                $nCount = 0;
59                $dbRecordSet = reset($arRecordSets);
60                foreach ($dbRecordSet as $row) {
61                    $nCount += intval($row['count']);
62                    break;
63                }
64
65                if ($strResultForm == 'ask') {
66                    $strcode = json_encode(array('boolean' => ($nCount > 0)));
67                } else {
68                    $strcode = json_encode(array('int' => $nCount ));
69                }
70                break;
71            default:
72                throw new Exception('Error');
73        }
74       
75        return $strCode;
76       
77
78    }
79    /**
80     * Method to create from record with specific resultform (not used yet)
81     *
82     * @param array $arRecordSets  Array of (possibly several) SQL query results.
83     * @param unknown_type $strResultForm
84     * @return array ready for json conversion
85     */
86        protected function createFromRecords($arRecordSets, $strResultForm) {
87               
88                $arVarAssignments = $this->sg->arVarAssignments;
89                $code = '';
90                $arResultVars = $this ->getResultVars();
91                $results = array();
92                foreach ($arRecordSets[0] as $value) {
93                        $node = array();
94                        foreach ($arResultVars as $ResultVar) {
95                                $nodeType = $value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_is']];
96                                if ($nodeType == 'r') {
97                                        $node[substr($ResultVar,1)] = array('type'=> 'uri','value'=>$value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_value']]);
98                                       
99                                }
100                               
101                                if ($value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_is']] == 'l') {
102                                        $literalType = $value[$arVarAssignments[$ResultVar][0] .
103                                                '.' . $arVarAssignments[$ResultVar]['sql_type']];
104                                        $literalLang = $value[$arVarAssignments[$ResultVar][0] .
105                                                '.' . $arVarAssignments[$ResultVar]['sql_lang']];
106                                        $literalValue = $value[$arVarAssignments[$ResultVar][0] .
107                                                '.' . $arVarAssignments[$ResultVar]['sql_value']];
108                                        $node[substr($ResultVar,1)] = $this->getLiteral($literalValue,$literalLang,$literalType);
109                                }
110                               
111                                if ($nodeType === 'b') {
112                                        $literalValue = $value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_value']];
113                                        $node[substr($ResultVar,1)] = array ('type'=>'bnode' ,'value'=>$value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_value']]);
114                                }
115                        }
116                        $results[]=$node;
117                       
118                       
119                }
120                return $results;
121
122        }
123       
124        /**
125         * COnverting Literals to array ready for json
126         *
127         * @param unknown_type $value
128         * @param unknown_type $lang
129         * @param unknown_type $type
130         * @return unknown
131         */
132        private function getLiteral($value, $lang , $type) {
133               
134                $ret = array();
135               
136                $type = 'literal';
137               
138                if ($value != 'literal') {
139                        $ret['value'] = $value;
140                }
141               
142                if ($lang != '') {
143                        $ret['lang'] = $lang;
144                }
145               
146                if ($type != '') {
147                        $ret['type'] = $type;
148                }
149               
150                return $ret;
151        }
152       
153        /**
154         * Giving array of used Vars also resolves the all-quantifier *
155         *
156         * @return array of vars as strings
157         */
158        protected function getResultVars() {
159                $arResultVars = $this->query->getResultVars();
160                if (in_array('*', $arResultVars)) {
161                        $arResultVars   = array_keys($this->sg->arVarAssignments);
162                } else {
163                        $arResultVars = array();
164                        foreach ($this->query->getResultVars() as $Var) {
165                                $arResultVars[] = (string) $Var;
166                        }
167                }
168               
169                return $arResultVars;
170        }
171
172}
173
174?>
Note: See TracBrowser for help on using the repository browser.