org.aitools.programd.graph
Class Graphmaster

java.lang.Object
  extended by org.aitools.programd.graph.Graphmaster

public class Graphmaster
extends java.lang.Object

The Graphmaster is the "brain" of a Program D bot. It consists of a collection of nodes called Nodemappers. These Nodemapper s map the branches from each node. The branches are either single words or wildcards.

The root of the Graphmaster is a Nodemapper with many branches, one for each of the first words of all the patterns. The number of leaf nodes in the graph is equal to the number of categories, and each leaf node contains the <template> tag.

Author:
Richard Wallace, Jon Baer, Thomas Ringate/Pedro Colla, Noel Bush, Eion Robb

Nested Class Summary
private static class Graphmaster.MatchState
          Match states.
 
Field Summary
private  java.lang.String aimlNamespaceURI
          The AIML namespace URI in use.
static java.lang.String ASTERISK
          The * wildcard.
protected  float averageNodemapperSize
          A running average of Nodemapper size.
static java.lang.String BOTID
          A botid marker.
protected  java.util.Map<java.net.URL,java.util.Set<Nodemapper>> botidNodes
          A map of KB URLs to <BOTID> nodes.
private  int categoryLoadNotifyInterval
          How frequently to provide a category load count.
protected  Core core
          The Core with which this Graphmaster is associated.
private  int duplicateCategories
          The total number of path-identical categories that have been encountered.
private static java.lang.String EMPTY_STRING
          An empty string.
static java.lang.String FILENAME
          A filename marker.
private  org.apache.log4j.Logger logger
          The logger.
private static java.lang.String MARKER_START
          The start of a marker.
private  org.apache.log4j.Logger matchLogger
          The match logger.
private  java.lang.String mergeAppendSeparator
          The separator string to use with the "append" merge policy.
private  CoreSettings.MergePolicy mergePolicy
          The merge policy.
protected  int nodemapperCount
          A count of Nodemappers.
protected  NodemapperFactory nodemapperFactory
          The factory that will be used to create Nodemappers.
private  boolean noteEachMerge
          Whether to note each merge.
static java.lang.String PATH_SEPARATOR
          A path separator.
protected  int responseTimeout
          The response timeout.
protected  Nodemapper root
          The root NodemapperFactory .
private static java.lang.String SPACE
          A space.
static java.lang.String TEMPLATE
          A template marker.
static java.lang.String THAT
          A that marker.
static java.lang.String TOPIC
          A topic marker.
private  int totalCategories
          The total number of categories read.
static java.lang.String UNDERSCORE
          The _ wildcard.
protected  java.util.Map<java.net.URL,java.util.Set<java.lang.String>> urlCatalog
          A map of loaded file URLs to botids.
 
Constructor Summary
Graphmaster(Core coreToUse)
          Creates a new Graphmaster, reading settings from the given Core.
 
Method Summary
private  Nodemapper add(java.util.ListIterator<java.lang.String> pathIterator, Nodemapper parent, java.net.URL source)
          Adds a new path to the Graphmaster at a given node.
 Nodemapper add(java.lang.String pattern, java.lang.String that, java.lang.String topic, java.lang.String botid, java.net.URL source)
          Adds a new pattern-that-topic path to the Graphmaster root.
 void addCategory(java.lang.String pattern, java.lang.String that, java.lang.String topic, java.lang.String template, java.lang.String botid, Bot bot, java.net.URL source)
          Adds a new category to the Graphmaster.
 void addForBot(java.net.URL path, java.lang.String botid)
          Adds the given botid to the <botid> node for all branches associated with the given URL.
 void addURL(java.net.URL path, java.lang.String botid)
          Adds the given URL to the catalog of URLs loaded for the given botid.
 java.lang.String appendTemplate(java.lang.String existingTemplate, java.lang.String newTemplate)
          Appends the contents of one template to another.
 java.lang.String combineTemplates(java.lang.String existingTemplate, java.lang.String newTemplate)
          Combines two template content strings into a single template, using a random element so that either original template content string has an equal chance of being processed.
 double getAverageNodemapperSize()
          Returns the average Nodemapper size.
 int getCategoryCount()
          Returns the number of categories presently loaded.
 java.lang.String getCategoryReport()
          Returns a string reporting the current number of total categories
 int getDuplicateCategoryCount()
          Returns the number of path-identical categories encountered.
 int getNodemapperCount()
          Returns the number of Nodemappers in the Graphmaster.
 java.util.Map<java.net.URL,java.util.Set<java.lang.String>> getURLCatalog()
          Returns an unmodifiable view of the url-to-botid catalog.
 boolean hasAlreadyLoaded(java.net.URL path)
          Returns whether or not the Graphmaster has already loaded the given URL.
 boolean hasAlreadyLoadedForBot(java.net.URL path, java.lang.String botid)
          Returns whether or not the Graphmaster has already loaded the given URL for the given botid.
private  Match match(Nodemapper nodemapper, Nodemapper parent, java.util.List<java.lang.String> input, java.lang.String wildcardContent, java.lang.StringBuilder path, Graphmaster.MatchState matchState, long expiration)
           Searches for a match in the Graphmaster to a given path.
 Match match(java.lang.String input, java.lang.String that, java.lang.String topic, java.lang.String botid)
           Searches for a match in the Graphmaster to a given path.
private  void remove(Nodemapper nodemapper)
          Removes a node, as well as many of its ancestors as have no descendants other than this node or its ancestors.
 void unload(java.net.URL path, Bot bot)
          Removes all nodes associated with a given filename, and removes the file from the list of loaded files.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TEMPLATE

public static final java.lang.String TEMPLATE
A template marker.

See Also:
Constant Field Values

THAT

public static final java.lang.String THAT
A that marker.

See Also:
Constant Field Values

TOPIC

public static final java.lang.String TOPIC
A topic marker.

See Also:
Constant Field Values

BOTID

public static final java.lang.String BOTID
A botid marker.

See Also:
Constant Field Values

FILENAME

public static final java.lang.String FILENAME
A filename marker.

See Also:
Constant Field Values

ASTERISK

public static final java.lang.String ASTERISK
The * wildcard.

See Also:
Constant Field Values

UNDERSCORE

public static final java.lang.String UNDERSCORE
The _ wildcard.

See Also:
Constant Field Values

PATH_SEPARATOR

public static final java.lang.String PATH_SEPARATOR
A path separator.

See Also:
Constant Field Values

EMPTY_STRING

private static final java.lang.String EMPTY_STRING
An empty string.

See Also:
Constant Field Values

MARKER_START

private static final java.lang.String MARKER_START
The start of a marker.

See Also:
Constant Field Values

SPACE

private static final java.lang.String SPACE
A space.

See Also:
Constant Field Values

core

protected Core core
The Core with which this Graphmaster is associated.


logger

private org.apache.log4j.Logger logger
The logger.


matchLogger

private org.apache.log4j.Logger matchLogger
The match logger.


nodemapperFactory

protected NodemapperFactory nodemapperFactory
The factory that will be used to create Nodemappers.


root

protected Nodemapper root
The root NodemapperFactory .


urlCatalog

protected java.util.Map<java.net.URL,java.util.Set<java.lang.String>> urlCatalog
A map of loaded file URLs to botids.


botidNodes

protected java.util.Map<java.net.URL,java.util.Set<Nodemapper>> botidNodes
A map of KB URLs to <BOTID> nodes.


mergePolicy

private CoreSettings.MergePolicy mergePolicy
The merge policy.


mergeAppendSeparator

private java.lang.String mergeAppendSeparator
The separator string to use with the "append" merge policy.


noteEachMerge

private boolean noteEachMerge
Whether to note each merge.


aimlNamespaceURI

private java.lang.String aimlNamespaceURI
The AIML namespace URI in use.


categoryLoadNotifyInterval

private int categoryLoadNotifyInterval
How frequently to provide a category load count.


totalCategories

private int totalCategories
The total number of categories read.


duplicateCategories

private int duplicateCategories
The total number of path-identical categories that have been encountered.


responseTimeout

protected int responseTimeout
The response timeout.


nodemapperCount

protected int nodemapperCount
A count of Nodemappers.


averageNodemapperSize

protected float averageNodemapperSize
A running average of Nodemapper size.

Constructor Detail

Graphmaster

public Graphmaster(Core coreToUse)
Creates a new Graphmaster, reading settings from the given Core.

Parameters:
coreToUse - the CoreSettings object from which to read settings
Method Detail

add

public Nodemapper add(java.lang.String pattern,
                      java.lang.String that,
                      java.lang.String topic,
                      java.lang.String botid,
                      java.net.URL source)
Adds a new pattern-that-topic path to the Graphmaster root.

Parameters:
pattern - <pattern/> path component
that - <that/> path component
topic - <topic/> path component
botid -
source - the source of this path
Returns:
Nodemapper which is the result of adding the path.

add

private Nodemapper add(java.util.ListIterator<java.lang.String> pathIterator,
                       Nodemapper parent,
                       java.net.URL source)
Adds a new path to the Graphmaster at a given node.

Parameters:
pathIterator - an iterator over the List containing the elements of the path
parent - the Nodemapper parent to which the child should be appended
source - the source of the original path
Returns:
Nodemapper which is the result of adding the node
Since:
4.1.3

remove

private void remove(Nodemapper nodemapper)
Removes a node, as well as many of its ancestors as have no descendants other than this node or its ancestors.

Parameters:
nodemapper - the mapper for the node to remove

match

public Match match(java.lang.String input,
                   java.lang.String that,
                   java.lang.String topic,
                   java.lang.String botid)
            throws NoMatchException

Searches for a match in the Graphmaster to a given path.

This is a high-level prototype, used for external access. It is not synchronized!

Parameters:
input - <input/> path component
that - <that/> path component
topic - <topic/> path component
botid - <botid/> path component
Returns:
the resulting Match object
Throws:
NoMatchException - if no match was found
See Also:
match(Nodemapper, Nodemapper, List, String, StringBuilder, MatchState, long)

match

private Match match(Nodemapper nodemapper,
                    Nodemapper parent,
                    java.util.List<java.lang.String> input,
                    java.lang.String wildcardContent,
                    java.lang.StringBuilder path,
                    Graphmaster.MatchState matchState,
                    long expiration)

Searches for a match in the Graphmaster to a given path.

This is a low-level prototype, used for internal recursion.

Parameters:
nodemapper - the nodemapper where we start matching
parent - the parent of the nodemapper where we start matching
input - the input path (possibly a sublist of the original)
wildcardContent - contents absorbed by a wildcard
path - the path matched so far
matchState - state variable tracking which part of the path we're in
expiration - when this response process expires
Returns:
the resulting Match object
See Also:
match(String, String, String, String)

addCategory

public void addCategory(java.lang.String pattern,
                        java.lang.String that,
                        java.lang.String topic,
                        java.lang.String template,
                        java.lang.String botid,
                        Bot bot,
                        java.net.URL source)
Adds a new category to the Graphmaster.

Parameters:
pattern - the category's pattern
that - the category's that
topic - the category's topic
template - the category's template
botid - the bot id for whom to add the category
bot - the bot for whom the category is being added
source - the path from which the category comes

unload

public void unload(java.net.URL path,
                   Bot bot)
Removes all nodes associated with a given filename, and removes the file from the list of loaded files.

Parameters:
path - the filename
bot - the bot for whom to remove the given path

combineTemplates

public java.lang.String combineTemplates(java.lang.String existingTemplate,
                                         java.lang.String newTemplate)
Combines two template content strings into a single template, using a random element so that either original template content string has an equal chance of being processed. The order in which the templates are supplied is important: the first one (existingTemplate) is processed as though it has already been stored in the Graphmaster, and hence might itself be the result of a previous combine() operation. If this is the case, the in-memory representation of the template will have a special attribute indicating this fact, which will be used to "balance" the combine operation.

Parameters:
existingTemplate - the template with which the new template should be combined
newTemplate - the template which should be combined with the existing template
Returns:
the combined result

appendTemplate

public java.lang.String appendTemplate(java.lang.String existingTemplate,
                                       java.lang.String newTemplate)
Appends the contents of one template to another.

Parameters:
existingTemplate - the template to which to append
newTemplate - the template whose content should be appended
Returns:
the combined result

getCategoryCount

public int getCategoryCount()
Returns the number of categories presently loaded.

Returns:
the number of categories presently loaded

getCategoryReport

public java.lang.String getCategoryReport()
Returns a string reporting the current number of total categories

Returns:
a string reporting category information

getDuplicateCategoryCount

public int getDuplicateCategoryCount()
Returns the number of path-identical categories encountered.

Returns:
the number of path-identical categories encountered

getNodemapperCount

public int getNodemapperCount()
Returns the number of Nodemappers in the Graphmaster.

Returns:
the number of Nodemapper in the Graphmaster.

getAverageNodemapperSize

public double getAverageNodemapperSize()
Returns the average Nodemapper size. Note that this method actually performs the count when called.

Returns:
the average Nodemapper size

hasAlreadyLoaded

public boolean hasAlreadyLoaded(java.net.URL path)
Returns whether or not the Graphmaster has already loaded the given URL.

Parameters:
path -
Returns:
whether or not the Graphmaster has already loaded the given URL

hasAlreadyLoadedForBot

public boolean hasAlreadyLoadedForBot(java.net.URL path,
                                      java.lang.String botid)
Returns whether or not the Graphmaster has already loaded the given URL for the given botid.


addURL

public void addURL(java.net.URL path,
                   java.lang.String botid)
Adds the given URL to the catalog of URLs loaded for the given botid. This should only be called using a URL that has not previously been loaded for another bot.

Parameters:
path -
botid -
Throws:
java.lang.IllegalArgumentException - if the given path has already been loaded

addForBot

public void addForBot(java.net.URL path,
                      java.lang.String botid)
Adds the given botid to the <botid> node for all branches associated with the given URL. This should only be called using a URL that has previously been loaded for another bot.

Parameters:
path -
botid -
Throws:
java.lang.IllegalArgumentException - if the given path has not already been loaded, or if it has been loaded for the same botid

getURLCatalog

public java.util.Map<java.net.URL,java.util.Set<java.lang.String>> getURLCatalog()
Returns an unmodifiable view of the url-to-botid catalog.

Returns:
an unmodifiable view of the url-to-botid catalog