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!