jeops
Class AbstractKnowledgeBase

java.lang.Object
  |
  +--jeops.AbstractKnowledgeBase

public abstract class AbstractKnowledgeBase
extends java.lang.Object
implements InternalConflictSetListener

The main class of JEOPS. This class models the knowledge the agent has about the world. In it are stored the facts (objects) and (production) rules that act on the first. This class must be subclassed by a concrete knowledge base so that it is provided with the object and rule base needed for work.

Version:
2.1 13 Jul 2000 Implementation of a variation of Rete network.
Author:
Carlos Figueira Filho (csff@cin.ufpe.br)

Field Summary
private  java.util.Vector allJoinNodes
          All join nodes of the Rete network.
private  ConflictSet conflictSet
          The conflict set associated with this knowledge base.
private  java.util.Vector conflictSetListeners
          The set of conflict set listeners.
private  boolean debug
          Flag indicating whether there is any listener for rule events in this knowledge base.
protected  java.util.Map entryPoints
          The map that takes class names into the entry points of the Rete network.
private  java.util.Set forbiddenClasses
          The set of classes whose objects can't be inserted into the Rete network (because there is no compatible entry node).
private  boolean isListener
          Indicates whether this knowledge base is a listener of its conflict set.
private  int numberOfRules
          The number of rules in the rule base.
private  ObjectBase objectBase
          The fact (object) base.
private  AbstractRuleBase ruleBase
          The (production) rule base.
private  java.util.Vector ruleFiringListeners
          The set of rule firing listeners.
 
Constructor Summary
AbstractKnowledgeBase(ConflictSet conflictSet)
          Creates a new knowledge base.
 
Method Summary
 void addConflictSetListener(RuleFireListener l)
          Adds the specified conflict set listener to receive events from this knowledge base.
private  ClassFilterReteNode addNewEntryNode(java.lang.Class classType)
          Tries to find a way from which a class object may get into some entry point of the Rete network.
 void addRuleFireListener(RuleFireListener l)
          Adds the specified rule fire listener to receive events from this knowledge base.
 void assert(java.lang.Object obj)
          Inserts a new object in this knowledge base.
private  void createReteEntryPoints(int ruleIndex)
          Stores in the entryPoints field all the entry points for a given rule.
private  void createReteNetwork()
          Creates the Rete network that will be used in this knowledge base.
protected abstract  AbstractRuleBase createRuleBase()
          Factory method, used to instantiate the actual rule base.
private  void fireElementAddedEvent(ConflictSetEvent e)
          Dispatch an element added event to all registered listeners.
private  void fireElementRemovedEvent(ConflictSetEvent e)
          Dispatch an element removed event to all registered listeners.
private  void fireRuleFiredEvent(RuleEvent e)
          Dispatch a rule fired event to all registered listeners.
private  void fireRuleFiringEvent(RuleEvent e)
          Dispatch a rule firing event to all registered listeners.
 void flush()
          Remove all facts (objects) of the object base.
private  ClassFilterReteNode getEntryPoint(java.lang.Class classType)
          Returns the entry point of the rete network from where an object of a given class must enter.
 AbstractRuleBase getRuleBase()
          Returns the (production) rule base of this knowledge base.
 void internalElementAdded(InternalConflictSetEvent e)
          Invoked when an element is added to the conflict set.
 void internalElementRemoved(InternalConflictSetEvent e)
          Invoked when an element is removed from the conflict set.
protected  void modified(java.lang.Object obj)
          Informs this base that an object was modified, so that the rules can be retested against the object.
 void newFirableRule(int ruleIndex, java.lang.Object[] objects)
          Receives a message from the Rete network indicating that the given rule and objects should be inserted into the conflict set.
 java.util.Vector objects(java.lang.String className)
          Returns the objects of a given class.
 void removeConflictSetListener(RuleFireListener l)
          Removes the specified conflict set listener so that it no longer receives conflict set events from this knowledge base.
 void removeRuleFireListener(RuleFireListener l)
          Removes the specified rule fire listener so that it no longer receives rule fire events from this knowledge base.
 boolean retract(java.lang.Object obj)
          Removes a given object from this knowledge base.
 void run()
          Fires the rules in the rule base with the objects present in the object base, until no rule is fireable anymore.
private  ClassFilterReteNode searchInterfaces(java.lang.Class classType, ClassFilterReteNode classEntryNode)
          Searches the interface implementation hierarchy of the given class to see if it can be connected to some entry point of the network.
 void setConflictSet(ConflictSet conflictSet)
          Defines a conflict set policy for this knowledge base.
private  void setRuleBase(AbstractRuleBase ruleBase)
          Defines the (production) rule base of this knowledge base.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

objectBase

private ObjectBase objectBase
The fact (object) base.

ruleBase

private AbstractRuleBase ruleBase
The (production) rule base.

numberOfRules

private int numberOfRules
The number of rules in the rule base.

conflictSet

private ConflictSet conflictSet
The conflict set associated with this knowledge base.

ruleFiringListeners

private java.util.Vector ruleFiringListeners
The set of rule firing listeners.

conflictSetListeners

private java.util.Vector conflictSetListeners
The set of conflict set listeners.

isListener

private boolean isListener
Indicates whether this knowledge base is a listener of its conflict set.

debug

private boolean debug
Flag indicating whether there is any listener for rule events in this knowledge base.

entryPoints

protected java.util.Map entryPoints
The map that takes class names into the entry points of the Rete network. Each object enters the network at a node of its actual class.

allJoinNodes

private java.util.Vector allJoinNodes
All join nodes of the Rete network. Useful for removing objects from Rete.

forbiddenClasses

private java.util.Set forbiddenClasses
The set of classes whose objects can't be inserted into the Rete network (because there is no compatible entry node).
Constructor Detail

AbstractKnowledgeBase

public AbstractKnowledgeBase(ConflictSet conflictSet)
Creates a new knowledge base.
Parameters:
conflictSet - the conflict set associated with this knowledge base.
Method Detail

createRuleBase

protected abstract AbstractRuleBase createRuleBase()
Factory method, used to instantiate the actual rule base.
Returns:
the rule base to be used with this knowledge base.

addConflictSetListener

public void addConflictSetListener(RuleFireListener l)
Adds the specified conflict set listener to receive events from this knowledge base.
Parameters:
l - the conflict set listener

addNewEntryNode

private ClassFilterReteNode addNewEntryNode(java.lang.Class classType)
Tries to find a way from which a class object may get into some entry point of the Rete network. If so, creates the subnetwork that leads from the given class to the preexisting network, connecting them.
Parameters:
classType - the class object whose hierarchy is to be analysed to see if it fits into the current Rete network.
Returns:
the ClassFilterReteNode corresponding to the given class object, if some way could be found to connect it to the rest of the network; null otherwise.

addRuleFireListener

public void addRuleFireListener(RuleFireListener l)
Adds the specified rule fire listener to receive events from this knowledge base.
Parameters:
l - the rule base listener

assert

public void assert(java.lang.Object obj)
Inserts a new object in this knowledge base.
Parameters:
obj - the object being inserted.

createReteEntryPoints

private void createReteEntryPoints(int ruleIndex)
Stores in the entryPoints field all the entry points for a given rule.
Parameters:
ruleIndex - the index of the rule whose entry points will be created.

createReteNetwork

private void createReteNetwork()
Creates the Rete network that will be used in this knowledge base. At the end of this method, the field entryPoints will be filled with the main entry points of the network.

fireElementAddedEvent

private void fireElementAddedEvent(ConflictSetEvent e)
Dispatch an element added event to all registered listeners.

fireElementRemovedEvent

private void fireElementRemovedEvent(ConflictSetEvent e)
Dispatch an element removed event to all registered listeners.

fireRuleFiredEvent

private void fireRuleFiredEvent(RuleEvent e)
Dispatch a rule fired event to all registered listeners.

fireRuleFiringEvent

private void fireRuleFiringEvent(RuleEvent e)
Dispatch a rule firing event to all registered listeners.

flush

public void flush()
Remove all facts (objects) of the object base.

getEntryPoint

private ClassFilterReteNode getEntryPoint(java.lang.Class classType)
Returns the entry point of the rete network from where an object of a given class must enter. If there is no entry point for the given class, it may be due to two reasons: the objects of the class can't be matched against any declaration of the rules; or the class is not explicitly declared in the rules, but one of its superclasses or implemented interfaces appear in the declaration of some rule. In the latter case, the Rete network will be extended in order to accomodate the incoming object. In the former, null will be returned.
Parameters:
classType - the class object whose entry point is desired.
Returns:
the node from where the objects of the given class must enter in the Rete network, or null if such entry point doesn't exist and can't even be created.

getRuleBase

public AbstractRuleBase getRuleBase()
Returns the (production) rule base of this knowledge base.
Returns:
the (production) rule base of this knowledge base.

internalElementAdded

public void internalElementAdded(InternalConflictSetEvent e)
Invoked when an element is added to the conflict set.
Specified by:
internalElementAdded in interface InternalConflictSetListener
Parameters:
e - the conflict set event.

internalElementRemoved

public void internalElementRemoved(InternalConflictSetEvent e)
Invoked when an element is removed from the conflict set.
Specified by:
internalElementRemoved in interface InternalConflictSetListener
Parameters:
e - the conflict set event.

modified

protected void modified(java.lang.Object obj)
Informs this base that an object was modified, so that the rules can be retested against the object.
Parameters:
obj - the object that was modified.

newFirableRule

public void newFirableRule(int ruleIndex,
                           java.lang.Object[] objects)
Receives a message from the Rete network indicating that the given rule and objects should be inserted into the conflict set.
Parameters:
ruleIndex - the index of the firable rule.
objects - the objects that made the rule firable.

objects

public java.util.Vector objects(java.lang.String className)
Returns the objects of a given class.
Parameters:
className - the name of the class.

removeConflictSetListener

public void removeConflictSetListener(RuleFireListener l)
Removes the specified conflict set listener so that it no longer receives conflict set events from this knowledge base.
Parameters:
l - the conflict set listener

removeRuleFireListener

public void removeRuleFireListener(RuleFireListener l)
Removes the specified rule fire listener so that it no longer receives rule fire events from this knowledge base.
Parameters:
l - the rule fire listener

retract

public boolean retract(java.lang.Object obj)
Removes a given object from this knowledge base.
Parameters:
obj - the object being removed.
Returns:
true if the given object belonged to this base; false otherwise.

run

public void run()
Fires the rules in the rule base with the objects present in the object base, until no rule is fireable anymore.

searchInterfaces

private ClassFilterReteNode searchInterfaces(java.lang.Class classType,
                                             ClassFilterReteNode classEntryNode)
Searches the interface implementation hierarchy of the given class to see if it can be connected to some entry point of the network.
Parameters:
classType - the class object whose interface implementation hierarchy is to be analysed to see if it fits into the current rete network.
classEntryNode - the entry node corresponding to the given class, if such exists. If it already exists and some path is found, a new one won't have to be created.
Returns:
the ClassFilterReteNode corresponding to the given class object, if some way could be found to connect it to the rest of the network; null otherwise.

setConflictSet

public void setConflictSet(ConflictSet conflictSet)
Defines a conflict set policy for this knowledge base.
Parameters:
conflictSet - the new conflict set that implements its associated conflict resolution policy.

setRuleBase

private void setRuleBase(AbstractRuleBase ruleBase)
Defines the (production) rule base of this knowledge base.
Parameters:
ruleBase - the rule base of this knowledge base.