Jena - How to know whether a specific resource is in the model?
Asked Answered
D

2

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!

Deice answered 13/3, 2013 at 13:36 Comment(4)
maybe this link Interface Model is useful.Blotch
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))...;Deice
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! :)Frequency
@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! :)Deice
D
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!

December answered 14/3, 2013 at 10:12 Comment(4)
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.Deice
Checking the solution: there are several contains which fit with r, null, null. I would rather suggest: model.contains(resource, null, (RDFNode)null)Deice
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.December
This answer needs an update. getResource now behaves same as createResource: jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/…Horoscope
D
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))...;
Deice answered 13/3, 2013 at 22:55 Comment(1)
This is a right answer but I have to wait 2 days to mark it as rightDeice

© 2022 - 2024 — McMap. All rights reserved.