7

I am trying to discover whether I had a specific resource in the model. For that I am using:

model.getResource("example")

Checking the doc, this method behaves exactly as createResource. Then, even if it is not in the model, I will get a new resource.

How can I check I have the resource avoiding its creation when it's not?

Thanks in advance!

jevora
  • 469
  • 1
  • 5
  • 14
  • maybe this link [Interface Model](http://jena.apache.org/documentation/javadoc/jena/com/hp/hpl/jena/rdf/model/Model.html) is useful. – Maryam Arshi Mar 13 '13 at 13:43
  • 1
    After researching a little bit I have found the next way. I don't know if this is really the best way to achieve it, but works: Resource toSearch = ResourceFactory.createResource("example"); if(!model.containsResource(toSearch))...; – jevora Mar 13 '13 at 13:53
  • 1
    If it is solved then post an answer and accept it, so the next person with the same issue will find the answer, you also get a badge! :) – Boaz Mar 13 '13 at 13:57
  • @Boaz.Jan Thank you very much for your suggestion, but the system does not allow me to post now. I have to wait 8 hours. I'll do afterwards! :) – jevora Mar 13 '13 at 14:04

2 Answers2

12

In Jena, Resource objects by themselves are not in the model. The model only contains triples - Statement objects containing a subject, predicate and object (usually abbreviated SPO). Any one of S, P or O can be a resource (noting that a Property is a sub-type of Resource in Jena and in the RDF standard). So you need to refine your question from "does this model contain this resource" to either:

  • does model M contain resource R as a subject?

  • does model M contain resource R as a subject, predicate or object?

This can be achieved as:

Resource r = ... ;
Model m = ... ;

// does m contain r as a subject?
if (m.contains( r, null, (RDFNode) null )) {
  ..
}

// does m contain r as s, p or o?
if (m.containsResource( r )) {
  ..
}

Incidentally, in your code sample you have

model.getResource("example")

This returns a Resource object corresponding to the given URI, but does not side-effect the triples in the model. This is the reason that Model has both getResource and createResource - get is potentially slightly more efficient since it re-uses resource objects, but the semantics are essentially identical. However, the argument you pass to getResource or createResource should be a URI. You are borrowing trouble from the future if you start using tokens like "example" in place of full URI's, so I would advise stopping this bad habit before you get comfortable with it!

Ian Dickinson
  • 12,875
  • 11
  • 40
  • 67
  • Thank you very much. That's definitely the solution. About the bad habit, don't worry, in my development I'm using real URIs. I just used "example" because it didn't matter this time. – jevora Mar 14 '13 at 11:47
  • Checking the solution: there are several contains which fit with r, null, null. I would rather suggest: model.contains(resource, null, (RDFNode)null) – jevora Mar 14 '13 at 11:54
  • Good catch. Yes, it's a pain that the method signature for `contains()` is liberal enough to require a cast on the `object` argument, but it's a design decision that was made a long time ago in Jena's early history and would be too disruptive to change now. – Ian Dickinson Mar 14 '13 at 20:39
  • This answer needs an update. `getResource` now behaves same as `createResource`: https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/model/Model.html#getResource-java.lang.String- – Vijay Chavda Mar 20 '18 at 03:28
3

After researching a little bit I have found the next way. I don't know if this is really the best way to achieve it, but works:

Resource toSearch = ResourceFactory.createResource("example");
if(!model.containsResource(toSearch))...;
jevora
  • 469
  • 1
  • 5
  • 14