nz.ac.waikato.mcennis.rat.graph
Class DerbyGraph

java.lang.Object
  extended by nz.ac.waikato.mcennis.rat.graph.model.ModelShell
      extended by nz.ac.waikato.mcennis.rat.graph.DerbyGraph
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable, Graph, Listener, Model, ParsedObject
Direct Known Subclasses:
PostgresqlGraph

public class DerbyGraph
extends ModelShell
implements Graph, Listener, java.lang.Comparable

Class that implements all the JDBC code for a Derby backed graph model. This model differs from persistance in that all loading of actors, links, and properties is done using lazy techniques (they are not createde until requested). Provided that the appropriate actor class (DBActor by default) is used, persistance is maintained with generated references in a manner similar to hibernate.

The first time this system is utilized, it requires initialization to create the appropriate tables in the database. This database must be the same for the corresponding actor class, but is handled by default (unless the class is changed between startup and actor request.)

The system requires two paramters - the directory of the database and the database name. All JDBC calls are compiled and have auto-commit disabled. Periodic calls to commit() are required to maintain consistancy between the database and memory-resident components.

See Also:
Serialized Form

Nested Class Summary
 class DerbyGraph.ActorIterator
           
 
Field Summary
static long serialVersionUID
           
 
Fields inherited from class nz.ac.waikato.mcennis.rat.graph.model.ModelShell
listener
 
Constructor Summary
DerbyGraph()
          Creates a new instance of DerbyGraph
 
Method Summary
 void add(Actor u)
          Add a user to the graph.
 void add(Graph g)
          Add a graph (g) to the graph.
 void add(Link link)
          Adds a userlink to the graph.
 void add(PathSet pathSet)
          Add a PathSet to this graph.
 void add(Property prop)
          Add a property to this graph.
 void addChild(Graph g)
          FIXME: Does not save to database
 void anonymize()
          Replaces all actor IDs with an anonymous numeric ID.
 void clear()
          Removes all data from the database.
 void close()
          Attempts to release all resources created by the startup method
 void commit()
          Aquire a set of properties from the database attached to a given actor.
 int compareTo(java.lang.Object o)
           
 void deleteDatabase()
          Attempts to remove the database tables so that initializeDatabase will be needed to utilize the graph again.
protected  void finalize()
           
 java.util.List<Actor> getActor()
          Retrieve an array of all actors of all types.
 java.util.List<Actor> getActor(java.lang.String type)
          Retrieve an array of all actors of a given type.
 Actor getActor(java.lang.String type, java.lang.String ID)
          Retrieve the users that has the given ID.
protected  java.lang.String getActorClass()
          Determine what class will be used for the creation of actor objects.
 int getActorCount(java.lang.String mode)
          Returns the number of actors of the given type
 java.util.Iterator<Actor> getActorIterator()
           
 java.util.Iterator<Actor> getActorIterator(java.lang.String type)
          Create a read only iterator over all actors.
 java.util.List<java.lang.String> getActorTypes()
          List all types - returns null if no actors exist.
 java.util.List<Graph> getChildren()
          FIXME: Does not properly access database
 Graph getChildren(java.lang.String id)
          FIXME: Does not properly access database
 java.util.Iterator<Graph> getChildrenIterator()
           
 java.io.File getDirectory()
          Return the directory that this object will or is using.
 java.lang.String getID()
          Return the ID associated with this Graph
 java.util.List<Link> getLink()
          Return all user to user links in this graph in an array.
 java.util.List<Link> getLink(java.lang.String type)
          Retrieve an array of all userlinks of the given type from this graph or null if no such links are in this graph.
 java.util.List<Link> getLink(java.lang.String type, Actor sourceActor, Actor destActor)
          Returns all links of the given type (relation) type going from actor sourceActor to actor destActor or null.
 java.util.Iterator<Link> getLinkByDesinationIterator(java.lang.String type, Actor destActor)
           
 java.util.List<Link> getLinkByDestination(java.lang.String type, Actor destActor)
          Returns all links (edges, arcs) of type (relation) type that point to the given actor.
 java.util.List<Link> getLinkBySource(java.lang.String type, Actor sourceActor)
          Returns all links (edges, arcs) of type (relation) type that go from the given actor to any destination.
 java.util.Iterator<Link> getLinkBySourceIterator(java.lang.String type, Actor sourceActor)
           
 java.util.Iterator<Link> getLinkIterator()
           
 java.util.Iterator<Link> getLinkIterator(java.lang.String type)
           
 java.util.Iterator<Link> getLinkIterator(java.lang.String type, Actor sourceActor, Actor destActor)
           
 java.util.List<java.lang.String> getLinkTypes()
          Returns all link types currently in this graph or null if no links are present.
 Properties getParameter()
          Obtain a computer-readable description of the parameters and options this graph supports
 Parameter getParameter(java.lang.String name)
           
 Graph getParent()
          FIXME: Does not properly access database
 java.util.List<PathSet> getPathSet()
          Return all PathSets associated with this graph.
 PathSet getPathSet(java.lang.String id)
          Return the named PathSet.
 java.util.Iterator<PathSet> getPathSetIterator()
           
 java.util.List<Property> getProperty()
          Returns an array of all properties associated with this object or null if none exist
 Property getProperty(java.lang.String type)
          Return a property named by the given string or null if no property by this name exists
 java.util.Iterator<Property> getPropertyIterator()
           
 void init(Properties props)
          Set the parameters of this graph from the following property object.
protected  void initActor()
          Performs initialization of the DBActor class - a static call that prepares all subsequent calls for actor creation.
 void initializeDatabase()
          The first call to be made if the database has never been used before.
protected  void initLink()
           
 boolean isInitialized()
          Determine if 'startup' has been called for this graph.
protected  void prepareStatement()
          Prepares all statements utilized by DerbyGraph.
 DerbyGraph prototype()
           
 void publishChange(Model m, int type, int argument)
          Records changes in properties of this graph, updating the database as needed.
 void remove(Actor u)
          Identifies the given user and removes it and all links involving it from the graph.
 void remove(Link ul)
          remove all user links with the same type, user ID, and artist ID from the graph
 void removeProperty(java.lang.String ID)
           
protected  Actor retrieveActor(int id)
          Retrieve an actor object from the database with its database's numeric id.
protected  void setActorClass(java.lang.String actorClass)
          Determine what class will be used for the creation of actor objects.
 void setDirectory(java.io.File f)
          Set the directory that DerbyGraph will look for its database
 void setID(java.lang.String id)
          Set this graph's ID to this value.
 void startup()
          Starts the database Creates a database connection at the directory 'directory' with database name 'database' aquires the graph id of the root graph compiles all the prepared statements used in the class
 
Methods inherited from class nz.ac.waikato.mcennis.rat.graph.model.ModelShell
addListener, fireChange
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface nz.ac.waikato.mcennis.rat.graph.model.Model
addListener
 

Field Detail

serialVersionUID

public static final long serialVersionUID
See Also:
Constant Field Values
Constructor Detail

DerbyGraph

public DerbyGraph()
Creates a new instance of DerbyGraph

Method Detail

init

public void init(Properties props)
Description copied from interface: Graph
Set the parameters of this graph from the following property object.

Specified by:
init in interface Graph
Parameters:
props - properties to be set

startup

public void startup()
             throws java.sql.SQLException
Starts the database

initActor

protected void initActor()
Performs initialization of the DBActor class - a static call that prepares all subsequent calls for actor creation.

See Also:
DBActor

initLink

protected void initLink()

prepareStatement

protected void prepareStatement()
                         throws java.sql.SQLException
Prepares all statements utilized by DerbyGraph.

Throws:
java.sql.SQLException

initializeDatabase

public void initializeDatabase()
                        throws java.sql.SQLException
The first call to be made if the database has never been used before. It creates its own connection, so startup is not needed before. If called on an already generated table, the system will report errors, but not damage the table. Also closes its own connection, so close is not needed either.

Throws:
java.sql.SQLException

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

close

public void close()
Attempts to release all resources created by the startup method

Specified by:
close in interface Graph

isInitialized

public boolean isInitialized()
Determine if 'startup' has been called for this graph.

Returns:
whether or not this graph has been initialized

clear

public void clear()
Removes all data from the database. Assumes that startup has been called.


deleteDatabase

public void deleteDatabase()
Attempts to remove the database tables so that initializeDatabase will be needed to utilize the graph again.


add

public void add(Actor u)
Description copied from interface: Graph
Add a user to the graph. This replaces the user if a user with the same ID already exists.

Specified by:
add in interface Graph
Parameters:
u - user to be included in the graph.

add

public void add(Link link)
Description copied from interface: Graph
Adds a userlink to the graph. There is no limit on the number of links between users or even with the same type, source user, and destination user.

Specified by:
add in interface Graph
Parameters:
link - description of a named link between two users

remove

public void remove(Actor u)
Description copied from interface: Graph
Identifies the given user and removes it and all links involving it from the graph.

Specified by:
remove in interface Graph
Parameters:
u - user to be removed from the graph.

remove

public void remove(Link ul)
Description copied from interface: Graph
remove all user links with the same type, user ID, and artist ID from the graph

Specified by:
remove in interface Graph
Parameters:
ul - link to be removed from the graph.

getActor

public Actor getActor(java.lang.String type,
                      java.lang.String ID)
Description copied from interface: Graph
Retrieve the users that has the given ID. Returns null if no user is in the graph with that ID.

Specified by:
getActor in interface Graph
ID - ID string identifying an user
Returns:
user object who has this ID or null if it is not present

getActor

public java.util.List<Actor> getActor()
Description copied from interface: Graph
Retrieve an array of all actors of all types. Returns null if no actors are in the graph.

Specified by:
getActor in interface Graph
Returns:
array of all users in the graph

getActor

public java.util.List<Actor> getActor(java.lang.String type)
Description copied from interface: Graph
Retrieve an array of all actors of a given type. Returns null if there are no actors of that type in the graph.

Specified by:
getActor in interface Graph
Parameters:
type - class of actor to return
Returns:
array of actors of the given type

getActorIterator

public java.util.Iterator<Actor> getActorIterator(java.lang.String type)
Description copied from interface: Graph
Create a read only iterator over all actors.

Specified by:
getActorIterator in interface Graph
Parameters:
type - type (mode) of actor to return
Returns:
iterator over all actors of the given type

getActorTypes

public java.util.List<java.lang.String> getActorTypes()
Description copied from interface: Graph
List all types - returns null if no actors exist.

Specified by:
getActorTypes in interface Graph
Returns:
array of all type names in this graph

getLink

public java.util.List<Link> getLink()
Description copied from interface: Graph
Return all user to user links in this graph in an array. Returns null if no such user link exists.

Specified by:
getLink in interface Graph
Returns:
array of all userlinks or null

retrieveActor

protected Actor retrieveActor(int id)
Retrieve an actor object from the database with its database's numeric id. Returns null if no such actor exists.

Parameters:
id - numeric id of this object
Returns:
Actor object this id refers to

getLink

public java.util.List<Link> getLink(java.lang.String type)
Description copied from interface: Graph
Retrieve an array of all userlinks of the given type from this graph or null if no such links are in this graph.

Specified by:
getLink in interface Graph
Returns:
array of all user links of a given type or null

getLinkBySource

public java.util.List<Link> getLinkBySource(java.lang.String type,
                                            Actor sourceActor)
Description copied from interface: Graph
Returns all links (edges, arcs) of type (relation) type that go from the given actor to any destination. Returns null if no such links exist.

Specified by:
getLinkBySource in interface Graph
Parameters:
type - type (relation) of link to return
sourceActor - starting point of the link
Returns:
Array of links or null

getLinkByDestination

public java.util.List<Link> getLinkByDestination(java.lang.String type,
                                                 Actor destActor)
Description copied from interface: Graph
Returns all links (edges, arcs) of type (relation) type that point to the given actor. Returns null if no such links exist

Specified by:
getLinkByDestination in interface Graph
Parameters:
type - type (relation) of link to return
destActor - actor pointed to by links
Returns:
Array of links or null

getLink

public java.util.List<Link> getLink(java.lang.String type,
                                    Actor sourceActor,
                                    Actor destActor)
Description copied from interface: Graph
Returns all links of the given type (relation) type going from actor sourceActor to actor destActor or null. This is typically 1 link or null, but the spec does not forbid multiple links of the same type (relation) between two actors.

Specified by:
getLink in interface Graph
Parameters:
type - type (relation) of the link to return
sourceActor - starting point of the link
destActor - actor pointed to by links
Returns:
Array of links or null

getLinkTypes

public java.util.List<java.lang.String> getLinkTypes()
Description copied from interface: Graph
Returns all link types currently in this graph or null if no links are present.

Specified by:
getLinkTypes in interface Graph
Returns:
array of link types

getProperty

public java.util.List<Property> getProperty()
Description copied from interface: Graph
Returns an array of all properties associated with this object or null if none exist

Specified by:
getProperty in interface Graph
Returns:
Array of properties

getProperty

public Property getProperty(java.lang.String type)
Description copied from interface: Graph
Return a property named by the given string or null if no property by this name exists

Specified by:
getProperty in interface Graph
Parameters:
type - property's name
Returns:
Given property

add

public void add(Property prop)
         throws java.lang.NullPointerException
Description copied from interface: Graph
Add a property to this graph. If a property by this name already exists, it is overwritten with the given property.

Specified by:
add in interface Graph
Parameters:
prop - property to be added.
Throws:
java.lang.NullPointerException

getPathSet

public java.util.List<PathSet> getPathSet()
Description copied from interface: Graph
Return all PathSets associated with this graph. Null if no PathSets are in the current graph.

Specified by:
getPathSet in interface Graph
Returns:
Array of PathSets

getPathSet

public PathSet getPathSet(java.lang.String id)
Description copied from interface: Graph
Return the named PathSet. Returns null if no PathSet of the given ID exists.

Specified by:
getPathSet in interface Graph
Parameters:
id - ID of the PathSet to return
Returns:
PathSet specified by this ID

add

public void add(PathSet pathSet)
Description copied from interface: Graph
Add a PathSet to this graph. If another PathSet of the same ID exists, it will be overwritten.

Specified by:
add in interface Graph
Parameters:
pathSet - PathSet to be added to this graph

setID

public void setID(java.lang.String id)
Description copied from interface: Graph
Set this graph's ID to this value. Graph ID's must be unique.

Specified by:
setID in interface Graph
Parameters:
id - graph ID

getID

public java.lang.String getID()
Description copied from interface: Graph
Return the ID associated with this Graph

Specified by:
getID in interface Graph
Returns:
ID

setDirectory

public void setDirectory(java.io.File f)
Set the directory that DerbyGraph will look for its database

Parameters:
f - file representing the target directory.

getDirectory

public java.io.File getDirectory()
Return the directory that this object will or is using.

Returns:
directory object

commit

public void commit()
Aquire a set of properties from the database attached to a given actor. Returns a zero length array if no properties exist for this actor.

Specified by:
commit in interface Graph
Parameters:
a - unique id of the actor whose properties are to be retrieved
Throws:
java.sql.SQLException

add

public void add(Graph g)
Description copied from interface: Graph
Add a graph (g) to the graph. Will be changed to be a graph helper - deprecated by addChild(Graph g)

Specified by:
add in interface Graph
Parameters:
g - graph to be added

anonymize

public void anonymize()
Description copied from interface: Graph
Replaces all actor IDs with an anonymous numeric ID.

Specified by:
anonymize in interface Graph

publishChange

public void publishChange(Model m,
                          int type,
                          int argument)
Records changes in properties of this graph, updating the database as needed.

Specified by:
publishChange in interface Listener
Parameters:
m - property that is modified
type - currently only records adding a property

getParent

public Graph getParent()
FIXME: Does not properly access database

Specified by:
getParent in interface Graph
Returns:
parent graph

getChildren

public java.util.List<Graph> getChildren()
FIXME: Does not properly access database

Specified by:
getChildren in interface Graph
Returns:
Array of graphs that are children of this graph

getChildren

public Graph getChildren(java.lang.String id)
FIXME: Does not properly access database

Specified by:
getChildren in interface Graph
Parameters:
id - ID of the graph to return
Returns:
Graph with the given ID

addChild

public void addChild(Graph g)
FIXME: Does not save to database

Specified by:
addChild in interface Graph
Parameters:
g - Child Graph

removeProperty

public void removeProperty(java.lang.String ID)
Specified by:
removeProperty in interface Graph

setActorClass

protected void setActorClass(java.lang.String actorClass)
Determine what class will be used for the creation of actor objects.

Parameters:
actorClass - name of class of actor
See Also:
ActorFactory

getActorClass

protected java.lang.String getActorClass()
Determine what class will be used for the creation of actor objects.

Returns:
String used to create actors in ActorFactory
See Also:
ActorFactory

getParameter

public Properties getParameter()
Description copied from interface: Graph
Obtain a computer-readable description of the parameters and options this graph supports

Specified by:
getParameter in interface Graph
Returns:
Description of all parameters this graph object supports.

getActorCount

public int getActorCount(java.lang.String mode)
Description copied from interface: Graph
Returns the number of actors of the given type

Specified by:
getActorCount in interface Graph
Parameters:
mode - type of actor to query
Returns:
int number of actors of the given type

compareTo

public int compareTo(java.lang.Object o)
Specified by:
compareTo in interface java.lang.Comparable

prototype

public DerbyGraph prototype()
Specified by:
prototype in interface Graph

getActorIterator

public java.util.Iterator<Actor> getActorIterator()
Specified by:
getActorIterator in interface Graph

getLinkIterator

public java.util.Iterator<Link> getLinkIterator()
Specified by:
getLinkIterator in interface Graph

getLinkIterator

public java.util.Iterator<Link> getLinkIterator(java.lang.String type)
Specified by:
getLinkIterator in interface Graph

getLinkBySourceIterator

public java.util.Iterator<Link> getLinkBySourceIterator(java.lang.String type,
                                                        Actor sourceActor)
Specified by:
getLinkBySourceIterator in interface Graph

getLinkByDesinationIterator

public java.util.Iterator<Link> getLinkByDesinationIterator(java.lang.String type,
                                                            Actor destActor)
Specified by:
getLinkByDesinationIterator in interface Graph

getLinkIterator

public java.util.Iterator<Link> getLinkIterator(java.lang.String type,
                                                Actor sourceActor,
                                                Actor destActor)
Specified by:
getLinkIterator in interface Graph

getPropertyIterator

public java.util.Iterator<Property> getPropertyIterator()
Specified by:
getPropertyIterator in interface Graph

getPathSetIterator

public java.util.Iterator<PathSet> getPathSetIterator()
Specified by:
getPathSetIterator in interface Graph

getChildrenIterator

public java.util.Iterator<Graph> getChildrenIterator()
Specified by:
getChildrenIterator in interface Graph

getParameter

public Parameter getParameter(java.lang.String name)
Specified by:
getParameter in interface Graph

Get Relational Analysis Toolkit at SourceForge.net. Fast, secure and Free Open Source software downloads