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 = "";

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

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

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

Here's the results I got:

| name | surname |

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


asked 06 Jan '13, 13:59

talha06's gravatar image

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 '14, 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 :      <> .

: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 :      <>

select ?student ?name ?surname where {
  ?student :take :JavaProgramming ;
           :name ?name ;
           :surname ?surname .
| student | name    | surname   |
| :Talha  | "Talha" | "Kabakuş" |
permanent link

answered 29 Apr '14, 13:24

Joshua%20Taylor's gravatar image

Joshua Taylor
accept rate: 47%

edited 29 Apr '14, 13:25

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

PREFIX lib: <>

should be

PREFIX lib: <>


permanent link

answered 06 Jan '13, 14:42

lee's gravatar image

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

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

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 : 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 book:
  ?bookName ?isbn 
WHERE {?bookName book:bookName "Java Programming" }

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

@talha06, you have defined a prefix book which ends on "#JavaProgramming". The property book:bookName in your query therefore expands to

(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: <> 
PREFIX book: <> 
?bookName1 ?name1 
?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

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:

(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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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:


question asked: 06 Jan '13, 13:59

question was seen: 3,227 times

last updated: 28 Jan '15, 05:27