[Pinto] Graph queries and mapping

45 views
Skip to first unread message

Oleg Pavlov

unread,
Sep 5, 2016, 8:30:44 AM9/5/16
to Empire
Hello! I'm so sorry If I ask the questions in the wrong place.

I have some questions about mapping list of rdf objects into collection of java objects and mapping list of child objects into parent object.

Example:

I have query: 

CONSTRUCT
{
?Person    psn:firstName    ?firstName ;
           psn
:lastName     ?lastName ;
           psn
:contact      ?Contact .
?Contact   cnt:mobilePhone  ?mobilePhone ;
           cnt
:addess       ?address .

}WHERE {
?Person    a                psn:Person ;
           psn
:firstName    "Luis" .
}


After I execute the query I get a list of statements:


("http://domain.com/person/Luis_Smith", "http://domain.com/person/firstName", "Luis"^^<.../string>),
("http://domain.com/person/Luis_Smith", "http://domain.com/person/lastName",  "Smith"^^<.../string>),
("http://domain.com/person/Luis_Smith", "http://domain.com/person/contact",   "http://domain.com/contact/Luis_Smith_contact_1"),
("http://domain.com/person/Luis_Smith", "http://domain.com/person/contact",   "http://domain.com/contact/Luis_Smith_contact_2"),
("http://domain.com/contact/Luis_Smith_contact_1", "http://domain.com/contact/address", "1st Street"^^<.../string>),
("http://domain.com/contact/Luis_Smith_contact_2", "http://domain.com/contact/address", "2st Street"^^<.../string>),

("http://domain.com/person/Luis_Jerssy", "http://domain.com/person/firstName", "Luis"^^<.../string>),
("http://domain.com/person/Luis_Jerssy", "http://domain.com/person/lastName",  "Jerssy"^^<.../string>),
("http://domain.com/person/Luis_Jerssy", "http://domain.com/person/contact",   "http://domain.com/contact/Luis_Jerssy_contact_1"),
("http://domain.com/person/Luis_Jerssy", "http://domain.com/person/contact",   "http://domain.com/contact/Luis_Jerssy_contact_2"),
("http://domain.com/contact/Luis_Jerssy_contact_1", "http://domain.com/contact/address", "1st Street"^^<.../string>),
("http://domain.com/contact/Luis_Jerssy_contact_2", "http://domain.com/contact/address", "2st Street"^^<.../string>)

And I want to map them to objects.

class Person
{
 
String firstName; //Luis
  String lastName; //Smith
 
List<Contact> contacts; //1st Street, 2st Street
}

class Contact
{
 
String address; //1st Street
}

and so on...

To split statements by objects (entities) I do somthing like this:

Map<Resource, Model> models = new LinkedHashMap<>();
while (queryResult.hasNext())
{
 
Statement statement = queryResult.next();
 
Resource subject = statement.getSubject();
 
Model model = models.getOrDefault(subject, new LinkedHashModel());
 model
.add(statement);
 models
.put(subject, model);
}

Question: 
Maybe are some tools already existed to do it? Or my queries just are incorrect?
Question: How can I map collection of objects into object?

Thank you.

Oleg

Michael Grove

unread,
Sep 6, 2016, 7:15:36 AM9/6/16
to empir...@googlegroups.com
The tests show how to do this [1].

Cheers,

Mike

[1] https://github.com/Complexible/pinto/blob/develop/test/src/com/complexible/pinto/RDFMapperTests.java#L277-L292

>
> Thank you.
>
> Oleg
>
> --
> You received this message because you are subscribed to the Google Groups
> "Empire" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to empire-rdf+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Oleg Pavlov

unread,
Sep 6, 2016, 12:53:05 PM9/6/16
to empir...@googlegroups.com
Yes, thank you! I've seen the test. This solution requires to know id of building object, what should I do if I want to get list of objects where each element contains list of objects and so on? I mean If I want to get all persons and their addresses by one query?
At the time I see only one solution. Using one query to get persons and another one to get their addresses.


> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Empire" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/empire-rdf/kVRvXXwTeuM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to empire-rdf+unsubscribe@googlegroups.com.

Michael Grove

unread,
Sep 6, 2016, 1:01:38 PM9/6/16
to empir...@googlegroups.com
On Tue, Sep 6, 2016 at 12:53 PM, Oleg Pavlov <opavl...@gmail.com> wrote:
> Yes, thank you! I've seen the test. This solution requires to know id of
> building object,

Correct, you need to know the ID of the root object in these cases.

what should I do if I want to get list of objects where
> each element contains list of objects and so on? I mean If I want to get all
> persons and their addresses by one query?
> At the time I see only one solution. Using one query to get persons and
> another one to get their addresses.

You can get all of that data in a single construct query quite easily.
But then you need to iterate over the resulting graph to return the
list of people so that you can then feed that to pinto to create the
resulting objects.

Cheers,

Mike
>> > email to empire-rdf+...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Empire" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/empire-rdf/kVRvXXwTeuM/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> empire-rdf+...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Empire" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to empire-rdf+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages