biz.hammurapi.rules
Class Rule

java.lang.Object
  extended by biz.hammurapi.config.ComponentBase
      extended by biz.hammurapi.rules.AbstractRule
          extended by biz.hammurapi.rules.Rule
All Implemented Interfaces:
biz.hammurapi.config.Component, biz.hammurapi.config.Context, InvocationTarget, biz.hammurapi.metrics.MeasurementCollector, biz.hammurapi.metrics.MeasurementConsumer, Constants

public class Rule
extends AbstractRule

Base class for rules.

Author:
Pavel Vlasov

Nested Class Summary
static interface Rule.AcceptInfo
          Instances of this interface are passed to accept() methods as second parameter.
static interface Rule.ChangeDetector
          Interface to detect changes in arguments passed to inference methods.
 
Field Summary
 
Fields inherited from class biz.hammurapi.config.ComponentBase
owner
 
Fields inherited from interface biz.hammurapi.rules.Constants
COLLECTION_MANAGER, DESCRIPTION, HANDLE_MANAGER, KNOWLEDGE_COMPACTOR, NAME, NEGATOR_CLASS, OBJECT_FILTER, RETAIN_NEGATORS, RULES, URI_DIRECT, URI_PROPERTY, WORKER_REF
 
Constructor Summary
  Rule()
          Default constructor.
protected Rule(java.lang.String inferMethodName, java.lang.String removeMethodName, java.lang.String acceptMethodName)
           
 
Method Summary
 long getInvocationCounter()
           
 java.util.Collection getInvocationHandlers()
           
 java.util.Collection getRemoveHandlers()
           
protected  Rule.ChangeDetector newChangeDetector(java.lang.Object obj)
          Creates default change detector, which detects changes in versioned and observable objects.
protected  void post(java.lang.Object fact)
          Adds new fact to knowledge base.
protected  void remove(java.lang.Object fact)
          Invokes remove method of the knowledge base and adds trace action.
 void reset()
          Invoked in rule session reset() method.
protected  long resetInvocationCounter()
          Resets invocation counter.
protected  void setMethodFactTypes(java.lang.Class[] parameterTypes, java.lang.Class factType)
          Convenience method, delegates to setMethodFactTypes(Class[] parameterTypes, Class[] factTypes)
protected  void setMethodFactTypes(java.lang.Class[] parameterTypes, java.lang.Class[] factTypes)
          Rule methods can return facts and post facts.
protected  void setMethodFactTypes(java.lang.Class parameterType, java.lang.Class factType)
          Convenience method, delegates to setMethodFactTypes(Class[] parameterTypes, Class[] factTypes)
protected  void setMethodFactTypes(java.lang.Class parameterType, java.lang.Class[] factTypes)
          Convenience method, delegates to setMethodFactTypes(Class[] parameterTypes, Class[] factTypes)
 void start()
          Locates collection manager.
protected  void update(java.lang.Object fact)
          Invokes update method of the knowledge base and adds trace action.
 
Methods inherited from class biz.hammurapi.rules.AbstractRule
getCollection, getDescription, getName, setDescription, setName, stop, toString
 
Methods inherited from class biz.hammurapi.config.ComponentBase
addMeasurement, get, getChild, getMeasurementConsumer, getOwner, getOwner, setMeasurementConsumer, setOwner
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Rule

public Rule()
Default constructor. Uses "infer" for infer methods, "remove" for remove methods, and "accept" for filters.


Rule

protected Rule(java.lang.String inferMethodName,
               java.lang.String removeMethodName,
               java.lang.String acceptMethodName)
Parameters:
inferMethodName - Methods with this name and one or more arguments are invoked when an object of type compatible with one of parameters is posted to the object bus.
removeMethodName - Single-argument methods with this name will be invoked when rule set's remove method with compatible type is invoked. Generally rules shall not implement this method because collection manager and handle manager take care of removal of the object and conclusions made based on this object from the knowledge base.
acceptMethodName - Methods with this name and two arguments - the first of equal type and the second of AcceptInfo type are used to filter inputs to infer methods with more than one parameter. Type of the first argument of accept method and corresponding argument of infer must be equal. accept() method's return type must be boolean.
Method Detail

newChangeDetector

protected Rule.ChangeDetector newChangeDetector(java.lang.Object obj)
Creates default change detector, which detects changes in versioned and observable objects. Subclasses can override this method to detect changes as appropriate for the application domain.

Parameters:
obj -
Returns:
Change detector or null if change detection is not needed.

getInvocationCounter

public long getInvocationCounter()
Returns:
Number of invocations. Call of reset() method zeroes the counter.

start

public void start()
           throws biz.hammurapi.config.ConfigurationException
Locates collection manager.

Specified by:
start in interface biz.hammurapi.config.Component
Overrides:
start in class AbstractRule
Throws:
biz.hammurapi.config.ConfigurationException

getInvocationHandlers

public java.util.Collection getInvocationHandlers()
Returns:
Handlers for this target.

getRemoveHandlers

public java.util.Collection getRemoveHandlers()
Specified by:
getRemoveHandlers in class AbstractRule
Returns:
Collection of remove handlers.

post

protected void post(java.lang.Object fact)
Adds new fact to knowledge base. Returning value from inference methods has the same effect.

Parameters:
fact -

remove

protected void remove(java.lang.Object fact)
Invokes remove method of the knowledge base and adds trace action.


update

protected void update(java.lang.Object fact)
Invokes update method of the knowledge base and adds trace action.


reset

public void reset()
Description copied from class: AbstractRule
Invoked in rule session reset() method. Cleans acquired collections. Subclasses can put additional cleanup logic here.

Overrides:
reset in class AbstractRule

resetInvocationCounter

protected long resetInvocationCounter()
Resets invocation counter.

Returns:
counter value before reset.

setMethodFactTypes

protected final void setMethodFactTypes(java.lang.Class[] parameterTypes,
                                        java.lang.Class[] factTypes)
Rule methods can return facts and post facts. Backward reasoning engine needs to know types of rule outputs. From rule class introspection the rule system knows about rule method return type, but it doesn't know about types posted through post(). Also, return type might not be enough, as returned instances may implement interfaces which other rules are interested in, but which are not declared in the rule return type. This method allows rules to inform the inference system about posted types.

This method shall be invoked before rule is started. Place invocations of this method in rule constructors.

Only rules used in backward reasoning need/must call this method to ensure proper reasoning. For methods without fact types information set through this method, the rule system uses method return type.

Parameters:
parameterTypes - Reasoning method parameter types. Method name is not required as it is known.
factTypes - Fact types produced by given reasoning (inference) method. If rule method returns facts as well as posts them, then method return type must be included in the factTypes array.

setMethodFactTypes

protected final void setMethodFactTypes(java.lang.Class parameterType,
                                        java.lang.Class[] factTypes)
Convenience method, delegates to setMethodFactTypes(Class[] parameterTypes, Class[] factTypes)

Parameters:
parameterType -
factTypes -

setMethodFactTypes

protected final void setMethodFactTypes(java.lang.Class parameterType,
                                        java.lang.Class factType)
Convenience method, delegates to setMethodFactTypes(Class[] parameterTypes, Class[] factTypes)

Parameters:
parameterType -
factTypes -

setMethodFactTypes

protected final void setMethodFactTypes(java.lang.Class[] parameterTypes,
                                        java.lang.Class factType)
Convenience method, delegates to setMethodFactTypes(Class[] parameterTypes, Class[] factTypes)

Parameters:
parameterType -
factTypes -


Copyright © 2005 Hammurapi Group. All Rights Reserved.