RDF lookup from component object ?

5 views
Skip to first unread message

uoc

unread,
Apr 29, 2010, 12:26:17 PM4/29/10
to jenabe...@googlegroups.com
If i have a Class A and another Class B, and Class A has a property that is a reference to Class B is it possible to retrieve from Jena (via Selector or subjectsWithProperty perhaps ?) an instance or collection of instances of Class A filtered on an instance of Class B ?

That is to say, can I loadDeep a Collection<A> by passing an instance of A with properties that i want matched in the Collection of A returned

1. Create A and set B
A a = new A().
B b = new B();
b.setId("myId");

a.setB(b);
a.setXYZ(...);
a.setDEF(...);

A a1=new A();
a.setB(b);
a.setXYZ(---);
a.setDEF(---);

writer.save(a).
writer.save(a1);

2. Come back later and try and retrieve that instances of A that have a property b:
A myA = new A();
B myB = new B();
myB.setId("myId");
myA.setB(myB);


Collection<A> myAs = reader.load(A.class, myB);
myAs would contain a and a1 but not other instances of A that had different B properties. The instance of B, myB, set on the skeleton instance of A, myA acts as a filter in the selection of instances of A in the repository. Or in sparqlish terms

select ?myAs { ?myAs a :A . ?a :B <b.myId> }


--
You received this message because you are subscribed to the Google Groups "jenabean-dev" group.
To post to this group, send email to jenabe...@googlegroups.com.
To unsubscribe from this group, send email to jenabean-dev...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jenabean-dev?hl=en.

uoccou

unread,
May 1, 2010, 6:09:07 AM5/1/10
to jenabean-dev
Bump ?

On Apr 29, 5:26 pm, uoc <uoc...@gmail.com> wrote:
> If i have a Class A and another Class B, and Class A has a property that
> is a reference to Class B is it possible to retrieve from Jena (/via
> Selector or subjectsWithProperty perhaps ?/) an instance or collection

Taylor Cowan

unread,
May 6, 2010, 12:33:38 AM5/6/10
to jenabe...@googlegroups.com
No, not with JenaBean alone. You can however, use sparql (or the
model interface) to find:

?a where { ?a <prop> <b> }

Once you have a result set of ?a's, you can then use JenaBean to load
the Java Object representation of each ?a.

See the class "thewebsemantic.Sparql" and it's methods such as:

public static <T> LinkedList<T> exec(Model m, Class<T> c, String query) {


so we'd have:

List<AClass> results = Sparql.exec(myModel, AClass.class, "select ?s
where {?s ...", initialBindings);

What might be new to you are the "initialbindings"...did you know that
you can do parameterized queries similar lets say a prepared statement
in JDBC? Andy Seaborn has a good blog post on this, and I have some
examples in the code base. That alone is not JenaBean stuff, but
standard Jena/ARQ. Sparql.exec() and overloads help push the result
set into objects for you.

Taylor

uoccou

unread,
May 6, 2010, 3:22:40 AM5/6/10
to jenabe...@googlegroups.com, Taylor Cowan
The question for me is, though, if I am working away in the object world
and need to create this sparql statement to find objects of class A
given an instance B, what are the URIs of the rdf properties and objects
in the statement ? JenaBean uses annotations and/or reflection to work
out the properties and namespace prefixes when objects are serialised to
RDF with Jena - do I need now to duplicate this logic in my code ?

uoc

unread,
May 11, 2010, 10:09:27 AM5/11/10
to jenabe...@googlegroups.com, Taylor Cowan
1) I resorted to using TypeWrapper to discover the serialised URIs for
my object properties, along the lines of these methods. Useful for
generating the sparql required. Should be able to extend this to create
a method that works from a skeleton bean and then calls jena's
subjectWithProperties()/listResourcesWithProperty() method.


/**
* answer properties map<name,uri> of this Bean that are of a
certain type
* @return
*/
protected Map<String,String> getPropertiesAsUris(){
Map<String,String> propMap = new HashMap<String, String>();
for (ValuesContext p : TypeWrapper.valueContexts(this)){
propMap.put(p.getName(), p.uri() );
}
return propMap;
}
/**
* answer properties map<name,uri> of this Bean that are of a
certain type
* @param <R>
* @param r
* @return
*/
protected <R> Map<String,String> getTypedPropertiesAsUris(Class<R> r){
Map<String,String> propMap = new HashMap<String, String>();
for (ValuesContext p : TypeWrapper.valueContexts(this)){
if ( p.type().isAssignableFrom(r) )
propMap.put(p.getName(), p.uri() );
}
return propMap;
}


2) I came across this niggle (perhaps obvious to you) : If you make a
call to RDF2Bean.loadDeep(Class<T> c, Object id) and c is an interface
(I have several subclasses of another concrete class, subclasses
implementing an interface the parentclass doesnt) then the interface
needs to also declare the @Id field, or you get nothing back. Reasonable
enough, just took me 4 days to work out why tho.

On 06/05/2010 05:33, Taylor Cowan wrote:
Reply all
Reply to author
Forward
0 new messages