Class RDF2Bean

  • All Implemented Interfaces:
    Provider

    public class RDF2Bean
    extends Base
    implements Provider
    RDF2Bean converts one or more RDF nodes into java beans. Normally these are nodes created by the Bean2RDF class.
    See Also:
    Bean2RDF
    • Constructor Detail

      • RDF2Bean

        public RDF2Bean​(org.apache.jena.rdf.model.Model model)
        Constructs and instance of RDF2Bean bound to a particular Jena model. Operations have potential to modify the model.
        Parameters:
        model - a Jena Ontology Model instance
      • RDF2Bean

        public RDF2Bean​(org.apache.jena.rdf.model.Model model,
                        AnnotationHelper h)
    • Method Detail

      • loadDeep

        public <T> Collection<T> loadDeep​(Class<T> c)
        Deeply loads all individuals having RDF type which matches Class c. Depending on the density of your model this method has the potential to load the entire graph into memory as Java beans. For example, if you been has a hasFriends property, loadDeep() will load each friend, each friend's friends, and so on. Every reachable property that maps to a corresponding bean property will be loaded recursively.
        Type Parameters:
        T -
        Parameters:
        c - a java class which maps to individuals in your ontology.
        Returns:
        collection of java beans, all instances of Class c.
      • load

        public <T> Collection<T> load​(Class<T> c)
        Loads all individuals having RDF type which maps to Class c. Mappings are either based on Class annotations or bindings given at VM startup. load() is safe for dense graphs as it has a conservative or shallow policy. It loads all functional properties (where there is only one) but ignores bean properties of type Collection. All returned bean's Collection properties are initialized to size 0. Once loaded you may add to Collection properties and save. This will result in addition assertions being made in your jena ontology model, however, saving will not delete assertions unless the property is fully loaded. Consider the common customer/order scenario. We can load a customer, and fill the orders, leaving other properties alone: Collection customers = myRDF2Bean.load(Customer.class) ... myRDF2Bean.fill(aCustomer).with("orders"); foreach(Order o: aCustomer.getOrders())... You may also use this alternate method to fill collection properties: myRDF2Bean.fill(aCustomer, "orders");
        Type Parameters:
        T -
        Parameters:
        c -
        Returns:
        See Also:
        Namespace
      • load

        public <T> Collection<T> load​(Class<T> c,
                                      String... includes)
        Similar to load(Class c) except that you may include an array of property names to "include". The properties should be of type Collection. This allows you to be specific about wich children you want unmarshalled from the ontology model into java objects. For instance, you may just need a customer, their outstanding orders, and their recent addresses. Assuming your ontology relates customers to many more non-functional properties, it'd save time to just load exacly what you need.
        Type Parameters:
        T -
        Parameters:
        c -
        includes -
        Returns:
      • load

        protected <T> Collection<T> load​(Class<T> c,
                                         boolean shallow,
                                         String... includes)
        load all rdf entries that map to the bean.
        Type Parameters:
        T -
        Parameters:
        c -
        Returns:
      • loadDeep

        public <T> T loadDeep​(Class<T> c,
                              Object id)
                       throws NotFoundException
        loadDeep will load a particular individual and all it's properties, recursively. Beware, this could result in loading the entire model into memory as java objects depending on the density of your graph. Therefore use this method with care knowing that it's purpose is to load all information reachable via properties that bind to your objects.
        Parameters:
        c - java class of the bean. The class is converted to a URI based on its annotations or bindings.
        id - unique id of the bean to find
        Returns:
        An instance of T, otherwise null
        Throws:
        NotFoundException
        See Also:
        Namespace
      • load

        public <T> T load​(Class<T> c,
                          String id,
                          String[] includes)
                   throws NotFoundException
        Similar to load(Class, String), with the ability to include non-functional Collection based properties. includes should be an array of property names, for example, if you want to load a customer with their orders and recent purchases... RDF2Bean reader = new RDF2Bean(model); String[] includes = {"orders","recentPurchases"}; reader.load(Customer.class, "cust#2", includes);
        Type Parameters:
        T -
        Parameters:
        c -
        id -
        includes -
        Returns:
        Throws:
        NotFoundException
      • load

        public <T> T load​(Class<T> c,
                          Object id)
                   throws NotFoundException
        Loads an ontology individual as a java bean, based on annotations or bindings applied to Class c.
        Type Parameters:
        T -
        Parameters:
        c -
        id -
        Returns:
        instance of Classc matching id from model, if one exists.
        Throws:
        NotFoundException
      • load

        public <T> T load​(Class<T> c,
                          org.apache.jena.rdf.model.Resource r)
      • load

        public <A> A load​(A target)
        Loads an object from model with the same identifier as target.
        Parameters:
        target -
        Returns:
        Throws:
        NotFoundException
      • exists

        public boolean exists​(Object target)
        returns true if target exists in the model
        Parameters:
        target -
        Returns:
      • fill

        @Deprecated
        public Filler fill​(Object o)
        Deprecated.
        Returns a Filler for this bean. When beans are loaded they are normally shallow, ie, their Collections are still empty. This allows the client to decide which lists (sometimes large) they'd like to work with. This provides a certain type of calling style: RDF2Bean rdf2bean = new RDF2Bean(model); ... rdf2bean.fill(myBean).with("children");
        Parameters:
        o -
        Returns:
      • fill

        @Deprecated
        public void fill​(Object o,
                         String propertyName)
        Deprecated.
        collections a filled lazily. Simple access of collection in your loaded bean will cause it to load from the model.
        fill or reload a non-functional property with values from the model. This is useful when you've recently shallow loaded a bean from the triple store. non-functional properties can contain unlimited elements, so your app will need to be careful regarding when it loads them. in Jenabean, non-functional properties are represented as properties of type java.util.Collection. RDF2Bean rdf2bean = new RDF2Bean(model); ... rdf2bean.fill(myBean,"children");
        Parameters:
        o -
        propertyName -
      • exists

        public boolean exists​(Class<?> c,
                              String id)
        returns true if matching individual is found in the model.
        Parameters:
        c -
        id -
        Returns:
      • exists

        public boolean exists​(String uri)
      • init

        public void init​(Object target)
        Initializes null collections and sets with a lazy loader. You may use this in lieu of creating new collections in your bean constuctor, however, it's main purpose is to initialize beans for use in the JPA api, which requires that managed beans have non-null collection properties after being made persistent.
        Parameters:
        target -
      • lazySet

        public Set lazySet​(org.apache.jena.rdf.model.Resource i,
                           String propertyUri,
                           Class type)
        Specified by:
        lazySet in interface Provider
      • lazyList

        public List lazyList​(org.apache.jena.rdf.model.Resource i,
                             String propertyUri,
                             Class type)
        Specified by:
        lazyList in interface Provider
      • fillCollection

        protected <T> Set<T> fillCollection​(Class<T> c,
                                            org.apache.jena.rdf.model.StmtIterator nodes)
      • bindAll

        public void bindAll​(String... pkg)
        Prepares this reader to bind to all annotated classes in provided list of packages. If the RDF wasn't created using Jenabean, it lacks annotations specifying the source class for each individual. Invoking bindAll prepares the reader by indicating the packages where your jenabeans can be found.
        Parameters:
        pkg -
      • bind

        public void bind​(Class<? extends Object> class1)
        Prepares this reader to bind to a particular annotated class.
      • bind

        public void bind​(Package... packages)
        Prepares this reader to bind to all annotated classes in provided list of package.