|
From: <fu...@us...> - 2007-11-06 14:26:14
|
Revision: 546
http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=546&view=rev
Author: fusel2k
Date: 2007-11-06 06:26:06 -0800 (Tue, 06 Nov 2007)
Log Message:
-----------
[create] JSON result renderer roughly following http://www.w3.org/2001/sw/DataAccess/json-sparql/
Added Paths:
-----------
trunk/rdfapi-php/api/sparql/SparqlEngineDb/ResultRenderer/JSON.php
Added: trunk/rdfapi-php/api/sparql/SparqlEngineDb/ResultRenderer/JSON.php
===================================================================
--- trunk/rdfapi-php/api/sparql/SparqlEngineDb/ResultRenderer/JSON.php (rev 0)
+++ trunk/rdfapi-php/api/sparql/SparqlEngineDb/ResultRenderer/JSON.php 2007-11-06 14:26:06 UTC (rev 546)
@@ -0,0 +1,154 @@
+<?php
+require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb/ResultRenderer.php';
+
+/**
+* Sparql DB JSON Renderer. Roughly follows http://www.w3.org/2001/sw/DataAccess/json-sparql/;
+*
+* @author Christoph Rie\xDF
+* @license http://www.gnu.org/licenses/lgpl.html LGPL
+*
+* @package sparql
+*/
+class SparqlEngineDb_ResultRenderer_JSON implements SparqlEngineDb_ResultRenderer
+{
+
+ /**
+ * Converts the database results into JSON Format
+ *
+ * @param array $arRecordSets Array of (possibly several) SQL query results.
+ * @param Query $query SPARQL query object
+ * @param SparqlEngineDb $engine Sparql Engine to query the database
+ * @return mixed HTML code
+ */
+ public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine) {
+
+ $this->query = $query;
+ $this->sg = $engine->getSqlGenerator();
+ $strResultForm = $query->getResultForm();
+ switch ($strResultForm) {
+ case 'select':
+ case 'select distinct':
+ $results = $this->createFromRecords($arRecordSets, $strResultForm);
+ $strCode = json_encode(array('head' => array('vars'=>$this->getResultVars()),'results'=>array('bindings'=>$results)));
+ //$strCode = str_replace(',{',','.PHP_EOL.'{',$strCode);
+ break;
+ case 'construct':
+ case 'describe':
+ throw new Exception(
+ 'Construct and describe are not supported by the'
+ . ' JSON renderer'
+ );
+
+ case 'count':
+ case 'ask':
+ if (count($arRecordSets) > 1) {
+ throw new Exception(
+ 'More than one result set for a '
+ . $strResultForm . ' query not supported by JSON Renderer'
+ );
+ }
+
+ $nCount = 0;
+ $dbRecordSet = reset($arRecordSets);
+ foreach ($dbRecordSet as $row) {
+ $nCount += intval($row['count']);
+ break;
+ }
+
+ if ($strResultForm == 'ask') {
+ $strcode = json_encode(array('boolean' => ($nCount > 0)));
+ } else {
+ $strcode = json_encode(array('int' => $nCount ));
+ }
+ break;
+ default:
+ throw new Exception('Error');
+ }
+
+ return $strCode;
+
+
+ }
+ /**
+ * Method to create from record with specific resultform (not used yet)
+ *
+ * @param array $arRecordSets Array of (possibly several) SQL query results.
+ * @param unknown_type $strResultForm
+ * @return array ready for json conversion
+ */
+ protected function createFromRecords($arRecordSets, $strResultForm) {
+
+ $arVarAssignments = $this->sg->arVarAssignments;
+ $code = '';
+ $arResultVars = $this ->getResultVars();
+ $results = array();
+ foreach ($arRecordSets[0] as $value) {
+ $node = array();
+ foreach ($arResultVars as $ResultVar) {
+ $nodeType = $value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_is']];
+ if ($type == 'r' || $type === null ) {
+ $node[$ResultVar] = array('type'=> 'uri','value'=>$value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_value']]);
+
+ }
+
+ if ($value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_is']] == 'l') {
+ $literalType = $value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_type']];
+ $literalLang = $value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_lang']];
+ $literalValue = $value[$arVarAssignments[$ResultVar][0].'.'.$arVarAssignments[$ResultVar]['sql_value']];
+ $node[$ResultVar] = $this->getLiteral($literalValue,$literalLang,$literalType);
+ }
+ }
+ $results[]=$node;
+
+
+ }
+ return $results;
+
+ }
+
+ /**
+ * COnverting Literals to array ready for json
+ *
+ * @param unknown_type $value
+ * @param unknown_type $lang
+ * @param unknown_type $type
+ * @return unknown
+ */
+ private function getLiteral($value, $lang , $type) {
+
+ $ret = array();
+
+ $type = 'literal';
+
+ if ($value != 'literal') {
+ $ret['value'] = $value;
+ }
+
+ if ($lang != '') {
+ $ret['lang'] = $lang;
+ }
+
+ if ($type != '') {
+ $ret['type'] = $type;
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Giving array of used Vars also resolves the all-quantifier *
+ *
+ * @return array of vars as strings
+ */
+ protected function getResultVars() {
+ $arResultVars = $this->query->getResultVars();
+ if (in_array('*', $arResultVars)) {
+ $arResultVars = array_keys($this->sg->arVarAssignments);
+ }
+
+ return $arResultVars;
+ }
+
+}
+
+?>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|