0

I'm using Jena to read an ontology and it's working really well so far. Unfortunately I haven't been able to figure out how to use compact uris that I've defined in the model. I've defined the prefixes using the model's setNSPrefix(String prefix, String uri) method. When I try to retrieve statements using the prefix, I get nothing. Also, when I do successfully retrieve a Statement, it contains the full uri instead of the compact one that I defined. It will even do it for the xsd uri http://www.w3.org/2001/XMLSchema#

For example, I'm using the uri http://www.example.com#, I've defined my prefix mapping as ex, and my Statement is http://www.example.com#father http://www.example.com#parentOf http://www.example.com#child where father is the subject, parentOf is the predicate, and child is the object. If I try to retrieve it using ex:father I get no results, and when I do get the Statement back the full uri is there for the subject, predicate, and object. I've seen it use the prefix instead of the uri when I do model.write(OutputStream), but that isn't particularly helpful for me. Am I able to use the prefix as a substitute for the uri like I've been trying to do, or is that not something Jena will provide for me?

endorphins
  • 586
  • 1
  • 4
  • 21

1 Answers1

1

When I try to retrieve statements using the prefix, I get nothing.

You can't do, e.g.,

model.getResource("ex:foo")`

You have to do

model.getResource("http://example.org/foo");

You can make that simpler, of course, by

String EX = "http://example.org/";
model.getResource(EX+"foo");

The prefixes are really just for making the serializations nicer to read and write.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • So when I'm getting a Statement based on the resource, is there any way to show the prefix instead of the full uri, or is it in a similar situation? For example, statement.getObject() will return `http://www.example.com#child` when I really want `ex:child`. – endorphins Feb 23 '15 at 18:53
  • There's no general way to do that. What if, for instance, there were two prefixes defined in the model: "ex: http://www.example.com#" and "ex2: ex: http://www.example.com#chi"? Would you want ex:child or ex2:ld? What if there's no prefix? What if it's a non-abbreviatable IRI? If you want something like that, you're going to have to roll-your-own by checking whether a given IRI can be abbreviated by any of the prefixes declared in the model. – Joshua Taylor Feb 23 '15 at 18:58
  • Alright, that's what I figured I would have to do. My thought was to grab the prefix mapping from the model, use that to see if the uri has a prefix, and then write it as that prefix instead of the full uri. – endorphins Feb 23 '15 at 19:03
  • "if the uri has a prefix" It could have more than one, as well. – Joshua Taylor Feb 23 '15 at 19:43