org.semanticdesktop.aperture.rdf.util
Class ModelUtil

java.lang.Object
  extended by org.semanticdesktop.aperture.rdf.util.ModelUtil

public class ModelUtil
extends Object

This class contains utility methods that facilitate the generation of various values from an RDF2Go model. The interface is deliberately similar to that of a ValueFactory, but this class can be used in situations when the code has to work directly with a model, without it being wrapped in an RDFContainer.


Constructor Summary
ModelUtil()
           
 
Method Summary
static Object convertNode(Node node, Class<?> clazz)
          Converts a node to an instance of the given class.
static BlankNode createBlankNode(Model model)
           
static Literal createLiteral(Model model, boolean value)
           
static Literal createLiteral(Model model, byte value)
           
static Literal createLiteral(Model model, double value)
           
static Literal createLiteral(Model model, float value)
           
static Literal createLiteral(Model model, int value)
           
static Literal createLiteral(Model model, long value)
           
static Literal createLiteral(Model model, short value)
           
static Literal createLiteral(Model model, String label)
           
static Literal createLiteral(Model model, String label, URI datatype)
           
static Statement createStatement(Model model, Resource subject, URI predicate, Node object)
           
static URI createURI(Model model, String uri)
           
static URI createURI(Model model, String namespaceUri, String localName)
           
static Resource generateRandomResource(Model model)
          This method creates resources that are used by the framework wherever a blank node is needed.
static URI generateRandomURI(Model model)
          This method creates URIs that are used by the framework wherever a random URI is needed.
static List<Node> getAllPropertyValues(Model model, Resource subject, URI predicate)
          Returns all values of a given property for the given resource.
static Collection<Resource> getAllSubjectsWithProperty(Model model, URI predicate, Node object)
          Returns all resources that have a given property with the given value.
static List<Statement> getCBD(Resource node, Model model, boolean backwards)
          Returns the Concise Bounded Description of a RDF Resource in a given Model.
static String getFileName(URI uri)
          Returns the file name from the uri.
static String getLocalName(URI vx)
          The RDF2Go interface doesn't support getting a local name from the URI.
static Node getPropertyValue(Model model, Resource subject, URI predicate)
           
static Resource getSingleSubjectWithProperty(Model model, URI predicate, Node object)
          Returns a resource that has a given property with the given value.
static boolean hasStatement(Model model, Resource subject, URI predicate, Node object)
           
static void readFileFromResource(Model model, String path, Syntax syntax)
          Reads rdf from the resource at the given resource path to the given model
static void removeAllPropertyValues(Model model, Resource subject, URI predicate)
          Removes all values of the given property for the given resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ModelUtil

public ModelUtil()
Method Detail

createLiteral

public static Literal createLiteral(Model model,
                                    String label)
                             throws ModelException
Throws:
ModelException

createLiteral

public static Literal createLiteral(Model model,
                                    String label,
                                    URI datatype)
                             throws ModelException
Throws:
ModelException

createLiteral

public static Literal createLiteral(Model model,
                                    boolean value)
                             throws ModelException
Throws:
ModelException

createLiteral

public static Literal createLiteral(Model model,
                                    long value)
                             throws ModelException
Throws:
ModelException

createLiteral

public static Literal createLiteral(Model model,
                                    int value)
                             throws ModelException
Throws:
ModelException

createLiteral

public static Literal createLiteral(Model model,
                                    short value)
                             throws ModelException
Throws:
ModelException

createLiteral

public static Literal createLiteral(Model model,
                                    byte value)
                             throws ModelException
Throws:
ModelException

createLiteral

public static Literal createLiteral(Model model,
                                    double value)
                             throws ModelException
Throws:
ModelException

createLiteral

public static Literal createLiteral(Model model,
                                    float value)
                             throws ModelException
Throws:
ModelException

createStatement

public static Statement createStatement(Model model,
                                        Resource subject,
                                        URI predicate,
                                        Node object)

createURI

public static URI createURI(Model model,
                            String uri)
                     throws ModelException
Throws:
ModelException

createURI

public static URI createURI(Model model,
                            String namespaceUri,
                            String localName)
                     throws ModelException
Throws:
ModelException

createBlankNode

public static BlankNode createBlankNode(Model model)

getCBD

public static List<Statement> getCBD(Resource node,
                                     Model model,
                                     boolean backwards)
                              throws ModelException
Returns the Concise Bounded Description of a RDF Resource in a given Model.

Parameters:
node - The starting node.
model - The model holding the RDF Graph.
backwards - Indicates whether to traverse nodes backwards (null,null,RESOURCE).
Returns:
A List of Statements.
Throws:
ModelException - Whenever access to the Model throws a ModelException.
See Also:
W3C CBD Spec

hasStatement

public static boolean hasStatement(Model model,
                                   Resource subject,
                                   URI predicate,
                                   Node object)
                            throws ModelException
Throws:
ModelException

getSingleSubjectWithProperty

public static Resource getSingleSubjectWithProperty(Model model,
                                                    URI predicate,
                                                    Node object)
Returns a resource that has a given property with the given value. This method assumes that the inverse of this property is obligatory and functional. That is exactly one such subject must exist. In other cases (no subject or more than one subject) an exception is thrown.

Parameters:
model -
predicate -
object -
Returns:
a resource that has a given property and a give value
Throws:
NullPointerException - if any of the arguments are null
ModelException - if there are no, or more than one resources with the given property and the given value

getAllSubjectsWithProperty

public static Collection<Resource> getAllSubjectsWithProperty(Model model,
                                                              URI predicate,
                                                              Node object)
Returns all resources that have a given property with the given value.

Parameters:
model -
predicate -
object -
Returns:
all resources that have a given property with the given value

getPropertyValue

public static Node getPropertyValue(Model model,
                                    Resource subject,
                                    URI predicate)
Parameters:
model - The model to work with.
subject - The subject.
predicate - The property we would like to find.
Returns:
the value of a given property applied to the given subject. If there is more than value, one of the values is returned, if there is none, null is returned.

getAllPropertyValues

public static List<Node> getAllPropertyValues(Model model,
                                              Resource subject,
                                              URI predicate)
Returns all values of a given property for the given resource.

Parameters:
model - The model in which to look for values.
subject - The resource.
predicate - The property.
Returns:
A list of values for of the given property for the given resource.

removeAllPropertyValues

public static void removeAllPropertyValues(Model model,
                                           Resource subject,
                                           URI predicate)
Removes all values of the given property for the given resource.

Parameters:
model - the model in which to look for values
subject - the resource
predicate - the property

convertNode

public static Object convertNode(Node node,
                                 Class<?> clazz)
Converts a node to an instance of the given class.

Parameters:
node - the node
clazz - class to which the node should be converted, currently only primitive types are supported and following RDF2Go classes: URI, Node and Literal
Returns:
the converted object or null if the conversion doesn't work

generateRandomResource

public static Resource generateRandomResource(Model model)
This method creates resources that are used by the framework wherever a blank node is needed. This method currently creates uris of the form urn:uuid: with a random UUID at the end. In the future some way to configure the behaviour of this method may be implemented. For instance blank nodes may be used.

Parameters:
model - a model for which the random resource should be generated
Returns:
a random resource.

generateRandomURI

public static URI generateRandomURI(Model model)
This method creates URIs that are used by the framework wherever a random URI is needed. This method currently creates uris of the form urn:uuid: with a random UUID at the end, hence they are globally unique. In the future some way to configure the behaviour of this method may be implemented, generating different algorithms or URI prefixes.

Parameters:
model - a model for which the random URI should be generated
Returns:
a random URI.

readFileFromResource

public static void readFileFromResource(Model model,
                                        String path,
                                        Syntax syntax)
                                 throws FileNotFoundException,
                                        IOException,
                                        ModelException
Reads rdf from the resource at the given resource path to the given model

Parameters:
model - the model where the RDF data should be stored
path - the resource path
syntax - the syntax
Throws:
FileNotFoundException - if the resource has not been foud
IOException - if an I/O error occurs in the process
ModelException - ...

getFileName

public static String getFileName(URI uri)
Returns the file name from the uri. More specifically the substring before the first hash, but after the last slash.

Parameters:
uri -
Returns:
the last path element

getLocalName

public static String getLocalName(URI vx)
The RDF2Go interface doesn't support getting a local name from the URI. I 'borrowed' this snippet from the Sesame LiteralImpl.



Copyright © 2010 Aperture Development Team. All Rights Reserved.