First of all, I created my own, sample ontology using Protege 4. My actual goal is developing a semantic web application that benefits from object properties to offer something to users. After I created my ontology, I'll add dynamic data, so I need to use Jena API . For the sake of demonstration I created sample instances. But when I try to retrieve results, I got nothing.

Maybe I'm missing something in my SPARQL query.

Here's what I've tried so far:

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);
}

Here's the results I got:

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

Finally here's my ontology file which is generated by Protege (.owl)

alisveris.owl

asked 06 Jan '13, 13:59

talha06's gravatar image

talha06
4516
accept rate: 0%

Pleas at Single time You should check Your Owl File its Properly Closed according to your sending code add at last </rdf:rdf>.

(29 Apr, 10:52) priyank priyank's gravatar image

This answer is also at the duplicate on Stack Overflow.

Your query is asking for a triple in which the object is the literal "JavaProgramming", but the code Resource javaProgramming = model.createResource(baseUri + "JavaProgramming"); creates a resource. You need to query for the resource rather than a string. The model that you create, aside from the ontology code, is this (notice that we see :JavaProgramming, and not "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" .

On this data, you can run the following query (with :JavaProgramming) and get the following results.

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ş" |
---------------------------------
permanent link

answered 29 Apr, 13:24

Joshua%20Taylor's gravatar image

Joshua Taylor
2.6k310
accept rate: 48%

edited 29 Apr, 13:25

At the very least, you're missing a "#" in your PREFIX declaration in your SPARQL query:

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

should be

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

Lee

permanent link

answered 06 Jan '13, 14:42

lee's gravatar image

lee
3.2k39
accept rate: 37%

edited 06 Jan '13, 14:42

thanks a lot for your care but this trick didn't work too; still same..

(06 Jan '13, 14:43) talha06 talha06's gravatar image
2

The object of your lib:take predicate in your code is a resource, not the literal string "Java Programming". That is why your SPARQL query is not matching.

(06 Jan '13, 15:23) lee lee's gravatar image

Despite I changed query criteria to lib:bookName, it still gives no result.

(06 Jan '13, 16:02) talha06 talha06's gravatar image
1

My Kreskin act is at an end. You're going to have to do some debugging on your own. If you can output the RDF you've generated (e.g. in Turtle) and also show a revised SPARQL query that doesn't work against that RDF, it will be much easier to help you.

(06 Jan '13, 16:06) lee lee's gravatar image

Here's Turtle form of RDF : http://copypastebin.com/HpCm7Aex Thanks a lot for your help and patient.

(06 Jan '13, 16:14) talha06 talha06's gravatar image

also I tried this one too:

PREFIX lib: http://www.semanticweb.com/ontologies/alisveris.owl#
PREFIX book: http://www.semanticweb.com/ontologies/alisveris.owl#JavaProgramming
SELECT 
  ?bookName ?isbn 
WHERE {?bookName book:bookName "Java Programming" }

(06 Jan '13, 16:20) talha06 talha06's gravatar image
1

@talha06, you have defined a prefix book which ends on "#JavaProgramming". The property book:bookName in your query therefore expands to http://www.semanticweb.com/ontologies/alisveris.owl#JavaProgrammingbookName.

(06 Jan '13, 20:39) Jeen Broekstra ♦ Jeen%20Broekstra's gravatar image

thanks, I corrected it; now I'm getting

QueryParseException: Lexical error at line 1, column 195.  Encountered: " " (32), after : "book"

(07 Jan '13, 03:04) talha06 talha06's gravatar image

I created another query that lists the books' and their owners' names:

PREFIX lib: <http://www.semanticweb.com/ontologies/alisveris.owl#> 
PREFIX book: <http://www.semanticweb.com/ontologies/alisveris.owl#JavaProgramming> 
SELECT 
?bookName1 ?name1 
WHERE {
?book1 lib:isTakenBy ?user1 .
?user1 lib:name ?name1 .
?book1 lib:bookName ?bookName1 .
}

This query returns no results too.

(07 Jan '13, 04:19) talha06 talha06's gravatar image
1

With respect to that last query, your Turtle snippet doesn't contain lib:isTakenBy in the predicate position, though it can be inferred (as an inverse of lib:taken). Sounds like you want to turn on reasoning in Jena:

http://jena.apache.org/documentation/inference/index.html#owl

(07 Jan '13, 10:33) Signified ♦ Signified's gravatar image
showing 5 of 10 show 5 more comments
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×1,297
×611
×296
×206
×150

question asked: 06 Jan '13, 13:59

question was seen: 2,020 times

last updated: 29 Apr, 13:25