jess
Class Deftemplate

java.lang.Object
  extended byjess.Deftemplate
All Implemented Interfaces:
Modular, Named, java.io.Serializable, Visitable

public class Deftemplate
extends java.lang.Object
implements java.io.Serializable, Visitable, Modular

A Deftemplate defines a class of Jess facts. Its definition includes a list of properties or "slots", each of which can have a default value and a type. Deftemplates can be created by hand, but they usually come from executing the "deftemplate" construct or the "defclass" function in the Jess language. To build a useful template, you need to specify the name to the constructor, then add the named slots one by one:

 Rete r = new Rete();
 Deftemplate dt = new Deftemplate("point", "A 2D point", r);
 Value zero = new Value(0, RU.INTEGER);
 dt.addSlot("x", zero, "NUMBER");
 dt.addSlot("y", zero, "NUMBER");
 r.addDeftemplate(dt);
 
(C) 2005 Sandia National Laboratories

See Also:
Serialized Form

Constructor Summary
Deftemplate(java.lang.String name, java.lang.String docstring, Deftemplate dt, Rete engine)
          Create a deftemplate 'derived from' another one.
Deftemplate(java.lang.String name, java.lang.String docstring, Rete engine)
          Create a template that extends the root template.
 
Method Summary
 java.lang.Object accept(Visitor v)
          This class participates in the Visitor pattern.
 void addMultiSlot(java.lang.String name, Value value)
          Create a new multislot in this deftemplate.
 void addSlot(java.lang.String name, Value value, java.lang.String typename)
          Create a new slot in this deftemplate.
 void doBackwardChaining(Rete engine)
          Make this deftemplate backwards-chaining reactive.
 boolean equals(java.lang.Object o)
          Compare this deftemplate to another one for equality.
static Deftemplate getAccumTemplate()
          The template for a special fact used internally by Jess.
 Deftemplate getBackchainingTemplate(Rete engine)
          Return a new backward-chaining trigger template for this template.
 java.lang.String getBackchainingTemplateName()
          Get the name of the backward chaining trigger template that would be associated with this template, whether it exists or not.
 boolean getBackwardChaining()
          Get the backchaining reactivity of this deftemplate.
 java.lang.String getBaseName()
          Get the name of this deftemplate unqualified by the module name.
static Deftemplate getClearTemplate()
          The template for a special fact used internally by Jess.
 java.lang.String getConstructType()
          Return the String "deftemplate".
 java.lang.String getDocstring()
          Get the docstring of this deftemplate.
static Deftemplate getInitialTemplate()
          The template for "initial-fact".
 java.lang.String getModule()
          Return the name of the module this deftemplate is in.
 java.lang.String getName()
          Get the name of this deftemplate.
 java.lang.String getNameWithoutBackchainingPrefix()
          Return the undecorated template name.
 int getNSlots()
          Return the number of slots in this deftemplate.
static Deftemplate getNullTemplate()
          The template for a special fact used internally by Jess.
 Deftemplate getParent()
          Return the parent of this deftemplate.
static Deftemplate getRootTemplate()
          The root template that serves as the ultimate parent of all other templates.
 java.lang.String getShadowClassName()
          If this is a template for shadow facts, return the Java class name.
 int getSlotDataType(int index)
          Returns the slot data type (one of the constants in jess.RU) for the slot given by the zero-based index.
 Value getSlotDefault(int index)
          Returns the default value of a slot given by the zero-based index.
 int getSlotIndex(java.lang.String slotname)
          Return the index (0 through getNSlots()-1) of the named slot, or -1 if there is no such slot.
 java.lang.String getSlotName(int index)
          Return the name of a given slot in this deftemplate.
 java.lang.String[] getSlotNames()
          Return a new array containing the names of all the slots in this deftemplate.
 int getSlotType(int index)
          Returns the slot type (RU.SLOT or RU.MULTISLOT) of the slot in this deftemplate given by the zero-based index.
 int getSlotType(java.lang.String name)
          Returns the slot type (RU.SLOT or RU.MULTISLOT) of the slot in this deftemplate given by the zero-based index.
static Deftemplate getSpecialTemplate(java.lang.String name)
          Returns one of the special templates "test" or "initial-fact" by name.
static Deftemplate getTestTemplate()
          The template for a special fact used internally by Jess.
 int hashCode()
          Return a hash code for this template.
 boolean isBackwardChainingTrigger()
          Indicate whether this template is a backward chaining trigger.
 boolean isMultislot(int index)
          Returns true if the slot at the given index is a multislot.
 boolean isOrdered()
          Return true if this is an ordered template.
 boolean isShadowTemplate()
          Return true if this is a template for shadow facts.
 boolean isSlotSpecific()
          Query the slot-specific activation behavior for this template.
static boolean isSpecialName(java.lang.String name)
          Returns true if the argument is a special template name.
 void setSlotSpecific(boolean b)
          Set the slot-specific activation behavior for this template.
 java.lang.String toString()
          Turn this deftemplate into a String.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Deftemplate

public Deftemplate(java.lang.String name,
                   java.lang.String docstring,
                   Rete engine)
            throws JessException
Create a template that extends the root template.

Parameters:
name - the deftemplate name
docstring - the deftemplate's documentation string
engine - the rule engine to create the template in
Throws:
JessException - if anything goes wrong

Deftemplate

public Deftemplate(java.lang.String name,
                   java.lang.String docstring,
                   Deftemplate dt,
                   Rete engine)
            throws JessException
Create a deftemplate 'derived from' another one. If the name contains a module name, it will be used. Otherwise, the template will be in the current module.

Parameters:
name - the deftemplate name
docstring - the deftemplate's documentation string
dt - the 'parent' of this deftemplate
engine - the engine to create the template in
Throws:
JessException - if anything goes wrong
Method Detail

getRootTemplate

public static Deftemplate getRootTemplate()
The root template that serves as the ultimate parent of all other templates. This template is to deftemplates as java.lang.Object is to Java objects.

Returns:
The singleton root template.

getInitialTemplate

public static Deftemplate getInitialTemplate()
The template for "initial-fact".

Returns:
A deftemplate.

getClearTemplate

public static Deftemplate getClearTemplate()
The template for a special fact used internally by Jess.

Returns:
A deftemplate

getNullTemplate

public static Deftemplate getNullTemplate()
The template for a special fact used internally by Jess.

Returns:
A deftemplate

getTestTemplate

public static Deftemplate getTestTemplate()
The template for a special fact used internally by Jess.

Returns:
A deftemplate

getAccumTemplate

public static Deftemplate getAccumTemplate()
The template for a special fact used internally by Jess.

Returns:
A deftemplate

equals

public boolean equals(java.lang.Object o)
Compare this deftemplate to another one for equality. Two templates are equal if the have the same full name, the same parent, and all the same slots.

Parameters:
o - another template to compare this this one
Returns:
true if the templates are the same.

hashCode

public int hashCode()
Return a hash code for this template.

Returns:
the hashcode

getParent

public Deftemplate getParent()
Return the parent of this deftemplate. The parent is another deftemplate this one extends. If this template extends no other template explicitly, then it extends the root template.

Returns:
the parent deftemplate

getName

public final java.lang.String getName()
Get the name of this deftemplate. The name is qualified by the module name.

Specified by:
getName in interface Named
Returns:
the name of this deftemplate

getBaseName

public final java.lang.String getBaseName()
Get the name of this deftemplate unqualified by the module name.

Returns:
Tte name of this deftemplate

getDocstring

public final java.lang.String getDocstring()
Get the docstring of this deftemplate.

Specified by:
getDocstring in interface Named
Returns:
The docstring

doBackwardChaining

public final void doBackwardChaining(Rete engine)
                              throws JessException
Make this deftemplate backwards-chaining reactive. Sets a flag in this template, and creates the backward-chaining trigger template.

Parameters:
engine - the engine the new template is created in
Throws:
JessException - if anything goes wrong

getBackwardChaining

public final boolean getBackwardChaining()
Get the backchaining reactivity of this deftemplate.

Returns:
True if this deftemplate can stimulate backwards chaining.

addSlot

public void addSlot(java.lang.String name,
                    Value value,
                    java.lang.String typename)
             throws JessException
Create a new slot in this deftemplate. If the slot already exists, just change the default value. The type parameter is currently stored with the deftemplate, but not used by Jess for any purpose.

Parameters:
name - name of the slot
value - default value for the slot
typename - type of the slot: INTEGER, FLOAT, ANY, etc.
Throws:
JessException - If something goes wrong

addMultiSlot

public void addMultiSlot(java.lang.String name,
                         Value value)
                  throws JessException
Create a new multislot in this deftemplate. If the slot already exists, just change the default value.

Parameters:
name - name of the slot
value - default value for the slot
Throws:
JessException - if something goes wrong

getSlotDataType

public int getSlotDataType(int index)
                    throws JessException
Returns the slot data type (one of the constants in jess.RU) for the slot given by the zero-based index.

Parameters:
index - The zero-based index of the slot (0, 1, 2 ... getNSlots()-1)
Returns:
The data type of that slot (RU.INTEGER, RU.SYMBOL, etc., or RU.NONE)
Throws:
JessException - if anything goes wrong

getSlotDefault

public Value getSlotDefault(int index)
                     throws JessException
Returns the default value of a slot given by the zero-based index.

Parameters:
index - The zero-based index of the slot (0, 1, 2 ... getNSlots()-1)
Returns:
The default value for that slot (can be Funcall.NIL or Funcall.NILLIST for none
Throws:
JessException - if anything goes wrong

getSlotType

public int getSlotType(int index)
                throws JessException
Returns the slot type (RU.SLOT or RU.MULTISLOT) of the slot in this deftemplate given by the zero-based index.

Parameters:
index - The zero-based index of the slot (0, 1, 2 ... getNSlots()-1)
Returns:
The type of that slot (RU.SLOT or RU.MULTISLOT)
Throws:
JessException - if anything goes wrong

getSlotType

public int getSlotType(java.lang.String name)
                throws JessException
Returns the slot type (RU.SLOT or RU.MULTISLOT) of the slot in this deftemplate given by the zero-based index.

Parameters:
name - The name of the slot
Returns:
The type of that slot (RU.SLOT or RU.MULTISLOT)
Throws:
JessException - if anything goes wrong

isMultislot

public boolean isMultislot(int index)
                    throws JessException
Returns true if the slot at the given index is a multislot.

Parameters:
index - the index of the slot
Returns:
true if the slot is a multislot
Throws:
JessException - if the index is invalid

getSlotIndex

public int getSlotIndex(java.lang.String slotname)
Return the index (0 through getNSlots()-1) of the named slot, or -1 if there is no such slot.

Parameters:
slotname - The name of the slot
Returns:
The zero-based index of the slot

getSlotName

public java.lang.String getSlotName(int index)
                             throws JessException
Return the name of a given slot in this deftemplate.

Parameters:
index - The zero-based index of the slot (0, 1, 2 ... getNSlots()-1)
Returns:
The name of that slot
Throws:
JessException - If something is horribly wrong

getSlotNames

public java.lang.String[] getSlotNames()
Return a new array containing the names of all the slots in this deftemplate.

Returns:
a new array

getNSlots

public int getNSlots()
Return the number of slots in this deftemplate.

Returns:
The number of slots in this deftemplate

toString

public java.lang.String toString()
Turn this deftemplate into a String.

Returns:
a string representation of the Deftemplate

accept

public java.lang.Object accept(Visitor v)
This class participates in the Visitor pattern.

Specified by:
accept in interface Visitable
Parameters:
v - a Visitor that is interested in working with this object
Returns:
whatever the visitor's visitDeftemplate() method returns e

getModule

public java.lang.String getModule()
Return the name of the module this deftemplate is in.

Specified by:
getModule in interface Modular
Returns:
the module name

isBackwardChainingTrigger

public boolean isBackwardChainingTrigger()
Indicate whether this template is a backward chaining trigger.

Returns:
true if this template is a backward chaining trigger

getBackchainingTemplateName

public java.lang.String getBackchainingTemplateName()
Get the name of the backward chaining trigger template that would be associated with this template, whether it exists or not.

Returns:
the template name

getNameWithoutBackchainingPrefix

public java.lang.String getNameWithoutBackchainingPrefix()
Return the undecorated template name. If this template is not a backward chaining trigger, then just return its name. If it is, then return the name of the template this template serves as a trigger for.

Returns:
the name

getBackchainingTemplate

public Deftemplate getBackchainingTemplate(Rete engine)
                                    throws JessException
Return a new backward-chaining trigger template for this template.

Parameters:
engine - the engine containing the template
Returns:
the template
Throws:
JessException - if anything goes wrong

isOrdered

public boolean isOrdered()
Return true if this is an ordered template. An ordered template is one that has a single multislot named "__data".

Returns:
true if this is an ordered template.

setSlotSpecific

public void setSlotSpecific(boolean b)
Set the slot-specific activation behavior for this template.

Parameters:
b - true if this template should use slot-specific behavior

isSlotSpecific

public boolean isSlotSpecific()
Query the slot-specific activation behavior for this template.

Returns:
true if this template uses slot-specific behavior

getConstructType

public final java.lang.String getConstructType()
Return the String "deftemplate".

Specified by:
getConstructType in interface Named
Returns:
"deftemplate"

getShadowClassName

public java.lang.String getShadowClassName()
If this is a template for shadow facts, return the Java class name. Otherwise, return null.

Returns:
a class name, or null

isShadowTemplate

public boolean isShadowTemplate()
Return true if this is a template for shadow facts.

Returns:
true if this is a shadow template

isSpecialName

public static boolean isSpecialName(java.lang.String name)
Returns true if the argument is a special template name.

Parameters:
name - the name of a template
Returns:
true for "test" or "initial-fact", false otherwise.

getSpecialTemplate

public static Deftemplate getSpecialTemplate(java.lang.String name)
Returns one of the special templates "test" or "initial-fact" by name.

Parameters:
name - "test" or "initial-fact"
Returns:
the names template or null

© 2006 Sandia Corporation