KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* @author rich
2  * Created on 10-Sep-2004
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  * @author Rich Morris
17  * Created on 10-Sep-2004
18  */

19 public class MinArg extends SumType {
20
21     static Comparative comp = new Comparative(Comparative.LE);
22
23     public MinArg() {
24         super("MinArg");
25     }
26
27     public Object JavaDoc evaluate(Object JavaDoc[] elements) throws ParseException {
28         throw new ParseException("MinArg: call to evaluate(Object[] elements) should not have happened.");
29     }
30
31     public Object JavaDoc evaluate(
32         Node node,
33         Variable var,
34         double min,
35         double max,
36         double inc,
37         Object JavaDoc data,
38         ParserVisitor pv,
39         Stack JavaDoc stack)
40         throws ParseException {
41
42             int i=0;
43             double val;
44             Object JavaDoc minVal=null;
45             Object JavaDoc minArg=null;
46             for(i=0,val=min;val<=max;++i,val=min+i*inc)
47             {
48                 Object JavaDoc curArg = new Double JavaDoc(val);
49                 var.setValue(curArg);
50                 
51                 node.jjtGetChild(0).jjtAccept(pv,data);
52                 checkStack(stack); // check the stack
53
Object JavaDoc res = stack.pop();
54                 if(i==0)
55                 {
56                     minVal = res;
57                     minArg = curArg;
58                 }
59                 else if(comp.lt(res,minVal))
60                 {
61                     minVal = res;
62                     minArg = curArg;
63                 }
64             }
65             stack.push(minArg);
66             return minArg;
67
68     }
69
70 }
71
Popular Tags