Class JESSCommunicator

java.lang.Object
  |
  +--JESSCommunicator

public class JESSCommunicator
extends java.lang.Object

The JESSCommunicator is a shell around the JESS engine. All JESS rules are created in this class. This is also the class that is called from the JESS engine for the feedback of the conclusions of the rules. These conclusion are then passed to the SituationRecognizer. The methods that are called from the engine are setSituationState(java.lang.String, java.lang.String) and addPerformedAction(java.lang.String).


Field Summary
protected  jess.Rete engine_
          The inference engine of Jess.
protected  SituationRecognizer situationRecognizer
          The situation recognizer.
protected  java.util.HashMap variableMap
          The variables with the values that have been stored in the JESS engine.
 
Constructor Summary
JESSCommunicator(SituationRecognizer sitRec)
          This constructor initiates the engine, resets the factbase and adds itself as an instance of an object to the engine, so that the engine can call the methods of this class.
 
Method Summary
protected  void addClassTemplate(java.lang.String className)
          The method addClassTemplate adds a template of the given class to the factbase.
protected  void addObjectFact(java.lang.Object obj)
          The method addObjectFact adds a shadow fact of the given object to the factbase.
 void addPerformedAction(java.lang.String situationName)
          This method is called from the JESS engine when one of the action rules fire.
 void addSituationRules(java.util.HashMap situations)
          This method is called by the situationRecognizer after the xml file has been parsed.
 void bindVariable(java.lang.String variableName, java.lang.String variableValue)
          This method is called from the VariableReader to add a fact to the knowledge base that states the value of one of the flight sim variables.
protected  java.lang.String convertValueToRule(java.lang.String variableName, java.lang.String val, boolean trueIfNotPresent)
          This method creates a rule that checks the value of the given variable.
protected  java.lang.String formatRule(java.lang.String variableName, java.lang.String condition, boolean trueIfNotPresent)
          This method returns a condition formatted like a rule in JESS.
protected  java.lang.String getActionName(java.lang.String situationName, int index)
          Create a unique name for the action variable.
 java.lang.Object[][] getFacts()
          The method showFacts reads the facts from Jess and returns them.
 java.util.Vector getGlobals()
          Returns the names of the global variables in the engine and their values.
 java.lang.Object[][] getRules()
          The method showRules reads the rules from Jess and returns them.
 void reset()
          The method reset resets the rulebase, but leaves the values of the global variables intact.
 void resetEngine()
          The method resetEngine resets the rulebase including the global variables.
 void resetSituation(Situation situation)
          This method is called when the end of a situation is detected.
 void runEngine()
          The method runEngine evaluates the rules in the rulebase.
 void setSituationState(java.lang.String situationName, java.lang.String state)
          This method is called from the JESS engine when one of the start or end conditions fire.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

engine_

protected jess.Rete engine_
The inference engine of Jess.


situationRecognizer

protected SituationRecognizer situationRecognizer
The situation recognizer.


variableMap

protected java.util.HashMap variableMap
The variables with the values that have been stored in the JESS engine. These must be stored here, because they must be retracted from the engine in the next inference loop.

Constructor Detail

JESSCommunicator

public JESSCommunicator(SituationRecognizer sitRec)
This constructor initiates the engine, resets the factbase and adds itself as an instance of an object to the engine, so that the engine can call the methods of this class.

Parameters:
sitRec - The situation recognizer.
Method Detail

runEngine

public void runEngine()
The method runEngine evaluates the rules in the rulebase.


resetEngine

public void resetEngine()
The method resetEngine resets the rulebase including the global variables. This method is called when the recognition process is started all over again. For a soft reset where only the facts are removed from the engine use reset.


reset

public void reset()
The method reset resets the rulebase, but leaves the values of the global variables intact. This means that only the facts are removed from the rulebase.


addSituationRules

public void addSituationRules(java.util.HashMap situations)
This method is called by the situationRecognizer after the xml file has been parsed. It is this method's responsibility to convert all information about the situations that has been stored in Situation objects to JESS rules and export them to the JESS engine. For every Situation in the given HashMap the conditions are formatted to condition rules first. Two global variables are created for every situation that state whether or not the start or end conditions have been satisfied. A condition rule checks the values of all the variables of a condition and sets the global condition variable to true if all the variables have the correct values. The conditions are only evaluated if the global variable has not already been set. The end condition is furthermore only evaluated if the global start variable has been set to true, because and end condition can only end if the situation has already started. Furthermore, when one of the condition rules fires, the method setSituationState(java.lang.String, java.lang.String) is called to notify the JESSCommunicator that the state of the situation has changed. A condition rule is built up in the following way:

IF the condition rule has not yet fired AND
the values of all the variables of the condition are correct THEN
call the setSituationState method of the JESSCommunicator AND
set the global condition variable

After the conditions have been converted to rules the actions belonging to the siutations are interpreted and converted to another set of rules. For every action a rule and a global action variable are created. The global action variable contains a string that identifies whether or not an action has been performed yet. The action rule sets the global action variable to the performed state if the start conditions of the situation have been satisfied, the action has not been performed yet and the flight sim variable belonging to the action has the correct value that was specified in the xml file. If the rule fires the global action variable is set and the method addPerformedAction(java.lang.String) is called to notify the JESSCommunicator that an action has been performed. An action rule is built up in the following way:

IF the start condition rule has fired AND
the action has not been performed yet AND
the value of the variable belonging to the action is correct THEN
call the addPerformedAction method of the JESSCommunicator AND
set the global action variable>

Parameters:
situations - A HashMap containing all the situations that have been parsed from the XML file.
See Also:
addPerformedAction(java.lang.String), setSituationState(java.lang.String, java.lang.String)

resetSituation

public void resetSituation(Situation situation)
This method is called when the end of a situation is detected. The method sets the values of global variables of the start and end conditions to false and sets the actions of the situation to their unperformed state.

Parameters:
situation - The situation that has ended.

bindVariable

public void bindVariable(java.lang.String variableName,
                         java.lang.String variableValue)
This method is called from the VariableReader to add a fact to the knowledge base that states the value of one of the flight sim variables. If a fact has previously been added for this variable this first is first retracted form the engine.

Parameters:
variableName - The name of the flight sim variable.
variableValue - The value of the flight sim variable.

getGlobals

public java.util.Vector getGlobals()
Returns the names of the global variables in the engine and their values. This method is called by the JessViewer which can be used to take a look in the engine. This is very useful for debugging.

Returns:
A vector of vectors containing two strings, the name of the variable and its value.

getFacts

public java.lang.Object[][] getFacts()
The method showFacts reads the facts from Jess and returns them. Of each fact its id, name, template, and content is returned.

Returns:
2-Dimensional array with content of factbase

getRules

public java.lang.Object[][] getRules()
The method showRules reads the rules from Jess and returns them. Of each rule its id, name, and content is returned.

Returns:
2-Dimensional array with content of rulebase

setSituationState

public void setSituationState(java.lang.String situationName,
                              java.lang.String state)
This method is called from the JESS engine when one of the start or end conditions fire. This method just passes the new state of the situation to the SituationRecognizer.

Parameters:
situationName - The name of the situation.
state - The new state of the situation.

addPerformedAction

public void addPerformedAction(java.lang.String situationName)
This method is called from the JESS engine when one of the action rules fire. This method just calls a method of the SituationRecognizer.

Parameters:
situationName - The name of the situation.

getActionName

protected java.lang.String getActionName(java.lang.String situationName,
                                         int index)
Create a unique name for the action variable. This name consists of the name of the situation the action belongs to and a number that identifies the action.

Parameters:
situationName - The name of the situation.
index - The number of the action.
Returns:
The unique name of the action variable

addClassTemplate

protected void addClassTemplate(java.lang.String className)
The method addClassTemplate adds a template of the given class to the factbase.

Parameters:
className - The name of the class the template is based on.

addObjectFact

protected void addObjectFact(java.lang.Object obj)
The method addObjectFact adds a shadow fact of the given object to the factbase. The appropriate template should have been created with addClassTemplate.

Parameters:
obj - The Object that has to be added to the factbase.

formatRule

protected java.lang.String formatRule(java.lang.String variableName,
                                      java.lang.String condition,
                                      boolean trueIfNotPresent)
This method returns a condition formatted like a rule in JESS. A condition will have the following syntactical form (from the xml file):

condition = value (lop value)*
lop = & or |, these can not both be used in one condition!!!
value = (op)val
op = ! or < or > or <= or >=

First the kind of lop will be determined, this is either a &, a | or nothing. Then the string will be split with the lops as separators. The parts will then be converted to a rule in the method convertValueToRule(java.lang.String, java.lang.String, boolean).

Parameters:
variableName - The name of the flight sim variable
condition - The condition on the value of the variable
trueIfNotPresent - If true then the rule must evaluate to true if no value for the variable is known, to false otherwise.
See Also:
convertValueToRule(java.lang.String, java.lang.String, boolean)

convertValueToRule

protected java.lang.String convertValueToRule(java.lang.String variableName,
                                              java.lang.String val,
                                              boolean trueIfNotPresent)
This method creates a rule that checks the value of the given variable. The val parameter will look like this:

val = (op)value
op = ! or < or > or <= or >=

First the val will be checked to see if it starts with one of the ops. If so it is stripped from the val. What remains is the value. The value can be either a String or a Number, which will be determined by trying to convert the string representation to a double. If that conversion succeeds the numerical operators will be used in the JESS rule (=, >, < etc.), otherwise the rule will just check if the fact corresponding to the variable value is present in the fact base.

Parameters:
variableName - The name of the flight sim variable
val - The condition on the value of the variable
trueIfNotPresent - If true then the rule must evaluate to true if no value for the variable is known, to false otherwise.