org.topbraid.spin.util
Class JenaUtil

java.lang.Object
  extended by org.topbraid.spin.util.JenaUtil

public class JenaUtil
extends java.lang.Object

Some convenience methods to operate on Jena Models. These methods are not as stable as the rest of the API, but they may be of general use.

Author:
Holger Knublauch

Constructor Summary
JenaUtil()
           
 
Method Summary
static java.util.Set<Property> asProperties(java.util.Collection<Resource> resources)
          Creates a Set of Properties from a Collection of Resources.
static Property asProperty(Resource resource)
          Casts a Resource into a Property.
static Model createDefaultModel()
          Creates a memory Model with ReificationStyle.Minimal.
static java.util.Set<Resource> getAllInstances(Resource cls)
          Gets all instances of a given class and its subclasses.
static java.util.Set<Resource> getAllSubClasses(Resource cls)
           
static java.util.Set<Resource> getAllSubProperties(Property superProperty)
           
static java.util.Set<Resource> getAllSuperClasses(Resource cls)
           
static java.util.Set<Resource> getAllSuperProperties(Property subProperty)
           
static java.util.Collection<Resource> getAllTypes(Resource instance)
           
static java.lang.Integer getIntegerProperty(Resource subject, Property predicate)
           
static java.lang.String getNsPrefixURI(Model model, java.lang.String prefix)
          Overcomes a bug in Jena: if the base model does not declare a default namespace then the default namespace of an import is returned!
static java.util.List<Resource> getResourceProperties(Resource subject, Property predicate)
           
static Resource getResourceProperty(Resource subject, Property predicate)
           
static java.util.List<Statement> getStatementsList(StmtIterator it)
           
static java.lang.String getStringProperty(Resource subject, Property predicate)
           
static java.util.Collection<Resource> getSuperClasses(Resource instance)
          Gets a Set of all superclasses (rdfs:subClassOf) of a given Resource.
static java.util.Collection<Resource> getTypes(Resource instance)
          Gets a Set of all rdf:types of a given Resource.
static boolean hasIndirectType(Resource instance, Resource type)
          Checks whether a given Resource is an instance of a given type, or a subclass thereof.
static boolean hasSuperProperty(Property subProperty, Property superProperty)
          Checks whether a given property has a given (transitive) super property.
static StmtIterator listAllProperties(Resource subject, Property predicate)
          Gets an Iterator over all Statements of a given property or its sub-properties at a given subject instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JenaUtil

public JenaUtil()
Method Detail

asProperty

public static Property asProperty(Resource resource)
Casts a Resource into a Property.

Parameters:
resource - the Resource to cast
Returns:
resource as an instance of Property

asProperties

public static java.util.Set<Property> asProperties(java.util.Collection<Resource> resources)
Creates a Set of Properties from a Collection of Resources.

Parameters:
resources - the Resource to cast
Returns:
resource as an instance of Property

createDefaultModel

public static Model createDefaultModel()
Creates a memory Model with ReificationStyle.Minimal.

Returns:
a new memory Model

getAllInstances

public static java.util.Set<Resource> getAllInstances(Resource cls)
Gets all instances of a given class and its subclasses.

Parameters:
cls - the class to get the instances of
Returns:
the instances

getAllSubClasses

public static java.util.Set<Resource> getAllSubClasses(Resource cls)

getAllSubProperties

public static java.util.Set<Resource> getAllSubProperties(Property superProperty)

getAllSuperClasses

public static java.util.Set<Resource> getAllSuperClasses(Resource cls)

getAllSuperProperties

public static java.util.Set<Resource> getAllSuperProperties(Property subProperty)

getAllTypes

public static java.util.Collection<Resource> getAllTypes(Resource instance)

getIntegerProperty

public static java.lang.Integer getIntegerProperty(Resource subject,
                                                   Property predicate)

getNsPrefixURI

public static java.lang.String getNsPrefixURI(Model model,
                                              java.lang.String prefix)
Overcomes a bug in Jena: if the base model does not declare a default namespace then the default namespace of an import is returned!

Parameters:
model - the Model to operate on
prefix - the prefix to get the URI of
Returns:
the URI of prefix

getResourceProperty

public static Resource getResourceProperty(Resource subject,
                                           Property predicate)

getResourceProperties

public static java.util.List<Resource> getResourceProperties(Resource subject,
                                                             Property predicate)

getStatementsList

public static java.util.List<Statement> getStatementsList(StmtIterator it)

getStringProperty

public static java.lang.String getStringProperty(Resource subject,
                                                 Property predicate)

getSuperClasses

public static java.util.Collection<Resource> getSuperClasses(Resource instance)
Gets a Set of all superclasses (rdfs:subClassOf) of a given Resource.

Parameters:
subClass - the subClass Resource
Returns:
a Collection of class resources

getTypes

public static java.util.Collection<Resource> getTypes(Resource instance)
Gets a Set of all rdf:types of a given Resource.

Parameters:
instance - the instance Resource
Returns:
a Collection of type resources

hasIndirectType

public static boolean hasIndirectType(Resource instance,
                                      Resource type)
Checks whether a given Resource is an instance of a given type, or a subclass thereof. Make sure that the type parameter is associated with the right Model, because the system will try to walk down into the subclasses of type.

Parameters:
instance - the Resource to test
type - the type
Returns:
true if resource has type type

hasSuperProperty

public static boolean hasSuperProperty(Property subProperty,
                                       Property superProperty)
Checks whether a given property has a given (transitive) super property.

Parameters:
subProperty - the sub-property
superProperty - the super-property
Returns:
true if subProperty has superProperty (somewhere up the tree)

listAllProperties

public static StmtIterator listAllProperties(Resource subject,
                                             Property predicate)
Gets an Iterator over all Statements of a given property or its sub-properties at a given subject instance. Note that the predicate and subject should be both attached to a Model to avoid NPEs.

Parameters:
subject - the subject (may be null)
predicate - the predicate
Returns:
a StmtIterator