How to generate Pojos from TDB?

62 views
Skip to first unread message

Adrian

unread,
Oct 18, 2011, 5:35:23 AM10/18/11
to Empire
Hi All,
We are beginning to work with Empire and we have a couple of (dummy)
questions:
- We have manually created some Pojos just for testing. Is it possible
to generate the Pojos from a TDB database using a pluging or API?
- Is there any way to ask for the classes and its attributes trough
some Empire API? I have tried it using simple SPARQL but I can not
because I am not able to manage blank nodes...

Thank you for your attention. Best regards,
Adrián

Mike Grove

unread,
Oct 18, 2011, 7:38:39 AM10/18/11
to empir...@googlegroups.com
On Tue, Oct 18, 2011 at 5:35 AM, Adrian <adrianq...@gmail.com> wrote:
> Hi All,
> We are beginning to work with Empire and we have a couple of (dummy)
> questions:
> - We have manually created some Pojos just for testing. Is it possible
> to generate the Pojos from a TDB database using a pluging or API?

There is the BeanGenerator [1] which will do basically what you're
asking, but it works only on Sesame Repository objects. It would be
very straightforward to change that to use an instance of an Empire
DataSource. As a general word of warning, BeanGenerator is not
tested. I'll put some time into it to make it more robust at some
point, but it's not in my immediate work queue.

If you want to help round this out, I can get you started on what
needs to be done.

> - Is there any way to ask for the classes and its attributes trough
> some Empire API? I have tried it using simple SPARQL but I can not
> because I am not able to manage blank nodes...
>

Not sure what you're asking about here, could you explain a little
more? What are you trying exactly? What bnodes are problematic?
Empire's internals should select a query dialect that supports the
Jena bnode syntax for specifying a particular bnode.

Cheers,

Mike

[1] https://github.com/mhgrove/Empire/blob/master/core/src/com/clarkparsia/empire/codegen/BeanGenerator.java

Adrian

unread,
Oct 20, 2011, 9:28:29 AM10/20/11
to Empire
Thank you very much for the answers Mike. I have a couple of extra
questions..

I have been testing the BeanGenerator from a owl file and it works
quite well... but just to confirm I am using it properly:
BeanGenerator generates a set of interfaces. Each of them is a mapping
of an ontology class. Now, I have to create an implementation for each
ontology class I am going to use in my code. Is that correct?

About the bnodes...
I am trying to get all the attributes of a class (maybe without
member). Firstly I tried to ask for the equivalentClass or the uri
associated to a class but I get no good answer. The only way I have
found is to ask for the range of the class and its superclasses but it
is a little bit tricky. I took a look at the source code of the
BeanGenerator and I guess it is doing something like this:
select distinct ?p {?s rdf:type >class>.?s ?p ?o}
However it only works if you have a member of the class...
I guess the reason why I can not query classes is because my
misunderstanding about SPARQL and ontologies, so sorry if I am asking
a stupid thing.

Thanks!!
> [1]https://github.com/mhgrove/Empire/blob/master/core/src/com/clarkparsi...

Mike Grove

unread,
Oct 20, 2011, 10:01:45 AM10/20/11
to empir...@googlegroups.com
On Thu, Oct 20, 2011 at 9:28 AM, Adrian <adrianq...@gmail.com> wrote:
> Thank you very much for the answers Mike. I have a couple of extra
> questions..
>
> I have been testing the BeanGenerator from a owl file and it works
> quite well... but just to confirm I am using it properly:
> BeanGenerator generates a set of interfaces. Each of them is a mapping
> of an ontology class. Now, I have to create an implementation for each
> ontology class I am going to use in my code. Is that correct?

No, if you give Empire a Standard pojo interface, it will create an
instance of that interface on the fly and use that, you don't need to
create the implementation yourself. It's pretty easy to generate the
getters & setters for the bean. If there's anything non-standard, or
you want to encode additional logic in the bean other than simple
getters & setters, you'll have to implement it yourself.

Empire can actually fill out abstract classes in this manner; I've not
done this myself but another user has done this with some success.
InstanceGenerator is what handles this on the fly generation, its in
the same package as BeanGenerator.

>
> About the bnodes...
> I am trying to get all the attributes of a class (maybe without
> member). Firstly I tried to ask for the equivalentClass or the uri
> associated to a class but I get no good answer.

Where are you trying to do this? Generally asking for equivalentClass
is not as useful as you would hope unless you're talking to something
that can do equality reasoning or you've materialized the
equivalences.

> The only way I have
> found is to ask for the range of the class and its superclasses but it
> is a little bit tricky.

Yeah, it's certainly tricky, especially with ontologies that are not
well defined.

I took a look at the source code of the
> BeanGenerator and I guess it is doing something like this:
> select distinct ?p {?s rdf:type >class>.?s ?p ?o}

Yep. Trying to guess properties that could be on the bean based on
actual usage in the data. This is combined with checking the range of
properties explicitly in the classes domain. Usually does a pretty
good job if you have lots of instance data and/or a good ontology.
But its obviously not fool proof.

> However it only works if you have a member of the class...
> I guess the reason why I can not query classes is because my
> misunderstanding about SPARQL and ontologies, so sorry if I am asking
> a stupid thing.

You can query classes with SPARQL quite easily. BeanGenerator
actually uses SeRQL for the most part, bits of it predate SPARQL. But
for what BeanGenerator is doing w/ SeRQL, the same things could be
easily done with SPARQL.

Cheers,

Mike

Adrian

unread,
Oct 24, 2011, 11:21:34 AM10/24/11
to Empire
I have been testing the InstanceGenerator and I am having some
problems. I think they are related to the annotations. This is what I
am trying to do:
Class<pojos.interfaces.User> generateInstanceClass =
InstanceGenerator.generateInstanceClass(pojos.interfaces.User.class);
List instanceClass=EmpireUtil.all(entityManager,
generateInstanceClass);

This code works perfect when I use an instance taylor-made class but
if I use a generated instance class it throws an exception when I am
querying the dB. I have been debugging the code a little bit. I guess
the problem comes from this line (line 222, EmpireUtil.java):
RdfsClass aClass = theClass.getAnnotation(RdfsClass.class);

It looks like there is not any annotation in the generated instance
class so it fails when constructing the query in line 232 in
EmpireUtil.java. I checked the InstanceGenerator, as well, and it is
generating some annotations (inheritAnnotations method) but maybe they
are not properly being insert in the class.

Is this a bug or am I doing somethign wrong?

Thank you very much. Regards,
Adrián




On 20 oct, 16:01, Mike Grove <m...@clarkparsia.com> wrote:

Mike Grove

unread,
Oct 24, 2011, 11:45:22 AM10/24/11
to empir...@googlegroups.com
On Mon, Oct 24, 2011 at 11:21 AM, Adrian <adrianq...@gmail.com> wrote:
> I have been testing the InstanceGenerator and I am having some
> problems. I think they are related to the annotations. This is what I
> am trying to do:
> Class<pojos.interfaces.User> generateInstanceClass =
> InstanceGenerator.generateInstanceClass(pojos.interfaces.User.class);
> List instanceClass=EmpireUtil.all(entityManager,
> generateInstanceClass);

The instance generator is not intended for use by other devs, Empire
will automatically invoke it if you pass as an argument your
pojos.interfaces.User interface to an EntityManager.

>
> This code works perfect when I use an instance taylor-made class but
> if I use a generated instance class it throws an exception when I am
> querying the dB. I have been debugging the code a little bit. I guess
> the problem comes from this line (line 222, EmpireUtil.java):
> RdfsClass aClass = theClass.getAnnotation(RdfsClass.class);
>
> It looks like there is not any annotation in the generated instance
> class so it fails when constructing the query in line 232 in
> EmpireUtil.java. I checked the InstanceGenerator, as well, and it is
> generating some annotations (inheritAnnotations method) but maybe they
> are not properly being insert in the class.
>
> Is this a bug or am I doing somethign wrong?
>

Could be a bug. You're definitely not supposed to be using
InstanceGenerator directly. So I would suggest changing your code and
letting Empire handle the instance generation on its own and see if
that changes the result. If not, then it's probably a bug.
Annotations don't inherit the way I'd like, so its done by hand in the
various parts of the code. We've had problems in the past with
annotations not being 'visible' in their subclasses, so it's possible
you've come across another corner case where they're not propagating
on like we expect. If you still get the issue after changing your
code, please send along an example of what causes the issue and I can
look into it.

Cheers,

Mike

Reply all
Reply to author
Forward
0 new messages