我正在使用这个艺术本体开发一个语义Web项目(我对这个主题完全是新手):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”的所有三元组。 我得到两个结果:
但我可以在本体论中看到,有许多画作只有那些谓词noy。但是他们没有属性“标签”,例如“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>
有些画作具有某些结构和一些重要属性(如label或createdBy),但有些则没有,这使得处理起来更加困难。我怎样才能搜索像这样的本体?难道我做错了什么?。本体是否构建得很糟糕?
答案 0 :(得分:1)
由于与第一个三重模式匹配的某些资源没有rdfs:label
,因此您可以使用OPTIONAL
获取所有结果,无论是否已定义标签。
SELECT *
WHERE {
?subject painting:createdBy ?object .
OPTIONAL { ?subject rdfs:label ?title .}
}
关于SPARQL语法的说明。 .
表示三重模式的结束。 ;
表示来自先前三重模式的主题用于创建三重模式。我不确定您的SPARQL解析器将对;.
执行什么操作。