org.aitools.programd.multiplexor
Class PredicateMaster

java.lang.Object
  extended by org.aitools.programd.multiplexor.PredicateMaster

public class PredicateMaster
extends java.lang.Object

Maintains in-memory predicate values for userids. Every public set and get method checks the size of the cache, and saves out part of it if it has exceeded a configurable limit.

This currently has the defect that it doesn't choose intelligently which userids' predicates to cache (it should do this for the ones who have not been heard from the longest). The HashMap that contains the predicates (keyed by userid) makes no guarantees about order. :-(

Since:
4.1.4'
Author:
Noel Bush

Field Summary
protected  Bots bots
          The Bots object in use.
private  int cacheMax
          The maximum size of the cache.
private  int cacheMin
          The preferred minimum value for the cache (starts at half of cacheMax, may be adjusted).
protected static int cacheSize
          A counter for tracking the number of predicate value cache operations.
protected  Core core
          The Core that owns this.
protected  org.apache.log4j.Logger logger
          The general Program D logger.
static int MAX_INDEX
          Maximum index of indexed predicates.
protected  Multiplexor multiplexor
          The Multiplexor in use.
protected  java.lang.String predicateEmptyDefault
          The predicate empty default.
 
Constructor Summary
PredicateMaster(Core coreOwner)
          Creates a new PredicateMaster with the given Core as its owner.
 
Method Summary
private  java.lang.String bestAvailableDefault(java.lang.String name, java.lang.String botid)
          Returns the best available default predicate value for a predicate name
 java.lang.String get(java.lang.String name, int index, java.lang.String userid, java.lang.String botid)
          Gets the predicate value associated with a name at a given index for a given userid.
 java.lang.String get(java.lang.String name, java.lang.String userid, java.lang.String botid)
          Gets the predicate value associated with a name for a given userid.
private  PredicateValue getLoadOrCreateMultivaluedPredicateValue(java.lang.String name, PredicateMap userPredicates, java.lang.String userid, java.lang.String botid)
          Returns a value list one way or another: first tries to get it from the cache, then tries to load it from the ActiveMultiplexor; finally creates a new one if the preceding failed.
private static PredicateValue getMultivaluedPredicateValue(java.lang.String name, PredicateMap userPredicates)
          Returns, from the cache, an ArrayList of values assigned to a name for a predicate for a userid.
private  void checkCache()
          Checks the predicate cache, and saves out predicates if necessary.
private  PredicateValue loadMultivaluedPredicateValue(java.lang.String name, PredicateMap userPredicates, java.lang.String userid, java.lang.String botid)
          Tries to load a predicate with name for userid from the ActiveMultiplexor into the userPredicates.
private  java.lang.String nameOrValue(java.lang.String name, java.lang.String value, java.lang.String botid)
          Returns the name or value of a predicate, depending on whether or not it is "return-name-when-set".
 java.lang.String push(java.lang.String name, java.lang.String newValue, java.lang.String userid, java.lang.String botid)
          Pushes a new value onto an indexed predicate name for a given userid, and returns either the name or the value, depending on the predicate type.
private  int save(int dumpCount)
          Attempts to dump a given number of predicate values from the cache, starting with the oldest userid first.
 void saveAll()
          Dumps the entire cache.
 java.lang.String set(java.lang.String name, int index, java.lang.String valueToSet, java.lang.String userid, java.lang.String botid)
          Sets a value of an indexed predicate name for a given userid, and returns either the name or the value, depending on the predicate type.
 java.lang.String set(java.lang.String name, java.lang.String value, java.lang.String userid, java.lang.String botid)
          Sets a predicate value against a predicate name for a given userid, and returns either the name or the value, depending on the predicate type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_INDEX

public static final int MAX_INDEX
Maximum index of indexed predicates.

See Also:
Constant Field Values

cacheMax

private int cacheMax
The maximum size of the cache.


cacheMin

private int cacheMin
The preferred minimum value for the cache (starts at half of cacheMax, may be adjusted).


cacheSize

protected static int cacheSize
A counter for tracking the number of predicate value cache operations.


predicateEmptyDefault

protected java.lang.String predicateEmptyDefault
The predicate empty default.


core

protected Core core
The Core that owns this.


multiplexor

protected Multiplexor multiplexor
The Multiplexor in use.


bots

protected Bots bots
The Bots object in use.


logger

protected org.apache.log4j.Logger logger
The general Program D logger.

Constructor Detail

PredicateMaster

public PredicateMaster(Core coreOwner)
Creates a new PredicateMaster with the given Core as its owner.

Parameters:
coreOwner - the Core that owns this PredicateMaster
Method Detail

set

public java.lang.String set(java.lang.String name,
                            java.lang.String value,
                            java.lang.String userid,
                            java.lang.String botid)
Sets a predicate value against a predicate name for a given userid, and returns either the name or the value, depending on the predicate type.

Parameters:
name - the predicate name
value - the predicate value
userid - the userid
botid -
Returns:
the name or the value, depending on the predicate type

set

public java.lang.String set(java.lang.String name,
                            int index,
                            java.lang.String valueToSet,
                            java.lang.String userid,
                            java.lang.String botid)
Sets a value of an indexed predicate name for a given userid, and returns either the name or the value, depending on the predicate type.

Parameters:
name - the predicate name
index - the index at which to set the value
valueToSet - the predicate value to set
userid - the userid
botid -
Returns:
the name or the value, depending on the predicate type

push

public java.lang.String push(java.lang.String name,
                             java.lang.String newValue,
                             java.lang.String userid,
                             java.lang.String botid)
Pushes a new value onto an indexed predicate name for a given userid, and returns either the name or the value, depending on the predicate type.

Parameters:
name - the predicate name
newValue - the new predicate value
userid - the userid
botid -
Returns:
the name or the value, depending on the predicate type

get

public java.lang.String get(java.lang.String name,
                            java.lang.String userid,
                            java.lang.String botid)
Gets the predicate value associated with a name for a given userid.

Parameters:
name - the predicate name
userid - the userid
botid -
Returns:
the value associated with the given name, for the given userid

get

public java.lang.String get(java.lang.String name,
                            int index,
                            java.lang.String userid,
                            java.lang.String botid)
Gets the predicate value associated with a name at a given index for a given userid.

Parameters:
name - the predicate name
index - the index
userid - the userid
botid -
Returns:
the value associated with the given name at the given index, for the given userid

getMultivaluedPredicateValue

private static PredicateValue getMultivaluedPredicateValue(java.lang.String name,
                                                           PredicateMap userPredicates)
                                                    throws NoSuchPredicateException
Returns, from the cache, an ArrayList of values assigned to a name for a predicate for a userid. If the name exists in a predicate for the userid but it is not indexed, it is converted into an indexed value. If it does not exist at all, a NoSuchPredicateException is thrown.

Parameters:
name - the name of the predicate
userPredicates - the existing map of predicates
Returns:
a list of values assigned to a name for a predicate for a userid
Throws:
NoSuchPredicateException - if no values are assigned to the name

loadMultivaluedPredicateValue

private PredicateValue loadMultivaluedPredicateValue(java.lang.String name,
                                                     PredicateMap userPredicates,
                                                     java.lang.String userid,
                                                     java.lang.String botid)
                                              throws NoSuchPredicateException,
                                                     java.lang.NullPointerException
Tries to load a predicate with name for userid from the ActiveMultiplexor into the userPredicates. If successful, tries to get the value list for name. If unsuccessful, throws a NoSuchPredicateException.

Parameters:
name - the predicate name
userPredicates - the user predicates (must not be null!)
userid - the userid
botid -
Returns:
an ArrayList of values assigned to a name for a predicate for a userid
Throws:
NoSuchPredicateException - if no values are assigned to the name
java.lang.NullPointerException - if userPredicates is null

getLoadOrCreateMultivaluedPredicateValue

private PredicateValue getLoadOrCreateMultivaluedPredicateValue(java.lang.String name,
                                                                PredicateMap userPredicates,
                                                                java.lang.String userid,
                                                                java.lang.String botid)
Returns a value list one way or another: first tries to get it from the cache, then tries to load it from the ActiveMultiplexor; finally creates a new one if the preceding failed.

Parameters:
name - the predicate name
userPredicates - the user predicates map
userid - the userid for which to return the value list
botid - the botid for which to return the value list
Returns:
a multi-valued PredicateValue from userPredicates for name for userid

bestAvailableDefault

private java.lang.String bestAvailableDefault(java.lang.String name,
                                              java.lang.String botid)
Returns the best available default predicate value for a predicate name

Parameters:
name - the predicate name
botid -
Returns:
the best available default predicate

nameOrValue

private java.lang.String nameOrValue(java.lang.String name,
                                     java.lang.String value,
                                     java.lang.String botid)
Returns the name or value of a predicate, depending on whether or not it is "return-name-when-set".

Parameters:
name - the predicate name
value - the predicate value
botid -
Returns:
the appropriate result (name or value depending on predicate settings)

save

private int save(int dumpCount)
Attempts to dump a given number of predicate values from the cache, starting with the oldest userid first.

Parameters:
dumpCount - the number of values to try to dump
Returns:
the number that were actually dumped

saveAll

public void saveAll()
Dumps the entire cache.


checkCache

private void checkCache()
Checks the predicate cache, and saves out predicates if necessary.