PageRenderTime 90ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/s3dbcore/sparql_read4.php

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