You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by David Moss <ad...@gmail.com> on 2014/11/20 23:13:44 UTC

Using Model inside Java Objects.

Until now I have been treating Jena and RDF like a database connection.
I retrieved data and immediately converted it to familiar Java objects with
fields, getters, setters and methods.


Recently I have been wondering if it might be better to keep the data as a
Jena Model within the object and use Jena to do the manipulation.

 

Retrieving a property value seems overly complex in syntax however.
Is there a better way to do this?

.

Model m = qe.execConstruct();

m.setNsPrefixes(p);

System.out.println(m.getProperty(null,
m.getProperty(m.expandPrefix("dc:title"))).getObject().asLiteral().toString(
));

.

 

DM


RE: Using Model inside Java Objects.

Posted by David Moss <ad...@gmail.com>.
Thank you for pointing me to this Claude, it is exactly what I was trying to achieve, but much more elegant.

DM

-----Original Message-----
From: Claude Warren [mailto:claude@xenei.com] 
Sent: Friday, 21 November 2014 6:04 PM
To: users@jena.apache.org
Subject: Re: Using Model inside Java Objects.

If you want a half way step, take a look at PA4RDF (
http://pa4rdf.sourceforge.net/) -- Persistence Annotations for RDF.

This package takes interfaces, abstract classes or concrete classes, and through the magic of annotations and dynamic proxies overlays them onto a graph so that a specific node is declared as a class type.  Then each property of the node can be associated as a method and each value of the property the result of the method so:

<http://example.com/person> <http:example.com/firstname> "joe" .

could have <http://example.com/person> associated with

interface PersonI {
   String getFirstname()
}

so that calling a constructed object (using the entity manager) PersonI p = EntityManager.create(  <http://example.com/person>, PersonI);
p.getFirstName() will return "joe".

All data is retained within the graph.
Access to the underlying resource associated with "p" is provided.
Multiple classes can be provided on the EntityManager.create() call.
Handles returning collections.
Handles returning classes that are also annotated to be in the graph.

There are mechanisms to support concrete classes where an implemented method is overridden to access a property.

Multiple namespaces are handled.

Works with most/all? Jena Model implementations.

It is by nature model I/O intensive so

The package has been uses in several projects I have worked on.

Claude

On Thu, Nov 20, 2014 at 10:13 PM, David Moss <ad...@gmail.com> wrote:

> Until now I have been treating Jena and RDF like a database connection.
> I retrieved data and immediately converted it to familiar Java objects 
> with fields, getters, setters and methods.
>
>
> Recently I have been wondering if it might be better to keep the data 
> as a Jena Model within the object and use Jena to do the manipulation.
>
>
>
> Retrieving a property value seems overly complex in syntax however.
> Is there a better way to do this?
>
> .
>
> Model m = qe.execConstruct();
>
> m.setNsPrefixes(p);
>
> System.out.println(m.getProperty(null,
>
> m.getProperty(m.expandPrefix("dc:title"))).getObject().asLiteral().toS
> tring(
> ));
>
> .
>
>
>
> DM
>
>


--
I like: Like Like - The likeliest place on the web <http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren


Re: Using Model inside Java Objects.

Posted by Claude Warren <cl...@xenei.com>.
If you want a half way step, take a look at PA4RDF (
http://pa4rdf.sourceforge.net/) -- Persistence Annotations for RDF.

This package takes interfaces, abstract classes or concrete classes, and
through the magic of annotations and dynamic proxies overlays them onto a
graph so that a specific node is declared as a class type.  Then each
property of the node can be associated as a method and each value of the
property the result of the method so:

<http://example.com/person> <http:example.com/firstname> "joe" .

could have <http://example.com/person> associated with

interface PersonI {
   String getFirstname()
}

so that calling a constructed object (using the entity manager)
PersonI p = EntityManager.create(  <http://example.com/person>, PersonI);
p.getFirstName() will return "joe".

All data is retained within the graph.
Access to the underlying resource associated with "p" is provided.
Multiple classes can be provided on the EntityManager.create() call.
Handles returning collections.
Handles returning classes that are also annotated to be in the graph.

There are mechanisms to support concrete classes where an implemented
method is overridden to access a property.

Multiple namespaces are handled.

Works with most/all? Jena Model implementations.

It is by nature model I/O intensive so

The package has been uses in several projects I have worked on.

Claude

On Thu, Nov 20, 2014 at 10:13 PM, David Moss <ad...@gmail.com> wrote:

> Until now I have been treating Jena and RDF like a database connection.
> I retrieved data and immediately converted it to familiar Java objects with
> fields, getters, setters and methods.
>
>
> Recently I have been wondering if it might be better to keep the data as a
> Jena Model within the object and use Jena to do the manipulation.
>
>
>
> Retrieving a property value seems overly complex in syntax however.
> Is there a better way to do this?
>
> .
>
> Model m = qe.execConstruct();
>
> m.setNsPrefixes(p);
>
> System.out.println(m.getProperty(null,
>
> m.getProperty(m.expandPrefix("dc:title"))).getObject().asLiteral().toString(
> ));
>
> .
>
>
>
> DM
>
>


-- 
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: Using Model inside Java Objects.

Posted by Martynas Jusevičius <ma...@graphity.org>.
David,

in my experience this approach only gives you the object-model
impedance mismatch and no real advantages.

Have you considered working directly on Jena Models and Resources?


Martynas
graphityhq.com

On Thu, Nov 20, 2014 at 11:13 PM, David Moss <ad...@gmail.com> wrote:
> Until now I have been treating Jena and RDF like a database connection.
> I retrieved data and immediately converted it to familiar Java objects with
> fields, getters, setters and methods.
>
>
> Recently I have been wondering if it might be better to keep the data as a
> Jena Model within the object and use Jena to do the manipulation.
>
>
>
> Retrieving a property value seems overly complex in syntax however.
> Is there a better way to do this?
>
> .
>
> Model m = qe.execConstruct();
>
> m.setNsPrefixes(p);
>
> System.out.println(m.getProperty(null,
> m.getProperty(m.expandPrefix("dc:title"))).getObject().asLiteral().toString(
> ));
>
> .
>
>
>
> DM
>

Re: Using Model inside Java Objects.

Posted by Andrea Scarpino <as...@noemalife.com>.
2014-11-20 23:13 GMT+01:00 David Moss <ad...@gmail.com>:

> Until now I have been treating Jena and RDF like a database connection.
> I retrieved data and immediately converted it to familiar Java objects with
> fields, getters, setters and methods.
>
>
> Recently I have been wondering if it might be better to keep the data as a
> Jena Model within the object and use Jena to do the manipulation.
>

There is also jenabean[1], but it seems to be unmaintained.

[1] https://code.google.com/p/jenabean

-- 
Andrea

Re: Using Model inside Java Objects.

Posted by Claude Warren <cl...@xenei.com>.
PA4RDF uses the graph directly for storage.  So there is no detachment from
the model.  This changes the semantics somewhat.

That being said, PA4RDF only performs locks when it goes to update an
Object (in the SPO sense).  Each write is atomic.

I would have to go look at the code but I think that PA4RDF does not do any
transactions.  (It may check to see if transactions are supported and if
there is one already in use but I don't recall if that code actually
worked).

If there is a need to perform transactions or locking at a higher level it
is left to the calling code to do so.

Claude

On Fri, Nov 28, 2014 at 9:24 AM, Olivier Rossel <ol...@gmail.com>
wrote:

> I have been using Elmo for some years now.
> It is the Sesame-equivalent of PA2RDF.
> And I think it works really well.
> Graph DB maps nicely to an object structure.
>
> One thing I learnt to be careful about, in Elmo:
> Entities, once instanciated, are detached from the model.
> They do not synchronize automatically if you modify the underlying graph.
> There is (of course) a synchronization mechanism, but you must learn to
>  activate it at the correct moments.
> (btw, it is not different from Hibernate or any other ORM)
>
> PS : is there a godd design pattern to implement (optimistic) locks with
> PA2RDF?
> I could not figure out a correct one when I used Elmo.
>
>
> On Fri, Nov 28, 2014 at 9:38 AM, Claude Warren <cl...@xenei.com> wrote:
> > PA4RDF works by mapping java methods to values in the graph and returning
> > those values converted to the proper Java type.  It uses the graph data
> > directly (i.e. it does not make a copy)
> >
> > so that given a properly annotated interface like:
> >
> > interface X {
> >   String getName();
> >   void setName(String name);
> > }
> >
> >
> > and a triple in a graph of
> >
> > <foo> <name> "bart";
> >
> > A call to the entity manager like
> > X xinst = entityManager.read( <foo>, X.class);
> >
> > will allow you to call
> > System.out.println( xinst.getName())
> > xinst.setName("Simpson");
> > System.out.println( xinst.getName())
> >
> > to print out
> > bart Simpson
> >
> > If the interface returns another (or the same) annotated class, the
> entity
> > manager is automatically invoked to create the instance.
> >
> > for example
> > interface Y {
> > X getThing();
> > setThing( X x );
> > }
> >
> > and triples
> > <bar> <thing> <foo>
> > <foo> <name> "bart"
> >
> > Y yinst = entityManager.read( <bar>, Y.class);
> >
> > will allow you to call
> > X xinst = Y.getThing();
> > System.out.println( xinst.getName())
> > xinst.setName("Simpson");
> > System.out.println( xinst.getName())
> >
> > full access to the underlying Resource object (e.g. <foo>) is also
> provided.
> >
> > On Fri, Nov 28, 2014 at 1:59 AM, Stian Soiland-Reyes <
> > soiland-reyes@cs.manchester.ac.uk> wrote:
> >
> >> I have find it really nice to use the OntModel and the more specific
> >> types there, like Individual.
> >>
> >> It means I have to pre-load an ontology or RDFS schema (from URI or
> >> classpath) and set static fields for the different properties and
> >> classes I will enquire/construct with.
> >>
> >> See
> >>
> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/ProvModel.java
> >>
> >>
> >> When I have loaded I do null-checks as getObjectProperty returns null
> >> if I misspell the property name - thus I can catch this here rather
> >> than six steps later when trying to consume the broken RDF.
> >>
> >>
> >> After that I just use Individual as my domain objects and pass them to
> >> an instance of ProvModel (e.g. a graph) for any getters and setters.
> >> Example usage:
> >>
> >>
> >>
> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L253
> >>
> >> Saving is straight forward:
> >>
> >>
> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L388
> >>
> >>
> >>
> >> (You might notice that my approach fell apart as soon as I had
> >> multiple ontologies to use.. I did this as subclasses of ProvModel
> >> here
> >>
> >>
> >>
> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/TavernaProvModel.java
> >>
> >> .. but I was unable to get the inferencing right (you might not need
> >> this) - probably because I did the mistake of loading always a new
> >> OntModel instead of adding to it - I was hampered by my
> >> loadModelFromClassPath :) )
> >>
> >>
> >>
> >> Some of the things I wish was better:
> >>
> >> null-safe getObjectProperty etc.
> >> looking up values of object properties to get back pre-casted
> >> Individuals - my workaround:
> >>
> >>
> https://github.com/wf4ever/robundle/blob/master/src/main/java/org/purl/wf4ever/robundle/manifest/RDFToManifest.java#L214
> >>
> >> (one day I should prepare a patch to add that as a method on
> >> Individual or ObjectProperty)
> >>
> >>
> >>
> >>
> >> On 20 November 2014 at 22:13, David Moss <ad...@gmail.com> wrote:
> >> > Until now I have been treating Jena and RDF like a database
> connection.
> >> > I retrieved data and immediately converted it to familiar Java objects
> >> with
> >> > fields, getters, setters and methods.
> >> >
> >> >
> >> > Recently I have been wondering if it might be better to keep the data
> as
> >> a
> >> > Jena Model within the object and use Jena to do the manipulation.
> >> >
> >> >
> >> >
> >> > Retrieving a property value seems overly complex in syntax however.
> >> > Is there a better way to do this?
> >> >
> >> > .
> >> >
> >> > Model m = qe.execConstruct();
> >> >
> >> > m.setNsPrefixes(p);
> >> >
> >> > System.out.println(m.getProperty(null,
> >> >
> >>
> m.getProperty(m.expandPrefix("dc:title"))).getObject().asLiteral().toString(
> >> > ));
> >> >
> >> > .
> >> >
> >> >
> >> >
> >> > DM
> >> >
> >>
> >>
> >>
> >> --
> >> Stian Soiland-Reyes, myGrid team
> >> School of Computer Science
> >> The University of Manchester
> >> http://soiland-reyes.com/stian/work/
> http://orcid.org/0000-0001-9842-9718
> >>
> >
> >
> >
> > --
> > I like: Like Like - The likeliest place on the web
> > <http://like-like.xenei.com>
> > LinkedIn: http://www.linkedin.com/in/claudewarren
>



-- 
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: Using Model inside Java Objects.

Posted by Andy Seaborne <an...@apache.org>.
On 07/01/15 23:50, Martynas Jusevičius wrote:
> I was reading about Java 8 Lambda Expressions and it appeared to me
> that they maybe could be used as a sort of functional ORM on Jena:
> http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html
>
> Just a thought. Has anyone looked at how Jena could be improved using lambdas?

Jena is setup to support multiple APIs over the core of Graph/Triple/Node.

There is no reason not to have a new RDF API using Java8 features 
alongside the existing APIs.

In fact, there is no block on just doing it - it does not need to be in 
the distribution from day one.

c.f. https://github.com/commons-rdf/commons-rdf is Java8.

That is more like an RDF1.1 specific Graph/Triple/Node and it does not 
have the necessary generalization for rules and SPARQL, and has no 
resource-in-graph (getProperty, listProperty) where streams and lambdas 
might be quite useful.

	Andy

>
>
> Martynas
>
> On Fri, Nov 28, 2014 at 10:24 AM, Olivier Rossel
> <ol...@gmail.com> wrote:
>> I have been using Elmo for some years now.
>> It is the Sesame-equivalent of PA2RDF.
>> And I think it works really well.
>> Graph DB maps nicely to an object structure.
>>
>> One thing I learnt to be careful about, in Elmo:
>> Entities, once instanciated, are detached from the model.
>> They do not synchronize automatically if you modify the underlying graph.
>> There is (of course) a synchronization mechanism, but you must learn to
>>   activate it at the correct moments.
>> (btw, it is not different from Hibernate or any other ORM)
>>
>> PS : is there a godd design pattern to implement (optimistic) locks with PA2RDF?
>> I could not figure out a correct one when I used Elmo.
>>
>>
>> On Fri, Nov 28, 2014 at 9:38 AM, Claude Warren <cl...@xenei.com> wrote:
>>> PA4RDF works by mapping java methods to values in the graph and returning
>>> those values converted to the proper Java type.  It uses the graph data
>>> directly (i.e. it does not make a copy)
>>>
>>> so that given a properly annotated interface like:
>>>
>>> interface X {
>>>    String getName();
>>>    void setName(String name);
>>> }
>>>
>>>
>>> and a triple in a graph of
>>>
>>> <foo> <name> "bart";
>>>
>>> A call to the entity manager like
>>> X xinst = entityManager.read( <foo>, X.class);
>>>
>>> will allow you to call
>>> System.out.println( xinst.getName())
>>> xinst.setName("Simpson");
>>> System.out.println( xinst.getName())
>>>
>>> to print out
>>> bart Simpson
>>>
>>> If the interface returns another (or the same) annotated class, the entity
>>> manager is automatically invoked to create the instance.
>>>
>>> for example
>>> interface Y {
>>> X getThing();
>>> setThing( X x );
>>> }
>>>
>>> and triples
>>> <bar> <thing> <foo>
>>> <foo> <name> "bart"
>>>
>>> Y yinst = entityManager.read( <bar>, Y.class);
>>>
>>> will allow you to call
>>> X xinst = Y.getThing();
>>> System.out.println( xinst.getName())
>>> xinst.setName("Simpson");
>>> System.out.println( xinst.getName())
>>>
>>> full access to the underlying Resource object (e.g. <foo>) is also provided.
>>>
>>> On Fri, Nov 28, 2014 at 1:59 AM, Stian Soiland-Reyes <
>>> soiland-reyes@cs.manchester.ac.uk> wrote:
>>>
>>>> I have find it really nice to use the OntModel and the more specific
>>>> types there, like Individual.
>>>>
>>>> It means I have to pre-load an ontology or RDFS schema (from URI or
>>>> classpath) and set static fields for the different properties and
>>>> classes I will enquire/construct with.
>>>>
>>>> See
>>>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/ProvModel.java
>>>>
>>>>
>>>> When I have loaded I do null-checks as getObjectProperty returns null
>>>> if I misspell the property name - thus I can catch this here rather
>>>> than six steps later when trying to consume the broken RDF.
>>>>
>>>>
>>>> After that I just use Individual as my domain objects and pass them to
>>>> an instance of ProvModel (e.g. a graph) for any getters and setters.
>>>> Example usage:
>>>>
>>>>
>>>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L253
>>>>
>>>> Saving is straight forward:
>>>>
>>>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L388
>>>>
>>>>
>>>>
>>>> (You might notice that my approach fell apart as soon as I had
>>>> multiple ontologies to use.. I did this as subclasses of ProvModel
>>>> here
>>>>
>>>>
>>>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/TavernaProvModel.java
>>>>
>>>> .. but I was unable to get the inferencing right (you might not need
>>>> this) - probably because I did the mistake of loading always a new
>>>> OntModel instead of adding to it - I was hampered by my
>>>> loadModelFromClassPath :) )
>>>>
>>>>
>>>>
>>>> Some of the things I wish was better:
>>>>
>>>> null-safe getObjectProperty etc.
>>>> looking up values of object properties to get back pre-casted
>>>> Individuals - my workaround:
>>>>
>>>> https://github.com/wf4ever/robundle/blob/master/src/main/java/org/purl/wf4ever/robundle/manifest/RDFToManifest.java#L214
>>>>
>>>> (one day I should prepare a patch to add that as a method on
>>>> Individual or ObjectProperty)
>>>>
>>>>
>>>>
>>>>
>>>> On 20 November 2014 at 22:13, David Moss <ad...@gmail.com> wrote:
>>>>> Until now I have been treating Jena and RDF like a database connection.
>>>>> I retrieved data and immediately converted it to familiar Java objects
>>>> with
>>>>> fields, getters, setters and methods.
>>>>>
>>>>>
>>>>> Recently I have been wondering if it might be better to keep the data as
>>>> a
>>>>> Jena Model within the object and use Jena to do the manipulation.
>>>>>
>>>>>
>>>>>
>>>>> Retrieving a property value seems overly complex in syntax however.
>>>>> Is there a better way to do this?
>>>>>
>>>>> .
>>>>>
>>>>> Model m = qe.execConstruct();
>>>>>
>>>>> m.setNsPrefixes(p);IRIFactoryI
>>>>>
>>>>> System.out.println(m.getProperty(null,
>>>>>
>>>> m.getProperty(m.expandPrefix("dc:title"))).getObject().asLiteral().toString(
>>>>> ));
>>>>>
>>>>> .
>>>>>
>>>>>
>>>>>
>>>>> DM
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Stian Soiland-Reyes, myGrid team
>>>> School of Computer Science
>>>> The University of Manchester
>>>> http://soiland-reyes.com/stian/work/ http://orcid.org/0000-0001-9842-9718
>>>>
>>>
>>>
>>>
>>> --
>>> I like: Like Like - The likeliest place on the web
>>> <http://like-like.xenei.com>
>>> LinkedIn: http://www.linkedin.com/in/claudewarren


Re: Using Model inside Java Objects.

Posted by Martynas Jusevičius <ma...@graphity.org>.
I was reading about Java 8 Lambda Expressions and it appeared to me
that they maybe could be used as a sort of functional ORM on Jena:
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html

Just a thought. Has anyone looked at how Jena could be improved using lambdas?


Martynas

On Fri, Nov 28, 2014 at 10:24 AM, Olivier Rossel
<ol...@gmail.com> wrote:
> I have been using Elmo for some years now.
> It is the Sesame-equivalent of PA2RDF.
> And I think it works really well.
> Graph DB maps nicely to an object structure.
>
> One thing I learnt to be careful about, in Elmo:
> Entities, once instanciated, are detached from the model.
> They do not synchronize automatically if you modify the underlying graph.
> There is (of course) a synchronization mechanism, but you must learn to
>  activate it at the correct moments.
> (btw, it is not different from Hibernate or any other ORM)
>
> PS : is there a godd design pattern to implement (optimistic) locks with PA2RDF?
> I could not figure out a correct one when I used Elmo.
>
>
> On Fri, Nov 28, 2014 at 9:38 AM, Claude Warren <cl...@xenei.com> wrote:
>> PA4RDF works by mapping java methods to values in the graph and returning
>> those values converted to the proper Java type.  It uses the graph data
>> directly (i.e. it does not make a copy)
>>
>> so that given a properly annotated interface like:
>>
>> interface X {
>>   String getName();
>>   void setName(String name);
>> }
>>
>>
>> and a triple in a graph of
>>
>> <foo> <name> "bart";
>>
>> A call to the entity manager like
>> X xinst = entityManager.read( <foo>, X.class);
>>
>> will allow you to call
>> System.out.println( xinst.getName())
>> xinst.setName("Simpson");
>> System.out.println( xinst.getName())
>>
>> to print out
>> bart Simpson
>>
>> If the interface returns another (or the same) annotated class, the entity
>> manager is automatically invoked to create the instance.
>>
>> for example
>> interface Y {
>> X getThing();
>> setThing( X x );
>> }
>>
>> and triples
>> <bar> <thing> <foo>
>> <foo> <name> "bart"
>>
>> Y yinst = entityManager.read( <bar>, Y.class);
>>
>> will allow you to call
>> X xinst = Y.getThing();
>> System.out.println( xinst.getName())
>> xinst.setName("Simpson");
>> System.out.println( xinst.getName())
>>
>> full access to the underlying Resource object (e.g. <foo>) is also provided.
>>
>> On Fri, Nov 28, 2014 at 1:59 AM, Stian Soiland-Reyes <
>> soiland-reyes@cs.manchester.ac.uk> wrote:
>>
>>> I have find it really nice to use the OntModel and the more specific
>>> types there, like Individual.
>>>
>>> It means I have to pre-load an ontology or RDFS schema (from URI or
>>> classpath) and set static fields for the different properties and
>>> classes I will enquire/construct with.
>>>
>>> See
>>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/ProvModel.java
>>>
>>>
>>> When I have loaded I do null-checks as getObjectProperty returns null
>>> if I misspell the property name - thus I can catch this here rather
>>> than six steps later when trying to consume the broken RDF.
>>>
>>>
>>> After that I just use Individual as my domain objects and pass them to
>>> an instance of ProvModel (e.g. a graph) for any getters and setters.
>>> Example usage:
>>>
>>>
>>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L253
>>>
>>> Saving is straight forward:
>>>
>>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L388
>>>
>>>
>>>
>>> (You might notice that my approach fell apart as soon as I had
>>> multiple ontologies to use.. I did this as subclasses of ProvModel
>>> here
>>>
>>>
>>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/TavernaProvModel.java
>>>
>>> .. but I was unable to get the inferencing right (you might not need
>>> this) - probably because I did the mistake of loading always a new
>>> OntModel instead of adding to it - I was hampered by my
>>> loadModelFromClassPath :) )
>>>
>>>
>>>
>>> Some of the things I wish was better:
>>>
>>> null-safe getObjectProperty etc.
>>> looking up values of object properties to get back pre-casted
>>> Individuals - my workaround:
>>>
>>> https://github.com/wf4ever/robundle/blob/master/src/main/java/org/purl/wf4ever/robundle/manifest/RDFToManifest.java#L214
>>>
>>> (one day I should prepare a patch to add that as a method on
>>> Individual or ObjectProperty)
>>>
>>>
>>>
>>>
>>> On 20 November 2014 at 22:13, David Moss <ad...@gmail.com> wrote:
>>> > Until now I have been treating Jena and RDF like a database connection.
>>> > I retrieved data and immediately converted it to familiar Java objects
>>> with
>>> > fields, getters, setters and methods.
>>> >
>>> >
>>> > Recently I have been wondering if it might be better to keep the data as
>>> a
>>> > Jena Model within the object and use Jena to do the manipulation.
>>> >
>>> >
>>> >
>>> > Retrieving a property value seems overly complex in syntax however.
>>> > Is there a better way to do this?
>>> >
>>> > .
>>> >
>>> > Model m = qe.execConstruct();
>>> >
>>> > m.setNsPrefixes(p);
>>> >
>>> > System.out.println(m.getProperty(null,
>>> >
>>> m.getProperty(m.expandPrefix("dc:title"))).getObject().asLiteral().toString(
>>> > ));
>>> >
>>> > .
>>> >
>>> >
>>> >
>>> > DM
>>> >
>>>
>>>
>>>
>>> --
>>> Stian Soiland-Reyes, myGrid team
>>> School of Computer Science
>>> The University of Manchester
>>> http://soiland-reyes.com/stian/work/ http://orcid.org/0000-0001-9842-9718
>>>
>>
>>
>>
>> --
>> I like: Like Like - The likeliest place on the web
>> <http://like-like.xenei.com>
>> LinkedIn: http://www.linkedin.com/in/claudewarren

Re: Using Model inside Java Objects.

Posted by Olivier Rossel <ol...@gmail.com>.
I have been using Elmo for some years now.
It is the Sesame-equivalent of PA2RDF.
And I think it works really well.
Graph DB maps nicely to an object structure.

One thing I learnt to be careful about, in Elmo:
Entities, once instanciated, are detached from the model.
They do not synchronize automatically if you modify the underlying graph.
There is (of course) a synchronization mechanism, but you must learn to
 activate it at the correct moments.
(btw, it is not different from Hibernate or any other ORM)

PS : is there a godd design pattern to implement (optimistic) locks with PA2RDF?
I could not figure out a correct one when I used Elmo.


On Fri, Nov 28, 2014 at 9:38 AM, Claude Warren <cl...@xenei.com> wrote:
> PA4RDF works by mapping java methods to values in the graph and returning
> those values converted to the proper Java type.  It uses the graph data
> directly (i.e. it does not make a copy)
>
> so that given a properly annotated interface like:
>
> interface X {
>   String getName();
>   void setName(String name);
> }
>
>
> and a triple in a graph of
>
> <foo> <name> "bart";
>
> A call to the entity manager like
> X xinst = entityManager.read( <foo>, X.class);
>
> will allow you to call
> System.out.println( xinst.getName())
> xinst.setName("Simpson");
> System.out.println( xinst.getName())
>
> to print out
> bart Simpson
>
> If the interface returns another (or the same) annotated class, the entity
> manager is automatically invoked to create the instance.
>
> for example
> interface Y {
> X getThing();
> setThing( X x );
> }
>
> and triples
> <bar> <thing> <foo>
> <foo> <name> "bart"
>
> Y yinst = entityManager.read( <bar>, Y.class);
>
> will allow you to call
> X xinst = Y.getThing();
> System.out.println( xinst.getName())
> xinst.setName("Simpson");
> System.out.println( xinst.getName())
>
> full access to the underlying Resource object (e.g. <foo>) is also provided.
>
> On Fri, Nov 28, 2014 at 1:59 AM, Stian Soiland-Reyes <
> soiland-reyes@cs.manchester.ac.uk> wrote:
>
>> I have find it really nice to use the OntModel and the more specific
>> types there, like Individual.
>>
>> It means I have to pre-load an ontology or RDFS schema (from URI or
>> classpath) and set static fields for the different properties and
>> classes I will enquire/construct with.
>>
>> See
>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/ProvModel.java
>>
>>
>> When I have loaded I do null-checks as getObjectProperty returns null
>> if I misspell the property name - thus I can catch this here rather
>> than six steps later when trying to consume the broken RDF.
>>
>>
>> After that I just use Individual as my domain objects and pass them to
>> an instance of ProvModel (e.g. a graph) for any getters and setters.
>> Example usage:
>>
>>
>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L253
>>
>> Saving is straight forward:
>>
>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L388
>>
>>
>>
>> (You might notice that my approach fell apart as soon as I had
>> multiple ontologies to use.. I did this as subclasses of ProvModel
>> here
>>
>>
>> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/TavernaProvModel.java
>>
>> .. but I was unable to get the inferencing right (you might not need
>> this) - probably because I did the mistake of loading always a new
>> OntModel instead of adding to it - I was hampered by my
>> loadModelFromClassPath :) )
>>
>>
>>
>> Some of the things I wish was better:
>>
>> null-safe getObjectProperty etc.
>> looking up values of object properties to get back pre-casted
>> Individuals - my workaround:
>>
>> https://github.com/wf4ever/robundle/blob/master/src/main/java/org/purl/wf4ever/robundle/manifest/RDFToManifest.java#L214
>>
>> (one day I should prepare a patch to add that as a method on
>> Individual or ObjectProperty)
>>
>>
>>
>>
>> On 20 November 2014 at 22:13, David Moss <ad...@gmail.com> wrote:
>> > Until now I have been treating Jena and RDF like a database connection.
>> > I retrieved data and immediately converted it to familiar Java objects
>> with
>> > fields, getters, setters and methods.
>> >
>> >
>> > Recently I have been wondering if it might be better to keep the data as
>> a
>> > Jena Model within the object and use Jena to do the manipulation.
>> >
>> >
>> >
>> > Retrieving a property value seems overly complex in syntax however.
>> > Is there a better way to do this?
>> >
>> > .
>> >
>> > Model m = qe.execConstruct();
>> >
>> > m.setNsPrefixes(p);
>> >
>> > System.out.println(m.getProperty(null,
>> >
>> m.getProperty(m.expandPrefix("dc:title"))).getObject().asLiteral().toString(
>> > ));
>> >
>> > .
>> >
>> >
>> >
>> > DM
>> >
>>
>>
>>
>> --
>> Stian Soiland-Reyes, myGrid team
>> School of Computer Science
>> The University of Manchester
>> http://soiland-reyes.com/stian/work/ http://orcid.org/0000-0001-9842-9718
>>
>
>
>
> --
> I like: Like Like - The likeliest place on the web
> <http://like-like.xenei.com>
> LinkedIn: http://www.linkedin.com/in/claudewarren

Re: Using Model inside Java Objects.

Posted by Claude Warren <cl...@xenei.com>.
PA4RDF works by mapping java methods to values in the graph and returning
those values converted to the proper Java type.  It uses the graph data
directly (i.e. it does not make a copy)

so that given a properly annotated interface like:

interface X {
  String getName();
  void setName(String name);
}


and a triple in a graph of

<foo> <name> "bart";

A call to the entity manager like
X xinst = entityManager.read( <foo>, X.class);

will allow you to call
System.out.println( xinst.getName())
xinst.setName("Simpson");
System.out.println( xinst.getName())

to print out
bart Simpson

If the interface returns another (or the same) annotated class, the entity
manager is automatically invoked to create the instance.

for example
interface Y {
X getThing();
setThing( X x );
}

and triples
<bar> <thing> <foo>
<foo> <name> "bart"

Y yinst = entityManager.read( <bar>, Y.class);

will allow you to call
X xinst = Y.getThing();
System.out.println( xinst.getName())
xinst.setName("Simpson");
System.out.println( xinst.getName())

full access to the underlying Resource object (e.g. <foo>) is also provided.

On Fri, Nov 28, 2014 at 1:59 AM, Stian Soiland-Reyes <
soiland-reyes@cs.manchester.ac.uk> wrote:

> I have find it really nice to use the OntModel and the more specific
> types there, like Individual.
>
> It means I have to pre-load an ontology or RDFS schema (from URI or
> classpath) and set static fields for the different properties and
> classes I will enquire/construct with.
>
> See
> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/ProvModel.java
>
>
> When I have loaded I do null-checks as getObjectProperty returns null
> if I misspell the property name - thus I can catch this here rather
> than six steps later when trying to consume the broken RDF.
>
>
> After that I just use Individual as my domain objects and pass them to
> an instance of ProvModel (e.g. a graph) for any getters and setters.
> Example usage:
>
>
> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L253
>
> Saving is straight forward:
>
> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L388
>
>
>
> (You might notice that my approach fell apart as soon as I had
> multiple ontologies to use.. I did this as subclasses of ProvModel
> here
>
>
> https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/TavernaProvModel.java
>
> .. but I was unable to get the inferencing right (you might not need
> this) - probably because I did the mistake of loading always a new
> OntModel instead of adding to it - I was hampered by my
> loadModelFromClassPath :) )
>
>
>
> Some of the things I wish was better:
>
> null-safe getObjectProperty etc.
> looking up values of object properties to get back pre-casted
> Individuals - my workaround:
>
> https://github.com/wf4ever/robundle/blob/master/src/main/java/org/purl/wf4ever/robundle/manifest/RDFToManifest.java#L214
>
> (one day I should prepare a patch to add that as a method on
> Individual or ObjectProperty)
>
>
>
>
> On 20 November 2014 at 22:13, David Moss <ad...@gmail.com> wrote:
> > Until now I have been treating Jena and RDF like a database connection.
> > I retrieved data and immediately converted it to familiar Java objects
> with
> > fields, getters, setters and methods.
> >
> >
> > Recently I have been wondering if it might be better to keep the data as
> a
> > Jena Model within the object and use Jena to do the manipulation.
> >
> >
> >
> > Retrieving a property value seems overly complex in syntax however.
> > Is there a better way to do this?
> >
> > .
> >
> > Model m = qe.execConstruct();
> >
> > m.setNsPrefixes(p);
> >
> > System.out.println(m.getProperty(null,
> >
> m.getProperty(m.expandPrefix("dc:title"))).getObject().asLiteral().toString(
> > ));
> >
> > .
> >
> >
> >
> > DM
> >
>
>
>
> --
> Stian Soiland-Reyes, myGrid team
> School of Computer Science
> The University of Manchester
> http://soiland-reyes.com/stian/work/ http://orcid.org/0000-0001-9842-9718
>



-- 
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: Using Model inside Java Objects.

Posted by Stian Soiland-Reyes <so...@cs.manchester.ac.uk>.
I have find it really nice to use the OntModel and the more specific
types there, like Individual.

It means I have to pre-load an ontology or RDFS schema (from URI or
classpath) and set static fields for the different properties and
classes I will enquire/construct with.

See https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/ProvModel.java


When I have loaded I do null-checks as getObjectProperty returns null
if I misspell the property name - thus I can catch this here rather
than six steps later when trying to consume the broken RDF.


After that I just use Individual as my domain objects and pass them to
an instance of ProvModel (e.g. a graph) for any getters and setters.
Example usage:

https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L253

Saving is straight forward:
https://github.com/taverna/taverna-prov/blob/master/prov-taverna-export/src/main/java/org/purl/wf4ever/provtaverna/export/W3ProvenanceExport.java#L388



(You might notice that my approach fell apart as soon as I had
multiple ontologies to use.. I did this as subclasses of ProvModel
here

https://github.com/taverna/taverna-prov/blob/master/prov-taverna-owl-bindings/src/main/java/org/purl/wf4ever/provtaverna/owl/TavernaProvModel.java

.. but I was unable to get the inferencing right (you might not need
this) - probably because I did the mistake of loading always a new
OntModel instead of adding to it - I was hampered by my
loadModelFromClassPath :) )



Some of the things I wish was better:

null-safe getObjectProperty etc.
looking up values of object properties to get back pre-casted
Individuals - my workaround:
   https://github.com/wf4ever/robundle/blob/master/src/main/java/org/purl/wf4ever/robundle/manifest/RDFToManifest.java#L214

(one day I should prepare a patch to add that as a method on
Individual or ObjectProperty)




On 20 November 2014 at 22:13, David Moss <ad...@gmail.com> wrote:
> Until now I have been treating Jena and RDF like a database connection.
> I retrieved data and immediately converted it to familiar Java objects with
> fields, getters, setters and methods.
>
>
> Recently I have been wondering if it might be better to keep the data as a
> Jena Model within the object and use Jena to do the manipulation.
>
>
>
> Retrieving a property value seems overly complex in syntax however.
> Is there a better way to do this?
>
> .
>
> Model m = qe.execConstruct();
>
> m.setNsPrefixes(p);
>
> System.out.println(m.getProperty(null,
> m.getProperty(m.expandPrefix("dc:title"))).getObject().asLiteral().toString(
> ));
>
> .
>
>
>
> DM
>



-- 
Stian Soiland-Reyes, myGrid team
School of Computer Science
The University of Manchester
http://soiland-reyes.com/stian/work/ http://orcid.org/0000-0001-9842-9718