You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不实例化类的情况下查询带属性限制的类的个体?

编辑:解决方案如下

需求描述

我需要查找属于某类的个体,该类对另一类存在特定对象属性限制。在owlready2中,我可以搜索所有类的INDIRECT_is_a并进行筛选,但这种方法效率低下,因为我必须创建并搜索所有类的祖先树,而非直接通过hasRelation的实例(Protégé能轻松实现此操作)。

我找到最接近的方案,但如果不想指定对象属性的定义域和值域,而是将类的限制指定为searched_class hasRelation some object_class,该如何操作?

换而言之,SPARQL查询核心需求:哪些个体所属的类(含继承的)存在hasRelation some test:object_class限制?

测试本体代码

RDF格式

<?xml version="1.0"?>
<rdf:RDF xmlns="urn:absolute:test_onto#"
     xml:base="urn:absolute:test_onto"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    <owl:Ontology rdf:about="urn:absolute:test_onto"/>
    <!-- urn:absolute:test_onto#hasExactRelation -->
    <owl:ObjectProperty rdf:about="urn:absolute:test_onto#hasExactRelation"/>
    <!-- urn:absolute:test_onto#hasSomeRelation -->
    <owl:ObjectProperty rdf:about="urn:absolute:test_onto#hasSomeRelation"/>
    <!-- urn:absolute:test_onto#child_class -->
    <owl:Class rdf:about="urn:absolute:test_onto#child_class">
        <rdfs:subClassOf rdf:resource="urn:absolute:test_onto#parent_class"/>
    </owl:Class>
    <!-- urn:absolute:test_onto#object_class -->
    <owl:Class rdf:about="urn:absolute:test_onto#object_class"/>
    <!-- urn:absolute:test_onto#parent_class -->
    <owl:Class rdf:about="urn:absolute:test_onto#parent_class">
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="urn:absolute:test_onto#hasSomeRelation"/>
                <owl:someValuesFrom rdf:resource="urn:absolute:test_onto#object_class"/>
            </owl:Restriction>
        </rdfs:subClassOf>
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="urn:absolute:test_onto#hasExactRelation"/>
                <owl:qualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">1</owl:qualifiedCardinality>
                <owl:onClass rdf:resource="urn:absolute:test_onto#object_class"/>
            </owl:Restriction>
        </rdfs:subClassOf>
    </owl:Class>
    <!-- urn:absolute:test_onto#instance -->
    <owl:NamedIndividual rdf:about="urn:absolute:test_onto#instance">
        <rdf:type rdf:resource="urn:absolute:test_onto#child_class"/>
    </owl:NamedIndividual>
</rdf:RDF>

Turtle格式

@prefix : <urn:absolute:test_onto#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <urn:absolute:test_onto> .

<urn:absolute:test_onto> rdf:type owl:Ontology .

:hasExactRelation rdf:type owl:ObjectProperty .

:hasSomeRelation rdf:type owl:ObjectProperty .

:child_class rdf:type owl:Class ;
             rdfs:subClassOf :parent_class .

:object_class rdf:type owl:Class .

:parent_class rdf:type owl:Class ;
              rdfs:subClassOf [ rdf:type owl:Restriction ;
                                owl:onProperty :hasSomeRelation ;
                                owl:someValuesFrom :object_class
                              ] ,
                              [ rdf:type owl:Restriction ;
                                owl:onProperty :hasExactRelation ;
                                owl:qualifiedCardinality "1"^^xsd:nonNegativeInteger ;
                                owl:onClass :object_class
                              ] .

:instance rdf:type owl:NamedIndividual ,
                   :child_class .

尝试的无效SPARQL查询

prefix :        <http://example.org/test_onto#>
prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>

select ?x where { 
  [ ?x a owl:Class ] :hasSomeRelation some :object_class .
}

该查询无法运行,原因是未实例化object_class。

有效解决方案

要实现需求,需在SPARQL中遍历类的继承关系,并直接匹配OWL限制的RDF结构,无需实例化object_class。正确查询如下:

prefix : <urn:absolute:test_onto#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?individual where {
  # 匹配个体及其所属的类(含继承关系)
  ?individual a ?class .
  ?class rdfs:subClassOf* ?restrictedClass .
  
  # 匹配类对应的someValuesFrom限制结构
  ?restrictedClass rdfs:subClassOf [
    a owl:Restriction ;
    owl:onProperty :hasSomeRelation ;
    owl:someValuesFrom :object_class
  ] .
}

查询说明

  1. ?class rdfs:subClassOf* ?restrictedClass:通过传递性遍历类的所有祖先类,确保包含继承的属性限制。
  2. 直接匹配OWL限制的RDF结构:查找作为restrictedClass子类的owl:Restriction,且该限制指定了目标属性:hasSomeRelation和目标类:object_class。
  3. 测试本体中的:instance会被匹配到——它属于:child_class,而:child_class继承自:parent_class,后者包含目标限制。

内容的提问来源于stack exchange,提问作者Philipp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 03:48:35