All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class oaa.agentlib.AgentLib

oaa.agentlib.AgentLib

public class AgentLib
AgentLib implements an OAA 1.0 agent library in Java
For more general information about OAA, look at the Tutorial


Variable Index

 o client
Reference to the communication layer
 o connected
Status of the connection
 o copyright
Copyright information.
 o KSname
Knowledge source name
 o loop
To control the thread that performs the main oaa loop
 o rcsId
Version information.
 o received
Current incoming buffer
 o server
Reference to the communication layer (If working as server)
 o solveParams
Current parameters used to solve predicates

Constructor Index

 o AgentLib(AgentInterface, String)
Constructor.
 o AgentLib(AgentInterface, String, String)
Constructor.

Method Index

 o addRemoteTrigger(String, String, String, String, String, String)
Adds a trigger to the appropriate agent.
 o addTrigger(String, String, String, String)
Adds a trigger to the appropriate agent.
 o addTriggerToKs(String, String, String, String, String)
Adds a trigger to the specified agent.
 o args(String)
Extract the params from a term "head(params)"
 o argument(String, int)
Returns the nth argument in a term
 o argumentAsInt(String, int)
Returns the nth argument in a term as integer
 o delaySolution()
Defer solving a doEvent request until a later time.
 o deref(String, DynArray)
See if a variable can be resolved using existing variable bindings
 o disconnect()
Disconnects client from Facilitator.
 o doubleQuotes(String)
Doubles any "'" in the string, to prepare the string to be surrounded by "'".
 o extractEvent(String)
Parses an event (message from the facilitator) and returns KS,Event.
 o first(String)
Takes a list of terms (comma separated) and returns the first (car) one.
 o func(String)
Extract the head from a term "head(params)"
 o functor(String)
Splits a string containing a prolog-style term into a functor and arguments.
 o getEvent(boolean)
Reads an event from the TCP stream.
 o ifTraceOn(String, String)
Print a trace message if TraceOn is set.
 o isList(String)
Returns true if the term is an ICL list (in form [1,2,3])
 o isVar(String)
Returns true if the term is an ICL variable
 o listLen(String)
Returns the length of a comma separated list of ICL terms
 o listToTerms(String)
Converts an incoming ICL list [elt1, elt2, elt3] form) into a list of terms, on which we can use list_len, NthElt, etc.
 o nthElt(String, int)
Returns the Nth term in a list of terms
 o oaaLoop(boolean)
Heart of the application, provides the get_event-interpret loop for a java application.
 o oaaReady(boolean)
Let the Server know that setupCommunication is done any personal initializations.
 o paramValue(String, String)
Extracts the value from a parameter list
 o pollUntilEvent(String)
Waits until a matching event is returned from server
 o postEvent(String)
Sends an event (message) to the facilitator.
 o readBB(String, String)
Retrieves information from Facilitator's global data store.
 o readBB(String, String, boolean)
Retrieves information from Facilitator's global data store.
 o readBB(String, String, String)
Retrieves information from Facilitator's global data store.
 o readBB(String, String, String, boolean)
Retrieves information from Facilitator's global data store.
 o readSetupFile(String[])
Finds the host and port values for the facilitator, and possibly the name for the client agent, looking on the command line and/or in the setup file.
 o removeQuotes(String)
Removes leading and trailing ' and " marks
 o replaceBB(String, String, String)
Changes item value from Old to New.
 o rest(String)
Takes a list of terms (comma separated) and returns the rest (cdr) one.
 o retractBB(String, String)
Remove item from global data on blackboard.
 o returnDelayedSolutions(String, String)
Sends the solutions to a delayed event to the Facilitator, and removes the event from the delay table.
 o run()
Implements the run method of the thread (parent) class.
 o setupCommunication(String, String, int, String, boolean)
Establishes tcp connection to the facilitator.
 o solve(String, String)
Requests data or actions from distributed agents
 o startAsServer(String, int)
Starts an agent as a server.
 o term(String)
Takes a list of terms (comma separated) and returns the first (car) and rest (cdr) of the list.
 o undoubleQuotes(String)
Converts any '' marks inside a string to just '.
 o unify(String, String)
Perform true unification and return resulting term, or "" if failure
 o writeBB(String, String)
Add information to global data stored on blackboard server.
 o writeOnceBB(String, String)
Add information to global data stored on blackboard server at most one times (if exists, doesn't write).
 o writeReplaceBB(String, String)
Add information to global data stored on blackboard server, replacing old information with new information.

Variables

 o copyright
 public static final String copyright
Copyright information.

 o rcsId
 public static final String rcsId
Version information.

 o client
 public CommLib client
Reference to the communication layer

 o server
 public CommServer server
Reference to the communication layer (If working as server)

 o received
 public String received
Current incoming buffer

 o loop
 public boolean loop
To control the thread that performs the main oaa loop

 o KSname
 public String KSname
Knowledge source name

 o solveParams
 public String solveParams
Current parameters used to solve predicates

 o connected
 public static boolean connected
Status of the connection

Constructors

 o AgentLib
 public AgentLib(AgentInterface a,
                 String inKSName)
Constructor.

Parameters:
AgentInterface - The interface between the agentlib and the agent
inKSName - Agent name
Remarks:
"KS" in the second argument stands for Knowledge Source, which
historically is synonymous with Agent
See Also:
AgentInterface
 o AgentLib
 public AgentLib(AgentInterface a,
                 String m1,
                 String m2)
Constructor.

Parameters:
AgentInterface - The interface between the agentlib and the agent
m1 - Agent name
m2 - Agent knowledge source name
Remarks:
This is obsolete; has been retained for backwards compatibility.
New code should use the 2-argument version instead.
See Also:
AgentInterface

Methods

 o run
 public void run()
Implements the run method of the thread (parent) class.

 o readSetupFile
 public static String[] readSetupFile(String args[])
Finds the host and port values for the facilitator, and possibly the name for the client agent, looking on the command line and/or in the setup file.

Parameters:
args - Command line arguments. May contain :
-oaa_port PORT_NUMBER : Facilitator's port number
-oaa_host HOST_NAME : Facalitator's host
-oaa_name NAME : Agent name
-oaa : Ask for an automatic connection to the facilitator
Returns:
- String[0] : Port for the server process
- String[1] : Host for the server process
- String[2] : Name for agent provided on command line
- String[3] : Boolean: should autoconnect? Yes if -oaa or -oaa_host given on command line.
Remarks:
The function first looks on the command line (args). If it fails to
find BOTH host and port there, then it tries to find the information
in a setup.pl file (looking first in the current directory, then in the
root directory). If an agent name is found on the command line,
that is also returned.
 o setupCommunication
 public boolean setupCommunication(String KS,
                                   String Host,
                                   int Port,
                                   String Solvable,
                                   boolean ServerQP32)
Establishes tcp connection to the facilitator.

Parameters:
KS: - Name for the Agent
Host: - host to connect to
Port: - port to connect to
Solvable: - ICL-formatted list of solvables provided by the Agent
ServerQP32: - Not used currently -- always pass in True
Returns:
True if the connection to the facilitator succeeded, False otherwise
Remarks:
"KS" in the first argument stands for Knowledge Source, which
historically is synonymous with Agent.
 o disconnect
 public void disconnect()
Disconnects client from Facilitator.

 o startAsServer
 public void startAsServer(String host,
                           int port)
Starts an agent as a server.

Parameters:
host: - host number
port: - port number
Remarks:
This method is normally called only by a facilitator, not by a client
agent.
 o postEvent
 public void postEvent(String Event)
Sends an event (message) to the facilitator.

Parameters:
Event: - event to be posted
 o extractEvent
 public String[] extractEvent(String one_event)
Parses an event (message from the facilitator) and returns KS,Event.

Parameters:
one_event: - event to be parsed
Returns:
- String[0] : KS
- String[1] : Event
- null if an error occurs (The event is not correctly wrapped)
Remarks:
If the event is in form "term(Result)." then String[0] = null, String[1] = Event
If the event is in form "term(KS,Result)." then String[0] = KS, String[1] = Event
 o writeBB
 public void writeBB(String Item,
                     String Data)
Add information to global data stored on blackboard server.

Parameters:
Item: - type of data being written
Data: - value for the data type
Remarks:
ex: write_bb(name, "Adam Cheyer") adds name("Adam Cheyer") on blackboard
 o writeReplaceBB
 public void writeReplaceBB(String Item,
                            String Data)
Add information to global data stored on blackboard server, replacing old information with new information.

Parameters:
Item: - type of data being written
Data: - value for the data type
 o writeOnceBB
 public void writeOnceBB(String Item,
                         String Data)
Add information to global data stored on blackboard server at most one times (if exists, doesn't write).

Parameters:
Item: - type of data being written
Data: - value for the data type
 o retractBB
 public void retractBB(String Item,
                       String Data)
Remove item from global data on blackboard.

Parameters:
Item: - type of data being written
Data: - value for the data type
Remarks:
If Data points to a var, it will remove any value of type "item"
 o replaceBB
 public void replaceBB(String Item,
                       String OldData,
                       String NewData)
Changes item value from Old to New.

Parameters:
Item: - type of data being written
Data: - value for the data type
NewData: - newdata: new value of data
 o readBB
 public String readBB(String Item,
                      String Data,
                      boolean Async)
Retrieves information from Facilitator's global data store.

Parameters:
String - item: type of data being written
String - data: value for the data type, variable if requesting values
boolean - Async: Whether should return results asynchronously
String - answers: list containing results.
Remarks:
If Async is True, returns immediately with the empty list ("[]").
In this case, the doEvent method will be called with the results
when they arrive.
 o readBB
 public String readBB(String Item,
                      String Data)
Retrieves information from Facilitator's global data store.

Parameters:
String - item: type of data being written
String - data: value for the data type, variable if requesting values
Remarks:
Returns results synchronously
See Also:
readBB
 o readBB
 public String readBB(String KS,
                      String Item,
                      String Data,
                      boolean Async)
Retrieves information from Facilitator's global data store.

Parameters:
String - KS: (numeric) id of Agent having "ownership" of the data
String - item: type of data being written
String - data: value for the data type, variable if requesting values
String - answers: list containing results.
Remarks:
Allows to specify a KS
See Also:
readBB
 o readBB
 public String readBB(String KS,
                      String Item,
                      String Data)
Retrieves information from Facilitator's global data store.

Parameters:
String - KS: id of KS (agent) to read data about
String - item: type of data being written
String - data: value for the data type, variable if requesting values
String - answers: list containing results.
Remarks:
Allows to specify a KS
Returns results synchronously
See Also:
readBB
 o addTrigger
 public void addTrigger(String Kind,
                        String Type,
                        String Condition,
                        String Action)
Adds a trigger to the appropriate agent.

Parameters:
String - Kind: may be 'test', 'data', or 'alarm'
String - Type: may be 'when' or 'if' (single-use), or 'whenever' (permanent)
String - Condition: test condition
String - Action: action to be performed if Condition succeeds
Remarks:
add_trigger/4 does not require a KS to be specified, because the KS
will be determined automatically:
-data: always installed on Server
-test: installed on appropriate agent by solvable
-alarm: installed on alarm agent
For 'event' triggers, use add_trigger_on_ks()

addRemoteTrigger provides a more general functionality for creating
a trigger
 o addTriggerToKs
 public void addTriggerToKs(String KS,
                            String Kind,
                            String Type,
                            String Condition,
                            String Action)
Adds a trigger to the specified agent.

Parameters:
String - KS: KS on which the trigger should be installed
String - Kind: may be 'test', 'event', or 'alarm' (not 'data'!)
String - Type: may be 'when', 'if' (single), 'whenever' (permanent)
String - Condition: test condition
String - Action: action to be performed if Condition succeeds
Remarks:
Cannot be used to install a data trigger.

addRemoteTrigger provides a more general functionality for creating
a trigger.
 o addRemoteTrigger
 public void addRemoteTrigger(String Kind,
                              String Type,
                              String OpMask,
                              String Template,
                              String Condition,
                              String Action)
Adds a trigger to the appropriate agent.

Parameters:
String - Kind: may be 'test', 'data', or 'alarm'
String - Type: may be 'when', 'if' (single), 'whenever' (permanent)
String - OpMask: on_write, on_replace, on_retract, on_write_replace, or a list combining several of these
String - Template: template against which to match the trigger (e.g. data, or test)
String - Condition: test condition
String - Action: action to be performed if Template is matched and Condition succeeds
Remarks:
add_trigger/6 does not require a KS to be specified, because the KS
will be determined automatically:
-data: always installed on Server
-test: installed on appropriate agent by solvable
-alarm: installed on alarm agent
For 'event' triggers, use add_trigger_on_ks()
 o ifTraceOn
 public void ifTraceOn(String msg,
                       String arg)
Print a trace message if TraceOn is set.

Parameters:
msg: - message to be printed
arg: - arguments to be printed
 o delaySolution
 public String delaySolution()
Defer solving a doEvent request until a later time. This procedure should be called in doEvent() when a programmer wants to return from doEvent() without returning an immediate solution. The return value should be returned from doEvent and should also be saved in a variable, which should be passed to returnDelayedSolutions(Var,Solutions) when ready.

Returns:
Returns an appropriate result such that answers can be deferred until a later time.
Implementation:
Answers will contain "DELAY_PRED(i)": since usually answers must contain a list, the Interpret function will know that this is a special case, and the event to be solved can be added to the delay_table at index i.
Remarks:
Returns null if it is not possible to delay the solution (table full).
 o returnDelayedSolutions
 public void returnDelayedSolutions(String var,
                                    String answers)
Sends the solutions to a delayed event to the Facilitator, and removes the event from the delay table.

Parameters:
var: - the value previously returned by function delaySolution()
answers: - the solution list for the delayed event
Remarks:
If answer=NULL, an empty solution list ([]) is sent to the Facilitator.
 o func
 public static String func(String inTerm)
Extract the head from a term "head(params)"

Parameters:
String - predicate: In form "head(arg1, arg2, ...)"
Returns:
A string containing the functor of a prolog-style term.
 o args
 public static String args(String inTerm)
Extract the params from a term "head(params)"

Parameters:
String - predicate: In form "head(arg1, arg2, ...)"
Returns:
A string containing the arguments of a prolog-style term.
Remarks:
If no arguments exist, returns an empty string.
 o functor
 public static String[] functor(String predicate)
Splits a string containing a prolog-style term into a functor and arguments.

Parameters:
predicate: - In form "a(arg1, arg2, ...)"
Returns:
outputs[] : array of strings
- outputs[0] contains "a"
- outputs[1] contains "arg1, arg2, ..."
Remarks:
If no arguments exist, the empty string is returned in outputs[].
 o term
 public static String[] term(String terms)
Takes a list of terms (comma separated) and returns the first (car) and rest (cdr) of the list.

Parameters:
terms: - a comma-separated list of ICL (prolog-style) terms.
Returns:
String outputs[] : array of strings
- outputs[0] : the first term in the list
- outputs[1] : the rest of the terms
Remarks:
This method, and others calling for a "comma-separated" list,
should NOT be passed an ICL list; that is, the list should not
begin and end with square brackets. Before calling this method,
square brackets should be removed by a call to listToTerms.

Terms may be nested arbitrarily deep; e.g., terms could be
"a(b(c(d,1))), X".
 o first
 public static String first(String inTerm)
Takes a list of terms (comma separated) and returns the first (car) one.

Parameters:
inTerms: - a comma-separated list of ICL terms
Returns:
The first term in the list
 o rest
 public static String rest(String inTerm)
Takes a list of terms (comma separated) and returns the rest (cdr) one.

Parameters:
inTerms: - a list of ICL terms
Returns:
The rest of the list
 o nthElt
 public static String nthElt(String list,
                             int n)
Returns the Nth term in a list of terms

Parameters:
list: - a string containing a comma-separated list of terms
n: - index into a list (nth term)
Returns:
String elt: the nth term in the list.
Remarks:
Returns null if index is longer than number of elements
The first element is at index 1 (not 0)
 o argument
 public static String argument(String predicate,
                               int n)
Returns the nth argument in a term

Parameters:
predicate: - a prolog-style term: eg func(a1,a2,...)
n: - index into a list (nth term)
Returns:
String argument: the nth argument in the term. eg (a2 if n = 2)
Remarks:
Returns null if index is longer than number of elements
The first element is at index 1 (not 0)
 o argumentAsInt
 public static int argumentAsInt(String predicate,
                                 int n)
Returns the nth argument in a term as integer

Parameters:
predicate: - a prolog-style term: eg func(a1,a2,...)
n: - index into a list (nth term)
Returns:
The nth argument in the term as an integer
Remarks:
Returns null if index is longer than number of elements
The first element is at index 1 (not 0)
 o isList
 public static boolean isList(String s)
Returns true if the term is an ICL list (in form [1,2,3])

Parameters:
s: - a string containing an ICL term
Returns:
True if the term is a list
 o isVar
 public static boolean isVar(String t)
Returns true if the term is an ICL variable

Parameters:
String - term: a string containing an ICL term (or variable)
Returns:
True if the term is a variable
 o paramValue
 public static String paramValue(String aList,
                                 String param)
Extracts the value from a parameter list

Parameters:
aList: - a parameter list, in the form "[attr1(Value1), attr2(Value2)]"
param: - parameter to extract (e.g. attr1)
Returns:
Value of parameter or null if not found
 o doubleQuotes
 public static String doubleQuotes(String s)
Doubles any "'" in the string, to prepare the string to be surrounded by "'".

Parameters:
s: - a string containing ICL terms
Returns:
The string with any "'" --> "''"
 o undoubleQuotes
 public static String undoubleQuotes(String s)
Converts any '' marks inside a string to just '. Single quotes inside strings will be doubled when coming from ICL

Parameters:
s: - a string containing ICL terms
Returns:
The string with all '' --> '
 o removeQuotes
 public static String removeQuotes(String s)
Removes leading and trailing ' and " marks

Parameters:
s: - a string containing ICL terms
Returns:
The string minus leading and trailing quotes
 o listLen
 public static int listLen(String list)
Returns the length of a comma separated list of ICL terms

Parameters:
list: - An comma-separated list of ICL terms
Returns:
The length of the list
 o listToTerms
 public static String listToTerms(String s)
Converts an incoming ICL list [elt1, elt2, elt3] form) into a list of terms, on which we can use list_len, NthElt, etc. i.e. a comma-separated list "elt1, elt2, etl3". Basically, just removes the brackets...

Parameters:
s: - a list in ICL syntax
Returns:
The inner incoming list. (Removes brackets)
 o deref
 public String deref(String var,
                     DynArray var_bindings)
See if a variable can be resolved using existing variable bindings

Parameters:
var: - will return value if one can be found in var_bindings
var_bindings: - A list stored with var/value in l[N]/l[N+1]
 o unify
 public String unify(String term1,
                     String term2)
Perform true unification and return resulting term, or "" if failure

Parameters:
term1: - first term
term2: - second term
Returns:
If the two terms unify, returns the unified term. Otherwise returns "", representing failure
 o pollUntilEvent
 public String pollUntilEvent(String wait_event)
Waits until a matching event is returned from server

Parameters:
wait_event: - event template we are waiting for
Returns:
The specific event returned
 o solve
 public String solve(String Goal,
                     String Params)
Requests data or actions from distributed agents

Parameters:
goal: - an ICL goal to be solve/performed
params: - control parameters describing how the goal should be sent/resolved (see below).
Returns:
A list containing solutions to the requested goal. The answer value "[]" constitutes failure of the goal.
OAA parameters:
The params argument is a list which may contain:
cache : cache all solutions locally
level_limit(N) : highest number of levels to climb for solutions
address(KS) : ask a specific KS to solve goal
and_parallel : and-parallel solve of Goal list
or_parallel : or-parallel solve of Goal list
test(Test) : only solve goal on blackboards where Test succeeds locally
broadcast : just disseminate message, do not request results
asynchronous : solve will not block until solution returns, the solutions will be returned asynchronously in solved(FromKs,Goal,Params,Solutions) messages which should be handled by in doEvent()
solution_limit(N): limits the number of solutions found. Currently only works for 1...to N
time_limit(N) : Waits a maximum of N seconds before returning (failure if no solution)
 o getEvent
 public String getEvent(boolean IsServer)
Reads an event from the TCP stream.

Returns:
The event read.
Remarks:
Called by CommLib.java each time something is received
 o oaaReady
 public void oaaReady(boolean print_ready)
Let the Server know that setupCommunication is done any personal initializations. Should be called by the agent just before entering the MainLoop().

 o oaaLoop
 public void oaaLoop(boolean print_ready)
Heart of the application, provides the get_event-interpret loop for a java application.


All Packages  Class Hierarchy  This Package  Previous  Next  Index