PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/s3db3.5.10/s3dbcore/sparql_read7.php

https://github.com/drobbins/s3db
PHP | 1715 lines | 1053 code | 421 blank | 241 comment | 207 complexity | bcf954939a8cb85ffd1389d6c0fd418c MD5 | raw file
  1. <?php
  2. /**
  3. * @author Helena F Deus <helenadeus@gmail.com>
  4. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  5. * @package S3DB http://www.s3db.org
  6. */
  7. ########################################################
  8. ##Debug box
  9. #echo '<pre>';print_r($outputData);exit;
  10. #$timer ->display();
  11. #$timer->stop();$profiling = $timer->getProfiling();
  12. #echo $profiling[count($profiling)-1]['total'].' sec';exit;
  13. ########################################################
  14. function sparql($I)
  15. {
  16. ##Parse the query and build the dataset
  17. #global $timer;
  18. if(is_file(S3DB_SERVER_ROOT.'/pearlib/Benchmark/Timer.php')){
  19. require_once S3DB_SERVER_ROOT.'/pearlib/Benchmark/Timer.php';
  20. $timer = new Benchmark_Timer();
  21. $timer->start();
  22. }
  23. extract($I);
  24. ##To use SPARQL with ARC library, we will need it to work with a remote endpoint. That means that we do not want to configure ARC as a datastore, but rather to retrieve the data from s3db deployments, convert it to RDF and then use ARC to run the query on it
  25. /* ARC2 static class inclusion */
  26. ini_set("include_path", S3DB_SERVER_ROOT."/pearlib/arc". PATH_SEPARATOR. ini_get("include_path"));
  27. include_once("ARC2.php");
  28. $s3ql['url'] = ($in['url']!='')?$in['url']:$default_uri;
  29. $s3ql['key'] = ($in['key']!='')?$in['key']:get_user_key($user_id, $db);
  30. $q = $in['query'];
  31. list($query, $triples, $prefixes) = parse_sparql_query($q, $s3ql);
  32. #Altered 4/23/2010
  33. #from this moment on, we need first to SELECT even if that is not the query form; the query form will matter when we finally return the data
  34. $query_form = ($query['select']!='')?'select':(($query['ask']!='')?'ask': (($query['describe']!='')?'describe':(($query['construct']!='')?'construct':'??')));
  35. if($query_form=='describe'){
  36. $format = 'n3';
  37. }
  38. $bq .= "PREFIX ".implode("\n PREFIX ", $query['prefix'])."\n ";
  39. $bq .= "SELECT ".$query[$query_form][0]."\n ";
  40. $bq .= "FROM".implode(" FROM ", $query['from'])."\n ";
  41. $bq .= "WHERE ".$query['where'][0]."\n ";
  42. /*
  43. $bq .= "PREFIX ".implode("\n PREFIX ", $query['prefix'])."\n ";
  44. $bq .= "SELECT ".$query['select'][0]."\n ";
  45. $bq .= "FROM".implode(" FROM ", $query['from'])."\n ";
  46. $bq .= "WHERE ".$query['where'][0]."\n ";
  47. */
  48. preg_match_all('(\?[A-Za-z0-9_]+) ', $bq, $vars);
  49. if($vars[0]) {
  50. $vars = array_unique($vars[0]);
  51. $sparql_vars = implode(" ",$vars);
  52. }
  53. if($query['select'][0]!="" && $query['select'][0]!="*"){
  54. $outputCols = explode(" ", trim($query['select'][0]));
  55. $outputCols = array_filter($outputCols);
  56. $outputCols = array_intersect($vars, $outputCols);
  57. }
  58. $sparql=ereg_replace("FROM(.*)WHERE", "WHERE",$bq);
  59. #Validate the query first
  60. include_once(RDFAPI_INCLUDE_DIR."sparql/SparqlParser.php");
  61. try {
  62. $parser = new SparqlParser();
  63. $parsed = $parser->parse($sparql);
  64. }
  65. catch (Exception $e) {
  66. echo formatReturn('1', 'Parse error: '.$e->getMessage(), $_REQUEST['format'],'');
  67. exit;
  68. }
  69. #lets preprocess the order by which the must be queries must be performed to optimize speedness
  70. $filename = S3DB_SERVER_ROOT.'/tmp/'.md5($sparql.$user_id);
  71. if($clean && is_file($filename)){
  72. unlink($filename);
  73. }
  74. if(!is_file($filename)) {
  75. list($iterations, $scrambled) = iterationOrder($triples,$prefixes, true);
  76. ##$rdf_results will contain the totality of triples retrieved from s3db;
  77. ##Start a rdf-api model
  78. $iterations = array_values($iterations);
  79. $rdf = S3DB_URI_BASE.'/s3dbcore/model.n3';#base s3db rdf model
  80. $rdffilename = md5($rdf);
  81. $file_place = $GLOBALS['uploads'].'/';
  82. #$queryModel = rdf2php($rdf);
  83. #$data = $queryModel->sparqlQuery($sparql);
  84. #echo '<pre>';print_r($data);exit;
  85. if($timer) $timer->setMarker('Core model read into results');
  86. $rdf_results = array();
  87. # add the dictionary data
  88. if($complete){
  89. $dicfile = S3DB_SERVER_ROOT.'/tmp/'.md5('query_dictionary');
  90. if($clean && is_file($dicfile) && $_REQUEST['clean']=='dic'){
  91. unlink($dicfile);
  92. }
  93. if(!is_file($dicfile)){
  94. include_once(S3DB_SERVER_ROOT.'/s3dbcore/dictionary.php');
  95. $s3qlN=compact('user_id','db');
  96. $s3qlN['from']='link';
  97. $s3qlN['format'] = 'php';
  98. $links = query_user_dictionaries($s3qlN,$db,$user_id);
  99. file_put_contents($dicfile, $links);
  100. $links = unserialize($links);
  101. }
  102. else {
  103. $links = unserialize(file_get_contents($dicfile));
  104. }
  105. $rdf_results['E'][0] = $links;
  106. $nsfile = S3DB_SERVER_ROOT.'/tmp/'.md5('query_ns');
  107. if($clean && is_file($nsfile) && $_REQUEST['clean']=='ns'){
  108. unlink($nsfile);
  109. }
  110. if(!is_file($nsfile)){
  111. include_once(S3DB_SERVER_ROOT.'/s3dbcore/dictionary.php');
  112. $s3qlN=compact('user_id','db');
  113. $s3qlN['from']='namespaces';
  114. $s3qlN['format'] = 'php';
  115. $ns = query_user_dictionaries($s3qlN,$db,$user_id);
  116. file_put_contents($nsfile, $ns);
  117. $ns = unserialize($ns);
  118. }
  119. else {
  120. $ns = unserialize(file_get_contents($nsfile));
  121. }
  122. if($timer) $timer->setMarker('Dictionary links retrieved');
  123. ##Because dictionary queries are too generic, variables need to be bound before writting S3QL
  124. $tmp_triples = rdf_encode($links,"E", 'array', $s3ql['db'],$ns);
  125. $file=make_rdf_model($tmp_triples);
  126. $queryModel = rdf2php($file);
  127. #Query needs to be parcial, triple by triple...
  128. foreach ($triples as $g=>$gPat) {
  129. $tmp = explode(" ",$gPat);
  130. foreach ($tmp as $t) {
  131. if(ereg("http", $t)) { $t = "<".$t.">";}
  132. $tgPat .= $t." ";
  133. }
  134. $part .= "PREFIX ".implode("\n PREFIX ", $query['prefix'])."\n ";
  135. $part .= "SELECT ".$query['select'][0]."\n ";
  136. $part .= "FROM".implode(" FROM ", $query['from'])."\n ";
  137. $part .= "WHERE {".$tgPat." . }";
  138. $dic_bound_data = $queryModel->sparqlQuery($part);
  139. if($dic_bound_data){
  140. foreach ($dic_bound_data as $l=>$varsvals) {
  141. if($vars){
  142. foreach ($vars as $v) {
  143. if($varsvals[$v]->uri){
  144. if(!is_array($discovered[$v]) || !in_array($varsvals[$v]->uri, $discovered[$v]))
  145. $discovered[$v][] = $varsvals[$v]->uri;
  146. }
  147. elseif ($varsvals[$v]->literal) {
  148. if(!is_array($discovered[$v]) || !in_array($varsvals[$v]->literal, $discovered[$v]))
  149. $discovered[$v][] = $varsvals[$v]->literal;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. $performedQueries = array();
  158. $r=0;
  159. foreach ($iterations as $it=>$triples2query) {
  160. $S3QL=array();
  161. $S3QLfinal = array();
  162. $queried_elements = array();
  163. $lQueries = array();
  164. $rQueries = array();
  165. foreach ($triples2query as $i=>$tripleInd) {
  166. $tripleString = $tripleInd;
  167. $tmp = explode(" ", trim($tripleString));
  168. $subject =$tmp[0];
  169. //once the subjec is known, predicate may be found
  170. ereg("^\\".$subject.'(.*)', trim($tripleString), $tmp1);
  171. $tmp2 = explode(" ", trim($tmp1[1]));
  172. $predicate = $tmp2[0];
  173. //With known subject and predicate, object will be the string with that portion truncated
  174. ereg("^\\".$predicate.'(.*)', trim($tmp1[1]), $tmp3);
  175. $object = trim($tmp3[1]);
  176. #list($subject, $predicate) = explode(' ',trim($tripleString));
  177. #$object = trim(str_replace(array($subject, $predicate), array("", ""), $tripleString));
  178. //Method 1 to breaking the triple
  179. /*$tmp = explode(' ',trim($tripleString));
  180. $subject = $tmp[0];
  181. $predicate = $tmp[1];
  182. if(count($tmp)>3){
  183. for ($t=2; $t <count($tmp) ; $t++) {
  184. if($object!='') $object .= " ";
  185. $object .= $tmp[$t];
  186. }
  187. }
  188. */
  189. $subject = ereg_replace('^<|>$','',$subject);
  190. $predicate = ereg_replace('^<|>$','',$predicate);
  191. $object = ereg_replace('^<|>$','',$object);
  192. $triple = compact('subject','predicate','object');
  193. #sparql triple is used to calculate the values of the variables in the triple
  194. #$sparql_triple = $sparql_prefixes_default.' SELECT * WHERE { '.ltrim($tripleString).' . }';
  195. #now lets interpret the triple to explore the space of possible queries on S3QL
  196. $pack = compact('triple', 's3ql','user_id', 'db','prefixes','varType','discoveredData','discovered','it','varTypeWhere','collected_data','performedQueries');
  197. $sp = sparql_navigator($pack);
  198. extract($sp);
  199. # if($timer) $timer->setMarker('Built query '.$i);
  200. ##Remove queries that were already performed
  201. if($S3QL[0]){
  202. foreach ($S3QL as $s=>$q) {
  203. if(!in_array($q, $S3QLfinal)){
  204. $S3QLfinal[] =$q;
  205. $queried_elements[] = $element[$s];
  206. $lQueries[] = $localQueries[$s];
  207. $rQueries[] = $remoteQueries[$s];
  208. }
  209. }
  210. #$S3QLfinal[] =$S3QL[0];
  211. #$queried_elements[] = $element;
  212. #$localQueries[$tripleString] = $localQueries[0];
  213. #$remoteQueries[$tripleString] = $remoteQueries[0];
  214. #$localQueries = array_filter($localQueries);
  215. #$remoteQueries = array_filter($remoteQueries);
  216. }
  217. }
  218. $S3QL = $S3QLfinal;
  219. $localQueries = $lQueries;
  220. $remoteQueries = $rQueries;
  221. ##Remove repeated queries
  222. #$S3QL=array_unique($S3QL);
  223. #$S3QL = array_values($S3QL);
  224. #if only the s3ql is requested, we can return it now
  225. if($in['output']=='S3QL')
  226. {
  227. foreach ($localQueries as $sparqlVersion=>$s3qlVersion) {
  228. $Q[]['S3QL'] = S3QLQuery($s3qlVersion);
  229. }
  230. foreach ($remoteQueries as $rq) {
  231. $Q[]['S3QL'] = $rq;
  232. }
  233. $root = 's3ql';#root is just the word that xml should parse as the root for each entry
  234. $data = $Q;
  235. $cols = array('S3QL');
  236. $format = ($in['format']=='')?'html':$in['format'];
  237. $z = compact('data','cols','format','root');
  238. $out=outputFormat($z);
  239. return array(true,$out);
  240. }
  241. #If paralel library is activated, use it for the data. Otherwise use the custom version
  242. #$query_answers_file = 'sparql_query_ans'.rand(100,200); $a=fopen($query_answers_file, 'a');
  243. if(!empty($S3QL)){
  244. if(extension_loaded ('curl') && $goparallel){
  245. // Create cURL handlers
  246. if($timer) $timer->setMarker('Starting queries from group '.$it);
  247. foreach ($S3QL as $k=>$url) {
  248. $qURL = $url;
  249. $ch[$k] = curl_init();
  250. // Set options
  251. curl_setopt($ch[$k], CURLOPT_URL, $qURL.'&format=php');
  252. curl_setopt($ch[$k], CURLOPT_RETURNTRANSFER, 1);
  253. }
  254. $mh = curl_multi_init();
  255. foreach ($S3QL as $k=>$url) {
  256. curl_multi_add_handle($mh,$ch[$k]);
  257. }
  258. $running=null;
  259. do {
  260. curl_multi_exec($mh,$running);
  261. if($timer) $timer->setMarker('Query '.$k.' of group '.$it.' executed');
  262. } while ($running > 0);
  263. foreach ($S3QL as $k=>$url) {
  264. $answer[$k] = curl_multi_getcontent($ch[$k]);
  265. if(!empty($answer[$k]))
  266. {
  267. #@fwrite($a, $answer[$k]);
  268. ##This is what takes the longest after the query, can it be replaced?
  269. $ans = unserialize($answer[$k]);
  270. #$letter = $queried_elements[$r][0];
  271. $letter = $queried_elements[$k];
  272. if(empty($ans)){
  273. ##is this query part is not optional, then the result will be null
  274. ##TO BE DEVELOPED SOON
  275. }
  276. else {
  277. $rdf_results[$letter][] = $ans;
  278. }
  279. $r++;
  280. ##Add the triples to already existing triples
  281. #Line up the answer with the model
  282. if($timer) $timer->setMarker('Query '.$it.'=>'.$k.' converted to php ');
  283. }
  284. }
  285. curl_multi_close($mh);
  286. ####Time count
  287. #$time_end = microtime(true);
  288. #$time = $time_end - $time_start;
  289. #echo "Query took ".$time." seconds\n";exit;
  290. ###
  291. }
  292. else
  293. {
  294. #Now solve the remaining triples with the constants found in this one
  295. if(is_array($localQueries) && !empty($localQueries)) {
  296. foreach ($localQueries as $s=>$locals3ql) {
  297. $locals3ql = array_filter(array_diff_key($locals3ql,array('url'=>'')));
  298. $tmpqueryfile = S3DB_SERVER_ROOT.'/tmp/'.md5(S3QLQuery($locals3ql));
  299. ##Has this query been performed?
  300. if($clean=='2' && is_file($tmpqueryfile)){
  301. unlink($tmpqueryfile);
  302. }
  303. #If query results are stored in cache, use them!
  304. if (is_file($tmpqueryfile)) {
  305. $answer = unserialize(file_get_contents($tmpqueryfile));
  306. }
  307. else {
  308. $answer = S3QLAction($locals3ql);
  309. file_put_contents($tmpqueryfile, serialize($answer));
  310. }
  311. //$answer = S3QLAction($locals3ql);
  312. if(!empty($answer))
  313. {
  314. $letter = letter($locals3ql['from']);
  315. $rdf_results[$letter][] = $answer;
  316. }
  317. }
  318. }
  319. if(is_array($remoteQueries) && !empty($remoteQueries[0])) {
  320. $k=0;
  321. foreach ($remoteQueries as $remoteQuery) {
  322. $answer = remoteQ($remoteQuery);
  323. if(!empty($answer))
  324. {
  325. $letter = $queried_elements[$k];
  326. $rdf_results[$letter][] = $answer;
  327. $k++;
  328. #$rdfanswer = rdf2php($answer);
  329. #Line up the answer with the model
  330. #$queryModel->addModel($rdfanswer);
  331. #Now perform the query on the small model to find a constant for the remaining queries
  332. #list($data,$discovered, $discoveredData,$queryModel) = executeQuery($queryModel,$sparql_triple,$discovered,$format);
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. ##Get the data from the file
  340. ##Convert the result into an RDF file
  341. $data_triples = array();
  342. if(is_array($rdf_results)){
  343. foreach ($rdf_results as $letter=>$results2rdfize) {
  344. $dont_skip_core_name = false;
  345. $dont_skip_serialized=true;
  346. if(ereg('S', $letter)) $dont_skip_serialized=false;
  347. if(ereg('C|R|P', $letter)) $dont_skip_core_name = true;
  348. foreach ($results2rdfize as $k=>$data) {
  349. $tmp_triples = rdf_encode($data,$letter, 'array', $s3ql['db'],$ns,$collected_data,$dont_skip_serialized,$dont_skip_core_name);
  350. if(is_array($tmp_triples))
  351. $data_triples=array_merge($data_triples, $tmp_triples);
  352. }
  353. }
  354. }
  355. if(is_array($outputCols) && !empty($outputCols)){
  356. ##only this one are to be shown in the final result
  357. $vars = $outputCols;
  358. }
  359. $cleanCols = array();
  360. foreach ($vars as $varname) {
  361. $cleanCols[] = ereg_replace('^\?','', $varname);
  362. }
  363. if(empty($data_triples)){
  364. ##still return the headers
  365. $cols = $cleanCols;
  366. $format = ($_REQUEST['format']!="")?$_REQUEST['format']:'html';
  367. $data = array();
  368. $z = compact('data','cols','format','root');
  369. $out=outputFormat($z);
  370. return (array(1,$out));#return true because query was valid... just did not return any results
  371. }
  372. #echo $filename;exit;
  373. $tmp['ns'] = $prefixes;
  374. /*
  375. #this one for turtle
  376. $parser = ARC2::getComponent('TurtleParser', $a);
  377. $index = ARC2::getSimpleIndex($triples, false) ; # false -> non-flat version
  378. $rdf_doc = $parser->toTurtle($index,$prefixes);
  379. */
  380. $parser = ARC2::getComponent('RDFXMLParser');
  381. $index = ARC2::getSimpleIndex($data_triples, false) ; /* false -> non-flat version */
  382. $rdf_doc = $parser->toRDFXML($index);
  383. #$filename = S3DB_SERVER_ROOT.'/tmp/'.md5($sparql.date('d'));
  384. $rr= fopen($filename, 'a+');
  385. fwrite($rr, $rdf_doc);
  386. fclose($rr);
  387. if($timer) $timer->setMarker(count($data_triples).' triples written to file '.$filename);
  388. ##The better strategy would be to let the client cpu resolve the query; return the graphs with the rdf so that a sparql on the client can handle it
  389. }
  390. if($_REQUEST['filename']){
  391. $url2search = str_replace(S3DB_SERVER_ROOT, S3DB_URI_BASE, $filename);
  392. if(filesize($filename)>0){
  393. return (array(true,$url2search));
  394. }
  395. else {
  396. return (array(false));
  397. }
  398. exit;
  399. }
  400. if($redirect){
  401. ##And now use an external service ( I gave up with ARC) to parse the query
  402. $url2search = str_replace(S3DB_SERVER_ROOT, S3DB_URI_BASE, $filename);
  403. $bq = stripslashes($bq);
  404. ##Giving up on ARC, surrender to sparql.com
  405. $remote_endpoint = "http://sparql.org/sparql?query=";
  406. $bq=ereg_replace("FROM <.*>", "FROM <".$url2search.">", $bq);
  407. #$sparql=ereg_replace("FROM <.*>", "FROM <".$url2search.">", $sparql);
  408. $bq = urlencode($bq);
  409. $remote_endpoint .= $bq.'&default-graph-uri=&stylesheet=/xml-to-html.xsl';
  410. #$remote_endpoint .= $sparql.'&default-graph-uri=&stylesheet=/xml-to-html.xsl';
  411. return (array(true, $remote_endpoint));
  412. }
  413. if($format=='rdf'){
  414. return (array(true, $rdf_doc));
  415. }
  416. if ($format=='turtle') {
  417. if(!$index) {
  418. $parser = ARC2::getRDFParser();
  419. $parser->parse($filename);
  420. $index = $parser->getSimpleIndex(0);
  421. }
  422. $rdf_doc = $parser->toTurtle($index);
  423. return (array(true, $rdf_doc));
  424. }
  425. if ($format=='n3') {
  426. if(!$index) {
  427. $parser = ARC2::getRDFParser();
  428. $parser->parse($filename);
  429. $index = $parser->getSimpleIndex(0);
  430. }
  431. $rdf_doc = $parser->toNTriples($index);
  432. return (array(true, $rdf_doc));
  433. }
  434. #And finally perform the query on the model.
  435. $queryModel = rdf2php($filename);
  436. $format = ($in['format']!='')?$in['format']:'html';
  437. if($timer) $timer->setMarker('Data converted to a model the rdf-api can query');
  438. if(eregi('^(sparql-xml|sparql-html)$', $format)){
  439. switch ($format) {
  440. case 'sparql-xml':
  441. $result = $queryModel->sparqlQuery($sparql, 'XML');
  442. break;
  443. case 'sparql-html':
  444. $result = $queryModel->sparqlQuery($sparql, 'HTML');
  445. if($_REQUEST['su3d']){
  446. $timer->stop();$profiling = $timer->getProfiling();
  447. echo "Query took ".$profiling[count($profiling)-1]['total'].' sec';
  448. }
  449. break;
  450. }
  451. if($result){
  452. return array(true,$result);
  453. }
  454. else {
  455. return (false);
  456. }
  457. }
  458. elseif($format=='html.form'){
  459. $form .= '
  460. <html>
  461. <head>
  462. </head><body>
  463. <form method="GET" action="sparql.php" id="sparqlform">
  464. <h5>Target Deployment(s)</h5>
  465. <input type="hidden" name="key" value="'.$s3ql['key'].'"/>
  466. <input type="hidden" name="format" value="'.$_REQUEST['format'].'"/>
  467. <input type = "text" id="url" size = "100%" value="'.$GLOBALS['url'].'" name="url">
  468. <h5>SPARQL <a href="http://www.w3.org/TR/rdf-sparql-query/" target="_blank">(help!!)</a></h5>
  469. <br />
  470. <textarea cols="100" id="sparql" rows="10" name = "query">'.stripslashes($sparql).'</textarea><br />
  471. <input type="submit" value="SPARQL this!" id="submitsparql"></body>
  472. </form>
  473. ';
  474. $form .= '<br />'.count($data)." rows";
  475. $form .= '<br />Query took '.(strtotime(date('His'))-$start).' sec';
  476. if(count($data)>0){
  477. return (array(true, $form));
  478. }
  479. else {
  480. return (array(false));
  481. }
  482. }
  483. else {
  484. #and output the result according to requested format
  485. $queryResultCache = S3DB_SERVER_ROOT.'/tmp/'.md5($sparql.$user_id.'result');
  486. if($clean && is_file($queryResultCache)){
  487. unlink($queryResultCache);
  488. }
  489. $sparql = stripslashes($sparql);
  490. if(!is_file($queryResultCache)) {
  491. $data = $queryModel->sparqlQuery($sparql);
  492. file_put_contents($queryResultCache, serialize($data));
  493. chmod($queryResultCache, 0777);
  494. if($timer) $timer->setMarker('Query on SPARQL data executed by rdf-api.');
  495. }
  496. else {
  497. $data = unserialize(file_get_contents($queryResultCache));
  498. }
  499. if(is_array($outputCols) && !empty($outputCols)){
  500. ##only this one are to be shown in the final result
  501. $vars = $outputCols;
  502. }
  503. $cleanCols = array();
  504. foreach ($vars as $varname) {
  505. $cleanCols[] = ereg_replace('^\?','', $varname);
  506. }
  507. $outputData = array();
  508. if(is_array($data))
  509. foreach ($data as $s=>$sparql_line) {
  510. foreach ($sparql_line as $sparql_var=>$sparql_var_value) {
  511. if($sparql_var_value->uri!=''){
  512. $outputData[$s][ereg_replace('^\?','', $sparql_var)] = $sparql_var_value->uri;
  513. }
  514. elseif($sparql_var_value->label!='') {
  515. $outputData[$s][ereg_replace('^\?','', $sparql_var)] = $sparql_var_value->label;
  516. }
  517. else {
  518. $outputData[$s][ereg_replace('^\?','', $sparql_var)] = "";
  519. }
  520. }
  521. }
  522. if($timer) $timer->setMarker('Data converted in a format that fun outputformat can read');
  523. #$timer ->display();
  524. #root is just the word that xml should parse as the root for each entry
  525. $root = 'sparql';
  526. if($timer) $timer->setMarker('All variables fitted into their places to represent in the final output');
  527. $data = $outputData;
  528. $cols = $cleanCols;
  529. if($_REQUEST['su3d']){
  530. if($timer) {$timer->stop();$profiling = $timer->getProfiling();}
  531. echo "Query took ".$profiling[count($profiling)-1]['total'].' sec<br>';
  532. }
  533. $z = compact('data','cols','format','root');
  534. $out=outputFormat($z);
  535. #if($_REQUEST['su3d']){
  536. # echo $out;exit;
  537. #}
  538. #if(count($data)>0){
  539. return (array(true, $out));
  540. #}
  541. #else {
  542. # return (array(false));
  543. #}
  544. }
  545. #else {
  546. #$out= formatReturn($GLOBALS['error_codes']['no_results'], 'Your query did not return any results.', $format,'');
  547. #}
  548. }
  549. function isSPARQLVar($e){
  550. if($e->uri!='')
  551. return (False);
  552. elseif($e->label!='')
  553. return (False);
  554. elseif (ereg('^\?', $e)) {
  555. return (true);
  556. }
  557. else {
  558. return (false);
  559. }
  560. }
  561. function isS3DBCore($e, $call=false,$format='rdf')
  562. {
  563. ##First slip into url + finalization; check if url is s3db's
  564. if(!ereg('^(http.*)/(D|G|U|P|C|R|I|S)([0-9]+)$', $e, $uri_out))
  565. return (False);
  566. else {
  567. $s3dbquery = $uri_out[1].'/URI.php?format='.$format.'&uid='.$uri_out[2].$uri_out[3].'&key='.$GLOBALS['key'];
  568. if($call){
  569. $uri_dat = stream_get_contents(fopen($s3dbquery,'r'));
  570. if($format=='rdf')
  571. {$model[$e] = rdf2php($uri_dat); }
  572. else {
  573. if($format=='php')
  574. {
  575. $model[$e] = unserialize($uri_dat);
  576. }
  577. }
  578. }
  579. #$msg=html2cell($uri_dat);$msg = $msg[2];
  580. #if(is_array($model[$e]->triples))
  581. #{
  582. #echo $uri_out[2]; echo '<pre>';print_r($GLOBALS['s3dbCore'][$uri_out[2]]);
  583. #$next=(!is_array($GLOBALS['s3dbCore'][$uri_out[2]]))?0:count($GLOBALS['s3dbCore'][$uri_out[2]]+1);
  584. #$GLOBALS['s3dbQueries'][count($GLOBALS['s3dbQueries'])+1] = $s3dbquery;
  585. #$GLOBALS['s3dbCore'][$uri_out[2]][$next] = $uri_out[2].$uri_out[3];
  586. #$GLOBALS['s3dbURI'][count($GLOBALS['s3dbURI'])+1]=$uri_out[2].$uri_out[3];
  587. return (array('query'=>$s3dbquery, 'url'=>$uri_out[1],'letter'=>$uri_out[2], 'value'=>$uri_out[3], 'data'=>$model[$e]));
  588. }
  589. #else {
  590. # return (False);
  591. #}
  592. #}
  593. }
  594. function switchFromCore($E)
  595. {
  596. return ($GLOBALS['s3codes'][$E]);
  597. }
  598. function switchToCore($E)
  599. {
  600. return ($GLOBALS['s3codesInv'][$E]);
  601. }
  602. function sparql_navigator($c)
  603. {global $timer;
  604. extract($c);
  605. ##
  606. #React to the triples individually.
  607. #
  608. $crew = array('subject','predicate','object');
  609. #
  610. #no answer just yet
  611. #
  612. $ans=array();
  613. $triple_vars = array();
  614. $q='';
  615. $fromSpace = array_map('switchToCore', array_keys($GLOBALS['COREids']));
  616. $whereSpace = array_combine(array('D','G','U','P','C','R','I','S'), $GLOBALS['queriable']);
  617. $selectSpace = $GLOBALS['queriable'];
  618. #
  619. #first we'll try to answer the question with the captain himself - the subject has the most chances of winning the game; the subject can answer the question totally or partially. In case it is partially, predicate and object will complete it.
  620. #
  621. $from = $fromSpace;
  622. foreach ($crew as $crew_member) {
  623. ##if any of the triples is just 'a', replace by rdf:type
  624. if($triple[$crew_member]=='a'){
  625. $triple[$crew_member] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
  626. }
  627. $isCore=false;
  628. $isCore =isS3DBCore($triple[$crew_member],true,'php');
  629. if($isCore){
  630. $collected_data[$isCore['letter'].$isCore['value']] = $isCore['data']; ##save it for later
  631. }
  632. switch ($crew_member) {
  633. case 'subject':
  634. #
  635. #subject can tells us for sure where the query should target; relationships associated with this core member can be retrieved.
  636. #
  637. if($isCore)
  638. {
  639. #
  640. #Because the core allows only collections and items as subjects
  641. #
  642. list($from, $where) = trimBasedOnSub(compact('from','isCore','where','triple','fromSpace'));
  643. ##Where cannot be determined yet, but we can constrain the where space
  644. $whereSpace = array_intersect_key($whereSpace, array_flip(array_map('switchToCore',$from)));
  645. }
  646. elseif(isSPARQLVar($triple[$crew_member]))#is subj var?
  647. {
  648. #If ?var was not found already, assign it to empty vars
  649. array_push($triple_vars, $triple[$crew_member]);
  650. ##Build the from and the where; if these were already performed, they will not be performed again
  651. #11TCGA
  652. if($varType[$triple[$crew_member]]!='')
  653. {$from = array_intersect($from, $varType[$triple[$crew_member]]);
  654. if($varTypeWhere[$triple[$crew_member]]!=''){
  655. $where[letter($varType[$triple[$crew_member]])] = $varTypeWhere[$triple[$crew_member]];
  656. }
  657. }
  658. }
  659. elseif($triple[$crew_member]) {
  660. if (isCoreOntology($triple[$crew_member])) {
  661. #The query is to be oriented towards the core. Since the core is already part of the model.n3, we need to leave the form and where empty. Model reamians as was an query is porformed on top of it.
  662. $from = array();
  663. $where = array();
  664. }
  665. }
  666. else {
  667. #the only time subject is not in the core is if the rdf comes from external sources. These will be left to the sparql enggine
  668. }
  669. ##P and O can be used to trim the answer if they are constants; otherwise they can be dicovered
  670. if($timer) $timer->setMarker('subject '.$triple[$crew_member].' interpreted');
  671. break;
  672. case 'predicate':
  673. #Which in the core? Predicate can now choose between rule or item, otherwise it does not make sense within the core
  674. if($isCore)
  675. {
  676. list($from, $where, $varType, $varTypeWhere) = trimBasedOnPred(compact('isCore','where', 'triple','varType','varTypeWhere'));
  677. }
  678. elseif(isSPARQLVar($triple[$crew_member]))#is pred var?
  679. {
  680. array_push($triple_vars, $triple[$crew_member]);
  681. }
  682. elseif(ereg('^http',$triple[$crew_member])) {
  683. #When the predicate is a known property, "from" can be trimmed to involve those that do have that property.
  684. #try to translate which property if being requested via uri:
  685. $tmp = $triple[$crew_member];
  686. if ($tmp=='http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {##When predicate is "type" something, query can be either on core or on a particular collections
  687. $objectIsCore =isS3DBCore($triple['object']);
  688. if($objectIsCore)
  689. switch ($objectIsCore['letter']) {
  690. case 'C':
  691. $from = array_intersect($from, array('I'));
  692. ##Where will be resolved when we look at the object
  693. #if(!is_array($where['I'])) $where['I'] = array();
  694. #array_push($where['I'], array('collection_id'=>$objectIsCore['value']));
  695. $varType[$triple['subject']][] = 'I';
  696. $varTypeWhere[$triple['subject']][] = 'C'.$objectIsCore['value'];
  697. break;
  698. case 'P':
  699. $varType[$triple['subject']][] = 'P';
  700. $varTypeWhere[$triple['subject']][] = 'P'.$objectIsCore['value'];
  701. break;
  702. }
  703. $objectIsEntity =isCoreOntology($triple['object']);
  704. #10TCGA
  705. if($objectIsEntity){
  706. $varType[$triple['subject']][] = letter($objectIsEntity);
  707. }
  708. ##Added 131109
  709. #Object has been discovered?
  710. $objectDiscovered = WasDiscovered($triple['object'],$discovered);
  711. if($objectDiscovered){
  712. ##Find the possibilities
  713. foreach ($objectDiscovered as $pObj) {
  714. $objectIsDiscoveredCore =isS3DBCore($pObj);
  715. switch ($objectIsDiscoveredCore['letter']) {
  716. case 'C':
  717. $from = array_intersect($from, array('I'));
  718. if(!$where['I']){
  719. $where['I'] = array();
  720. }
  721. #$varType[$triple['subject']][] = 'I';
  722. #$varTypeWhere[$objectIsDiscoveredCore['letter']][] = 'C'.$objectIsDiscoveredCore['value'];
  723. $where['I'][] = array('collection_id'=>$objectIsDiscoveredCore['value']);
  724. break;
  725. }
  726. }
  727. }
  728. }
  729. #elseif($tmp==rdfs.'subClassOf'){
  730. elseif($tmp=='http://www.w3.org/2000/01/rdf-schema#subClassOf'){
  731. $from = array_intersect($from, array('C','R','P','U','G'));
  732. $objectIsCore =isS3DBCore($triple['object']);
  733. $objectDiscovered = WasDiscovered($triple['object'],$varType);
  734. $subjectType = WasDiscovered($triple['subject'],$varType);
  735. if($objectIsCore)
  736. {switch ($objectIsCore['letter']) {
  737. case 'P':
  738. $from = array_intersect($from, array('C','R'));
  739. $varTypeWhere[$triple['subject']][] = 'P'.$objectIsCore['value'];
  740. #if(!is_array($where['I'])) $where['I'] = array();
  741. #array_push($where['I'], array('collection_id'=>$objectIsCore['value']));
  742. break;
  743. case 'D':
  744. $from = array_intersect($from, array('P','U', 'G'));
  745. break;
  746. case 'G':
  747. $from = array_intersect($from, array('U'));
  748. break;
  749. }
  750. }
  751. elseif($objectType) {
  752. foreach ($objectType as $gold) {
  753. $isObjectCore = isS3DBCore($gold);
  754. if($isObjectCore){
  755. list($from, $where) = trimBasedOnObj(array('from'=>$from,'isCore'=>$isObjectCore,'where'=>$where));
  756. }
  757. }
  758. }
  759. }
  760. #elseif($tmp==rdfs.'label' || $tmp==rdfs.'comment'){
  761. elseif(in_array($tmp, $GLOBALS['not_uid_specific'])){
  762. #is subject found?
  763. $subjDiscovered = WasDiscovered($triple['subject'],$varType, $varTypeWhere);
  764. #how about object?
  765. $objDiscovered = WasDiscovered($triple['object'],$varType,$varTypeWhere);
  766. #$subjDataDiscovered = $discoveredData[$triple['subject']];
  767. if($subjDiscovered)
  768. {
  769. #echo 'ola';exit;
  770. #$from = array();
  771. $where = array();
  772. foreach ($subjDiscovered as $g=>$gold) {
  773. $isSubjectCore = isS3DBCore($gold);
  774. if($isSubjectCore){
  775. list($from, $where) = trimBasedOnSub(array('fromSpace'=>$fromSpace,'from'=>$from,'isCore'=>$isSubjectCore,'where'=>$where));
  776. }
  777. elseif(in_array($gold, array('P','C','R','I','S') ))
  778. {
  779. $from = array_intersect($from, array($gold));
  780. if($varTypeWhere[$triple['subject']][$g]){
  781. if(!is_array($where[$gold])) $where[$gold] = array();
  782. $eid = $GLOBALS['COREletterInv'][letter($varTypeWhere[$triple['subject']][$g])];
  783. array_push($where[$gold], array($eid=>substr($varTypeWhere[$triple['subject']][$g], 1,strlen($varTypeWhere[$triple['subject']][$g]))));
  784. }
  785. }
  786. }
  787. #echo '<pre>';print_r($from);
  788. #echo '<pre>';print_r($where);
  789. #exit;
  790. }
  791. #echo '<pre>';print_r($from);echo '<pre>';print_r($where);exit;
  792. }
  793. else
  794. {
  795. foreach ($from as $E) {
  796. if(in_array($tmp, $GLOBALS['propertyURI'][$E]))
  797. {
  798. $fromSubSpace[] = $E;
  799. #
  800. #The object's help here will matter, as it will encapsulate the value to be read into the query
  801. #
  802. $objectIsCore =isS3DBCore($triple['object']);
  803. if(!is_array($where[$E])) $where[$E] = array();
  804. if($objectIsCore) {
  805. array_push($where[$E], array(array_search($tmp,$GLOBALS['propertyURI'][$E]) => $objectIsCore['value']));
  806. }
  807. elseif($triple['object']) {
  808. array_push($where[$E], array(array_search($tmp,$GLOBALS['propertyURI'][$E]) => $triple['object']));
  809. }
  810. }
  811. #$from = array_intersect($from,$fromSubSpace);
  812. }
  813. #Added 111109
  814. if(is_array($from) && is_array($fromSubSpace))
  815. $from = array_intersect($from,$fromSubSpace);
  816. if(count($from)==8 || empty($where)) ##all entities will be queried, not a good move; this can be a query for the dictionary
  817. {$from=array();
  818. }
  819. }
  820. #echo '<pre>';print_r($from);
  821. #echo '<pre>';print_r($where);exit;
  822. }
  823. elseif($triple[$crew_member]) {
  824. }
  825. else {
  826. }
  827. break;
  828. case 'object':
  829. #echo '<pre>';print_r($where);exit;
  830. if($isCore) {
  831. ##Who can be connected to an element of the core? The object can eliminate some "from" options by discarding those that,according to the core, cannot be connected to this property as object
  832. #attr is always the same: it i sthe ID of the core element specified. For example, if it is rule, then attr is rule_id, etc.
  833. ##Where can finally be retrieved;
  834. switch ($isCore['letter']){
  835. case 'D':
  836. $subSpace = array('P','U','G','D');
  837. $from = array_intersect($from, $subSpace);
  838. break;
  839. case 'P':
  840. #$subSpace = array('R','C','U','G','P');
  841. $subSpace = array('R','C','P');
  842. $from = array_intersect($from, $subSpace);
  843. foreach ($from as $e) {
  844. if(!is_array($where[$e])) $where[$e]=array();
  845. array_push($where[$e], array('project_id' => $isCore['value']));
  846. }
  847. break;
  848. case 'R':
  849. #$subSpace = array('U','G','R');
  850. $subSpace = array('R');
  851. $from = array_intersect($from, $subSpace);
  852. foreach ($from as $e) {
  853. array_push($where[$e], array('rule_id' => $isCore['value']));
  854. }
  855. break;
  856. case 'C':
  857. #$subSpace =array('I','R','U','G','C');
  858. $subSpace =array('I','R','C');
  859. $from = array_intersect($from, $subSpace);
  860. foreach ($from as $e) {
  861. switch ($e) {
  862. case 'R':
  863. if(!is_array($where[$e])) $where[$e]=array();
  864. array_push($where[$e], array('object_id' => $isCore['value']));
  865. #$where['R'][end+1]['object_id'] = $isCore['value'];
  866. break;
  867. default:
  868. if(!is_array($where[$e])) $where[$e]=array();
  869. array_push($where[$e], array('collection_id' => $isCore['value']));
  870. break;
  871. }
  872. #if(!is_array($where[$e])) $where[$e]=array();
  873. #array_push($where[$e], array('collection_id' => $isCore['value']));
  874. }
  875. break;
  876. case 'I':
  877. #$subSpace=array('S','R','U','G','I');
  878. $subSpace=array('S','R','I');
  879. $from = array_intersect($from, $subSpace);
  880. foreach ($from as $e) {
  881. switch ($e) {
  882. case 'S':
  883. #16022010; avoided splitting the query
  884. #this is one of the few cases when we do want the object to be inthe same query as that for the predicate
  885. if(!is_array($where[$e])) $where[$e] =array('value'=>$isCore['value']);
  886. else {
  887. $where[$e][max(array_keys($where[$e]))]['value']=$isCore['value'];
  888. }
  889. #array_push($where['S'], array('value' => $isCore['value']));
  890. break;
  891. case 'R':
  892. array_push($where['R'], array('verb_id' => $isCore['value']));
  893. break;
  894. default :
  895. array_push($where[$e], array('item_id' => $isCore['value']));
  896. break;
  897. }
  898. }
  899. break;
  900. case 'S':
  901. #$subSpace=array('S','U','G');
  902. $subSpace=array('S');
  903. $from = array_intersect($from, $subSpace);
  904. foreach ($from as $e) {
  905. array_push($where[$e], array('statement_id' => $isCore['value']));
  906. }
  907. break;
  908. }
  909. #$from = array_intersect($from, $subSpace);
  910. }
  911. elseif(isSPARQLVar($triple[$crew_member]))#is subj var?
  912. {
  913. array_push($triple_vars, $triple[$crew_member]);
  914. }
  915. elseif(ereg('^http',$triple[$crew_member])) {
  916. #Is this an element of the CoreOntology
  917. $isOnt = isCoreOntology($triple[$crew_member]);
  918. if($isOnt)
  919. {
  920. $from = array($GLOBALS['s3codesInv'][strtolower($isOnt)]);
  921. $where[$GLOBALS['s3codesInv'][strtolower($isOnt)]]=array();
  922. }
  923. else {
  924. #to be parsed by SPARQL algebra;
  925. }
  926. }
  927. elseif(!ereg('^http',$triple[$crew_member])) {
  928. $ob = $triple[$crew_member];
  929. ereg('"(.*)"', $ob, $ob_parts);
  930. if($ob_parts) $ob=$ob_parts[1];
  931. foreach ($from as $e) {
  932. switch ($e) {
  933. case 'S':
  934. if(!is_array($where[$e])) $where[$e] =array('value'=>$ob);
  935. else {
  936. $where[$e][max(array_keys($where[$e]))]['value']=$ob;
  937. }
  938. #this is one of the few cases when we do want the object to be inthe same query as that for the predicate
  939. break;
  940. case 'R':
  941. #$where[$e][end]['object']=$triple[$crew_member]->label;
  942. break;
  943. }
  944. }
  945. }
  946. break;
  947. }
  948. }
  949. ##Once we go through all the triples, we should have reached a from and a where space; It's time to build the queries necessary for assigning values to variables; constraining the query space
  950. #fisrt thing first: let's think about efficiency? Is it the local deployment that is being queries? if so, let's call S3QLaction right here.
  951. if($s3ql['url']==S3DB_URI_BASE || $s3ql['url']==S3DB_URI_BASE.'/')
  952. {
  953. $s3ql['user_id']=$user_id;
  954. $s3ql['db']=$db;
  955. $remote=0;
  956. }
  957. else {
  958. $s3ql['user_id']=S3DB_URI_BASE.'/U'.$user_id;
  959. $remote=1;
  960. }
  961. $bQ=buildQuery(compact('s3ql','from','where','remote','performedQueries','varType','varTypeWhere','it'));
  962. extract($bQ);
  963. return(compact('remoteQueries','localQueries','S3QL','varType','varTypeWhere', 'element','collected_data','performedQueries'));
  964. }
  965. function buildQuery($bQ)
  966. {
  967. extract($bQ);
  968. global $timer;
  969. $element=array();
  970. $select_fields = array('P'=>'name', 'C'=>'collection_id,project_id,name,notes','R'=>'rule_id,project_id,subject_id,verb_id,object_id,object,notes', 'I'=>'item_id,collection_id,notes','S'=>'statement_id,rule_id,item_id,value');
  971. foreach ($from as $e) {
  972. $tri_s3ql = $s3ql;
  973. ##Let's only select a few fields, as the more triples there are, the more has to be outputed
  974. $tri_s3ql['select']=$select_fields[$e];
  975. $tri_s3ql['from']=switchFromCore($e);
  976. array_push($element, $e);
  977. if(!is_array($where[$e]) || empty($where[$e])) {
  978. ##this queries are useless unless they are on project
  979. #12TCGA
  980. if($e!='P')
  981. {
  982. $tri_s3ql=array();
  983. }
  984. else{
  985. if(!$remote)
  986. $tri_s3ql = array_filter(array_diff_key($tri_s3ql, array('user_id'=>'')));
  987. $query = S3QLQuery($tri_s3ql);
  988. $S3QL[] = $query;
  989. if($remote){
  990. $tri_s3ql['format'] = 'php';
  991. $remoteQueries[] = $query;
  992. }
  993. else {
  994. $start = strtotime('His');
  995. $localQueries[] = $tri_s3ql;
  996. }
  997. array_push($performedQueries, $e);
  998. }
  999. }
  1000. else {
  1001. for ($i=0; $i < count($where[$e]); $i++) {
  1002. $tmp = $where[$e][$i];
  1003. $ind= $i;
  1004. if($tmp)
  1005. $tri_s3ql['where']=$tmp;
  1006. #now stringize the query such that we can check if it has been built
  1007. #12TCGA
  1008. $stringized_query="";
  1009. foreach ($tri_s3ql['where'] as $w_name=>$w_value) {
  1010. if($stringized_query!="") $stringized_query .="&&";
  1011. else $stringized_query .="(";
  1012. $stringized_query .= $w_name.'='.$w_value;
  1013. }
  1014. if($stringized_query!="") $stringized_query .=")";
  1015. if(in_array($e.$stringized_query, $performedQueries)){
  1016. ##Do NOT perform this query again, it was already seen
  1017. $repeated = true;
  1018. }
  1019. else {
  1020. array_push($performedQueries, $e.$stringized_query);
  1021. ##TCGA change - due to localQueries, moved this section over here
  1022. #again... efficiency
  1023. if($remote){
  1024. $tri_s3ql['format'] = 'php';
  1025. $query = S3QLQuery($tri_s3ql);
  1026. $remoteQueries[] = $query;
  1027. }
  1028. else {
  1029. #$tri_s3ql = array_filter(array_diff_key($tri_s3ql, array('user_id'=>'')));
  1030. $localQueries[] = $tri_s3ql;
  1031. $query = S3QLQuery($tri_s3ql);
  1032. }
  1033. $S3QL[] = $query;
  1034. }
  1035. }
  1036. }
  1037. }
  1038. return (compact('remoteQueries','localQueries', 'S3QL', 'element','performedQueries'));
  1039. }
  1040. function buildAndExecuteQ($b)
  1041. {
  1042. extract($b);
  1043. $q = array();
  1044. $ans = array();
  1045. $queryModel = $model;
  1046. foreach ($from as $e) {
  1047. $tri_s3ql = $s3ql;
  1048. $tri_s3ql['select']='*';
  1049. $tri_s3ql['from']=switchFromCore($e);
  1050. if(!is_array($where[$e]) || empty($where[$e])) {
  1051. if($tri_s3ql['url']){
  1052. $tri_s3ql['format'] = 'rdf';
  1053. $query = S3QLQuery($tri_s3ql);
  1054. $rQ[] = $query;
  1055. }
  1056. else {
  1057. $start = strtotime('His');
  1058. $lQ[] = $tri_s3ql;
  1059. }
  1060. }
  1061. else {
  1062. for ($i=0; $i < count($where[$e]); $i++) {
  1063. $tmp = $where[$e][$i];
  1064. $ind= $i;
  1065. if($tmp)
  1066. $tri_s3ql['where']=$tmp;
  1067. #again... efficiency
  1068. if($tri_s3ql['url']){
  1069. $tri_s3ql['format'] = 'rdf';
  1070. $query = S3QLQuery($tri_s3ql);
  1071. $rQ[] = $query;
  1072. }
  1073. else {
  1074. $start = strtotime('His');
  1075. $lQ[] = $tri_s3ql;
  1076. }
  1077. }
  1078. }
  1079. }
  1080. if(is_array($lQ))
  1081. foreach ($lQ as $localQuery) {
  1082. $answer = localQ($tri_s3ql);
  1083. if(!empty($answer))
  1084. {
  1085. $rdfanswer = rdf2php($answer);
  1086. #Line up the answer with the model
  1087. $queryModel->addModel($rdfanswer);
  1088. }
  1089. }
  1090. if(is_array($rQ))
  1091. foreach ($rQ as $remoteQuery) {
  1092. $answer = remoteQ($q);
  1093. if(!empty($answer))
  1094. {
  1095. $rdfanswer = rdf2php($answer);
  1096. #Line up the answer with the model
  1097. $queryModel->addModel($rdfanswer);
  1098. }
  1099. }
  1100. return $queryModel;#$t is the array with the vars that were discovered in this triple
  1101. }
  1102. function scrubSPARQLVar($a,$b)
  1103. {
  1104. $c = ($a[$b]->uri!='')?$a[$b]->uri:$a[$b]->label;
  1105. return ($c);
  1106. }
  1107. function isDiscovered($v, $ans)
  1108. {
  1109. if(is_array($ans) && in_array($v, array_keys($ans)))
  1110. return ($ans[$v]);
  1111. else {
  1112. return (False);
  1113. }
  1114. }
  1115. function trimBasedOnObj($z)
  1116. {extract($z);
  1117. switch ($isCore['letter']) {
  1118. case 'P':
  1119. $from = array_intersect($from, array('C','R'));
  1120. foreach ($from as $e) {
  1121. if(!is_array($where[$e])) $where[$e]=array();
  1122. array_push($where[$e], array('project_id'=> $isCore['value']));
  1123. }
  1124. break;
  1125. case 'D':
  1126. $from = array_intersect($from, array('P','U', 'G'));
  1127. foreach ($from as $e) {
  1128. if(!is_array($where[$e])) $where[$e]=array();
  1129. array_push($where[$e], array('deployment_id'=> $isCore['value']));
  1130. }
  1131. break;
  1132. case 'G':
  1133. $from = array_intersect($from, array('U'));
  1134. foreach ($from as $e) {
  1135. if(!is_array($where[$e])) $where[$e]=array();
  1136. array_push($where[$e], array('group_id'=> $isCore['value']));
  1137. }
  1138. break;
  1139. }
  1140. return (array($from, $where));
  1141. }
  1142. function trimBasedOnPred($z)
  1143. {extract($z);
  1144. switch ($isCore['letter']){
  1145. case 'R':
  1146. #echo '<pre>';print_r($isCore['data']);exit;
  1147. $from=array('S');
  1148. if(!is_array($where['S'])) $where['S']=array();
  1149. array_push($where['S'], array('rule_id'=>$isCore['value']));
  1150. if($triple['object']->label!='')
  1151. {
  1152. $where['S'][max(array_keys($where['S']))]['value']=$triple['object']->label;#this is one of the few cases when we want the predicate and the object to work together int he same query
  1153. }
  1154. ##We can infer the "type" of subject to use in further queries by looking at the "from" part of the query
  1155. $varType[$triple['subject']][] = 'I';
  1156. $varType[$triple['predicate']][] = 'R';
  1157. $varType[$triple['object']][] = 'I';
  1158. ##in some cases, we can even know which collection/rule the item/statement belongs to
  1159. if($isCore['data'][0]['subject_id']){
  1160. $varTypeWhere[$triple['subject']][] = 'C'.$isCore['data'][0]['subject_id'];
  1161. }
  1162. if($isCore['data'][0]['object_id']){
  1163. $varTypeWhere[$triple['object']][] = 'C'.$isCore['data'][0]['object_id'];
  1164. }
  1165. break;
  1166. case 'I':
  1167. $from=array('I');
  1168. if(!is_array($where['R'])) $where['R']=array();
  1169. array_push($where['R'], array('verb_id'=>$isCore['value']));
  1170. ##We can infer the "type" of subject to use in further queries by looking at the "from" part of the query
  1171. $varType[$triple['subject']][] = 'C';
  1172. $varType[$triple['predicate']][] = 'I';
  1173. $varType[$triple['object']][] = 'C';
  1174. break;
  1175. }
  1176. return (array($from, $where,$varType,$varTypeWhere));
  1177. }
  1178. function trimBasedOnSub($s)
  1179. {extract($s);
  1180. $from = array_intersect($fromSpace, array($isCore['letter']));
  1181. if(!is_array($where[$isCore['letter']]))
  1182. $where[$isCore['letter']] = array();
  1183. switch ($isCore['letter']) {
  1184. case 'P':
  1185. array_push($where['P'], array('project_id' => $isCore['value']));
  1186. break;
  1187. case 'C':
  1188. #collection cen be the subject of a rule
  1189. array_push($from, 'R');
  1190. array_push($where['R'], array('subject_id' => $isCore['value']));
  1191. break;
  1192. case 'I':
  1193. #item can be the subject of a statemnet
  1194. array_push($from, 'S');
  1195. if(!is_array($where['S']))
  1196. $where['S'] = array();
  1197. array_push($where['S'], array('item_id'=>$isCore['value']));
  1198. array_push($where['I'], array('item_id'=>$isCore['value']));
  1199. break;
  1200. break;
  1201. }
  1202. return (array($from, $where));
  1203. }
  1204. function isCoreOntology($uri)
  1205. {
  1206. if(ereg('^http://www.s3db.org/core.owl#s3db(.*)', $uri,$ont))
  1207. {return ($ont[1]);
  1208. }
  1209. else {
  1210. return False;
  1211. }
  1212. }
  1213. function hasNotation($uri,$qname,$prefixes)
  1214. { $url = $prefixes[$qname];
  1215. if(ereg('<'.$url.'(.*)>', $uri,$ont))
  1216. {return ($ont[1]);
  1217. }
  1218. else {
  1219. return False;
  1220. }
  1221. }
  1222. function executeQuery($queryModel,$sparql_triple,$discovered,$format)
  1223. {global $timer;
  1224. $tripleData = $queryModel->sparqlQuery($sparql_triple);
  1225. if(!empty($tripleData)){
  1226. foreach ($tripleData as $datakey=>$datavar) {
  1227. foreach ($datavar as $valName=>$varVal) {
  1228. if(is_object($datavar[$valName]))
  1229. {
  1230. if($format!='xml')
  1231. {
  1232. $tripleData[$datakey][$valName] = (($tripleData[$datakey][$valName]->uri!='')?$tripleData[$datakey][$valName]->uri:$tripleData[$datakey][$valName]->label);
  1233. $discoveredData[$valName][] = $queryModel;
  1234. $discovered[$valName][] = $tripleData[$datakey][$valName];
  1235. }
  1236. else
  1237. {$newVarName = ereg_replace('^\?', '', $valName);$oldVarName = $valName;
  1238. $tripleData[$datakey][$newVarName]=($tripleData[$datakey][$valName]->uri!='')?$tripleData[$datakey][$valName]->uri:$data[$datakey][$valName]->label;
  1239. $tripleData[$datakey][$oldVarName]='';
  1240. $tripleData[$datakey]=array_filter($tripleData[$datakey]);
  1241. $discovered[$valName][] = $tripleData[$datakey];
  1242. $discoveredData[$valName][] = $tripleData;
  1243. }
  1244. }
  1245. }
  1246. }
  1247. $data[] = $tripleData;
  1248. }
  1249. return (array($data,$discovered, $discoveredData,$queryModel));
  1250. }
  1251. function remoteQ($q){
  1252. $b = strtotime(date('His'));
  1253. $c = fopen($q, 'r');
  1254. $answer = stream_get_contents($c);
  1255. return ($answer);
  1256. }
  1257. function localQ($tri_s3ql){
  1258. $query = S3QLAction($tri_s3ql);
  1259. ##Now force the RDF output
  1260. $format=$tri_s3ql['format'];
  1261. $data = $query;
  1262. $db=$tri_s3ql['db'];
  1263. if(is_array($query[0])){
  1264. $cols = array_keys($query[0]);
  1265. $letter = letter($tri_s3ql['from']);
  1266. $z = compact('data','cols','format', 'db','letter');
  1267. $answer = outputFormat($z);
  1268. }
  1269. else {
  1270. $answer = array();
  1271. }
  1272. return ($answer);
  1273. }
  1274. function iterationOrder($triples,$pref=array(),$return_order=false)
  1275. {
  1276. /**
  1277. * @author Helena F Deus <helenadeus@gmail.com>
  1278. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  1279. * @package S3DB http://www.s3db.org
  1280. */
  1281. #Find triple order is based on the premisse that the more contsnts the system has, the more likely it is to trim down the query. So this simple function scores the number of constants in each triple and re-sorts them as each triple is being solved and providing constants for the remaining triples
  1282. #break and reorder the triples; retaining the original order will be important
  1283. if(count($triples)>=1){
  1284. for ($i=0;$i<count($triples);$i++) {
  1285. #before multisored
  1286. $unscrambled[$triples[$i]]=$i;
  1287. $triple = trim($triples[$i]);
  1288. $solver = explode(' ',$triple);
  1289. list($s,$p,$o) = $solver;
  1290. $puzzle = array(!ereg('^\?',$s),!ere