jess
Class Deftemplate

java.lang.Object
  extended by jess.Deftemplate
All Implemented Interfaces:
java.io.Serializable, Modular, Named, 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) 2013 Sandia Corporation

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 addMultiSlot(java.lang.String name, Value value, java.lang.String typename)
          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.
 void freeze(Rete engine)
          Complete the initialization of this template.
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.
 ValueVector getSlotAllowedValues(java.lang.String slotName)
          Returns a list of allowed values for a slot.
 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.
 int getSlotDataType(java.lang.String slotName)
          Returns the slot data type (one of the constants in jess.RU) for the named slot
 Value getSlotDefault(int index)
          Returns the default value of a slot given by the zero-based index.
 Value getSlotDefault(java.lang.String slotName)
          Returns the default value of a named slot
 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 includesVariables()
          Return true if this is a template for shadow facts, and public variables were included when it was created.
 boolean isAllowedValue(java.lang.String slotName, Value slotValue)
           
 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 setSlotAllowedValues(java.lang.String slotName, ValueVector allowed)
           
 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.

Overrides:
equals in class java.lang.Object
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.

Overrides:
hashCode in class java.lang.Object
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:
The 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. You must wait until you've defined all the slots in a template before calling this; otherwise the behavior is undefined.

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,
                         java.lang.String typename)
                  throws JessException
Create a new multislot 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 - name of Jess data type for slot contents
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. The type parameter is currently stored with the deftemplate, but not used by Jess for any purpose. The slot type will be set to "ANY".

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.ANY)
Throws:
JessException - if anything goes wrong

getSlotDataType

public int getSlotDataType(java.lang.String slotName)
                    throws JessException
Returns the slot data type (one of the constants in jess.RU) for the named slot

Parameters:
slotName - The name of the slot
Returns:
The data type of that slot (RU.INTEGER, RU.SYMBOL, etc., or RU.ANY)
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

getSlotDefault

public Value getSlotDefault(java.lang.String slotName)
                     throws JessException
Returns the default value of a named slot

Parameters:
slotName - The name of the slot
Returns:
The default value for that slot (can be Funcall.NIL or Funcall.NILLIST for none)
Throws:
JessException - if anything goes wrong

getSlotAllowedValues

public ValueVector getSlotAllowedValues(java.lang.String slotName)
                                 throws JessException
Returns a list of allowed values for a slot. If any value is allowed, this method returns null.

Parameters:
slotName - the slot of interest
Returns:
a list of allowed values, or null if none are specified
Throws:
JessException

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.

Overrides:
toString in class java.lang.Object
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

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

includesVariables

public boolean includesVariables()
                          throws JessException
Return true if this is a template for shadow facts, and public variables were included when it was created.

Returns:
true if this is a shadow template with variables included *throws JessException if anything goes wrong
Throws:
JessException

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

setSlotAllowedValues

public void setSlotAllowedValues(java.lang.String slotName,
                                 ValueVector allowed)
                          throws JessException
Throws:
JessException

isAllowedValue

public boolean isAllowedValue(java.lang.String slotName,
                              Value slotValue)
                       throws JessException
Throws:
JessException

freeze

public void freeze(Rete engine)
            throws JessException
Complete the initialization of this template. In particular, this method will propagate inherited template properties to this child template. You generally don't have to call it, because Rete.addDeftemplate(Deftemplate) calls it automatically.

Throws:
JessException - if anything goes wrong

© 2013 Sandia Corporation