net.java.rdf.sommer
Class SesameMapper

java.lang.Object
  extended by net.java.rdf.sommer.Mapping
      extended by net.java.rdf.sommer.SesameMapper
All Implemented Interfaces:
Mapper, RDFFactory, RewriteMapper

public class SesameMapper
extends Mapping

Author:
Henry Story

Nested Class Summary
(package private)  class SesameMapper.FilterFieldsSerialiser
          filter for fields that have given annotations Note: this is used by findFromCifpConstructor (perhaps the class should be inernal to the method) also: this should be more subtle: if fields are private and in a superclass, then should we find a solution?
(package private)  class SesameMapper.IdFinderSerialiser
          Tries to find the Identity of an object by looking for identiy or inverse functional properties.
(package private)  class SesameMapper.MappingSerialiser
          take an unmapped object and add all its relations to the graph
 class SesameMapper.QueryByExampleSerialiser
           
static class SesameMapper.SesameInit
           
 
Nested classes/interfaces inherited from interface net.java.rdf.sommer.Mapper
Mapper.Init
 
Field Summary
(package private)  org.openrdf.model.Resource[] graphs
           
private  boolean inference
           
(package private)  SesameMapper.SesameInit init
           
(package private)  java.util.HashMap<java.lang.Class,JavaInstanceMapper> literalMap
           
(package private) static java.util.logging.Logger log
           
private  java.util.WeakHashMap<java.lang.Object,org.openrdf.model.Resource> obj2Resource
          A mapping from objects to resources is one to one.
(package private)  org.openrdf.repository.RepositoryConnection rep
           
private  java.util.WeakHashMap<org.openrdf.model.Resource,java.util.List<java.lang.Object>> resource2Obj
          A mapping from resources to objects is one to many.
private static java.util.TimeZone TZ
           
(package private)  java.util.Date updateTime
           
 org.openrdf.model.ValueFactory vf
           
(package private)  org.openrdf.model.Resource writeGrphs
           
(package private) static javax.xml.datatype.DatatypeFactory xmldf
           
 
Fields inherited from class net.java.rdf.sommer.Mapping
classmap
 
Constructor Summary
SesameMapper(SesameMapper.SesameInit init, java.lang.String... urls)
          Constructs a repository tied to a graph
 
Method Summary
(package private)  void addContext(java.lang.String context)
          add a context to the list of 'read only contexts'
(package private)  void addJavaInstance(JavaInstanceMapper map)
           
<T> T
addObject(T object)
          Insert the object and all its dependent objects into the graph
 boolean addObjects(java.lang.Object... objects)
          Insert all objects
 boolean addRelation(org.openrdf.model.Resource source, org.openrdf.model.URI relation, boolean inverse, java.lang.Object obj)
           
private  void addStatement(Statement s)
           
 void addStatements(java.util.Collection<Statement> statements)
          Add the following statements to the writeable graph.
 void cifpName(java.lang.Object thiz, java.lang.Class clazz, java.lang.Class[] argTypes, java.lang.Object[] values)
          Set the name for thiz to an existing one if one exists.
<T> T
cifpObject(java.lang.Class<T> clazz, java.lang.Class[] argTypes, java.lang.Object[] values)
          Check to see if we don't allready have an object that corresponds to this cifp
 boolean clear()
          note this clear here only clears what was written to the graph.
 java.lang.Object createLiteral(java.lang.String s)
           
 java.lang.Object createLiteral(java.lang.String s, java.lang.String lang)
           
 java.lang.Object createLiteralType(java.lang.String s, java.lang.String uriType)
           
<T> T
createObjectWithId(java.lang.String uri, java.lang.Class<T> clazz)
          Create an object of type with a given uri
 java.lang.Object createResource(java.lang.String uri)
           
<T> RDFCollection<T>
createVirtualCollection(java.lang.Object thiz, rdf relation, java.util.Collection<T> oldFieldValue, java.lang.reflect.Type fieldType)
          get the value of a virtual collection field.
static java.lang.String dateToXsdString(java.util.Date date)
           
 void enableInferencing(boolean on)
           
 boolean equals(java.lang.Object mapped1, java.lang.Object mapped2)
          Equality of mapped objects.
private  org.openrdf.model.Resource findFromCifpConstructor(java.lang.reflect.Constructor con, java.lang.Object obj)
           
(package private)  org.openrdf.model.Value findKnownMappedValueFor(java.lang.Object obj)
          Just find the Value of the object if it is known or a literal
(package private)  org.openrdf.model.Value findKnownMappedValueFor(java.lang.Object obj, java.lang.String literalType)
          Find a known mapped value of the object, whith the given type
<T> java.util.Collection<T>
getAllObjectsOfType(java.lang.Class<T> clazz)
           
private  org.openrdf.model.Resource getCIFPValue(java.lang.Class clazz, java.lang.Class[] argTypes, java.lang.Object[] values)
          Find the object, if one exists that has the relations given by the rdf annotations on the arguments of the constructor clazz, to the values.
 java.util.List<java.lang.Class> getClassesOf(java.lang.String uri)
          find the classes of the object with id uri
<E> java.util.ArrayList<E>
getCollection(org.openrdf.model.Resource subj, org.openrdf.model.URI relationUri, boolean inverse, java.lang.Class<E> clazz)
          Get the collection starting with subj and the given relation, with contents of type clazz
<T> java.util.Collection<T>
getCollectionField(java.lang.Object thiz, rdf relation, java.util.Collection<T> oldFieldValue, java.lang.reflect.Type fieldType)
          get the value of a collection field.
 java.lang.Object getField(java.lang.Class fieldClass, java.lang.Object sourceObj, rdf relation, java.lang.Object fieldVal)
          Get the value of a field
<T> T
getObjectById(java.lang.String uri, java.lang.Class<T> clazz)
          Fetch an object of type with a given uri.
private  java.util.ArrayList<java.lang.String> getTypesOf(org.openrdf.model.Resource res)
          Get all the known types of resources res.
(package private)  org.openrdf.model.Resource getWriteGraphs()
           
 java.lang.String graphId()
           
 void importFrom(java.io.Reader r, java.lang.String baseUri, java.lang.String mimeType)
           
 void importInto(java.io.Reader r, java.lang.String baseUri, java.lang.String mimeType, java.lang.String context)
          import graph serialisation into the given context.
private  void initInstanceMapper()
           
 boolean isDirty(java.util.Date update)
           
 boolean isInferencingEnabled()
           
 boolean isMapped(java.lang.Object o)
          note: We should be able to ask the object directly now that it implements SommerMapable except of course that we are adding this directly
 org.openrdf.model.Value map(java.lang.Object obj)
          Map the java object to a RDF Value.
 org.openrdf.model.Value map(java.lang.Object obj, java.lang.String literalType)
          Map the java object to a RDF Value.
private
<T> T
map(org.openrdf.model.Value value, java.lang.Class<T> sprClzz)
          map the resource into an object of the given class
 void name(java.lang.Object obj, org.openrdf.model.Resource id)
          remember the name of the object
 void output(java.io.OutputStream out)
          Output the contents of this graph todo: add type of output (N3,rdf/xml)
 void output(java.io.Writer out)
          Output the contents of this graph todo: add type of output (N3,rdf/xml)
<T> java.util.Collection<T>
queryByExample(java.lang.Object eg, java.lang.Class<T> clazz)
          Query the map by giving an example object, and return all objects that fit the example.
 boolean remove(java.lang.Object obj)
          remove all statements concerning the resource this object is mapped to.
 void removeAll(org.openrdf.model.Resource source, org.openrdf.model.URI relation, boolean inverse)
           
 boolean removeRelation(org.openrdf.model.Resource source, org.openrdf.model.URI relation, boolean inverse, java.lang.Object obj)
           
private  void removeStatement(Statement s)
           
 void removeStatements(java.util.Collection<Statement> statements)
          remove the following statements from the graphs.
 org.openrdf.repository.RepositoryConnection rep()
          todo: It should be possible to set different repositories for different Maps.
<T> RDFCollection<T>
replaceVirtualCollection(java.lang.Object thiz, rdf relation, java.util.Collection<T> oldColl, java.util.Collection<T> newColl, java.lang.reflect.Type genType)
          Replace the elements in the collection with the new elements.
(package private)
<T> T
resourceGet(org.openrdf.model.Resource name, java.lang.Class<T> clazz)
          look in our map for an object with rdf name and the most precise subclass
(package private)  void setChanged()
           
<T> java.util.Collection<T>
setCollectionField(java.lang.Object sourceObj, rdf relation, java.util.Collection<T> newColl, java.lang.reflect.Type type)
          Set a Collection on a given field.
 java.lang.Object setField(java.lang.Class fieldClass, java.lang.Object obj, rdf relation, java.lang.Object value)
          Set a given field
 long size()
          The Size in triples of the graph, mapped to.
 void smush()
          Smush this graph.
 boolean unmap(java.lang.Object obj)
          This will remove the object from the map, but the relations will remain in the database.
 
Methods inherited from class net.java.rdf.sommer.Mapping
cogitate, mostSpecificSubClass, preload
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

static transient java.util.logging.Logger log

vf

public transient org.openrdf.model.ValueFactory vf

rep

transient org.openrdf.repository.RepositoryConnection rep

graphs

org.openrdf.model.Resource[] graphs

xmldf

static javax.xml.datatype.DatatypeFactory xmldf

TZ

private static final java.util.TimeZone TZ

inference

private boolean inference

init

SesameMapper.SesameInit init

writeGrphs

org.openrdf.model.Resource writeGrphs

obj2Resource

private java.util.WeakHashMap<java.lang.Object,org.openrdf.model.Resource> obj2Resource
A mapping from objects to resources is one to one. We only give one name to an object. The other owl:sameAs names can be found in the database.


resource2Obj

private java.util.WeakHashMap<org.openrdf.model.Resource,java.util.List<java.lang.Object>> resource2Obj
A mapping from resources to objects is one to many. This is because many objects can have the same name. For example - 2 objects implement different classes (one way to get multiple inheritance in java) - another object is created in a constructor that is an ifp or cifp and so the new object receives the same name


updateTime

java.util.Date updateTime

literalMap

java.util.HashMap<java.lang.Class,JavaInstanceMapper> literalMap
Constructor Detail

SesameMapper

SesameMapper(SesameMapper.SesameInit init,
             java.lang.String... urls)
Constructs a repository tied to a graph

Parameters:
init - Initialisation behavior
urls - the name of the graph or null for the default graph
Method Detail

getWriteGraphs

org.openrdf.model.Resource getWriteGraphs()

addContext

void addContext(java.lang.String context)
add a context to the list of 'read only contexts'

Parameters:
context -

isDirty

public boolean isDirty(java.util.Date update)

setChanged

void setChanged()

rep

public org.openrdf.repository.RepositoryConnection rep()
todo: It should be possible to set different repositories for different Maps. As such todo: we may not want this to be static... At the same time we may also want different maps for different todo: repositories

Returns:

importFrom

public void importFrom(java.io.Reader r,
                       java.lang.String baseUri,
                       java.lang.String mimeType)
                throws java.io.IOException,
                       GraphUpdateException,
                       ParseException
Parameters:
r - a Reader on the rdf
baseUri - the base to resolve relative uri from
mimeType - of the rdf serialisation: rdfxml, n3, turtle, ntriples, ...
Throws:
java.io.IOException
GraphUpdateException
ParseException

importInto

public void importInto(java.io.Reader r,
                       java.lang.String baseUri,
                       java.lang.String mimeType,
                       java.lang.String context)
                throws java.io.IOException,
                       GraphUpdateException,
                       ParseException
Description copied from interface: Mapper
import graph serialisation into the given context. Add the context to the list of contexts we are working with *

Parameters:
r - the reader containing the serialisation of the graph
baseUri - the base uri to xxx relative uris
mimeType - format of the serialisation ( currently the mime type)
context - the context to drop the information into
Throws:
java.io.IOException
GraphUpdateException
ParseException

clear

public boolean clear()
note this clear here only clears what was written to the graph. it does not remove all the facts from the read only graphs. Is this more useful that what we wanted before?

Returns:

replaceVirtualCollection

public <T> RDFCollection<T> replaceVirtualCollection(java.lang.Object thiz,
                                                     rdf relation,
                                                     java.util.Collection<T> oldColl,
                                                     java.util.Collection<T> newColl,
                                                     java.lang.reflect.Type genType)
Description copied from interface: RewriteMapper
Replace the elements in the collection with the new elements.

Parameters:
thiz - the object on which the field is attached
relation - the annotation on the field
oldColl - the old collection that the field was set to
newColl - the new collection the it should be replaced by
genType - the type of the objects in the collection
Returns:
the collection containing the objects related to thiz by relation. This may be the same object as oldColl

setCollectionField

public <T> java.util.Collection<T> setCollectionField(java.lang.Object sourceObj,
                                                      rdf relation,
                                                      java.util.Collection<T> newColl,
                                                      java.lang.reflect.Type type)
Description copied from interface: RewriteMapper
Set a Collection on a given field.

relation - rdf annotation on the field
Returns:
The object the field is set to

createVirtualCollection

public <T> RDFCollection<T> createVirtualCollection(java.lang.Object thiz,
                                                    rdf relation,
                                                    java.util.Collection<T> oldFieldValue,
                                                    java.lang.reflect.Type fieldType)
Description copied from interface: RewriteMapper
get the value of a virtual collection field. This will only be called once: on mapping of the object into the graph. Later the object present in the field will be returned. should this be also called when the object gets unmapped, so that it can return an unmapped collection?

Parameters:
thiz - the object on which the field is attached
relation - annotation on this field
oldFieldValue - the old value of the field - the one currently referred to
fieldType - Gernics type info of the Collection
Returns:
The object the field is set to

getCollectionField

public <T> java.util.Collection<T> getCollectionField(java.lang.Object thiz,
                                                      rdf relation,
                                                      java.util.Collection<T> oldFieldValue,
                                                      java.lang.reflect.Type fieldType)
Description copied from interface: RewriteMapper
get the value of a collection field. These are collections that have the (at)functional annotation (or something with a better name, when I get round to it) They are collections that really appear as collections in the rdf store.

relation - annotation on this field
oldFieldValue - the old value of the field - the one currently referred to
fieldType - the generic type of the collection
Returns:
The object the field is set to

setField

public java.lang.Object setField(java.lang.Class fieldClass,
                                 java.lang.Object obj,
                                 rdf relation,
                                 java.lang.Object value)
Description copied from interface: RewriteMapper
Set a given field

Parameters:
fieldClass - The class of the field being set
obj - The object on which the field is attached
relation - The rdf annotation on the field
value - the value that the field should be set to
Returns:
The object the field is set to

getField

public java.lang.Object getField(java.lang.Class fieldClass,
                                 java.lang.Object sourceObj,
                                 rdf relation,
                                 java.lang.Object fieldVal)
Description copied from interface: RewriteMapper
Get the value of a field

Parameters:
fieldClass - the class of the field
sourceObj - the object on which the field is located
relation - the rdf annotation on the field
fieldVal - the current value of the field
Returns:
the value of the field

name

public void name(java.lang.Object obj,
                 org.openrdf.model.Resource id)
remember the name of the object

Parameters:
obj -
id -

cifpName

public void cifpName(java.lang.Object thiz,
                     java.lang.Class clazz,
                     java.lang.Class[] argTypes,
                     java.lang.Object[] values)
Description copied from interface: RewriteMapper
Set the name for thiz to an existing one if one exists. The relations together form a CIFP (see http://esw.w3.org/topic/CIFP) The array size for relations and values should be the same

Parameters:
thiz - the object to be named
clazz - the class of the constructor
argTypes - the argument types identifying the particular constructor
values - the values

cifpObject

public <T> T cifpObject(java.lang.Class<T> clazz,
                        java.lang.Class[] argTypes,
                        java.lang.Object[] values)
Description copied from interface: RewriteMapper
Check to see if we don't allready have an object that corresponds to this cifp

Parameters:
clazz - the class on which the constructor is being called
argTypes - the types of the arguments of the constructor, to help find the constructor
Returns:
the object that allready corresponded to that cifp, or null if none

getCIFPValue

private org.openrdf.model.Resource getCIFPValue(java.lang.Class clazz,
                                                java.lang.Class[] argTypes,
                                                java.lang.Object[] values)
Find the object, if one exists that has the relations given by the rdf annotations on the arguments of the constructor clazz, to the values.

Parameters:
clazz - the class of the constructor
argTypes - the argument types identifying the particular constructor
values - the values that may have allready have a mapping in the rdf graph
Returns:
an rdf resource that has the required realtions to the mapped values, or null

addJavaInstance

void addJavaInstance(JavaInstanceMapper map)

initInstanceMapper

private void initInstanceMapper()

findKnownMappedValueFor

org.openrdf.model.Value findKnownMappedValueFor(java.lang.Object obj)
Just find the Value of the object if it is known or a literal

Parameters:
obj -
Returns:

findKnownMappedValueFor

org.openrdf.model.Value findKnownMappedValueFor(java.lang.Object obj,
                                                java.lang.String literalType)
Find a known mapped value of the object, whith the given type

Parameters:
obj - the object we are looking a mapping for
literalType - the type of the literal
Returns:

dateToXsdString

public static java.lang.String dateToXsdString(java.util.Date date)

map

public org.openrdf.model.Value map(java.lang.Object obj,
                                   java.lang.String literalType)
Map the java object to a RDF Value.

Parameters:
obj -
literalType - the type the object should be mapped to, or "" if not a literal
Returns:

map

public org.openrdf.model.Value map(java.lang.Object obj)
Map the java object to a RDF Value.

Parameters:
obj -
Returns:

findFromCifpConstructor

private org.openrdf.model.Resource findFromCifpConstructor(java.lang.reflect.Constructor con,
                                                           java.lang.Object obj)

smush

public void smush()
Smush this graph. Need this because don't have an inferencing engine


map

private <T> T map(org.openrdf.model.Value value,
                  java.lang.Class<T> sprClzz)
map the resource into an object of the given class

Parameters:
value -
sprClzz - the class or super class of any object returned
Returns:
the object, or null if no object could be constructed

getTypesOf

private java.util.ArrayList<java.lang.String> getTypesOf(org.openrdf.model.Resource res)
Get all the known types of resources res.

Parameters:
res -
Returns:
the known types as a list of URI types

resourceGet

<T> T resourceGet(org.openrdf.model.Resource name,
                  java.lang.Class<T> clazz)
look in our map for an object with rdf name and the most precise subclass

Parameters:
name - the rdf
clazz - the supertype of the object to be returned
Returns:
the object in our map, if one exists

removeRelation

public boolean removeRelation(org.openrdf.model.Resource source,
                              org.openrdf.model.URI relation,
                              boolean inverse,
                              java.lang.Object obj)

addRelation

public boolean addRelation(org.openrdf.model.Resource source,
                           org.openrdf.model.URI relation,
                           boolean inverse,
                           java.lang.Object obj)
Parameters:
source -
relation -
obj -

removeAll

public void removeAll(org.openrdf.model.Resource source,
                      org.openrdf.model.URI relation,
                      boolean inverse)

getCollection

public <E> java.util.ArrayList<E> getCollection(org.openrdf.model.Resource subj,
                                                org.openrdf.model.URI relationUri,
                                                boolean inverse,
                                                java.lang.Class<E> clazz)
Get the collection starting with subj and the given relation, with contents of type clazz

Parameters:
subj - the name of the object from which to go
relationUri - the relation from subj
inverse - if the inverse of the relationURi is desired
clazz - the class to return (could be a subclass if this is warranted by the database)
Returns:
a collection of java objects that are related by the relationUri relation to the collection

isMapped

public boolean isMapped(java.lang.Object o)
Description copied from interface: Mapper
note: We should be able to ask the object directly now that it implements SommerMapable except of course that we are adding this directly

Returns:
true if the object is mapped, false otherwise

getObjectById

public <T> T getObjectById(java.lang.String uri,
                           java.lang.Class<T> clazz)
Description copied from interface: Mapper
Fetch an object of type with a given uri. Note. There may be a good case for having a similar method that can also take a literal.

Parameters:
uri - the id of the object
clazz - the type of the object looked for
Returns:
null if none available in the database or an object mapped (if there is already an object mapped to that id, it should return that one)

createObjectWithId

public <T> T createObjectWithId(java.lang.String uri,
                                java.lang.Class<T> clazz)
Description copied from interface: Mapper
Create an object of type with a given uri

Parameters:
uri - the id of the object
clazz - the type of the object looked for
Returns:
an object (null only if the class has no @rdf annotation?)

getAllObjectsOfType

public <T> java.util.Collection<T> getAllObjectsOfType(java.lang.Class<T> clazz)

addObject

public <T> T addObject(T object)
Description copied from interface: Mapper
Insert the object and all its dependent objects into the graph

Parameters:
object - the object whose relations should be added to the graph
Returns:
the same object, for method chaining

addObjects

public boolean addObjects(java.lang.Object... objects)
Description copied from interface: Mapper
Insert all objects

Returns:

addStatements

public void addStatements(java.util.Collection<Statement> statements)
Description copied from interface: Mapper
Add the following statements to the writeable graph. The Subject and Object of the Statement can be mapped objects, in which case their id will be used. If they are not mapped objects, what should one do? Map them first and then add the statement?


addStatement

private void addStatement(Statement s)

removeStatements

public void removeStatements(java.util.Collection<Statement> statements)
Description copied from interface: Mapper
remove the following statements from the graphs. (this may require making diffs of non writeable graphs)


removeStatement

private void removeStatement(Statement s)

remove

public boolean remove(java.lang.Object obj)
Description copied from interface: Mapper
remove all statements concerning the resource this object is mapped to. The object will no longer appear in the rdf graph after this. note: On inferencing graphs this may not lead to the resource being removed if the resource is implied by other facts in the database. todo: this needs to be looked at a lot more carefully todo: is it a good thing to be able to remove an object? It could be quite a problem, as it could invalidate a number of other objects...

Returns:
false if it never was part of the graph

unmap

public boolean unmap(java.lang.Object obj)
Description copied from interface: Mapper
This will remove the object from the map, but the relations will remain in the database. todo: understand what this would mean for all the objects in points to or all objects pointed to it return true if unmapped, false if object was never mapped


output

public void output(java.io.OutputStream out)
Description copied from interface: Mapper
Output the contents of this graph todo: add type of output (N3,rdf/xml)


output

public void output(java.io.Writer out)
Description copied from interface: Mapper
Output the contents of this graph todo: add type of output (N3,rdf/xml)


graphId

public java.lang.String graphId()

equals

public boolean equals(java.lang.Object mapped1,
                      java.lang.Object mapped2)
Description copied from interface: Mapper
Equality of mapped objects. As far as the mapper knows these two objects are equals. Inferencing graphs will do a better job of finding object equality than non inferencing graphs. We can't use Object.equals(o) because that is too closely tied to the hash signature. In the graph equality between objects can be discovered over time, as new relations are added to the database. Equality between object may possibly also be broken, as relations are altered. if either object is not mapped, then this returns false if both objects are mapped and == then this is true if there is a owl:sameAs relation between them then it is true else it is false.

Parameters:
mapped1 - a mapped object
mapped2 - another mapped objects
Returns:
as far as this mapper knows mapped1 and mapped2 are the same

queryByExample

public <T> java.util.Collection<T> queryByExample(java.lang.Object eg,
                                                  java.lang.Class<T> clazz)
Description copied from interface: Mapper
Query the map by giving an example object, and return all objects that fit the example. Here the example is an object with @rdf annotated fields. Only looks at fields with values. Note: objects with simple literal values such as char, int, etc... are always set. So beware.

Parameters:
eg - example object. Should not be a mapped object or an object with a id specified by @rdf URI id since otherwise the only object returneable would be the one given. The object can have fields that are set either to mapped or unmapped objects.
clazz - of objects to return (should this not just be the same as the class of o?
Returns:
a Collection of objects of the given that fit the example.

createLiteral

public java.lang.Object createLiteral(java.lang.String s)

createLiteral

public java.lang.Object createLiteral(java.lang.String s,
                                      java.lang.String lang)

createLiteralType

public java.lang.Object createLiteralType(java.lang.String s,
                                          java.lang.String uriType)

createResource

public java.lang.Object createResource(java.lang.String uri)

size

public long size()
Description copied from interface: Mapper
The Size in triples of the graph, mapped to.

Returns:
the size of the graph, or -1 if there was a problem finding out

getClassesOf

public java.util.List<java.lang.Class> getClassesOf(java.lang.String uri)
Description copied from interface: Mapper
find the classes of the object with id uri

Parameters:
uri - the id of the object looked for
Returns:
a list of classes

enableInferencing

public void enableInferencing(boolean on)

isInferencingEnabled

public boolean isInferencingEnabled()