jess
Class Token

java.lang.Object
  extended by jess.Token
All Implemented Interfaces:
java.io.Serializable

public class Token
extends java.lang.Object
implements java.io.Serializable

A Token is the fundamental unit of communication in the Rete network. Each Token represents one or more facts and an indication of whether those facts are being asserted or being retracted.

The Token class is used to represent partial matches in the Rete network. You'll use it if you're writing an Accelerator (not documented here), if you're working with old-style queries, or possibly if you're writing a Strategy implementation.

(C) 2013 Sandia Corporation

See Also:
Accelerator, Strategy, Serialized Form

Constructor Summary
Token(Fact firstFact)
          Construct a token containing a single Fact.
Token(Token t)
          Create a new Token identical to an old one
Token(Token t, Fact newFact)
          Create a new Token containing the same data as an old one
Token(Token lt, Token rt)
          Create a new Token containing the same data as an old one
 
Method Summary
 boolean dataEquals(Token t)
          Compare the data in this token to another token.
 boolean equals(java.lang.Object o)
          Compare this token to another object.
 Fact fact(int i)
          Return a fact from this token.
 boolean fastDataEquals(Token t)
           
 int getTime()
          Returns the largest pseudotime recorded in any of the facts in this Token.
 int getTotalTime()
          Returns the sum of the pseudotimes recorded in all of the facts in this Token.
 int hashCode()
          Use the sortcode, based on the contained facts, as the hashcode.
 Token prepare(boolean b)
          A chance for a token to duplicate itself Used by extensions
 int size()
          Returns the number of facts in this token
 Fact topFact()
          Return the last fact in the Token (the "most recent" one).
 java.lang.String toString()
          Return a string (useful for debugging) describing this token.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Token

public Token(Fact firstFact)
      throws JessException
Construct a token containing a single Fact.

Throws:
JessException

Token

public Token(Token t,
             Fact newFact)
      throws JessException
Create a new Token containing the same data as an old one

Throws:
JessException

Token

public Token(Token lt,
             Token rt)
      throws JessException
Create a new Token containing the same data as an old one

Throws:
JessException

Token

public Token(Token t)
      throws JessException
Create a new Token identical to an old one

Throws:
JessException
Method Detail

topFact

public final Fact topFact()
Return the last fact in the Token (the "most recent" one). This may not be a Fact from working memory, but rather a "view" of a working memory element. You can call getIcon() on the returned fact to get the canonical working memory element corresponding to the returned fact.

Returns:
the fact
See Also:
fact(int)

fact

public final Fact fact(int i)
Return a fact from this token. This may not be a Fact from working memory, but rather a "view" of a working memory element. You can call getIcon() on the returned fact to get the canonical working memory element corresponding to the returned fact.

Parameters:
i - the index (0-based) of the fact to retrieve. The fact that matches the first pattern on the LHS of a corresponding rule is fact(0); the next is fact(1), etc.
Returns:
the fact
See Also:
Fact.getIcon()

size

public final int size()
Returns the number of facts in this token

Returns:
the size

getTime

public int getTime()
Returns the largest pseudotime recorded in any of the facts in this Token. The rule engine keeps track of a monotonically increasing quantity called pseudotime; facts asserted later will have larger pseudotime values. You can use pseudotime to order activations when writing a Strategy implementation.

Returns:
the largest pseudotime of any fact in this Token.
See Also:
Strategy

getTotalTime

public int getTotalTime()
Returns the sum of the pseudotimes recorded in all of the facts in this Token. The rule engine keeps track of a monotonically increasing quantity called pseudotime; facts asserted later will have larger pseudotime values. You can use pseudotime to order activations when writing a Strategy implementation.

Returns:
the sum of all pseudotimes of the facts in this Token.
See Also:
Strategy

dataEquals

public final boolean dataEquals(Token t)
Compare the data in this token to another token. The tokens are assumed to be of the same size (same number of facts). We have to compare all the fact data if the fact IDs are the same, since each fact can exist in different multifield versions. This could be skipped if we had a fast test for multislot existence...

Parameters:
t - Another token to compare to
Returns:
True if the tokens represent the same list of facts (tags are irrelevant)

fastDataEquals

public final boolean fastDataEquals(Token t)

equals

public boolean equals(java.lang.Object o)
Compare this token to another object.

Overrides:
equals in class java.lang.Object
Parameters:
o - Another object to compare to
Returns:
True if the object is a Token and dataEquals returns true.

toString

public java.lang.String toString()
Return a string (useful for debugging) describing this token.

Overrides:
toString in class java.lang.Object
Returns:
The formatted String

hashCode

public int hashCode()
Use the sortcode, based on the contained facts, as the hashcode.

Overrides:
hashCode in class java.lang.Object
Returns:
A semi-unique identifier

prepare

public Token prepare(boolean b)
              throws JessException
A chance for a token to duplicate itself Used by extensions

Parameters:
b - true if this token will represent test success
Returns:
this token, or possibly a duplicate, modified token
Throws:
JessException - if anything goes wrong

© 2013 Sandia Corporation