Algernon Software

org.algernon.aam
Class Processor

java.lang.Object
  extended byorg.algernon.aam.Processor

public class Processor
extends Object

This class represents a Processor in the AAM. It contains the registers, memory and code.


Field Summary
protected  Algernon f_algy
           
protected  Jatha f_lisp
           
 LispValue FAILURE
           
 LispValue KNOWN
           
protected  boolean m_isProfiling
           
protected  LispValue m_lastCommand
           
protected  int m_oldTraceLevel
           
protected  OpcodeSet m_opcodes
           
protected  Profiler m_profiler
           
protected  QueryHistory m_queryHistory
           
protected  RegisterSet m_registers
           
protected  int m_traceLevel
           
protected  Tracer m_tracer
           
 LispValue NOTHING
           
 LispValue SUCCESS
           
 
Constructor Summary
Processor(Algernon algy)
          Create a new AAM Processor.
 
Method Summary
 void activateClosures(Clause clause, AlgernonKBSet kbSet, int direction)
          Activates rule closures from assertions or queries.
 void addClosure(Closure closure)
          Adds a closure to the system.
 void addRule(Rule rule, AlgernonFrame associatedFrame)
          Adds a new rule.
 void addToActionQueue(Activation activation)
          Forwards to Algernon.
 LispValue appendRegisterValue(LispValue registerName, LispValue newValue)
          Pushes the given value onto the end of the register.
 void clearHistoricalQueries()
          Clears all of the query history.
 void clearHistoricalQueries(AlgernonFrame frame)
          Clears the query history for the given frame.
 void clearHistoricalQueries(LispValue frame, LispValue rel)
          Clears the query history for the given frame and relation.
 void clearHistoricalQuery(Clause clause)
          Clears one query from the history.
 Collection compileRule(Rule rule, AlgernonKBSet kbSet)
          Compiles the given rule and returns it.
 Processor copy()
          Returns a new processor that contains the same global variable bindings as this one.
 AlgernonInstance createInstance(LispValue name, AlgernonClass superclass)
          Forwards to the current KBSet.
 AlgernonRelation createRelation(AlgernonKB kb, String slotName, Set domains, Set ranges, Integer minCardinality, Integer maxCardinality, Object defaultValue, AlgernonRelation inverseRelation)
          Creates a new relation in the given KB.
 void createRule(Rule rule, AlgernonKBSet kbSet, AlgernonFrame frame)
          Creates a new rule - adds it to the RuleManager, compiles it, creates continuations and compiles them.
 Object evaluateExpression(String language, LispValue expression)
          Evaluates a method in the given language.
 Boolean evaluateExpressionAsBoolean(String language, LispValue expression)
          Evaluates a method in the given language.
 Collection getActivatedRules(AlgernonFrame frame, int direction, int type)
          Returns a list of activatable rules that match the given criteria.
 Algernon getAlgernon()
          Returns the value of Algernon.
 AlgernonCompiler getCompiler()
          Returns the current compiler used by Algernon.
 LispValue getLastCommand()
          Returns the value of LastCommand.
 Jatha getLisp()
           
 int getNumberOfStepsForward(int currentStep)
          When in (:TRACE :STEP) mode, this method reads in an integer number of steps to step forward.
 OpcodeSet getOpcodeSet()
           
 LispValue getRegisterEntry(LispValue registerName, int index)
          Return the nth entry in the given register, where the topmost item is entry number zero.
 RegisterSet getRegisters()
          Returns the RegisterSet used by this processor.
 long getRegisterSize(LispValue registerName)
          Return the current size of the given register.
 LispValue getRegisterValue(LispValue registerName)
          Return the value of the given register.
 Rule getRule(String name)
          Retrieves a rule by name.
 boolean getStepMode()
          Returns true if the tracer is in Step mode.
 int getTraceLevel()
          Returns the value of trace level.
 LispValue getTraceLevelAsString()
          Returns the current trace level as a symbol such as :NORMAL, :SILENT :VERBOSE, :DETAIL, :DEBUG or :ALWAYS.
 boolean isHistoricalQuery(Clause clause)
          Returns true if the clause or a generalization of it has been made before.
 boolean isInstance(LispValue clause, LispValue pattern)
          Determines whether clause is an instance of pattern, returning true or false.
 void nextClause()
          Increments the index of the clause we are examining.
 LispValue popRegisterValue(LispValue registerName)
          Pops the register and returns the popped entry.
 LispValue process(LispValue code, AlgernonKB kb, ErrorSet errors)
          Process the given code.
 LispValue process(LispValue code, AlgernonKBSet kbSet, BindingSet bindings, Rule rule, Process parent, ErrorSet errors)
          Process the given code.
 void profile(String name)
           
 LispValue pushRegisterValue(LispValue registerName, LispValue newValue)
          Pushes the given value onto the register.
 boolean relationIsFull(AlgernonFrame frame, AlgernonRelation relation)
          Returns true if the frame/relation combination is full - that is, the number of values stored in the slot is equal to the slot's maximum cardinality.
 void resetProfiler()
          Resets the statistics in the code profiler.
 void setAlgernon(Algernon newAlgernon)
          Sets the value of Algernon.
 void setLastCommand(LispValue newLastCommand)
          Sets the value of LastCommand.
private  void setOpcodeSet(OpcodeSet newSet)
           
 LispValue setRegisterValue(LispValue registerName, LispValue newValue)
          Sets the value of the given register.
 void setStepMode(boolean newState)
          Turns Step mode on or off.
 void setTraceLevel(int newTraceLevel)
          Sets the value of trace level
 void setTraceLevel(LispValue newTraceLevelString)
          Sets the value of trace level from a string such as ":NORMAL".
 void trace(int minLevel, String message)
          Prints or logs the message if the current system trace level is greater than or equal to the given trace level.
 void trace(int minLevel, String message, boolean newline)
          Prints or logs the message if the current system trace level is greater than or equal to the given trace level.
 Closure tryToInstantiate(Closure closure, Clause clause)
          Returns a closure if the rule can be instantiated from the clause.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FAILURE

public LispValue FAILURE

KNOWN

public LispValue KNOWN

NOTHING

public LispValue NOTHING

SUCCESS

public LispValue SUCCESS

m_registers

protected RegisterSet m_registers

m_opcodes

protected OpcodeSet m_opcodes

m_lastCommand

protected LispValue m_lastCommand

f_algy

protected Algernon f_algy

f_lisp

protected Jatha f_lisp

m_isProfiling

protected boolean m_isProfiling

m_traceLevel

protected int m_traceLevel

m_oldTraceLevel

protected int m_oldTraceLevel

m_tracer

protected Tracer m_tracer

m_profiler

protected Profiler m_profiler

m_queryHistory

protected QueryHistory m_queryHistory
Constructor Detail

Processor

public Processor(Algernon algy)
Create a new AAM Processor.

Method Detail

getAlgernon

public Algernon getAlgernon()
Returns the value of Algernon.


getCompiler

public AlgernonCompiler getCompiler()
Returns the current compiler used by Algernon.


setAlgernon

public void setAlgernon(Algernon newAlgernon)
Sets the value of Algernon.


process

public LispValue process(LispValue code,
                         AlgernonKB kb,
                         ErrorSet errors)
                  throws AlgernonException
Process the given code. Returns the final value in the RESULTS register, if no errors.

Parameters:
code - The AAM code to be executed.
kb - The KB to be used in the query.
Returns:
either NIL (indicating error), or a Result
Throws:
AlgernonException
See Also:
Result

process

public LispValue process(LispValue code,
                         AlgernonKBSet kbSet,
                         BindingSet bindings,
                         Rule rule,
                         Process parent,
                         ErrorSet errors)
                  throws AlgernonException
Process the given code. Returns the final value in the RESULTS register, if no errors.

Parameters:
code - The AAM code to be executed.
kbSet - KBSet to use when processing the code.
bindings - An existing BindingSet, if any.
rule - The rule being executed (for trace purposes).
parent - The parent process if this invocation is a subroutine.
errors - Error set to place errors in during processing.
Throws:
AlgernonException

addRule

public void addRule(Rule rule,
                    AlgernonFrame associatedFrame)
Adds a new rule. Forwards to Algernon.


addClosure

public void addClosure(Closure closure)
Adds a closure to the system. A closure is a rule plus bindings.


activateClosures

public void activateClosures(Clause clause,
                             AlgernonKBSet kbSet,
                             int direction)
                      throws AlgernonDataException
Activates rule closures from assertions or queries. Use RuleManager.FORWARD_CHAIN or RuleManager.BACKWARD_CHAIN.

Throws:
AlgernonDataException

tryToInstantiate

public Closure tryToInstantiate(Closure closure,
                                Clause clause)
Returns a closure if the rule can be instantiated from the clause. Otherwise, returns null.


compileRule

public Collection compileRule(Rule rule,
                              AlgernonKBSet kbSet)
Compiles the given rule and returns it.


getActivatedRules

public Collection getActivatedRules(AlgernonFrame frame,
                                    int direction,
                                    int type)
Returns a list of activatable rules that match the given criteria. Forwards to Algernon.

See Also:
Algernon

getRule

public Rule getRule(String name)
Retrieves a rule by name.


createRule

public void createRule(Rule rule,
                       AlgernonKBSet kbSet,
                       AlgernonFrame frame)
Creates a new rule - adds it to the RuleManager, compiles it, creates continuations and compiles them.


createInstance

public AlgernonInstance createInstance(LispValue name,
                                       AlgernonClass superclass)
                                throws AlgernonRuntimeException
Forwards to the current KBSet. Creates a new instance and returns it.

Throws:
AlgernonRuntimeException

createRelation

public AlgernonRelation createRelation(AlgernonKB kb,
                                       String slotName,
                                       Set domains,
                                       Set ranges,
                                       Integer minCardinality,
                                       Integer maxCardinality,
                                       Object defaultValue,
                                       AlgernonRelation inverseRelation)
                                throws AlgernonDataException
Creates a new relation in the given KB. Forwards the call to the Algernon class.

Throws:
AlgernonDataException

evaluateExpression

public Object evaluateExpression(String language,
                                 LispValue expression)
Evaluates a method in the given language. The language should be a string like "JAVA". The only language supported right now is JAVA. Forwards to Algernon.


evaluateExpressionAsBoolean

public Boolean evaluateExpressionAsBoolean(String language,
                                           LispValue expression)
Evaluates a method in the given language. The language should be a string like "JAVA". The only language supported right now is JAVA. Forwards to Algernon.


nextClause

public void nextClause()
Increments the index of the clause we are examining.


getRegisterValue

public LispValue getRegisterValue(LispValue registerName)
Return the value of the given register.


setRegisterValue

public LispValue setRegisterValue(LispValue registerName,
                                  LispValue newValue)
                           throws AlgernonDataException
Sets the value of the given register. Returns the value.

Throws:
AlgernonDataException

getRegisterEntry

public LispValue getRegisterEntry(LispValue registerName,
                                  int index)
Return the nth entry in the given register, where the topmost item is entry number zero.


getRegisterSize

public long getRegisterSize(LispValue registerName)
Return the current size of the given register.


popRegisterValue

public LispValue popRegisterValue(LispValue registerName)
Pops the register and returns the popped entry.


pushRegisterValue

public LispValue pushRegisterValue(LispValue registerName,
                                   LispValue newValue)
Pushes the given value onto the register. Returns the new value of the register.


appendRegisterValue

public LispValue appendRegisterValue(LispValue registerName,
                                     LispValue newValue)
Pushes the given value onto the end of the register. Returns the new value of the register.


getRegisters

public RegisterSet getRegisters()
Returns the RegisterSet used by this processor.


trace

public void trace(int minLevel,
                  String message)
Prints or logs the message if the current system trace level is greater than or equal to the given trace level.


trace

public void trace(int minLevel,
                  String message,
                  boolean newline)
Prints or logs the message if the current system trace level is greater than or equal to the given trace level.


addToActionQueue

public void addToActionQueue(Activation activation)
Forwards to Algernon. Adds the Activation to the activation queue, given the current rules for ordering the queue.


relationIsFull

public boolean relationIsFull(AlgernonFrame frame,
                              AlgernonRelation relation)
                       throws AlgernonException
Returns true if the frame/relation combination is full - that is, the number of values stored in the slot is equal to the slot's maximum cardinality.

Throws:
AlgernonException

resetProfiler

public void resetProfiler()
Resets the statistics in the code profiler.


profile

public void profile(String name)

getLastCommand

public LispValue getLastCommand()
Returns the value of LastCommand.


setLastCommand

public void setLastCommand(LispValue newLastCommand)
Sets the value of LastCommand.


getTraceLevel

public int getTraceLevel()
Returns the value of trace level.


getTraceLevelAsString

public LispValue getTraceLevelAsString()
Returns the current trace level as a symbol such as :NORMAL, :SILENT :VERBOSE, :DETAIL, :DEBUG or :ALWAYS.


setTraceLevel

public void setTraceLevel(int newTraceLevel)
Sets the value of trace level


setTraceLevel

public void setTraceLevel(LispValue newTraceLevelString)
Sets the value of trace level from a string such as ":NORMAL".


isHistoricalQuery

public boolean isHistoricalQuery(Clause clause)
Returns true if the clause or a generalization of it has been made before. If so, there is no need to activate backward-chaining rules. As a side effect, this also adds the clause to the query history.


clearHistoricalQueries

public void clearHistoricalQueries(LispValue frame,
                                   LispValue rel)
Clears the query history for the given frame and relation. This is done when the values of a slot are removed.

Parameters:
frame - An AlgernonFrame.
rel - An AlgernonRelation.

clearHistoricalQueries

public void clearHistoricalQueries()
Clears all of the query history. This is done when a new rule is added (although it doesn't have to perform such radical surgery...)


clearHistoricalQueries

public void clearHistoricalQueries(AlgernonFrame frame)
Clears the query history for the given frame. This is done when a frame is deleted.

Parameters:
frame - An AlgernonFrame.

clearHistoricalQuery

public void clearHistoricalQuery(Clause clause)
Clears one query from the history.

Parameters:
clause -

isInstance

public boolean isInstance(LispValue clause,
                          LispValue pattern)
Determines whether clause is an instance of pattern, returning true or false. This function is similar to BindingSet.unify except that variables in clause are treated as constants. Forwards to Algernon.


getLisp

public Jatha getLisp()

getOpcodeSet

public OpcodeSet getOpcodeSet()

copy

public Processor copy()
Returns a new processor that contains the same global variable bindings as this one.


setOpcodeSet

private void setOpcodeSet(OpcodeSet newSet)

getNumberOfStepsForward

public int getNumberOfStepsForward(int currentStep)
When in (:TRACE :STEP) mode, this method reads in an integer number of steps to step forward. If the user types GO or RUN (lower case or upper case) step mode is turned off. If the user types any positive number, the system executes that many opcodes before asking the user for more input. If the user types zero or a negative number, the system steps forward one opcode. Returns the number of steps to step forward.


getStepMode

public boolean getStepMode()
Returns true if the tracer is in Step mode.
   (:TRACE :STEP) turns step mode on.
   (:TRACE :RUN)  turns it off.
 


setStepMode

public void setStepMode(boolean newState)
Turns Step mode on or off.
   (:TRACE :STEP) turns step mode on.
   (:TRACE :RUN)  turns it off.
 


Algernon Software