0

まず、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>
4

2 に答える 2

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

于 2013-01-06T18:06:25.767 に答える
0

この回答は、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ş" |
---------------------------------
于 2014-04-29T17:23:31.703 に答える