简体   繁体   English

Jena Sparql仅返回实例的直接超类

[英]Jena Sparql only return direct superclass of instance

I'm using Jena in Java with Pellet as the reasoner. 我正在Java中使用Penat作为推理机的Jena。 Now I need to query the class of a specific URI, but the problem is is that I don't know before hand what kind of instance it is, in what kind of class hierarchy, so I can't use filter options. 现在,我需要查询特定URI的类,但是问题是我事先不知道它是什么样的实例,什么样的类层次结构,所以我不能使用过滤器选项。 When using: 使用时:

SELECT * WHERE {<" + uri + "> rdf:type ?x}

It returns all superclasses. 它返回所有超类。 My hierarchy for this specific example looks like: 此特定示例的层次结构如下:

-Thing
 -Person
  -Adult
 -Animal
  -Dog

Every class has multiple instances. 每个类都有多个实例。 I don't know what kind of instance I'm querying beforehand. 我不知道我要事先查询哪种实例。 But let's say for example I'm querying an URI of an instance who is a type Dog. 但是,例如,假设我要查询类型为Dog的实例的URI。 It will also return Animal and Thing. 它还将返回Animal and Thing。 But I only want it to return Dog. 但我只希望它还给Dog。 Is that possible? 那可能吗? Using LIMIT 1 only returns the direct superclass. 使用LIMIT 1仅返回直接超类。

There is an easy way to do this with Jena which provides a special predicate to query for direct types so in your code you can generate the query as follows: 使用Jena可以轻松实现这一目的,它提供了一个特殊的谓词来查询直接类型,因此可以在代码中按以下方式生成查询:

  "SELECT * { <" + ind + "> <" + ReasonerVocabulary.directRDFType + "> ?type }"

or simply use the URI for this predicate directly in the query: 或直接在查询中直接为此谓词使用URI:

  SELECT * { <ind> <urn:x-hp-direct-predicate:http_//www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type }

It is also possible to write a rather inefficient but portable SPARQL query that will query direct types: 还可以编写效率不高但可移植的SPARQL查询,该查询将查询直接类型:

  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
  PREFIX owl: <http://www.w3.org/2002/07/owl#> 
  SELECT * { 
    <ind> rdf:type ?directType .
    FILTER NOT EXISTS {
      <ind> rdf:type ?type .
      ?type rdfs:subClassOf ?directType .
      FILTER NOT EXISTS {
         ?type owl:equivalentClass ?directType .
      }
    }
  }

A class is a direct type of an individual if there is no other type of the individual that is a subclass of this type. 如果没有其他类型的个体是此类的子类,则该类是个体的直接类型。 But a direct type might have equivalent classes. 但是直接类型可能具有等效的类。 To handle these cases we have to use double negation in the query. 为了处理这些情况,我们必须在查询中使用双重否定。 This query is inefficient because it first retrieves all the types of the individual and filters the ones that are not direct. 该查询效率低下,因为它首先检索个人的所有类型,然后过滤非直接类型的个人。 But on the plus side you can use it with systems other than Jena that does not provide a dedicated direct type predicate. 但是从好的方面来说,您可以将其与Jena之外的其他系统一起使用,该系统不提供专用的直接类型谓词。

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

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