2011-06-17 21 views
2

我正在使用ARC2库的PHP来发布sparql查询,并且我在这个问题上陷入了困境(我不认为它有东西与lib一起做)。Sparql查询出错了导致不正确的地理位置+不需要的属性结果我需要

此查询工作就好了 - 在我的应用程序,并在DBpedia中snorql:

PREFIX dbo:<http://dbpedia.org/ontology/> 
PREFIX owl: <http://www.w3.org/2002/07/owl#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX dc: <http://purl.org/dc/elements/1.1/> 
PREFIX : <http://dbpedia.org/resource/> 
PREFIX dbpedia2: <http://dbpedia.org/property/> 
PREFIX dbpedia: <http://dbpedia.org/> 
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 
PREFIX geo: <http://www.geonames.org/ontology#> 

SELECT * WHERE { 
?c rdf:type dbo:Country; 
foaf:name "someCountryName"@en. 
} 

在另一方面,该查询不起作用:

SELECT * WHERE { 
?c rdf:type dbo:Country; 
foaf:name "someCountryName"@en; 
geo:lat ?lat. 
} 

注:查询使用与上面列出的相同的前缀完成。 我只需要采取拉长&长的一个国家。我也可以尝试Freebase,但我真的需要在这里工作。第二个查询在snorql中工作,看不出为什么它不能在我的应用程序中工作? 任何帮助非常感谢!

+1

现在8个小时已经结束了,你可以拆分问题和答案吗?并再次让标题问题成为一个问题? (寻求没有未解答的SPARQL问题) –

+0

罗马尼亚的'dissolutionDate'技巧也出现在[这个问题]中(http://stackoverflow.com/q/5921884/1281433)。 –

回答

0

注:本文是由@IrinaM提供的,但投入的问题为a revision因为八个小时的限制。现在已经晚了很多,并且@IrinaM作为答案发布的建议尚未采取行动。这是一个包含该文本的轻微编辑的CW回答。

问题中的代码有几处错误。

  1. 的2 第二查询(在一个没有“工作”)将返回几个结果。如果我使用foaf:name "Romania"搜索国家,它将返回"Communist Romania","Wallachian Romania","Romania"等。在这些结果中,只有一个具有geo:latgeo:long。 基本上,当所有结果都不满足要求的属性时,则不会返回任何结果。

  2. 错误的本体用于geo;它应该是<http://www.w3.org/2003/01/geo/wgs84_pos#>

  3. 其他奇怪的结果需要过滤掉,只保留所需的唯一结果。在这种情况下,dbpedia-owl:dissolutionDate无效后进行筛选。 使用FILTER (?name="someCountryName"^^xsd:string)的foaf:name的精确匹配不起作用。

这里的代码,成功地检索某一国家的纬度和经度值(注意,可能需要一些其他的过滤器正确的国家搜索时):

//setup prefixes 
$prefixes = 'PREFIX dbo:<http://dbpedia.org/ontology/> 
PREFIX owl: <http://www.w3.org/2002/07/owl#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX dc: <http://purl.org/dc/elements/1.1/> 
PREFIX : <http://dbpedia.org/resource/> 
PREFIX dbpedia2: <http://dbpedia.org/property/> 
PREFIX dbpedia: <http://dbpedia.org/> 
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> 
';  
//query no. 1 - find out "right" country - hoping for a unique result 
$q1 = $prefixes.'SELECT ?c WHERE { 
?c rdf:type dbo:Country; 
foaf:name "'.$userPreferences->country.'"@en. 
OPTIONAL {?c dbo:dissolutionDate ?x} 
FILTER(!bound(?x))}'; 
//note: $userPreferences->country represents a country entered by the user in an input 

//query no. 2 - find out long & lat 
$q2 = $prefixes.'SELECT * WHERE { 
<'.$countryReturned.'> geo:lat ?lat; 
geo:long ?long. 
}'; 
//note: $countryReturned represents the URI of the "unique" country my 1st query 
//retrieved, we use it directly. 

对于那些激情关于ARC2 PHP lib,这是如何进行查询的(注意:不要忘了包括ARC2.php文件):

$dbpediaEndpoint = array('remote_store_endpoint' =>'http://dbpedia.org/sparql'); 
$dbpediaStore = ARC2::getRemoteStore($dbpediaEndpoint); 
$rows1 = $dbpediaStore->query($q1,'rows'); 
var_dump($rows1); //to see quick results