Package jason.bb

Class JDBCPersistentBB

java.lang.Object
jason.bb.BeliefBase
jason.bb.ChainBBAdapter
jason.bb.JDBCPersistentBB
All Implemented Interfaces:
ToDOM, java.lang.Cloneable, java.lang.Iterable<Literal>
Direct Known Subclasses:
AgentJDBCPersistentBB

public class JDBCPersistentBB
extends ChainBBAdapter
Implementation of BB that stores some beliefs in a relational data base.

The parameters for this customisation are:

  • args[0] is the Database Engine JDBC drive
  • args[1] is the JDBC URL connection string
    The url can use the agent name as parameter as in "jdbc:mysql://localhost/%s". In this case, %s will be replaced by the agent's name.
  • args[2] is the username
  • args[3] is the password
  • args[4] is an AS list with all beliefs that are mapped to DB. Each element of the list is in the form

    predicate(arity [, table_name [, columns]])

    and columns is in the form

    columns( col_name(col_type), col_name(col_type), ....)

Example in .mas2j project, the agent c uses a JDBC belief base:

 agents:
   c beliefBaseClass jason.bb.JDBCPersistentBB(
      "org.hsqldb.jdbcDriver", // driver for HSQLDB
      "jdbc:hsqldb:bookstore", // URL connection
      "sa", // user
      "", // password
      "[a(1,tablea,columns(runs(integer))),
        book(5),
        book_author(2),
        author(2,author,columns(id(integer),name(varchar(30)))),
        publisher(2)]");
   

The predicate a/1 is mapped to a table called "tablea" with an integer column called runs; predicate book (with arity 5) is mapped to a table called "book"; and so on.

The name and type of the columns are used only if the table does not exits and have to be created. If no column name/type is provided, an arbitrary name is used with type varchar(256). If no table name is provided, the predicate name is used for the table name.

Author:
Jomi
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected java.lang.String agentName  
    protected java.util.Map<PredicateIndicator,​java.sql.ResultSetMetaData> belsDB  
    (package private) static java.lang.String COL_ANNOT  
    (package private) static java.lang.String COL_NEG  
    (package private) static java.lang.String COL_PREFIX  
    protected java.sql.Connection conn  
    protected int extraCols
    the number of columns that this customisation creates (default is 2: the j_negated and j_annots columns)
    private static java.util.logging.Logger logger  
    static java.lang.String timestampFunctor  
    protected java.lang.String url  

    Fields inherited from class jason.bb.ChainBBAdapter

    nextBB

    Fields inherited from class jason.bb.BeliefBase

    APercept, ASelf, lock, TPercept, TSelf
  • Constructor Summary

    Constructors
    Constructor Description
    JDBCPersistentBB()  
    JDBCPersistentBB​(BeliefBase next)  
  • Method Summary

    Modifier and Type Method Description
    boolean abolish​(PredicateIndicator pi)
    Removes all believes with some functor/arity in the default namespace
    boolean add​(int index, Literal l)
    Adds a belief in the BB at index position, returns true if succeed
    boolean add​(Literal l)
    Adds a belief in the end of the BB, returns true if succeed.
    void clear()
    removes all beliefs from BB
    Literal contains​(Literal l)
    Returns the literal l as it is in BB, this method does not consider annotations in the search.
    java.util.Iterator<Literal> getCandidateBeliefs​(Literal l, Unifier u)
    Returns an iterator for all literals relevant for l's predicate indicator, if l is a var, returns all beliefs.
    The unifier u may contain values for variables in l.
    protected java.lang.String getCountQuery​(PredicateIndicator pi)
    returns a SQL command to count the number of instances of a predicate
    protected java.lang.String getCreateTable​(java.lang.String table, int arity, Structure columns)
    returns the SQL command to create a new table
    private java.util.Set<Literal> getDBBPercepts()  
    protected java.lang.String getDeleteAll​(PredicateIndicator pi)
    returns a SQL command to delete all entries for a predicate
    protected java.lang.String getInsert​(Literal l)
    returns the SQL command to insert l into the DB
    protected java.lang.String getSelect​(Literal l)
    returns the SQL command for a select that retrieves the literal l from the DB
    protected java.lang.String getSelectAll​(PredicateIndicator pi)
    returns the SQL command the selects all literals of type pi
    protected java.lang.String getTableName​(Literal l)  
    protected java.lang.String getTableName​(PredicateIndicator pi)  
    protected java.lang.String getWhere​(Literal l)
    returns the where clausule for a select for literal l
    void init​(Agent ag, java.lang.String[] args)
    Called before the MAS execution with the agent that uses this BB and the args informed in .mas2j project.
    Example in .mas2j:
    agent BeliefBaseClass(1,bla);
    the init args will be ["1", "bla"].
    protected boolean isCreatedByJason​(PredicateIndicator pi)
    returns true if the table for pi was created by Jason
    protected boolean isDB​(Literal l)
    returns true if the literal is stored in a DB
    java.util.Iterator<Literal> iterator()
    Returns an iterator for all beliefs.
    boolean remove​(Literal l)
    Removes a literal from BB, returns true if succeed
    protected Literal resultSetToLiteral​(java.sql.ResultSet rs, PredicateIndicator pi)
    translates the current line of a result set into a Literal
    int size()
    Returns the number of beliefs in BB
    void stop()
    Called just before the end of MAS execution
    static java.sql.Timestamp structure2timestamp​(Term timestamp)
    translates structure like "timestamp(Y,M,D,H,M,S)" into a SQL timestamp
    void test()
    just create some data to test
    static Structure timestamp2structure​(java.sql.Timestamp timestamp)
    translates a SQL timestamp into a structure like "timestamp(Y,M,D,H,M,S)"

    Methods inherited from class jason.bb.BeliefBase

    abolish, getLock

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

    • logger

      private static java.util.logging.Logger logger
    • COL_PREFIX

      static final java.lang.String COL_PREFIX
      See Also:
      Constant Field Values
    • COL_NEG

      static final java.lang.String COL_NEG
      See Also:
      Constant Field Values
    • COL_ANNOT

      static final java.lang.String COL_ANNOT
      See Also:
      Constant Field Values
    • extraCols

      protected int extraCols
      the number of columns that this customisation creates (default is 2: the j_negated and j_annots columns)
    • conn

      protected java.sql.Connection conn
    • url

      protected java.lang.String url
    • agentName

      protected java.lang.String agentName
    • belsDB

      protected java.util.Map<PredicateIndicator,​java.sql.ResultSetMetaData> belsDB
    • timestampFunctor

      public static final java.lang.String timestampFunctor
      See Also:
      Constant Field Values
  • Constructor Details

    • JDBCPersistentBB

      public JDBCPersistentBB()
    • JDBCPersistentBB

      public JDBCPersistentBB​(BeliefBase next)
  • Method Details

    • init

      public void init​(Agent ag, java.lang.String[] args)
      Description copied from class: BeliefBase
      Called before the MAS execution with the agent that uses this BB and the args informed in .mas2j project.
      Example in .mas2j:
      agent BeliefBaseClass(1,bla);
      the init args will be ["1", "bla"].
      Overrides:
      init in class ChainBBAdapter
    • stop

      public void stop()
      Description copied from class: BeliefBase
      Called just before the end of MAS execution
      Overrides:
      stop in class ChainBBAdapter
    • clear

      public void clear()
      Description copied from class: BeliefBase
      removes all beliefs from BB
      Overrides:
      clear in class ChainBBAdapter
    • isDB

      protected boolean isDB​(Literal l)
      returns true if the literal is stored in a DB
    • isCreatedByJason

      protected boolean isCreatedByJason​(PredicateIndicator pi) throws java.sql.SQLException
      returns true if the table for pi was created by Jason
      Throws:
      java.sql.SQLException
    • contains

      public Literal contains​(Literal l)
      Description copied from class: BeliefBase
      Returns the literal l as it is in BB, this method does not consider annotations in the search.
      Example, if BB={a(10)[a,b]}, contains(a(10)[d]) returns a(10)[a,b].
      Overrides:
      contains in class ChainBBAdapter
    • add

      public boolean add​(Literal l)
      Description copied from class: BeliefBase
      Adds a belief in the end of the BB, returns true if succeed. The annots of l may be changed to reflect what was changed in the BB, for example, if l is p[a,b] in a BB with p[a], l will be changed to p[b] to produce the event +p[b], since only the annotation b is changed in the BB.
      Overrides:
      add in class ChainBBAdapter
    • add

      public boolean add​(int index, Literal l)
      Description copied from class: BeliefBase
      Adds a belief in the BB at index position, returns true if succeed
      Overrides:
      add in class ChainBBAdapter
    • remove

      public boolean remove​(Literal l)
      Description copied from class: BeliefBase
      Removes a literal from BB, returns true if succeed
      Overrides:
      remove in class ChainBBAdapter
    • getDBBPercepts

      private java.util.Set<Literal> getDBBPercepts()
    • abolish

      public boolean abolish​(PredicateIndicator pi)
      Description copied from class: BeliefBase
      Removes all believes with some functor/arity in the default namespace
      Overrides:
      abolish in class ChainBBAdapter
    • getCandidateBeliefs

      public java.util.Iterator<Literal> getCandidateBeliefs​(Literal l, Unifier u)
      Description copied from class: BeliefBase
      Returns an iterator for all literals relevant for l's predicate indicator, if l is a var, returns all beliefs.
      The unifier u may contain values for variables in l. Example, if BB={a(10),a(20),a(2,1),b(f)}, then getCandidateBeliefs(a(5), {}) = {{a(10),a(20)}.
      if BB={a(10),a(20)}, then getCandidateBeliefs(X) = {{a(10),a(20)}. The getCandidateBeliefs(a(X), {X -> 5}) should also return {{a(10),a(20)}.
      Overrides:
      getCandidateBeliefs in class ChainBBAdapter
    • size

      public int size()
      Description copied from class: BeliefBase
      Returns the number of beliefs in BB
      Overrides:
      size in class ChainBBAdapter
    • iterator

      public java.util.Iterator<Literal> iterator()
      Description copied from class: BeliefBase
      Returns an iterator for all beliefs.
      Specified by:
      iterator in interface java.lang.Iterable<Literal>
      Overrides:
      iterator in class ChainBBAdapter
    • resultSetToLiteral

      protected Literal resultSetToLiteral​(java.sql.ResultSet rs, PredicateIndicator pi) throws java.sql.SQLException
      translates the current line of a result set into a Literal
      Throws:
      java.sql.SQLException
    • getTableName

      protected java.lang.String getTableName​(Literal l) throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • getTableName

      protected java.lang.String getTableName​(PredicateIndicator pi) throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • getCreateTable

      protected java.lang.String getCreateTable​(java.lang.String table, int arity, Structure columns) throws java.sql.SQLException
      returns the SQL command to create a new table
      Throws:
      java.sql.SQLException
    • getSelect

      protected java.lang.String getSelect​(Literal l) throws java.sql.SQLException
      returns the SQL command for a select that retrieves the literal l from the DB
      Throws:
      java.sql.SQLException
    • getSelectAll

      protected java.lang.String getSelectAll​(PredicateIndicator pi) throws java.sql.SQLException
      returns the SQL command the selects all literals of type pi
      Throws:
      java.sql.SQLException
    • getWhere

      protected java.lang.String getWhere​(Literal l) throws java.sql.SQLException
      returns the where clausule for a select for literal l
      Throws:
      java.sql.SQLException
    • getInsert

      protected java.lang.String getInsert​(Literal l) throws java.lang.Exception
      returns the SQL command to insert l into the DB
      Throws:
      java.lang.Exception
    • getDeleteAll

      protected java.lang.String getDeleteAll​(PredicateIndicator pi) throws java.sql.SQLException
      returns a SQL command to delete all entries for a predicate
      Throws:
      java.sql.SQLException
    • getCountQuery

      protected java.lang.String getCountQuery​(PredicateIndicator pi) throws java.sql.SQLException
      returns a SQL command to count the number of instances of a predicate
      Throws:
      java.sql.SQLException
    • test

      public void test()
      just create some data to test
    • timestamp2structure

      public static Structure timestamp2structure​(java.sql.Timestamp timestamp) throws java.sql.SQLException
      translates a SQL timestamp into a structure like "timestamp(Y,M,D,H,M,S)"
      Throws:
      java.sql.SQLException
    • structure2timestamp

      public static java.sql.Timestamp structure2timestamp​(Term timestamp) throws java.lang.Exception
      translates structure like "timestamp(Y,M,D,H,M,S)" into a SQL timestamp
      Throws:
      java.lang.Exception