jess
Class Defrule

java.lang.Object
  extended byjess.Node
      extended byjess.HasLHS
          extended byjess.Defrule
All Implemented Interfaces:
jess.MatchInfoSource, Modular, Named, jess.NodeSink, java.io.Serializable, Visitable

public class Defrule
extends HasLHS
implements java.io.Serializable, jess.MatchInfoSource

A Defrule is a specific action meant to be taken when certain conditions are met in working memory. Although you can construct Defrules from the Java API, they are almost always parsed from "defrule" constructs in the Jess language. Building a Defrule from Java is very complicated.

(C) 2005 Sandia National Laboratories

See Also:
Serialized Form

Field Summary
 
Fields inherited from class jess.Node
LEFT, RIGHT
 
Constructor Summary
Defrule(java.lang.String name, java.lang.String docstring, Rete engine)
           
 
Method Summary
 java.lang.Object accept(Visitor jv)
          Defrule participates in the Visitor pattern.
 void addAction(Funcall fc)
          Add an action to this defrule
 void addCE(jess.ConditionalElementX ce, Rete engine)
          Add a conditional element (a pattern or group of patterns) to this rule.
 void callNodeLeft(int tag, Token token, Context context)
          An implementation detail, public only because Jave requires methods that implement an interface to be public.
 int evalSalience(Rete engine)
          Evaluate the salience of this rule.
 Funcall getAction(int idx)
          Fetch the idx-th RHS action of this rule.
 boolean getAutoFocus()
          Indicate whether this rule will automatically focus its module when it is activated.
 java.lang.String getConstructType()
          Return the type of this construct
 int getNActions()
          Fetch the number of actions on this rule's RHS
 int getSalience()
          Fetch the current salience value for this rule.
 Value getSalienceValue()
          Fetch the raw salience for this rule as a jess.Value.
 boolean hasNonDefaultSalience()
          Indicate whether this rule's salience is different from the default value.
 boolean isNoLoop()
          Indicate whether this rule can activate itself through actions it takes on its RHS.
 boolean isRelevantChange(int factIndex, Token token, Context context)
          Implementation detail.
 void setAutoFocus(boolean autoFocus)
          Tell this rule whether to automatically focus the module it appears in when the rule is activated.
 void setNoLoop(boolean b)
          Specify whether this rule can activate itself through actions it takes on its RHS.
 void setSalience(Value v, Rete engine)
          Set the salience of this rule.
 java.lang.String toString()
          Returns a brief description of this rule
 
Methods inherited from class jess.HasLHS
addNode, getConditionalElements, getDisplayName, getDocstring, getGroupSize, getModule, getName, getNext, getNodeCount, getNodeIndexHash, getNodes, getPatternCount, listNodes, setLHS, setNodeIndexHash
 
Methods inherited from class jess.Node
addJessListener, getSuccessors, removeJessListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Defrule

public Defrule(java.lang.String name,
               java.lang.String docstring,
               Rete engine)
        throws JessException
Method Detail

getSalience

public final int getSalience()
Fetch the current salience value for this rule. This method won't re-evaluate the salienc; the last value previously computed will be returned.

Returns:
the salience of this defrule

getSalienceValue

public Value getSalienceValue()
Fetch the raw salience for this rule as a jess.Value. For rules with dynamic salience, the Value returned will be a jess.FuncallValue.

Returns:
the salience of this defrule

setSalience

public void setSalience(Value v,
                        Rete engine)
                 throws JessException
Set the salience of this rule. The value van be a fixed integer, or it can be a function call. The default salience is 0.

Parameters:
v - the salience expression
engine - a Rete object to provide an execution context
Throws:
JessException - if anything goes wrong

evalSalience

public int evalSalience(Rete engine)
                 throws JessException
Evaluate the salience of this rule. If the salience was set to a Funcall value during parsing, then this function may return different values over time. If the salience is constant, this is equivalent to getSalience. The computed value will be the next value returned by getSalience().

Parameters:
engine - The Rete engine the rule belongs to
Returns:
The evaluated salience
Throws:
JessException - If something goes wrong

hasNonDefaultSalience

public boolean hasNonDefaultSalience()
Indicate whether this rule's salience is different from the default value.

Returns:
true if this rule's salience is not the default.

getAutoFocus

public boolean getAutoFocus()
Indicate whether this rule will automatically focus its module when it is activated.

Returns:
the value of this property

setAutoFocus

public void setAutoFocus(boolean autoFocus)
Tell this rule whether to automatically focus the module it appears in when the rule is activated. The default value is false.

Parameters:
autoFocus - the desired value of this property.

addCE

public void addCE(jess.ConditionalElementX ce,
                  Rete engine)
           throws JessException
Add a conditional element (a pattern or group of patterns) to this rule. Normally this will only be called by the Jess parser or the XML parser, but it is theoretically possible for users to build their own rules this way.

Overrides:
addCE in class HasLHS
Parameters:
ce - the conditional element to add
engine - the rule engine that provides the execution context
Throws:
JessException - if anything goes wrong

callNodeLeft

public void callNodeLeft(int tag,
                         Token token,
                         Context context)
                  throws JessException
An implementation detail, public only because Jave requires methods that implement an interface to be public.

Throws:
JessException

getNActions

public int getNActions()
Fetch the number of actions on this rule's RHS

Returns:
The number of actions

getAction

public Funcall getAction(int idx)
Fetch the idx-th RHS action of this rule.

Parameters:
idx - The zero-based index of the action to fetch
Returns:
the action as a function call

addAction

public void addAction(Funcall fc)
Add an action to this defrule

Parameters:
fc - the action as a function call

accept

public java.lang.Object accept(Visitor jv)
Defrule participates in the Visitor pattern. Will call accept(this) on the Visitor.

Specified by:
accept in interface Visitable
Specified by:
accept in class HasLHS
Parameters:
jv - a Visitor
Returns:
whatever jv.accept(this) returns

toString

public java.lang.String toString()
Returns a brief description of this rule

Returns:
the String "Defrule rule-name"

setNoLoop

public void setNoLoop(boolean b)
Specify whether this rule can activate itself through actions it takes on its RHS. If this property is set to true, then modifying a fact on the RHS of this rule can't reactivate the same rule, even if that fact is matched by this rule. The default value is false.

Parameters:
b - the desired value of the property.

isNoLoop

public boolean isNoLoop()
Indicate whether this rule can activate itself through actions it takes on its RHS. If this property is true, then modifying a fact on the RHS of this rule can't reactivate the same rule, even if that fact is matched by this rule.

Returns:
the current value of the property.

getConstructType

public final java.lang.String getConstructType()
Return the type of this construct

Specified by:
getConstructType in interface Named
Returns:
the String "defrule"

isRelevantChange

public boolean isRelevantChange(int factIndex,
                                Token token,
                                Context context)
Implementation detail. Public only because Java requires methods that implement an interface to be public.

Specified by:
isRelevantChange in interface jess.MatchInfoSource

© 2006 Sandia Corporation