KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > function > SumType


1 /* @author rich
2  * Created on 18-Nov-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.xjep.function;
9
10 import java.util.Stack JavaDoc;
11
12 import org.nfunk.jep.*;
13 import org.nfunk.jep.function.*;
14
15 /**
16  * A sum function Sum(x^2,x,1,10) finds the sum of x^2 with x running from 1 to 10.
17  * Sum(x^2,x,1,10,2) calculates the 1^2+3^2+5^2+7^2+9^2 i.e. in steps of 2.
18  * @author Rich Morris
19  * Created on 10-Sept-2004
20  */

21 public abstract class SumType extends PostfixMathCommand implements SpecialEvaluationI {
22     /** The name of the function, use in error reporting. */
23     protected String JavaDoc name;
24
25     public SumType(String JavaDoc funName)
26     {
27         numberOfParameters = -1;
28         name = funName;
29     }
30
31     public SumType()
32     {
33         numberOfParameters = -1;
34     }
35
36     /**
37      * Evaluates the operator in given context. Typically does not need to be sub-classed as the other evaluate methods are more useful. This method just checks the arguments.
38      *
39      * @see org.nfunk.jep.function.SpecialEvaluationI#evaluate(org.nfunk.jep.Node, java.lang.Object, org.nfunk.jep.ParserVisitor, java.util.Stack, org.nfunk.jep.SymbolTable)
40      */

41     public Object JavaDoc evaluate(
42         Node node,
43         Object JavaDoc data,
44         ParserVisitor pv,
45         Stack JavaDoc stack)
46         throws ParseException {
47
48         int numParams = node.jjtGetNumChildren();
49         if(numParams < 4 || numParams > 5)
50             throw new ParseException(name+": called with invalid number of parameters: "+numParams+" it should be either 4 or 5.");
51             
52         Node varNode = node.jjtGetChild(1);
53         Variable var=null;
54         if(varNode instanceof ASTVarNode)
55             var = ((ASTVarNode) varNode).getVar();
56         else
57             throw new ParseException(name+": second argument should be a variable");
58             
59         node.jjtGetChild(2).jjtAccept(pv,data);
60         checkStack(stack); // check the stack
61
Object JavaDoc minObj = stack.pop();
62         double min;
63         if(minObj instanceof Number JavaDoc)
64             min = ((Number JavaDoc ) minObj).doubleValue();
65         else throw new ParseException(name+": third argument (min) should evaluate to a number it is "+minObj.toString());
66             
67         node.jjtGetChild(3).jjtAccept(pv,data);
68         checkStack(stack); // check the stack
69
Object JavaDoc maxObj = stack.pop();
70         double max;
71         if(maxObj instanceof Number JavaDoc)
72             max = ((Number JavaDoc ) maxObj).doubleValue();
73         else throw new ParseException(name+": forth argument (max) should evaluate to a number it is "+minObj.toString());
74
75         if(min>max) throw new ParseException(name+": min value should be smaller than max value they are "+min+" and "+max+".");
76         
77         if(numParams == 5)
78         {
79             node.jjtGetChild(3).jjtAccept(pv,data);
80             checkStack(stack); // check the stack
81
Object JavaDoc incObj = stack.pop();
82             double inc;
83             if(incObj instanceof Number JavaDoc)
84                 inc = ((Number JavaDoc ) incObj).doubleValue();
85             else throw new ParseException(name+": fifth argument (steps) should evaluate to a number it is "+minObj.toString());
86             
87             evaluate(node.jjtGetChild(0),var,min,max,inc,data,pv,stack);
88         }
89         else
90             evaluate(node,var,min,max,1.0,data,pv,stack);
91         return null;
92     }
93
94     /** Evaluates the node by repeatibly setting the value of the variable from min to max, and calculating the value of the first argument.
95      * Sub classes generally do not need to implement this method as
96      * @link evaluate(Object elements[])
97      * is more useful. If they do they should follow the pattern used here.
98      *
99      * @param node
100      * @param var
101      * @param min
102      * @param max
103      * @param inc
104      * @param data
105      * @param pv
106      * @param stack
107      * @return
108      * @throws ParseException
109      */

110
111     public Object JavaDoc evaluate(
112         Node node,
113         Variable var,
114         double min, double max, double inc,
115         Object JavaDoc data,
116         ParserVisitor pv,
117         Stack JavaDoc stack)
118         throws ParseException {
119             
120             int i=0;
121             double val;
122             Object JavaDoc[] res=new Object JavaDoc[(int) ((max-min)/inc)+1];
123             for(i=0,val=min;val<=max;++i,val=min+i*inc)
124             {
125                 var.setValue(new Double JavaDoc(val));
126                 
127                 node.jjtGetChild(0).jjtAccept(pv,data);
128                 checkStack(stack); // check the stack
129
res[i] = stack.pop();
130             }
131             Object JavaDoc ret = evaluate(res);
132             stack.push(ret);
133             return ret;
134         }
135         
136         /** Evaluates the function given the set of y values.
137          * For example for Sum(x^2,x,1,5) the function will be passed the array [1,4,9,16,25].
138          *
139          * @param elements the y values
140          * @return the result of the function
141          * @throws ParseException
142          */

143         public abstract Object JavaDoc evaluate(Object JavaDoc elements[]) throws ParseException;
144         
145     /**
146      * run method. Should not be called.
147      */

148     public void run(Stack JavaDoc s) throws ParseException {
149         throw new ParseException(name+": run method called should not normally happen.");
150     }
151
152 }
153
Popular Tags