如何使用 ARC2 查询 ontology?

How to query an ontology with ARC2?

我正在使用这门艺术从事语义 Web 项目(我在这个主题上完全是新手)ontology:http://spraakdata.gu.se/svedd/painting-ontology/painting.owl 使用 SPARQL 和 PHP 库 ARC2 .我正在使用以下代码。它有效,但结果不完整:

<?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;

}


?>

在这种情况下,查询会查找所有具有谓词 "createdBy" 的三元组。 我只得到两个结果:

但我可以在 ontology 中看到有很多画都带有那个谓词,只有那些。但是他们没有 属性 "label",例如 "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>

有些绘画具有特定结构和一些重要属性(如标签或创建者),但其他绘画则没有,这使得处理起来更加困难。我怎样才能搜索像这样的 ontology?。难道我做错了什么?。 ontology 结构不好吗?

由于部分匹配第一个三元组模式的资源没有rdfs:label,您可以使用OPTIONAL获取所有结果,无论是否定义了标签。

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

关于 SPARQL 语法的注释。 . 表示三重模式的结尾。 ; 表示使用前一个三重模式的主题来创建三重模式。我不确定您的 SPARQL 解析器将如何处理 ;.