import org.tupeloproject.kernel.BeanSession; import org.tupeloproject.kernel.beans.reflect.BeanMapping; import org.tupeloproject.kernel.beans.reflect.BeanMappingCache; import org.tupeloproject.kernel.impl.MemoryContext; import org.tupeloproject.rdf.Resource; import org.tupeloproject.rdf.terms.Dc; import org.tupeloproject.rdf.terms.Foaf; import org.tupeloproject.rdf.terms.Rdf; public class BeanCache { static public void main(String[] args) throws Exception { MemoryContext mc = new MemoryContext(); Resource bob = Resource.uriRef("http://ncsa.uiuc.edu/person/bob"); mc.addTriple(bob, Rdf.TYPE, Foaf.PERSON); mc.addTriple(bob, Foaf.NAME, "Bob"); mc.addTriple(bob, Dc.IDENTIFIER, Resource.uriRef("http://ncsa.uiuc.edu/person/alice").toString()); Resource joe = Resource.uriRef("http://ncsa.uiuc.edu/person/joe"); mc.addTriple(joe, Rdf.TYPE, Foaf.PERSON); mc.addTriple(joe, Foaf.NAME, "Joe"); mc.addTriple(joe, Foaf.KNOWS, bob); mc.addTriple(joe, Dc.IDENTIFIER, joe.toString()); BeanSession bs = new BeanSession(mc); BeanMappingCache cache = new BeanMappingCache(); cache.put(addMapping()); bs.setBeanMappingCache(cache); try { PersonBean pb = (PersonBean) bs.fetchBean(joe); System.out.println(pb.toString()); } catch (Exception e) { System.out.println(e.getMessage()); } try { PersonBean pb = (PersonBean) bs.fetchBean(joe); System.out.println(pb.toString()); } catch (Exception e) { System.out.println(e.getMessage()); } try { PersonBean pb = (PersonBean) bs.fetchBean(bob); System.out.println(pb.toString()); } catch (Exception e) { System.out.println(e.getMessage()); } } static private BeanMapping addMapping() throws Exception { BeanMapping map = new BeanMapping(); // java class representing the bean map.setJavaClassName(PersonBean.class.getName()); // type of the bean map.setRdfType(Foaf.PERSON); //setting subject property as the uri map.setSubjectPropertyName("uri"); //$NON-NLS-1$ // properties for the bean map.addProperty(Dc.IDENTIFIER, "uri", String.class); //$NON-NLS-1$ map.addProperty(Foaf.NAME, "name", String.class); //$NON-NLS-1$ map.addProperty(Foaf.KNOWS, "knows", PersonBean.class); //$NON-NLS-1$ // add the mapping return map; } static public class PersonBean { private String uri; private String name; private PersonBean knows; /** * @return the uri */ public String getUri() { return uri; } /** * @param uri * the uri to set */ public void setUri(String uri) { this.uri = uri; } /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } public PersonBean getKnows() { return knows; } public void setKnows(PersonBean knows) { this.knows = knows; } public String toString() { return "[ URI=" + uri + ", NAME=" + name + ", KNOWS=" + knows + " ]"; } } }