简体   繁体   English

SPARQL查询以检索个人

[英]SPARQL query for retrieving individuals

Please help me with the SPARQL query. 请帮助我进行SPARQL查询。 I have an ontology with the class 'Building' as a subclass of owl:Thing . 我有一个以owl:Thing类为owl:Thing子类的本体。 'Building' has it's own sub classes like: Church, Medical, Shop. “建筑”拥有自己的子类,例如:教堂,医疗,商店。 Each of these sub classes has it's own labels ( seeAlso ). 这些子类中的每一个都有其自己的标签(请seeAlso )。 For instance: Shop has labels like supermarket, bakery, market etc. Church has labels like Chapel, Cathedral etc. 例如:商店带有超市,面包店,市场等标签,教堂带有教堂,大教堂等标签。

在此处输入图片说明

The Individuals look like this: 个人看起来像这样:

在此处输入图片说明

I need to do a SPARQL query, which will retrieve the individuals according to the labels. 我需要执行SPARQL查询,该查询将根据标签检索个人。 So let's say I want to get all the individuals of that subclass which has the labels of type seeAlso "bakery". 因此,假设我要获得该子类的所有个体,这些个体的标签类型为seeAlso “ bakery”。 In this case I expect to get bakery1 and supermarket1 在这种情况下,我希望有面包店1和超级市场1

I have tried this, but it seems like this query is wrong: 我已经尝试过了,但是看起来这个查询是错误的:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?individual ?class ?label
WHERE { 
    ?individual rdf:type owl:NamedIndividual .
    ?class rdf:type owl:Class .
    FILTER(?label="bakery")
}

If I delete the line with FILTER , I will get only the individuals and classes, but not related to each other as they should. 如果我用FILTER删除该行,则只会得到个人和类,而不会像他们应该的那样相互关联。 Protege just returns me all the possible connections Class - Individual Protege会退还我所有可能的联系类-个人

Given comments to the answer, the intent is to build synonyms, not separate instances. 给定答案的注释,目的是构建同义词,而不是单独的实例。 There are a few ways to do this, but SKOS is specifically designed for such vocabulary relationships. 有几种方法可以做到这一点,但是SKOS是专门为此类词汇关系设计的。 The property skos:prefLabel is used for the display label, and synonyms can be defined by skos:altLabel . 属性skos:prefLabel用于显示标签,并且同义词可以由skos:altLabel定义。 The hierarchy you have can be retained, just use skos:altLabel instead of 'rdf:seeAlso' (which is normally used for reference links ourtide of the ontology). 可以保留您拥有的层次结构,只需使用skos:altLabel而不是'rdf:seeAlso'(通常用于本体的参考链接)。

So the data, in a Turtle text serialization would look like: 因此,在Turtle文本序列化中,数据如下所示:

:Shop rdfs:subClassOf :Building .
:Shop skos:prefLabel "Shop"^^xsd:string .
:Shop skos:altLabel "supermarket"^^xsd:string .
:Shop skos:altLabel "bakery"^^xsd:string .
:Shop skos:altLabel "market"^^xsd:string .

For the query, the OP says to match by the string - the preflabel in this case: 对于查询,OP表示要匹配字符串-在这种情况下为preflabel

SELECT ?individual ?label
WHERE {
   ?individual skos:prefLabel "Shop"^^xsd:string .
   ?individual skos:altLabel ?label
}

I think what you are looking for is all of the instances of the class :Building and its subclasses. 我认为您正在寻找的是该类的所有实例:Building及其子类。 Also note that if ?label is not bound, the filter will always fail. 另请注意,如果未绑定?label ,则过滤器将始终失败。 So you'll need to do the following: 因此,您需要执行以下操作:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>SELECT 
WHERE {
   ?cls rdfs:subClassOf* :Building .
   ?individual a ?cls .
   ?individual rdfs:seeAlso ?label .
   FILTER (?label = "bakery")
}

…and that is a classic anti-pattern for SPARQL. ……这是SPARQL的经典反模式。 As much as possible, you should use basic graph patterns, so this is a more correct query: 您应尽可能使用基本的图形模式,因此这是一个更正确的查询:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>SELECT 
WHERE {
   ?cls rdfs:subClassOf* :Building .
   ?individual a ?cls .
   ?individual rdfs:seeAlso "bakery" .
}

Note the third triple pattern uses the literal in the triple pattern. 请注意,第三个三重模式使用三重模式中的文字。 Also, I don't think the model you have should be using rdfs:seeAlso for the label of the instance. 另外,我认为您拥有的模型不应该使用rdfs:seeAlso作为实例的标签。 Instead I'd suggest a separate instance for each kind Shop, etc. and use rdfs:label to represent the label of the instance. 相反,我会为每种Shop等建议一个单独的实例,并使用rdfs:label表示该实例的标签。

Lastly, if you need to do an inexact match you can use regex . 最后,如果您需要进行不精确的匹配,则可以使用regex For example to find "bakery" regardless of case, use the following: 例如,要查找“面包店”(无论大小写),请使用以下命令:

#-- …
?individual rdfs:seeAlso ?label .
FILTER regex(?label, "Bakery", "i")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM