无法使用Jena从owl检索结果

时间:2013-01-06 11:26:37

标签: semantics sparql semantic-web jena protege

首先,我使用 Protege 4 创建了我自己的示例本体。我的实际目标是开发一个语义Web应用程序,它可以从对象属性中受益,为用户提供一些东西。创建我的本体后,我将添加动态数据,因此我需要使用* Jena API * 。为了演示,我创建了示例实例。但是当我试图检索结果时,我什么也没得到。

也许我在 SPARQL 查询中遗漏了一些内容。

private static final String baseUri = "http://www.semanticweb.org/ontologies/alisveris.owl#";

private static void modelReadFile(String filename, Model model) {
    try {
        File f = new File(filename);
        FileReader fr = new FileReader(f);
        model.read(fr, baseUri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}


private static void printModel(Model model) {
    String queryString =
        "PREFIX lib: <http://www.semanticweb.com/ontologies/alisveris.owl>" +
        "SELECT ?name ?surname " +
        "FROM <http://www.semanticweb.com/ontologies/alisveris.owl>" +
        "WHERE {" +
        "?name lib:take \"JavaProgramming\" " +
        "}";
    System.out.println(queryString);
    Query query = QueryFactory.create(queryString);
    QueryExecution qe = QueryExecutionFactory.create(query, model);
    ResultSet results = qe.execSelect();
    ResultSetFormatter.out(System.out, results, query);
    qe.close();
}


public static void main(String[] args) {
    Model model = ModelFactory.createDefaultModel();
    modelReadFile("/home/talha/ontologies/alisveris.owl", model);

    // Create properties for the different types of relationship to represent
    Property take = model.createProperty(baseUri, "take");
    Property isTakenBy = model.createProperty(baseUri, "isTakenBy");
    Property published = model.createProperty(baseUri, "published");
    Property publishedBy = model.createProperty(baseUri, "publishedBy");

    // User data properties
    Property userId = model.createProperty(baseUri, "userId");
    Property name = model.createProperty(baseUri, "name");
    Property surname = model.createProperty(baseUri, "surname");
    Property username = model.createProperty(baseUri, "username");
    Property password = model.createProperty(baseUri, "password");
    Property gender = model.createProperty(baseUri, "gender");
    Property age = model.createProperty(baseUri, "age");
    // Publisher data properties
    Property publisherId = model.createProperty(baseUri, "publisherId");
    Property publisherName = model.createProperty(baseUri, "publisherName");
    // Book data properties
    Property bookId = model.createProperty(baseUri, "bookId");
    Property bookName = model.createProperty(baseUri, "bookName");
    Property bookGenre = model.createProperty(baseUri, "bookGenre");
    Property pageCount = model.createProperty(baseUri, "pageCount");
    Property isbn = model.createProperty(baseUri, "isbn");
    Property bookRating = model.createProperty(baseUri, "bookRating");


    Resource talha = model.createResource(baseUri + "Talha");
    Resource javaProgramming = model.createResource(baseUri + "JavaProgramming");
    // and so on for other family members

    talha.addProperty(userId, "1");
    talha.addProperty(name, "Talha");
    talha.addProperty(surname, "Kabakuş");
    talha.addProperty(username, "talhak");
    talha.addProperty(password, "00");
    talha.addProperty(gender, "1");
    talha.addProperty(age, "23");


    javaProgramming.addProperty(bookId, "1");
    javaProgramming.addProperty(publisherId, "1");
    javaProgramming.addProperty(bookName, "Java Programming");
    javaProgramming.addProperty(bookGenre, "1");
    javaProgramming.addProperty(pageCount, "200");
    javaProgramming.addProperty(isbn, "121321312");
    javaProgramming.addProperty(bookRating, "3");

    // Add properties to adam describing relationships to other family members
    talha.addProperty(take, javaProgramming);

    Statement s = ResourceFactory.createStatement(talha, take, javaProgramming);
    model.add(s); // add the statement (triple) to the model
    printModel(model);
}

以下是我得到的结果:

------------------
| name | surname |
==================
------------------

最后我的样本本体:

<?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#" >
    <!ENTITY alisveris "http://www.semanticweb.org/ontologies/alisveris.owl#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/ontologies/alisveris.owl#"
     xml:base="http://www.semanticweb.org/ontologies/alisveris.owl"
     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#"
     xmlns:alisveris="http://www.semanticweb.org/ontologies/alisveris.owl#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/ontologies/alisveris.owl"/>



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




    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#isTakenBy -->

    <owl:ObjectProperty rdf:about="&alisveris;isTakenBy">
        <rdfs:domain rdf:resource="&alisveris;Book"/>
        <rdfs:range rdf:resource="&alisveris;User"/>
    </owl:ObjectProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#published -->

    <owl:ObjectProperty rdf:about="&alisveris;published">
        <rdfs:range rdf:resource="&alisveris;Book"/>
        <rdfs:domain rdf:resource="&alisveris;Publisher"/>
    </owl:ObjectProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#publishedBy -->

    <owl:ObjectProperty rdf:about="&alisveris;publishedBy">
        <rdfs:domain rdf:resource="&alisveris;Book"/>
        <rdfs:range rdf:resource="&alisveris;Publisher"/>
        <owl:inverseOf rdf:resource="&alisveris;published"/>
    </owl:ObjectProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#take -->

    <owl:ObjectProperty rdf:about="&alisveris;take">
        <rdfs:range rdf:resource="&alisveris;Book"/>
        <rdfs:domain rdf:resource="&alisveris;User"/>
        <owl:inverseOf rdf:resource="&alisveris;isTakenBy"/>
    </owl:ObjectProperty>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Data properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#age -->

    <owl:DatatypeProperty rdf:about="&alisveris;age">
        <rdfs:domain rdf:resource="&alisveris;User"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#bookGenre -->

    <owl:DatatypeProperty rdf:about="&alisveris;bookGenre">
        <rdfs:domain rdf:resource="&alisveris;Book"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#bookId -->

    <owl:DatatypeProperty rdf:about="&alisveris;bookId">
        <rdfs:domain rdf:resource="&alisveris;Book"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#bookName -->

    <owl:DatatypeProperty rdf:about="&alisveris;bookName">
        <rdfs:domain rdf:resource="&alisveris;Book"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#bookRating -->

    <owl:DatatypeProperty rdf:about="&alisveris;bookRating">
        <rdfs:domain rdf:resource="&alisveris;Book"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#gender -->

    <owl:DatatypeProperty rdf:about="&alisveris;gender">
        <rdfs:domain rdf:resource="&alisveris;User"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#isbn -->

    <owl:DatatypeProperty rdf:about="&alisveris;isbn">
        <rdfs:domain rdf:resource="&alisveris;Book"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#name -->

    <owl:DatatypeProperty rdf:about="&alisveris;name">
        <rdfs:domain rdf:resource="&alisveris;User"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#pageCount -->

    <owl:DatatypeProperty rdf:about="&alisveris;pageCount">
        <rdfs:domain rdf:resource="&alisveris;Book"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#password -->

    <owl:DatatypeProperty rdf:about="&alisveris;password">
        <rdfs:domain rdf:resource="&alisveris;User"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#publisherId -->

    <owl:DatatypeProperty rdf:about="&alisveris;publisherId">
        <rdfs:domain rdf:resource="&alisveris;Book"/>
        <rdfs:domain rdf:resource="&alisveris;Publisher"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#publisherName -->

    <owl:DatatypeProperty rdf:about="&alisveris;publisherName">
        <rdfs:domain rdf:resource="&alisveris;Publisher"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#rating -->

    <owl:DatatypeProperty rdf:about="&alisveris;rating"/>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#surname -->

    <owl:DatatypeProperty rdf:about="&alisveris;surname">
        <rdfs:domain rdf:resource="&alisveris;User"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#userId -->

    <owl:DatatypeProperty rdf:about="&alisveris;userId">
        <rdfs:domain rdf:resource="&alisveris;User"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#username -->

    <owl:DatatypeProperty rdf:about="&alisveris;username">
        <rdfs:domain rdf:resource="&alisveris;User"/>
    </owl:DatatypeProperty>



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




    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#Book -->

    <owl:Class rdf:about="&alisveris;Book">
        <owl:disjointWith rdf:resource="&alisveris;Publisher"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#Female -->

    <owl:Class rdf:about="&alisveris;Female">
        <rdfs:subClassOf rdf:resource="&alisveris;Gender"/>
        <owl:disjointWith rdf:resource="&alisveris;Male"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#Gender -->

    <owl:Class rdf:about="&alisveris;Gender"/>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#Male -->

    <owl:Class rdf:about="&alisveris;Male">
        <rdfs:subClassOf rdf:resource="&alisveris;Gender"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#Publisher -->

    <owl:Class rdf:about="&alisveris;Publisher">
        <rdfs:subClassOf rdf:resource="&owl;Thing"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/ontologies/alisveris.owl#User -->

    <owl:Class rdf:about="&alisveris;User"/>
</rdf:RDF>

2 个答案:

答案 0 :(得分:1)

PREFIX lib: <http://www.semanticweb.com/ontologies/alisveris.owl>

你需要#同意baseURI:

PREFIX lib: <http://www.semanticweb.com/ontologies/alisveris.owl#>

你也不需要

FROM <http://www.semanticweb.com/ontologies/alisveris.owl>

您提供的数据为model

答案 1 :(得分:0)

此答案也在the duplicate on answers.semanticweb.com

您的查询要求的是三元组,其中对象是文字"JavaProgramming",但代码Resource javaProgramming = model.createResource(baseUri + "JavaProgramming");会创建资源。您需要查询资源而不是字符串。除了本体代码之外,您创建的模型就是这样(请注意我们看到的是:JavaProgramming,而不是"JavaProgramming"):

@prefix :      <http://www.semanticweb.org/ontologies/alisveris.owl#> .

:JavaProgramming  :bookGenre  "1" ;
        :bookId       "1" ;
        :bookName     "Java Programming" ;
        :bookRating   "3" ;
        :isbn         "121321312" ;
        :pageCount    "200" ;
        :publisherId  "1" .

:Talha  :age       "23" ;
        :gender    "1" ;
        :name      "Talha" ;
        :password  "00" ;
        :surname   "Kabakuş" ;
        :take      :JavaProgramming ;
        :userId    "1" ;
        :username  "talhak" .

在此数据上,您可以运行以下查询(使用:JavaProgramming)并获得以下结果。

prefix :      <http://www.semanticweb.org/ontologies/alisveris.owl#>

select ?student ?name ?surname where {
  ?student :take :JavaProgramming ;
           :name ?name ;
           :surname ?surname .
}
---------------------------------
| student | name    | surname   |
=================================
| :Talha  | "Talha" | "Kabakuş" |
---------------------------------