如何通过Sparql查询在耶拿推理 [英] how to do reasoning in Jena via Sparql Query

查看:80
本文介绍了如何通过Sparql查询在耶拿推理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jena和Sparql查询本体文件.

I'm using Jena and Sparql to query the ontology file.

我有

  1. class带有两个子类的标记:C ++和Java.
  2. class带有多个子类的学科,代表特定的大学学科:"C ++编程",系统编程","Java编程"等.
  3. ObjectProperty具有标签"域:主题范围:标签.每个课程主题都有​​一些标签,例如"Java","C ++"

执行查询时

SELECT ?subject
WHERE
{  ?subject owl:equivalentClass ?restriction .
   ?restriction owl:onProperty ont:hasTags .
   ?restriction ?restrictType ont:Java
}

它代表我成功接收所有带有标签"Java"的主题.

which stands for receiving all subjects with tag "Java" I succeed.

因此,目标是通过查询标签"来接收所有标有"Java"和"C ++"的主题,如下所示:

So, the aim is to receive all subjects tagged with "Java" and "C++", via quering for "Tag", like this:

SELECT ?subject
WHERE
{  ?subject owl:equivalentClass ?restriction .
   ?restriction owl:onProperty ont:hasTags .
   ?restriction ?restrictType ont:Tag
}

我认为该查询将返回所有标记有"Java"或"C ++"的实体,但不返回任何内容.

I supposed this query would return all entities tagged with "Java" or "C++", but it returns nothing.

我想接收带有标签"Java"或"C ++"的对象,在查询中仅编写"Tag".为此,我必须做些什么?Jena API是否有可能?

I want to receive objects with tags "Java" or "C++", writing just "Tag" in a query. What I have to do to achieve this, and is it possible with Jena API?

UPD:这是我使用RDF/XML语法的本体文件.

UPD: here is my ontology file in RDF/XML syntax.

   <?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#"
     xml:base="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11">
        <rdfs:label>University subjects ontology
</rdfs:label>
    </owl:Ontology>

    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Object Properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->


    <!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags -->

    <owl:ObjectProperty rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags">
        <rdf:type rdf:resource="&owl;TransitiveProperty"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject"/>
        <rdfs:range rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Tag"/>
    </owl:ObjectProperty>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++ -->

    <owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Programming"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++_programming -->

    <owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++_programming">
        <owl:equivalentClass>
            <owl:Restriction>
                <owl:onProperty rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags"/>
                <owl:someValuesFrom rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++"/>
            </owl:Restriction>
        </owl:equivalentClass>
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java -->

    <owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Programming"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java_programming -->

    <owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java_programming">
        <owl:equivalentClass>
            <owl:Restriction>
                <owl:onProperty rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags"/>
                <owl:someValuesFrom rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java"/>
            </owl:Restriction>
        </owl:equivalentClass>
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Programming -->

    <owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Programming">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Tag"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject -->

    <owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject"/>



    <!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#System_Programming -->

    <owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#System_Programming">
        <owl:equivalentClass>
            <owl:Restriction>
                <owl:onProperty rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags"/>
                <owl:someValuesFrom rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java"/>
            </owl:Restriction>
        </owl:equivalentClass>
        <owl:equivalentClass>
            <owl:Restriction>
                <owl:onProperty rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags"/>
                <owl:someValuesFrom rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++"/>
            </owl:Restriction>
        </owl:equivalentClass>
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Tag -->

    <owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Tag"/>
</rdf:RDF>



<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->

推荐答案

您需要询问属于Tag子类的事物.因此,类似

You need to ask for things that are subclasses of Tag. Thus, something like

?class rdfs:subClassOf* :Tag

*表示您需要匹配0次或更多次出现的rdfs:subClassOf的路径,因此?class可以是Tag或Tag的子类,或Tag的子类的子类,等等.完整的工作查询将是:

The * means you need to match a path of 0 or more occurrences of rdfs:subClassOf, so ?class can be Tag, or a subclass of Tag, or or subclass of a subclass of Tag, etc. A complete working query would be:

prefix :      <http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#> 
prefix owl:   <http://www.w3.org/2002/07/owl#> 
prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> 

select distinct ?subject where {
   ?subject owl:equivalentClass ?restriction .
   ?restriction owl:onProperty :hasTags .
   ?restriction ?restrictType ?class .
   ?class rdfs:subClassOf* :Tag 
}

-------------------------------------------------------------------------------------------
| subject                                                                                 |
===========================================================================================
| :Java_programming                                                                       |
| <http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++_programming> |
| :System_Programming                                                                     |
-------------------------------------------------------------------------------------------

这篇关于如何通过Sparql查询在耶拿推理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆