agentland.device.intelliCD
Class BrainMatrix

java.lang.Object
  |
  +--agentland.device.intelliCD.BrainMatrix

public class BrainMatrix
extends Object

The BrainMatrix talks to the backend database, and handles updates and other information that the matrix uses to keep song associations and reactions up to date. Effectively, it insulates the QuickBrain from the grungy database details. To efficiently process subsets of songs, BrainMatrix will set up a `temporary' table in the database and use it to make sure that any selected song is in the subset of songs we care about. (I use `temporary' in quotes, because this table will tend to stick around for a while unless the BrainMatrix exits cleanly.) the brain matrix looks like this: songs other song1 other song2 other song3 Pos | Neg Pos | Neg Pos | Neg #1 val val vA vB #2 ... vA would be weight for going from #1 to other2 if #1 liked. the history matrix looks like this: song/reaction other song1 other song2 ... #1 / pos v1 v2 where #1 is most recent, #2 is just before. v1 is #1/react to 1 -> other1

See Also:
QuickBrain, ManagedConnection, Song

Field Summary
static String NEG
           
static String POS
           
 
Constructor Summary
BrainMatrix(String name, ManagedConnection cnxn, int n)
          Creates a new BrainMatrix, with a name (used for creating a temporary table in the database, a SQL database connection, and a size for the matrix.
 
Method Summary
 void addHistory(Song s, String colName)
          Add a song to the history with a given reaction.
 void addNegativeHistory(Song s)
          Adds a negative history for a song.
 void addNeutralHistory(Song s)
          Adds a neutral history for a song.
 void addPositiveHistory(Song s)
          Adds a positive history for a song.
 String colName(int n)
          Returns the reaction column name in row n of the matrix.
 String convertReaction(double reaction)
          Gets a reaction column string, given a reaction code from the QuickBrain.
 void finalize()
           
 List getBestNeighborhood(Song s, String reacCol, int len)
           
 List getLikedSongs(String reacCol, byte min_val, int len)
          Get a vector of songs that people tend to like.
 byte getLocation(int n, Song s)
          Fetches the reaction score for the matrix element in row #n and col #s.
 byte getLocation(Song row, String colName, Song col)
          Gets the value for the match of two songs on the given parameter from the database.
 List getNeighborhood(Song s)
           
 List getNeighborhood(Song s, String reacCol)
           
 List getNeighborhood(Song s, String reacCol, byte val)
           
 List getNeighborhood(Song s, String reacCol, byte val, boolean greater, int len)
          Fetches a neighborhood of songs.
 List getUnderstoodSongs(int len)
          Gets the list of songs that have the most information stored about them, in order.
 void preloadSong(Song s)
          Notifies the matrix that a song is being played.
 void registerSongs(List songs)
          Registers the list of songs that the matrix cares about.
 int rows()
          Gets the number of songs currently recorded in play history.
 void setLocation(int n, Song s, byte b)
          Sets the matrix element to the given value.
 void setLocation(Song row, String colName, Song col, byte b)
          Sets the value for the match of two songs on the given parameter from the database.
 Song song(int n)
          Returns the song in row n of the matrix.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

POS

public static String POS

NEG

public static String NEG
Constructor Detail

BrainMatrix

public BrainMatrix(String name,
                   ManagedConnection cnxn,
                   int n)
Creates a new BrainMatrix, with a name (used for creating a temporary table in the database, a SQL database connection, and a size for the matrix. This size (the number of rows in the matrix), indicates the number of songs that the matrix can consider. This simplifies the QuickBrain, so the QB only has to worry about the virtual `matrix' that the BrainMatrix presents.
Parameters:
n - number of rows brain matrix will store.
Method Detail

registerSongs

public void registerSongs(List songs)
Registers the list of songs that the matrix cares about. This also builds the table in the database.

preloadSong

public void preloadSong(Song s)
Notifies the matrix that a song is being played. This method uses the song to preload all possible reactions and cache them for later use. This is mostly for speed in the user interface, since by preloading the possible reactions we can then quickly switch to the appropriate reaction when the reaction is actually recorded. Thus, the `reaction time' of the matrix is a bit quicker.

addHistory

public void addHistory(Song s,
                       String colName)
                throws Exception
Add a song to the history with a given reaction. If the song has been preloaded, it is fetched from the preload cache. Otherwise, it is fetched from the database anew.

addPositiveHistory

public void addPositiveHistory(Song s)
                        throws Exception
Adds a positive history for a song.
See Also:
addHistory(Song, String)

addNeutralHistory

public void addNeutralHistory(Song s)
                       throws Exception
Adds a neutral history for a song.
See Also:
addHistory(Song, String)

addNegativeHistory

public void addNegativeHistory(Song s)
                        throws Exception
Adds a negative history for a song.
See Also:
addHistory(Song, String)

getLocation

public byte getLocation(int n,
                        Song s)
Fetches the reaction score for the matrix element in row #n and col #s. Note the reaction will be implicit since row #n is both a song and the reaction to it.
Parameters:
n - is the row number for the history 'matrix'
s - is the Song to be matched.

setLocation

public void setLocation(int n,
                        Song s,
                        byte b)
Sets the matrix element to the given value.
Parameters:
n - is the row number for the history 'matrix'
s - is the id for the song to match with.
b - is the value to set.

getLocation

public byte getLocation(Song row,
                        String colName,
                        Song col)
Gets the value for the match of two songs on the given parameter from the database. This is a more generalized method than the `matricized' version above.
See Also:
getLocation(int, Song)

setLocation

public void setLocation(Song row,
                        String colName,
                        Song col,
                        byte b)
Sets the value for the match of two songs on the given parameter from the database. This is a more generalized method than the `matricized' version above.
See Also:
setLocation(int, Song, byte)

convertReaction

public String convertReaction(double reaction)
Gets a reaction column string, given a reaction code from the QuickBrain. Right now, positive reactions are positive, negative are negative :-)

getNeighborhood

public List getNeighborhood(Song s)

getNeighborhood

public List getNeighborhood(Song s,
                            String reacCol)

getNeighborhood

public List getNeighborhood(Song s,
                            String reacCol,
                            byte val)

getBestNeighborhood

public List getBestNeighborhood(Song s,
                                String reacCol,
                                int len)

getNeighborhood

public List getNeighborhood(Song s,
                            String reacCol,
                            byte val,
                            boolean greater,
                            int len)
Fetches a neighborhood of songs. This is to allow a more general interaction with the database, where we can find all songs that are considered `interesting' with respect to a given search song and a reaction code.
Parameters:
s - is the search song.
reacCol - is the name of the column to use for reaction strength
val - is a target reaction value to use for limiting the output.
greater - says whether we should return songs that are greater than val (true means return songs that have a value greater than the given target value, false means return songs with a value less than the given target value.
len - is a limit to the list size.
Returns:
List of Song, byte, etc. where byte is the reaction strength to the song just prior in vector.

getLikedSongs

public List getLikedSongs(String reacCol,
                          byte min_val,
                          int len)
Get a vector of songs that people tend to like. This tries to find entries in the database where people had a favorable reaction to a song.
Parameters:
reacCol - is the reaction type
min_val - is the minimum reaction strength allowed in the output.
len - is the limit to the number of songs returned.
Returns:
list of Songs which have some entry s.t. another song points to them with value at least min_val

getUnderstoodSongs

public List getUnderstoodSongs(int len)
Gets the list of songs that have the most information stored about them, in order.
Parameters:
len - The number of songs to return (maximum)
Returns:
list of songs which have some entry

rows

public int rows()
Gets the number of songs currently recorded in play history.

song

public Song song(int n)
Returns the song in row n of the matrix.

colName

public String colName(int n)
Returns the reaction column name in row n of the matrix.

finalize

public void finalize()
Overrides:
finalize in class Object