Redleafar Redleafar - 4 years ago 95
PHP Question

How to query an ontology with ARC2?

I'm working on a semantic web project (i'm a totally novice on the subject) with this art ontology: http://spraakdata.gu.se/svedd/painting-ontology/painting.owl using SPARQL and the PHP library ARC2. I'm using the following code . It works but the results are not complete:

<?php

include_once("arc2/ARC2.php");

$config = array(
/* db */
'db_name' => 'ontologia',
'db_user' => 'root',
'db_pwd' => '',
/* store */
'store_name' => 'arc_tests',
/* stop after 100 errors */
'max_errors' => 100,
);
$store = ARC2::getStore($config);
if (!$store->isSetUp()) {
$store->setUp();
}

/* LOAD will call the Web reader, which will call the
format detector, which in turn triggers the inclusion of an
appropriate parser, etc. until the triples end up in the store. */
//$store->query('LOAD <http://localhost/ontologia/painting2.owl.xml>');


$q = '
PREFIX painting: <http://spraakbanken.gu.se/rdf/owl/painting.owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT *
WHERE {
?subject painting:createdBy ?object;
rdfs:label ?title.

}';

$r = '';
$id = 0;
if ($rows = $store->query($q, 'rows')) {
/* display the results in an HTML table */
echo "<table border='1'>
<thead>
<th>#</th>
<th>(Subject)</th>
<th>title</th>

</thead>";

/* loop for each returned row */
foreach( $rows as $row ) {
print "<tr><td>".++$id. "</td>
<td>" . $row['subject']. "</td>".
"<td>" . $row['title']. "</td>";
}
echo "</table>" ;
}
else{
echo "No hay resultados";
}

//$rs = $store->query('...');
$p='';
if ($errs = $store->getErrors()) {
/* $errs contains errors from the store and any called
sub-component such as the query processor, parsers, or
the web reader */
foreach ($errs as $err) {
$p .= '<li>' . $err. '</li>';
}
echo $p;

}


?>


In this case the query looks for all the triples with the predicate "createdBy".
I get just two results:

enter image description here

but i can see in the ontology that there are many paintings with that predicate noy only those. However they don't have the property "label", For instance the "Guernica":

<owl:NamedIndividual rdf:about="http://spraakbanken.gu.se/rdf/owl/painting.owl#GuernicaObj">
<rdf:type rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#Painting"/>
<rdf:type rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#Reference"/>
<hasIntendedUse rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#Attention"/>
<hasColor rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#Black"/>
<hasColor rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#Gray"/>
<hasCreationDate rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#GuernicaCreationDate"/>
<hasDimension rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#GuernicaHeight"/>
<hasRepresentation rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#GuernicaJPG"/>
<hasTitle rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#GuernicaTitle"/>
<displayedAt rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#InternationalExposition"/>
<hasCurrentLocation rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#MuseoReinaSofía"/>
<createdBy rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#PabloPicasso"/>
<hasPaintType rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#StandOil"/>
<hasColor rdf:resource="http://spraakbanken.gu.se/rdf/owl/painting.owl#White"/>
</owl:NamedIndividual>


Some paintings have certain structures and some important properties (like label or createdBy) but others don't, which makes it harder to process. How can I search an ontology like this one?. Am I doing something wrong?. Is the ontology badly constructed?

Answer Source

Since some of the resources matching the first triple pattern do not have rdfs:label, you can use OPTIONAL to get all results, whether or not a label has been defined.

SELECT *
WHERE {  
   ?subject painting:createdBy ?object .
   OPTIONAL { ?subject rdfs:label ?title .}
}

A note about SPARQL syntax. A . means the end of a triple pattern. A ; means the subject from the previous triple pattern is used to create the triple pattern. I'm not sure what your SPARQL parser will do with ;..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download