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

/s3db3.5.10/s3dbcore/sparql_read5.php

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