jade.domain
Class DFDBKB

java.lang.Object
  extended by jade.domain.KBManagement.KB
      extended by jade.domain.KBManagement.DBKB
          extended by jade.domain.DFDBKB
Direct Known Subclasses:
DFHSQLKB

public class DFDBKB
extends jade.domain.KBManagement.DBKB

This class implements a knowledge base used by the DF which stores its content in an external database.

Author:
Elisabetta Cortese - TILab, Roland Mungenast - Profactor

Nested Class Summary
 
Nested classes/interfaces inherited from class jade.domain.KBManagement.DBKB
jade.domain.KBManagement.DBKB.ConnectionWrapper, jade.domain.KBManagement.DBKB.EmptyKBIterator
 
Field Summary
protected  java.lang.String DEFAULT_LONGVARCHAR_TYPE
          Default data type for very long strings
 
Fields inherited from class jade.domain.KBManagement.DBKB
cleanTables, driver
 
Fields inherited from class jade.domain.KBManagement.KB
lm, logger, sr
 
Constructor Summary
DFDBKB(int maxResultLimit, java.lang.String drv, java.lang.String url, java.lang.String user, java.lang.String passwd, boolean cleanTables)
          Constructor
 
Method Summary
protected  void createDFTables()
          Creates the proper DB tables.
protected  void createIndices()
          Adds explicit indices to the database to speed up queries
protected  void createTable(java.lang.String name, java.lang.String[] entries)
          Creates a new DB table
protected  void dropDFTables()
          Drops all existing DB tables used by the DF.
protected  void dropTable(java.sql.Statement stmt, java.lang.String tableName)
          Drops a DB table or does nothing if the table doesn't exist.
protected  java.lang.String getGUID()
          Returns a global unique identifier
protected  java.lang.String getHashValue(java.lang.Object obj)
          Returns an MD5 hash value for an object
protected  java.lang.String getLongVarCharType()
          Returns the name of the SQL type used in the database to represent very long strings.
 java.util.Enumeration getSubscriptions()
          Return all known subscriptions at the DF
protected  void initConnectionWrapper(jade.domain.KBManagement.DBKB.ConnectionWrapper wrapper)
          Subclasses can redefine this method to provide implementation specific ConnectionWrapper initializations
protected  java.lang.Object insertSingle(java.lang.Object name, java.lang.Object fact)
          Insert a new DFD object.
protected  jade.domain.KBManagement.KBIterator iteratorSingle(java.lang.Object template)
           
protected  java.lang.String prepDBStr(java.lang.String s)
          This method must be used when inserting text values into a DB.
protected  java.lang.Object removeSingle(java.lang.Object name)
          Remove the DFD object corresponding to the indicated AID.
protected  java.lang.String replace(java.lang.String str, java.lang.String pattern, java.lang.String replaceWith)
          Replaces all occurences of a pattern in str with replaceWith
protected  List searchSingle(java.lang.Object template, int maxResult)
          Retrieve the DFDs matching the given template
 void setup()
          Initializes all used SQL statements, the DB tables and the logging
protected  void subscribeSingle(java.lang.Object dfd, SubscriptionResponder.Subscription s)
           
protected  boolean tableExists(java.lang.String name)
          Check whether a database table already exists
protected  void unsubscribeSingle(SubscriptionResponder.Subscription sub)
           
 
Methods inherited from class jade.domain.KBManagement.DBKB
createDBConnection, getConnectionWrapper, insert, iterator, remove, search, subscribe, unsubscribe
 
Methods inherited from class jade.domain.KBManagement.KB
deregister, register, search, setLeaseManager, setSubscriptionResponder
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LONGVARCHAR_TYPE

protected java.lang.String DEFAULT_LONGVARCHAR_TYPE
Default data type for very long strings

Constructor Detail

DFDBKB

public DFDBKB(int maxResultLimit,
              java.lang.String drv,
              java.lang.String url,
              java.lang.String user,
              java.lang.String passwd,
              boolean cleanTables)
       throws java.sql.SQLException
Constructor

Parameters:
maxResultLimit - internal limit for the number of maximum search results.
drv - database driver
url - database URL
user - database user name
passwd - database password
cleanTables - specifies whether the KB should delete all existing tables for the DF at startup
Throws:
java.sql.SQLException - an error occured while opening a connection to the database
Method Detail

setup

public void setup()
           throws java.sql.SQLException
Initializes all used SQL statements, the DB tables and the logging

Specified by:
setup in class jade.domain.KBManagement.DBKB
Throws:
java.sql.SQLException

initConnectionWrapper

protected void initConnectionWrapper(jade.domain.KBManagement.DBKB.ConnectionWrapper wrapper)
                              throws java.sql.SQLException
Description copied from class: jade.domain.KBManagement.DBKB
Subclasses can redefine this method to provide implementation specific ConnectionWrapper initializations

Overrides:
initConnectionWrapper in class jade.domain.KBManagement.DBKB
Throws:
java.sql.SQLException

getLongVarCharType

protected java.lang.String getLongVarCharType()
Returns the name of the SQL type used in the database to represent very long strings.


getGUID

protected java.lang.String getGUID()
Returns a global unique identifier


dropTable

protected void dropTable(java.sql.Statement stmt,
                         java.lang.String tableName)
Drops a DB table or does nothing if the table doesn't exist.


dropDFTables

protected void dropDFTables()
                     throws java.sql.SQLException
Drops all existing DB tables used by the DF.

Throws:
java.sql.SQLException

tableExists

protected boolean tableExists(java.lang.String name)
Check whether a database table already exists

Parameters:
name - name of the table

createTable

protected void createTable(java.lang.String name,
                           java.lang.String[] entries)
Creates a new DB table

Parameters:
name - name of the table
entries - array of column and constraint specifications
Throws:
java.sql.SQLException - If the table cannot be created

createIndices

protected void createIndices()
Adds explicit indices to the database to speed up queries


createDFTables

protected void createDFTables()
Creates the proper DB tables.


insertSingle

protected java.lang.Object insertSingle(java.lang.Object name,
                                        java.lang.Object fact)
                                 throws java.sql.SQLException
Insert a new DFD object.

Specified by:
insertSingle in class jade.domain.KBManagement.DBKB
Returns:
the previous DFD (if any) corresponding to the same AID
Throws:
java.sql.SQLException

removeSingle

protected java.lang.Object removeSingle(java.lang.Object name)
                                 throws java.sql.SQLException
Remove the DFD object corresponding to the indicated AID.

Specified by:
removeSingle in class jade.domain.KBManagement.DBKB
Returns:
the removed DFD (if any)
Throws:
java.sql.SQLException

searchSingle

protected List searchSingle(java.lang.Object template,
                            int maxResult)
                     throws java.sql.SQLException
Retrieve the DFDs matching the given template

Specified by:
searchSingle in class jade.domain.KBManagement.DBKB
Throws:
java.sql.SQLException

iteratorSingle

protected jade.domain.KBManagement.KBIterator iteratorSingle(java.lang.Object template)
                                                      throws java.sql.SQLException
Specified by:
iteratorSingle in class jade.domain.KBManagement.DBKB
Throws:
java.sql.SQLException

subscribeSingle

protected void subscribeSingle(java.lang.Object dfd,
                               SubscriptionResponder.Subscription s)
                        throws java.sql.SQLException,
                               NotUnderstoodException
Specified by:
subscribeSingle in class jade.domain.KBManagement.DBKB
Throws:
java.sql.SQLException
NotUnderstoodException

getSubscriptions

public java.util.Enumeration getSubscriptions()
Return all known subscriptions at the DF

Specified by:
getSubscriptions in class jade.domain.KBManagement.DBKB
Returns:
Enumeration with instances of the class jade.proto.SubscriptionResponder&Subscription

unsubscribeSingle

protected void unsubscribeSingle(SubscriptionResponder.Subscription sub)
                          throws java.sql.SQLException
Specified by:
unsubscribeSingle in class jade.domain.KBManagement.DBKB
Throws:
java.sql.SQLException

getHashValue

protected java.lang.String getHashValue(java.lang.Object obj)
                                 throws java.lang.Exception
Returns an MD5 hash value for an object

Parameters:
obj - Object to use for the hash value calcualtion
Returns:
an MD5 value encoded in ISO-8859-1
Throws:
java.lang.Exception - The hash value couldn't be generated

prepDBStr

protected java.lang.String prepDBStr(java.lang.String s)
This method must be used when inserting text values into a DB. It doubles all ' and " characters If the passed parameter is null, then an empty string is returned.


replace

protected java.lang.String replace(java.lang.String str,
                                   java.lang.String pattern,
                                   java.lang.String replaceWith)
Replaces all occurences of a pattern in str with replaceWith

Parameters:
str - source string
pattern - pattern string to search for
replaceWith - new string