org.fraid.interpreter
Class SimpleNode

java.lang.Object
  extended by org.netlib.math.complex.Complex
      extended by org.fraid.interpreter.SimpleNode
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, FraidTreeConstants, Node
Direct Known Subclasses:
ASTAdd, ASTAnd, ASTAssignment, ASTDivide, ASTEqual, ASTEvalLater, ASTExpressionToCalculate, ASTFunctionCall, ASTFunctionDef, ASTGreater, ASTGreaterOrEqual, ASTIfThenExpression, ASTImaginaryNumber, ASTMeta, ASTMultiply, ASTNegate, ASTNot, ASTNotEqual, ASTOr, ASTPower, ASTProcessSingleLine, ASTProcessStream, ASTRealNumber, ASTSmaller, ASTSmallerOrEqual, ASTString, ASTSubtract, ASTVariable, ASTXor

public class SimpleNode
extends Complex
implements Node, FraidTreeConstants

A SimpleNode is generated for each token found in the FrAid input stream. After the javacc/jjtree generated code hands over the parse tree, process() is called on the root after which the parse tree is linked and evaluated. If the operation is assignment the tree is trimmed and optimized and a new DefinedFunction is registered in the SymbolTable . SimpleNode inherits from Complex so complex numbers can be used interchangeably everywhere in the parser code. (Which could have been achieved by some sort of composition?)

See Also:
Serialized Form

Nested Class Summary
static class SimpleNode.FunctionEvaluationType
           
 
Field Summary
protected  Node[] children
          Automatically generated by javacc.
protected  int id
          Automatically generated by javacc.
protected  ComplexFunction mFunction
          If the type of token is Function (JJTFUNCTIONCALL = 27) mFunction holds it.
protected  SimpleNode.FunctionEvaluationType mFunctionEvalType
          How many arguments need to be provided for this tree to be evaluated.
protected  DefinedFunction mLastDefinedFunction
          A reference to the last defined function.
protected  SymbolTable mSymbTable
          A local reference to the symbol table.
protected  java.lang.String mText
          If the type of token is String (JJTSTRING = 23) mText holds it.
protected  java.lang.Integer mVarIndex
          If the type of token is function variable (JJTVARIABLE = 28) mVarIndex holds the index of this variable in the variable list passed to the function.
protected  Node parent
          Automatically generated by javacc.
protected  Fraid parser
          Automatically generated by javacc.
protected static java.lang.String sHiddenFunctionNamePrefix
           
protected static int sHiddenFunctionNextIndex
          Internal counter for forming hidden function names.
 
Fields inherited from class org.netlib.math.complex.Complex
AUTHOR, DATE, iii, im, re, REMARK, TWO_PI, VERSION
 
Fields inherited from interface org.fraid.interpreter.FraidTreeConstants
JJTADD, JJTAND, JJTASSIGNMENT, JJTDIVIDE, JJTEQUAL, JJTEXPRESSIONTOCALCULATE, JJTFUNCTIONCALL, JJTFUNCTIONDEF, JJTGREATER, JJTGREATEROREQUAL, JJTIFTHENEXPRESSION, JJTIMAGINARYNUMBER, JJTMETA, JJTMULTIPLY, JJTNEGATE, jjtNodeName, JJTNOT, JJTNOTEQUAL, JJTOR, JJTPOWER, JJTPROCESSSINGLELINE, JJTPROCESSSTREAM, JJTREALNUMBER, JJTSMALLER, JJTSMALLEROREQUAL, JJTSTRING, JJTSUBTRACT, JJTVARIABLE, JJTVOID, JJTXOR
 
Constructor Summary
SimpleNode(Complex aC)
          Create a node of type JJTIMAGINARYNUMBER.
SimpleNode(ComplexFunction aFunction)
          Create a node of type JJTFUNCTIONCALL.
SimpleNode(double aReal, double aImaginary)
           
SimpleNode(Fraid p, int i)
          Create a node from the given type and with the given parser.
SimpleNode(int i)
          Create a node of the given type
SimpleNode(SimpleNode aNode)
          Copy constructor.
SimpleNode(java.lang.String aString)
          Create a node of type JJTSTRING.
 
Method Summary
protected  void _copy(SimpleNode aNode)
           
protected static void addArgumentNodesToMeta(SimpleNode aNode)
           
 void addChangeListener(javax.swing.event.ChangeListener aListener)
          Adds a change listener to this node.
protected static int childrenCountMissingArgs(SimpleNode aNode)
           
protected  void clean()
          The code executed before an eval takes place.
 java.lang.Object clone()
          Local implementation of the clone() method.
protected static void connectGeneratedF(DefinedFunction aGf, DefinedFunction aDf)
           
protected static boolean containsMeta(SimpleNode aNode)
           
static SimpleNode copyFrAidRelevantData(Complex aComplex)
           
static int countMissingArgs(SimpleNode aNode)
           
protected static java.util.HashMap<java.lang.String,java.lang.Integer> createArgumentList(SimpleNode aSignatureNode)
           
static void createSymbTableEntry(SimpleNode aNode)
          This is the main processor of the assignment operator (function/variable definition) in FrAid.
 java.lang.String dumpString(java.util.HashMap aArgs)
          Helper to dumpTree(java.lang.String) and toString()
 java.lang.String dumpTree(java.lang.String aPrefix)
          Dumps the parser generated expression tree of which this node is a root.
 void ensureUsable()
          Makes shure the node is usable outside of the interpreter.
static Complex eval(SimpleNode aNode, Complex[] aArgs)
          Each node in the expression tree evaluates to something for which this method is responsible.
static Complex evalFunction(SimpleNode aNode, Complex[] aArgs)
           
static SimpleNode evalGeneratedF(SimpleNode aNode)
          If the right hand side of an assignment operator is a org.fraid.complex.GeneratorFunction this method evaluates it so createSymbTableEntry(org.fraid.interpreter.SimpleNode) can create a function with the given signature.
 DefinedFunction findSampled()
          If this node is of type JJTFUNCTIONCALL go recursively through the children and find if any of them is a sampled function.
protected static java.lang.String generateHiddenFunctionName()
           
 void getDependancies(java.util.Vector<ComplexFunction> aVector, java.util.HashSet<ComplexFunction> aAllreadyPassed)
          Get all the functions this node depends on (is defined using).
 ComplexFunction getFunction()
          Getter for the mFunction member.
 int getId()
           
protected static SimpleNode getNextGeneratedF(SimpleNode aNode)
           
protected static SimpleNode getNextGraphicsF(SimpleNode aNode)
           
static int getParentIndexOfSelf(SimpleNode aNode)
           
 java.lang.String getText()
          Getter for the mText member
 void jjtAddChild(Node n, int i)
          Automatically generated by javacc.
 void jjtClose()
          Automatically generated by javacc.
 Node jjtGetChild(int i)
          Automatically generated by javacc.
 int jjtGetNumChildren()
          Automatically generated by javacc.
 Node jjtGetParent()
          Automatically generated by javacc.
 void jjtOpen()
          Automatically generated by javacc.
 void jjtSetParent(Node n)
          Automatically generated by javacc.
static void linkNode(SimpleNode aNode, java.util.HashMap<java.lang.String,java.lang.Integer> aEnvironment)
          The main routine to link the nodes in a tree to their respective values/functions/variables.
protected static void node2Function(SimpleNode aNode, java.util.HashMap<java.lang.String,java.lang.Integer> aEnvironment)
           
protected  void prelinkFunctionOrVariableNode(java.util.HashMap<java.lang.String,java.lang.Integer> aEnvironment)
          This function will prepare a node if it is a variable or a function.
protected static void preventMetaFromNesting(SimpleNode aNode)
           
protected static void printTreeProcessingInfo(SimpleNode aNode, java.lang.String aDescription)
           
protected static void printTreeProcessingInfo(java.lang.String aContent, java.lang.String aDescription)
           
 Complex process()
          After the parser builds the parse tree it calls process on the root (so process is called only once per expression).
protected static void pruneComputable(SimpleNode aNode)
           
 int removeChangeListener(javax.swing.event.ChangeListener aListener)
          Removes a listener.
protected static void setFunctionsEvalMode(SimpleNode aNode)
           
 void setText(java.lang.String text)
          Setter for the mText member.
 java.lang.String toString()
          Selfexplanatory.
static int treeDepthFromNode(SimpleNode aNode)
           
 
Methods inherited from class org.netlib.math.complex.Complex
abs, acos, acosh, add, arg, asin, asinh, atan, atanh, cart, conj, cos, cosec, cosh, cot, div, equals, exp, im, isInfinite, isNaN, log, main, mul, neg, norm, polar, pow, pow, pow, pow, re, real, scale, sec, sin, sinh, sqrt, sub, tan, tanh
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

parent

protected Node parent
Automatically generated by javacc.


children

protected Node[] children
Automatically generated by javacc.


id

protected int id
Automatically generated by javacc. The id of the node specifies the type of token it represents (derived from the grammar).


parser

protected Fraid parser
Automatically generated by javacc.


mSymbTable

protected SymbolTable mSymbTable
A local reference to the symbol table.


mLastDefinedFunction

protected DefinedFunction mLastDefinedFunction
A reference to the last defined function. Used by the scripting interface.


mText

protected java.lang.String mText
If the type of token is String (JJTSTRING = 23) mText holds it.


mFunction

protected ComplexFunction mFunction
If the type of token is Function (JJTFUNCTIONCALL = 27) mFunction holds it.


mVarIndex

protected java.lang.Integer mVarIndex
If the type of token is function variable (JJTVARIABLE = 28) mVarIndex holds the index of this variable in the variable list passed to the function.


mFunctionEvalType

protected SimpleNode.FunctionEvaluationType mFunctionEvalType
How many arguments need to be provided for this tree to be evaluated.


sHiddenFunctionNextIndex

protected static volatile int sHiddenFunctionNextIndex
Internal counter for forming hidden function names.


sHiddenFunctionNamePrefix

protected static final java.lang.String sHiddenFunctionNamePrefix
See Also:
Constant Field Values
Constructor Detail

SimpleNode

public SimpleNode(int i)
Create a node of the given type

Parameters:
i - node type
See Also:
#FraidTreeConstants

SimpleNode

public SimpleNode(Fraid p,
                  int i)
Create a node from the given type and with the given parser.

Parameters:
p - parser
i - node type
See Also:
#FraidTreeConstants

SimpleNode

public SimpleNode(Complex aC)
Create a node of type JJTIMAGINARYNUMBER.

Parameters:
aC - value for the node

SimpleNode

public SimpleNode(double aReal,
                  double aImaginary)

SimpleNode

public SimpleNode(java.lang.String aString)
Create a node of type JJTSTRING.

Parameters:
aString - value for the node.

SimpleNode

public SimpleNode(ComplexFunction aFunction)
           throws ComplexFunctionException
Create a node of type JJTFUNCTIONCALL.

Parameters:
aFunction - value for the node.
Throws:
ComplexFunctionException

SimpleNode

public SimpleNode(SimpleNode aNode)
Copy constructor. Makes a shallow copy...

Parameters:
aNode - value for the node.
Method Detail

setText

public void setText(java.lang.String text)
Setter for the mText member.

Parameters:
text - text

getText

public java.lang.String getText()
Getter for the mText member

Returns:
the text value

getFunction

public ComplexFunction getFunction()
Getter for the mFunction member.

Returns:
the function

_copy

protected void _copy(SimpleNode aNode)

copyFrAidRelevantData

public static SimpleNode copyFrAidRelevantData(Complex aComplex)

clone

public java.lang.Object clone()
Local implementation of the clone() method.

Overrides:
clone in class Complex
Returns:
a deep copy of this object
See Also:
Cloneable, Object.clone()

jjtOpen

public void jjtOpen()
Automatically generated by javacc.

Specified by:
jjtOpen in interface Node

jjtClose

public void jjtClose()
Automatically generated by javacc.

Specified by:
jjtClose in interface Node

jjtSetParent

public void jjtSetParent(Node n)
Automatically generated by javacc.

Specified by:
jjtSetParent in interface Node
Parameters:
n - a parent node

jjtGetParent

public Node jjtGetParent()
Automatically generated by javacc.

Specified by:
jjtGetParent in interface Node
Returns:
the parent

jjtAddChild

public void jjtAddChild(Node n,
                        int i)
Automatically generated by javacc. Add child n at position i.

Specified by:
jjtAddChild in interface Node
Parameters:
n - child to add
i - position in the list

jjtGetChild

public Node jjtGetChild(int i)
Automatically generated by javacc.

Specified by:
jjtGetChild in interface Node
Parameters:
i - index
Returns:
the child at position i

jjtGetNumChildren

public int jjtGetNumChildren()
Automatically generated by javacc.

Specified by:
jjtGetNumChildren in interface Node
Returns:
the number of children

toString

public java.lang.String toString()
Selfexplanatory.

Overrides:
toString in class Complex
Returns:
a string representation of the node.
See Also:
Complex.cart(double, double)

getId

public int getId()

clean

protected void clean()
The code executed before an eval takes place.


ensureUsable

public void ensureUsable()
                  throws SemanticException
Makes shure the node is usable outside of the interpreter. Only tokens of type JJTIMAGINARYNUMBER, JJTSTRING and JJTFUNCTIONCALL generate usable nodes.

Throws:
SemanticException - if the node is not usable outside of the interpreter

dumpTree

public java.lang.String dumpTree(java.lang.String aPrefix)
Dumps the parser generated expression tree of which this node is a root. Used by the dumpTree() FrAid function.

Parameters:
aPrefix - step before each level
Returns:
a string representation of the tree
See Also:
org.fraid.utils.functions.dumpTree

findSampled

public DefinedFunction findSampled()
If this node is of type JJTFUNCTIONCALL go recursively through the children and find if any of them is a sampled function.

Returns:
The first sampled DefineFunction found in the tree else null.

dumpString

public java.lang.String dumpString(java.util.HashMap aArgs)
Helper to dumpTree(java.lang.String) and toString()

Parameters:
aArgs - null or argInfo#DefinedFunction
Returns:
string representation of this node

getDependancies

public void getDependancies(java.util.Vector<ComplexFunction> aVector,
                            java.util.HashSet<ComplexFunction> aAllreadyPassed)
Get all the functions this node depends on (is defined using). This takes care of the SimpleNode tree portion, for the rest see GeneratorFunction.getDependancies(...).

Parameters:
aVector - a vector containing all the functions this node depends on (output value)
aAllreadyPassed - helper - a hash set of the already processed functions (to prevent duplicates in the output list)

addChangeListener

public void addChangeListener(javax.swing.event.ChangeListener aListener)
Adds a change listener to this node.

Parameters:
aListener - the listener to add

removeChangeListener

public int removeChangeListener(javax.swing.event.ChangeListener aListener)
Removes a listener.

Parameters:
aListener - the listener to remove
Returns:
the removed listener

process

public Complex process()
                throws java.lang.Exception
After the parser builds the parse tree it calls process on the root (so process is called only once per expression). process traverses the expression tree recursively linking and evaluating the expression.

Returns:
The result of the processing of this node.
Throws:
java.lang.Exception - needs to be able to throw any posible exception comming from the underlying processing

printTreeProcessingInfo

protected static void printTreeProcessingInfo(SimpleNode aNode,
                                              java.lang.String aDescription)

printTreeProcessingInfo

protected static void printTreeProcessingInfo(java.lang.String aContent,
                                              java.lang.String aDescription)

eval

public static Complex eval(SimpleNode aNode,
                           Complex[] aArgs)
                    throws java.lang.Exception
Each node in the expression tree evaluates to something for which this method is responsible.

Parameters:
aArgs - parameters for the evaluation
Returns:
the result
Throws:
java.lang.Exception - channels what may go wrong during the evaluation

evalFunction

public static Complex evalFunction(SimpleNode aNode,
                                   Complex[] aArgs)
                            throws java.lang.Exception
Throws:
java.lang.Exception

linkNode

public static void linkNode(SimpleNode aNode,
                            java.util.HashMap<java.lang.String,java.lang.Integer> aEnvironment)
                     throws java.lang.Exception
The main routine to link the nodes in a tree to their respective values/functions/variables.

Parameters:
aEnvironment - the defined so far variables
Throws:
java.lang.Exception - if something goes wrong

prelinkFunctionOrVariableNode

protected void prelinkFunctionOrVariableNode(java.util.HashMap<java.lang.String,java.lang.Integer> aEnvironment)
                                      throws SemanticException
This function will prepare a node if it is a variable or a function. It will not link function's args though (see finishLinkFunction).

Parameters:
aEnvironment - the environment to check if this is a proper variable
Throws:
SemanticException - Throws when the node is marked as a function or a variable but something is wrong. If this function returns and the node is a function or a variable they will be set properly.

createSymbTableEntry

public static void createSymbTableEntry(SimpleNode aNode)
                                 throws java.lang.Exception
This is the main processor of the assignment operator (function/variable definition) in FrAid.

Throws:
java.lang.Exception - channels any exception from bellow if something goes wrong

setFunctionsEvalMode

protected static void setFunctionsEvalMode(SimpleNode aNode)

treeDepthFromNode

public static int treeDepthFromNode(SimpleNode aNode)

getParentIndexOfSelf

public static int getParentIndexOfSelf(SimpleNode aNode)
                                throws SemanticException
Throws:
SemanticException

node2Function

protected static void node2Function(SimpleNode aNode,
                                    java.util.HashMap<java.lang.String,java.lang.Integer> aEnvironment)
                             throws SemanticException,
                                    ComplexFunctionException,
                                    java.lang.Exception
Throws:
SemanticException
ComplexFunctionException
java.lang.Exception

addArgumentNodesToMeta

protected static void addArgumentNodesToMeta(SimpleNode aNode)

pruneComputable

protected static void pruneComputable(SimpleNode aNode)
                               throws java.lang.Exception
Throws:
java.lang.Exception

getNextGeneratedF

protected static SimpleNode getNextGeneratedF(SimpleNode aNode)

getNextGraphicsF

protected static SimpleNode getNextGraphicsF(SimpleNode aNode)

containsMeta

protected static boolean containsMeta(SimpleNode aNode)
                               throws SemanticException
Throws:
SemanticException

preventMetaFromNesting

protected static void preventMetaFromNesting(SimpleNode aNode)
                                      throws SemanticException
Throws:
SemanticException

countMissingArgs

public static int countMissingArgs(SimpleNode aNode)
                            throws SemanticException
Throws:
SemanticException

childrenCountMissingArgs

protected static int childrenCountMissingArgs(SimpleNode aNode)
                                       throws SemanticException
Throws:
SemanticException

createArgumentList

protected static java.util.HashMap<java.lang.String,java.lang.Integer> createArgumentList(SimpleNode aSignatureNode)
                                                                                   throws SemanticException
Throws:
SemanticException

connectGeneratedF

protected static void connectGeneratedF(DefinedFunction aGf,
                                        DefinedFunction aDf)
                                 throws ComplexFunctionException,
                                        SemanticException,
                                        java.lang.Exception
Throws:
ComplexFunctionException
SemanticException
java.lang.Exception

evalGeneratedF

public static SimpleNode evalGeneratedF(SimpleNode aNode)
                                 throws java.lang.Exception
If the right hand side of an assignment operator is a org.fraid.complex.GeneratorFunction this method evaluates it so createSymbTableEntry(org.fraid.interpreter.SimpleNode) can create a function with the given signature. eval could be used here but with the price of one more if(aNode.mFunction instanceof GeneratorFunction) in there

Returns:
the new function returned by GeneratorFunction.eval
Throws:
java.lang.Exception - if something goes wrong

generateHiddenFunctionName

protected static java.lang.String generateHiddenFunctionName()


Fraid (system and language). (C) 2003-2007 Ivaylo Iliev
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.