Parsing variable to SPARQL and get the result

Events happening in the community are now at Drupal community events on www.drupal.org.
mr.aizek's picture

Hi community. I am a research student and currently looking at RAP. I have this code as below but with some problems

:: CODE ::

<?php
// Determine the query execution runtime
function getmicrotime()
  {
      list(
$usec, $sec) = explode(" ",microtime());
        return ((float)
$usec + (float)$sec);
   }
 
$start = getmicrotime();
  
  
// Include RAP
define("RDFAPI_INCLUDE_DIR", "C:/xampp/htdocs/monokb/rdfapi-php/api/");
include (
RDFAPI_INCLUDE_DIR . "RdfAPI.php");
include (
RDFAPI_INCLUDE_DIR . "sparql/SparqlEngine.php");
//include (RDFAPI_INCLUDE_DIR . 'sparql/SparqlParser.php');
include (RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb/SqlGeneratorException.php');
include (
RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb/FilterGenerator.php');

   
                  
// Connect to MySQL database with user defined connection settings
$database = ModelFactory::getDbStore('MySQL', 'localhost', 'monogeneankb', 'root', '');
                 
                  
// Load a DbModel
$dbModel = $database->getModel("Monogenea");
                  
  
// Check whether the model is exist or not
if ($dbModel === false) {
      die(
'Database does not have a model ' . $dbModel . "\n");
  }
             
              
//received data
$name="Oxyurida";
$order=$_POST['name'];
                             

//SPARQL query
$querystring = '
PREFIX Monogenea <http://202.185.70.191:8080/Monogenean/ontology/Monogenea.owl#>
PREFIX rdf <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT literal(?order)
WHERE    {?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://202.185.70.191:8080/Monogenean/ontology/Monogenea.owl#Order> .
?x <http://202.185.70.191:8080/Monogenean/ontology/Monogenea.owl#Name> ?order .
FILTER  (?order == \"${"order"}\")
}'
;
        

$result = $dbModel->sparqlQuery($querystring);
sort($result);
        
          
// View the execution runtime
echo "<br/>";
echo
"<b>The query took: </b>".(getmicrotime() - $start)." <b>seconds to return this result.</b>";
echo
"<br/>";
echo
"<br/>";
           
          
// Output the query result in html format
SPARQLEngine::writeQueryResultAsHtmlTable($result);
                
                  
// Close the connection
$database->close();
?>

When I run this code, the output is like this:

The query took: 0.126912117004 seconds to return this result.

No. ?order
1. Literal: Dactylogyridea
2. Literal: Echinostomida
3. Literal: Mazocraeidea
4. Literal: Oxyurida
5. Literal: Plagiorchiida
6. Literal: Polyopisthocotylea
7. Literal: Polystomatidea
8. Literal: Unknown

As you can see from my code, I wanted to get only "Oxyurida" rather than the whole 8 Orders as listed above.

I believe, the problem is from these lines:
$name="Oxyurida";
$order=$_POST['name']
.
.
.
.
.
FILTER (?order == \"${"order"}\")

When I omitted one the the "=" from FILTER (?order == \"${"order"}\"), the output is like this:

"array(3) { ["type"]=> string(5) "value" ["value"]=> string(1) "{" ["quoted"]=> bool(false) }
Fatal error: Uncaught exception 'SparqlEngineDb_SqlGeneratorException' with message 'Unexpected value "{" (expected datatype)' in C:\xampp\htdocs\monokb\rdfapi-php\api\sparql\SparqlEngineDb\FilterGenerator.php:186 Stack trace: #0 C:\xampp\htdocs\monokb\rdfapi-php\api\sparql\SparqlEngineDb\FilterGenerator.php(118): SparqlEngineDb_FilterGenerator->createValue(Array, false) #1 C:\xampp\htdocs\monokb\rdfapi-php\api\sparql\SparqlEngineDb\FilterGenerator.php(294): SparqlEngineDb_FilterGenerator->createTreeSql(Array, Array) #2 C:\xampp\htdocs\monokb\rdfapi-php\api\sparql\SparqlEngineDb\FilterGenerator.php(109): SparqlEngineDb_FilterGenerator->createEquation(Array) #3 C:\xampp\htdocs\monokb\rdfapi-php\api\sparql\SparqlEngineDb\FilterGenerator.php(97): SparqlEngineDb_FilterGenerator->createTreeSql(Array, NULL) #4 C:\xampp\htdocs\monokb\rdfapi-php\api\sparql\SparqlEngineDb\SqlGenerator.php(232): SparqlEngineDb_FilterGenerator->createFilterSql(Array, false, 0) #5 C:\xampp\htdocs\monokb\rdfapi-php\api\sparql\SparqlEngineDb.php(141): in C:\xampp\htdocs\monokb\rdfapi-php\api\sparql\SparqlEngineDb\FilterGenerator.php on line 186"

My question is how can I get "Oxyurida"? Are those lines are correct codes?

Please help.

Thank you.

from Isaac Alfred Simbun,
MALAYSIA.

Comments

My contact

mr.aizek's picture

I can be contacted directly through mr.aizek@yahoo.com

not related to drupal

Remon's picture

I think you should better be asking for help from RAP API community or docs, this question is not related to drupal :)

Indeed, a strange place to

milesw's picture

Indeed, a strange place to post this question. I learned a lot about SPARQL parsing etc. by playing around with Drupal's SPARQL module. You might want to try it out.

In your issue you were actually able to get results from your query though. Looks like you just need to fix the filter expression.

Try:
FILTER regex(?order, "Oxyurida", "i")

If that doesn't work I'd suggest looking through the SPARQL reference.

Milesw

mr.aizek's picture

Hi there.

Thanks for the comment and suggestion.

I must say, I have managed to get the answer and my "semantic search engine" using RAP (RDF API for PHP) is now working correctly.

:)

I've tried this "FILTER regex(?order, "Oxyurida", "i")" and it wasn't working.

But as I have said, I have got the answer.

:)

Cheers~
Alfred Simbun,
MALAYSIA.

parsing variable

dien jogja's picture

i am learn how to use variable php in sparql or rdql in RDF API FOR PHP(RAP)...?
in this problem like with my problem, query in variable $querystring. i dont know how to use variable $order=$_POST['name']; in query sparql.
i think, in RAP any class for use variable php to parsing into variable in sparql, but i don't know that.

FILTER (?order == \"${"order"}\"), this not working.

As Remon says above, I think

linclark.research's picture

As Remon says above, I think you should better be asking for help from RAP API community or docs, this question is not related to drupal :)

I don't think that (m)any of the drupal modules for RDF stuff use RAP.

Semantic Web

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: