Expression.java

Go to the documentation of this file.
00001 package com.graphbuilder.math;
00002 
00003 import com.graphbuilder.struc.Bag;
00004 
00013 public abstract class Expression {
00014 
00015     protected Expression parent = null;
00016 
00020     public abstract double eval(VarMap v, FuncMap f);
00021 
00026     public boolean isDescendent(Expression x) {
00027         Expression y = this;
00028 
00029         while (y != null) {
00030             if (y == x)
00031                 return true;
00032             y = y.parent;
00033         }
00034 
00035         return false;
00036     }
00037 
00041     public Expression getParent() {
00042         return parent;
00043     }
00044 
00051     protected void checkBeforeAccept(Expression x) {
00052         if (x == null)
00053             throw new IllegalArgumentException("expression cannot be null");
00054 
00055         if (x.parent != null)
00056             throw new IllegalArgumentException("expression must be removed parent");
00057 
00058         if (isDescendent(x))
00059             throw new IllegalArgumentException("cyclic reference");
00060     }
00061 
00065     public String[] getVariableNames() {
00066         return getTermNames(true);
00067     }
00068 
00072     public String[] getFunctionNames() {
00073         return getTermNames(false);
00074     }
00075 
00076     private String[] getTermNames(boolean varNames) {
00077         Bag b = new Bag();
00078         getTermNames(this, b, varNames);
00079         String[] arr = new String[b.size()];
00080         for (int i = 0; i < arr.length; i++)
00081             arr[i] = (String) b.get(i);
00082         return arr;
00083     }
00084 
00085     private static void getTermNames(Expression x, Bag b, boolean varNames) {
00086         if (x instanceof OpNode) {
00087             OpNode o = (OpNode) x;
00088             getTermNames(o.leftChild, b, varNames);
00089             getTermNames(o.rightChild, b, varNames);
00090         }
00091         else if (x instanceof VarNode) {
00092             if (varNames) {
00093                 VarNode v = (VarNode) x;
00094                 if (!b.contains(v.name))
00095                     b.add(v.name);
00096             }
00097         }
00098         else if (x instanceof FuncNode) {
00099             FuncNode f = (FuncNode) x;
00100 
00101             if (!varNames) {
00102                 if (!b.contains(f.name))
00103                     b.add(f.name);
00104             }
00105 
00106             for (int i = 0; i < f.numChildren(); i++)
00107                 getTermNames(f.child(i), b, varNames);
00108         }
00109     }
00110 
00114     public String toString() {
00115         StringBuffer sb = new StringBuffer();
00116         toString(this, sb);
00117         return sb.toString();
00118     }
00119 
00120     private static void toString(Expression x, StringBuffer sb) {
00121         if (x instanceof OpNode) {
00122             OpNode o = (OpNode) x;
00123             sb.append("(");
00124             toString(o.leftChild, sb);
00125             sb.append(o.getSymbol());
00126             toString(o.rightChild, sb);
00127             sb.append(")");
00128         }
00129         else if (x instanceof TermNode) {
00130             TermNode t = (TermNode) x;
00131 
00132             if (t.getNegate()) {
00133                 sb.append("(");
00134                 sb.append("-");
00135             }
00136 
00137             sb.append(t.getName());
00138 
00139             if (t instanceof FuncNode) {
00140                 FuncNode f = (FuncNode) t;
00141 
00142                 sb.append("(");
00143 
00144                 if (f.numChildren() > 0)
00145                     toString(f.child(0), sb);
00146 
00147                 for (int i = 1; i < f.numChildren(); i++) {
00148                     sb.append(", ");
00149                     toString(f.child(i), sb);
00150                 }
00151 
00152                 sb.append(")");
00153             }
00154 
00155             if (t.getNegate())
00156                 sb.append(")");
00157         }
00158         else if (x instanceof ValNode) {
00159             sb.append(((ValNode) x).val);
00160         }
00161     }
00162 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1