user2498899
user2498899

Reputation: 181

How to correctly use Jena property

I understand that I can retrieve an existing property from my model by using model.getProperty, for example:

Model model;
Property description_property = model.getProperty(NS.dcterms + "description");

But say I don't have the model available but want to create a local model I'm forced to use:

Property descriptionProperty= 
               ResourceFactory.createProperty(NS.dcterms + "description");

Can someone give a nice explanations of when and why to use model.getProperty vs ResourceFactory.createProperty and its implications.

Upvotes: 2

Views: 724

Answers (1)

Ian Dickinson
Ian Dickinson

Reputation: 13295

The two forms are actually pretty much equivalent. The principle difference is that, when you do a model.getXXX to get a property or resource object, that object then contains a pointer back to the model against which it was created. This can be helpful, as in Jena it's really only the model objects that hold state. Java classes such as Resource and Property denote resource and property identities, but the real content is the triples (i.e. Statements) in the model.

To make that concrete, if you do something like:

Resource s = ... ;
Property p = ... ;
RDFNode o = ... ;
p.getModel().addStatement( s, p, o );

that will succeed in your first case (i.e. with Model.getProperty) and fail in the second (i.e. ResourceFactory), since in the second case getModel() will return null. However, whether that's a practical concern in your application is something only you can say. I don't find it to be much of a problem in my code, to be honest.

Incidentally, you may like to know that Jena has a utility called schemagen, which can auto-generate Java source code containing constants corresponding to the classes, properties and individuals in your ontology. It can be clearer and more maintainable than manually creating such constants in your code.

Upvotes: 3

Related Questions