The Bean Session is Tupelo's stateful class to manage mapping java bean properties to and from RDF. It provides a higher level of abstraction than Thing Session and shares a very similar API.

Quick summary of features

Bean Session has a variety of features. A short list is given here for reference.

  • Bean sessions are fully compatible with Tupelo's Contexts.
  • Each bean is associated with an RDF subject.
  • Beans are not required to be altered in any way to be used with the system as long as they have supported property types.
  • The mappings between beans and RDF predicates may be customized, allowing great flexibility.
  • Tools for generating and working with RDF vocabularies. A great deal of the work making mappings can be done automatically.

Working with new beans

bean mapping is specified and added to the session for use. A bean is then registered if new, meaning it is associated with a subject and the session will track changes to it, or if it has already been serialized, it is fetched.

Context context = ... // get the context
BeanSession beanSession = new BeanSession(context); // create a new bean session
Person person = ... // create a new bean. It is assumed a mapping already exists in the context.
Resource subject = Resource.uriRef("urn:my/bean/subject"); // get or create a subject
beanSession.register(subject, person); // tell the session to manage it.
// At this point the person bean is just like any other and you may change properties
// as needed.
// Once any and all changes have been made to the bean, save it to the backing context.
beanSession.save(subject);

Fetching beans

Alternately, if data already exists, i.e., there is existing RDF that an application needs represented in a bean, then the application would fetch the bean:

Context context = ... // get the context
Resource subject = ... // set the correct subject
BeanSession beanSession = new BeanSession(context); // create a new bean session
Person person = (Person) beanSession.fetchBean(subject); // Now the bean is ready for use.

Refetching

Other capabilities include refetching beans. By refetching we mean that the values in the context are re-read and the state of the bean is updated. Since the bean session manages instances of beans, every effected bean would show changes seamlessly.

//.. assuming that a bean session exists and you have the subject of the bean
Person person = (Person) beanSession.fetch(subject);
// if some other process updates the context and you need these changes
beanSession.refetch(subject);
// now the stated person as well as any other beans it references, reflects these changes.
  • No labels