jess
Class Context

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

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

A Context represents a scope in which variables can be declared. It also holds a pointer to a Rete object in which code can be executed.

You can use getVariable(java.lang.String) and setVariable(java.lang.String, jess.Value) to get and change the value of a variable from Java code, respectively.

The function getEngine() gives any Userfunction access to the Rete object in which it is executing. When a Userfunction is called, a Context argument is passed in as the final argument. You should pass this jess.Context to any methods you call that expect a Context argument themselves.

(C) 2007 Sandia National Laboratories

See Also:
Serialized Form

Constructor Summary
Context(Context c)
          Create a new context subordinate to an existing one.
Context(Context c, Rete engine)
          Create a new context subordinate to an existing one.
 
Method Summary
 void clearReturnValue()
          Clear the return flag and return value for this context.
 Value getBindingVariable(java.lang.String varName)
          Return the value represented by a variable on the LHS of a rule; works only during pattern matching.
 Rete getEngine()
          Returns the Rete engine associated with this context.
 Fact getFact()
          If this context represents a join network node from a rule LHS, this will return the right input of the node.
 Funcall getFuncall()
          Return the currently executing Funcall object.
 LineNumberRecord getLineNumberRecord()
          Return information about the currently executing function.
 jess.LogicalNode getLogicalSupportNode()
          If this context represents the RHS of a rule which is firing, and the LHS of the rule has provided logical support, this method will return the LogicalNode that lends support.
 Context getParent()
          Return the parent of this context.
 Value getReturnValue()
          Get the value set via setReturnValue
 java.util.Stack getStackData()
          Returns some information about the runtime stack.
 Token getToken()
          If this context represents a join network node from a rule LHS, this will return the left input of the node.
 Value getVariable(java.lang.String name)
          Get the value of a variable
 java.util.Iterator getVariableNames()
          Returns an iterator over the names of all the variables defined in this context.
 boolean isVariableDefined(java.lang.String name)
          Report whether a variable has a value in this context
 Context pop()
          Pop this context off the execution stack.
 void popStackFrame(Funcall funcall)
          Remove stack information.
 Context push()
          Create and return a new context subordinate to this one.
 void pushStackFrame(Funcall funcall)
          Store stack information.
 void removeVariable(java.lang.String name)
          Undefine any variable by the given name in this context or any parent context.
 boolean returning()
          Returns true if the return flag has been set in this context.
 void setExistingVariable(java.lang.String name, Value value)
          Set an existing variable in an ancestor of this context to a value; if no existing variable, value is set in this context.
 Value setReturnValue(Value val)
          Set the return flag to true, and supply a value to be returned.
 void setVariable(java.lang.String name, Value value)
          Set a variable in this context to some type and value.
 java.lang.String toString()
          Returns a useful debug representation of the context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Context

public Context(Context c)
Create a new context subordinate to an existing one. The method getEngine() will return c.getEngine(). Use push() instead of calling this directly.

Parameters:
c - The parent for the new context
See Also:
push()

Context

public Context(Context c,
               Rete engine)
Create a new context subordinate to an existing one. The method getEngine() will return the given Rete object.

Parameters:
c - The parent for the new context
engine - The value to be returned from getEngine
Method Detail

getToken

public final Token getToken()
If this context represents a join network node from a rule LHS, this will return the left input of the node.

Returns:
The Token

getFact

public final Fact getFact()
If this context represents a join network node from a rule LHS, this will return the right input of the node.

Returns:
The Fact object

getLogicalSupportNode

public final jess.LogicalNode getLogicalSupportNode()
If this context represents the RHS of a rule which is firing, and the LHS of the rule has provided logical support, this method will return the LogicalNode that lends support.

Returns:
The supporting node.

returning

public final boolean returning()
Returns true if the return flag has been set in this context. The Jess (return) function sets the return flag in the local context.

Returns:
The value of the return flag

setReturnValue

public final Value setReturnValue(Value val)
Set the return flag to true, and supply a value to be returned.

Parameters:
val - The value that should be returned from this context
Returns:
The argument

getReturnValue

public final Value getReturnValue()
Get the value set via setReturnValue

Returns:
The return value

clearReturnValue

public final void clearReturnValue()
Clear the return flag and return value for this context.


getVariableNames

public java.util.Iterator getVariableNames()
Returns an iterator over the names of all the variables defined in this context.

Returns:
the iterator

getEngine

public final Rete getEngine()
Returns the Rete engine associated with this context.

Returns:
The engine to use with this context

push

public Context push()
Create and return a new context subordinate to this one.

Returns:
The next context

pop

public Context pop()
Pop this context off the execution stack. If this context has no parent, just return this context. If it has a parent, transfer the values of the return flag and the return value to the parent, then return the parent.

Returns:
The context as described

getParent

public Context getParent()
Return the parent of this context. The parent represents the stack frame "below" this one.

Returns:
the parent context, or null if none

getVariable

public Value getVariable(java.lang.String name)
                  throws JessException
Get the value of a variable

Parameters:
name - The name of the variable with no leading '?' or '$' characters
Throws:
JessException - If the variable is undefined

isVariableDefined

public boolean isVariableDefined(java.lang.String name)
Report whether a variable has a value in this context

Parameters:
name - The name of the variable with no leading '?' or '$' characters

setVariable

public void setVariable(java.lang.String name,
                        Value value)
                 throws JessException
Set a variable in this context to some type and value. Defglobals are always set in the global context.

Parameters:
name - Name of the variable
value - The value of the variable
Throws:
JessException

setExistingVariable

public void setExistingVariable(java.lang.String name,
                                Value value)
                         throws JessException
Set an existing variable in an ancestor of this context to a value; if no existing variable, value is set in this context.

Parameters:
name - Name of the variable
value - The value of the variable
Throws:
JessException

pushStackFrame

public void pushStackFrame(Funcall funcall)
                    throws JessException
Store stack information. Used by the JessDE debugger. Won't be useful to any other clients.

Parameters:
funcall - a function call about to be invoked
Throws:
JessException - if anything goes wrong

getStackData

public java.util.Stack getStackData()
                             throws JessException
Returns some information about the runtime stack. Only works in debug mode. Used by the JessDE implementation, but won't be useful to other clients.

Returns:
information about the runtime stack
Throws:
JessException

popStackFrame

public void popStackFrame(Funcall funcall)
                   throws JessException
Remove stack information. Used by the JessDE debugger. Won't be useful to any other clients.

Parameters:
funcall - a function call that has just been invoked
Throws:
JessException - if anything goes wrong

toString

public java.lang.String toString()
Returns a useful debug representation of the context.

Overrides:
toString in class java.lang.Object
Returns:
A string with information about this context.

getFuncall

public Funcall getFuncall()
                   throws JessException
Return the currently executing Funcall object. Only works in debug mode. Used by the JessDE implementation.

Returns:
the Funcall being executed.
Throws:
JessException - if anything goes wrong.

getLineNumberRecord

public LineNumberRecord getLineNumberRecord()
                                     throws JessException
Return information about the currently executing function. Only works in debug mode. Used by the JessDE implementation.

Returns:
information about the currently executing function
Throws:
JessException - if anything goes wrong

getBindingVariable

public Value getBindingVariable(java.lang.String varName)
                         throws JessException
Return the value represented by a variable on the LHS of a rule; works only during pattern matching. Used internally.

Parameters:
varName - the name of a variable
Returns:
the value of the variable
Throws:
JessException - if anything goes wrong.

removeVariable

public void removeVariable(java.lang.String name)
Undefine any variable by the given name in this context or any parent context.

Parameters:
name - the name of the variable to remove.

© 2013 Sandia Corporation