KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > djep > DPrintVisitor


1 /* @author rich
2  * Created on 26-Feb-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.djep;
9 import org.nfunk.jep.*;
10 import org.lsmp.djep.xjep.PrintVisitor;
11
12 /**
13  * An extension of PrintVisitor which will print the equations of a variable if required.
14  * The behavious of this class is determined by two modes
15  * PRINT_PARTIAL_EQNS and PRINT_VARIABLE_EQNS.
16  * When a variable or partial derivative is encountered then
17  * its equation may be printed.
18  * By default equations for PartialDerivatives are printed
19  * but equations for normal derivatives are not.
20  * TODO might want to print eqn for y=sin(x) but not x=3
21  *
22  * @author Rich Morris
23  * Created on 26-Feb-2004
24  */

25 public class DPrintVisitor extends PrintVisitor {
26     public static final int PRINT_PARTIAL_EQNS = 16;
27     public static final int PRINT_VARIABLE_EQNS = 32;
28     
29     /**
30      *
31      */

32     public DPrintVisitor() {
33         super();
34         setMode(PRINT_PARTIAL_EQNS,true);
35     }
36
37     /** Prints the variable or its equation.
38      * Depends on the statr of the flags and whether the variable has an equation.
39      */

40     public Object JavaDoc visit(ASTVarNode node, Object JavaDoc data) throws ParseException
41     {
42         Variable var = node.getVar();
43         if(var instanceof PartialDerivative)
44         {
45             PartialDerivative deriv = (PartialDerivative) var;
46             if(((mode & PRINT_PARTIAL_EQNS)!=0) && deriv.hasEquation())
47                 deriv.getEquation().jjtAccept(this,null);
48             else
49                 sb.append(node.getName());
50         }
51         else if(var instanceof DVariable)
52         {
53             DVariable dvar = (DVariable) var;
54             if(((mode & PRINT_VARIABLE_EQNS)!=0) && dvar.hasEquation())
55                 dvar.getEquation().jjtAccept(this,null);
56             else
57                 sb.append(node.getName());
58         }
59         else
60             sb.append(node.getName());
61
62       return data;
63     }
64 }
65
Popular Tags