Back to index

wims  3.65+svn20090927
ExpressionString.java
Go to the documentation of this file.
00001 /*
00002  * Created on 13.03.2006
00003  */
00004 package rene.zirkel.expression;
00005 
00006 import java.util.Enumeration;
00007 
00008 import rene.util.MyVector;
00009 import rene.zirkel.objects.ConstructionObject;
00010 
00011 class EquationExpressionString
00012 {      public Expression E;
00013        public EquationExpressionString (Expression o)
00014        {      E=o;
00015        }
00016        public String toString ()
00017        {      return E.toString(); 
00018        }
00019        public String getEquation ()
00020        {      ConstructionObject o=E.getObject();
00021               if (o==null) return "???";
00022               else return o.getEquation();
00023        }
00024        public void translate ()
00025        {      E.translate();
00026        }
00027        public void addDep (ConstructionObject o)
00028        {      E.addDep(o);
00029        }
00030 }
00031 
00032 class NameExpressionString
00033 {      public Expression E;
00034        public NameExpressionString (Expression o)
00035        {      E=o;
00036        }
00037        public String toString ()
00038        {      return E.toString(); 
00039        }
00040        public String getName ()
00041        {      ConstructionObject o=E.getObject();
00042               if (o==null) return "???";
00043               else return o.getName();
00044        }
00045        public void translate ()
00046        {      E.translate();
00047        }
00048        public void addDep (ConstructionObject o)
00049        {      E.addDep(o);
00050        }
00051 }
00052 
00057 public class ExpressionString 
00058 {      MyVector v=new MyVector(3); // Strings or Expressions
00059        ConstructionObject O;
00060 
00061        public ExpressionString (String s, ConstructionObject o)
00062        {      O=o;
00063               v.removeAllElements();
00064               int h1=0;
00065               int n;
00066               while ((n=s.indexOf("%",h1))>=0)
00067               {      int h2=s.indexOf("%",n+1);
00068                      if (h2==n+1) // found %%
00069                      {      v.addElement(s.substring(h1,n));
00070                             v.addElement("%");
00071                             h1=n+2;
00072                      }
00073                      else if (h2>=0) // found %...%
00074                      {      v.addElement(s.substring(h1,n));
00075                             if (s.charAt(n+1)=='~') // found %~...%
00076                             {      String name=s.substring(n+2,h2);
00077                                    v.addElement(new EquationExpressionString(
00078                                                  new Expression(s.substring(n+2,h2),O.getConstruction(),O)));
00079                                    h1=h2+1;
00080                             }
00081                             else if (s.charAt(n+1)=='=') // found %=...%
00082                             {      String name=s.substring(n+2,h2);
00083                                    v.addElement(new NameExpressionString(
00084                                                  new Expression(s.substring(n+2,h2),O.getConstruction(),O)));
00085                                    h1=h2+1;
00086                             }
00087                             else
00088                             {      Expression ex=new Expression(s.substring(n+1,h2),
00089                                           O.getConstruction(),O);
00090                                    v.addElement(ex);
00091                                    h1=h2+1;
00092                             }
00093                      }
00094                      else // only one %
00095                      {      v.addElement(s.substring(h1,n));
00096                             v.addElement("%");
00097                             h1=n+1;
00098                      }
00099               }
00100               if (!s.equals("")) v.addElement(s.substring(h1));
00101               // System.out.println(evaluate());
00102        }
00103 
00108        public String toString ()
00109        {      StringBuffer sb=new StringBuffer();
00110               Enumeration e=v.elements();
00111               while (e.hasMoreElements())
00112               {      Object o=e.nextElement();
00113                      if (o instanceof String)
00114                      {      if (((String)o).equals("%")) sb.append("%%");
00115                             else sb.append((String)o); 
00116                      }
00117                      else if (o instanceof Expression)
00118                      {      sb.append("%");
00119                             if (((Expression)o).isForcePlus()) sb.append("+");
00120                             sb.append(o.toString()); 
00121                             sb.append("%");
00122                      }
00123                      else if (o instanceof EquationExpressionString)
00124                      {      sb.append("%~");
00125                             sb.append(((EquationExpressionString)o).toString());
00126                             sb.append("%");
00127                      }
00128                      else if (o instanceof NameExpressionString)
00129                      {      sb.append("%=");
00130                             sb.append(((NameExpressionString)o).toString());
00131                             sb.append("%");
00132                      }
00133               }
00134               return sb.toString();
00135        }
00136        
00141        public String evaluate ()
00142        {      StringBuffer sb=new StringBuffer();
00143               Enumeration e=v.elements();
00144               while (e.hasMoreElements())
00145               {      Object o=e.nextElement();
00146                      if (o instanceof String)
00147                      {      if (((String)o).equals("%")) sb.append("%");
00148                             else sb.append((String)o); 
00149                      }
00150                      else if (o instanceof Expression)
00151                      {      try
00152                             {      double value=((Expression)o).getValue();
00153                                    value=O.round(value);
00154                                    if (((Expression)o).isForcePlus() && value>=0) sb.append("+");
00155                                    if (value==Math.floor(value+0.5))
00156                                           sb.append((int)(value));
00157                                    else
00158                                           sb.append(O.round(value)); 
00159                             }
00160                             catch (Exception exc)
00161                             {      sb.append("???");
00162                             }
00163                      }
00164                      else if (o instanceof EquationExpressionString)
00165                      {      sb.append(((EquationExpressionString)o).getEquation());
00166                      }
00167                      else if (o instanceof NameExpressionString)
00168                      {      sb.append(((NameExpressionString)o).getName());
00169                      }
00170               }
00171               return sb.toString();
00172        }
00173        
00177        public void translate ()
00178        {      Enumeration e=v.elements();
00179               while (e.hasMoreElements())
00180               {      Object o=e.nextElement();
00181                      if (o instanceof Expression)
00182                      {      ((Expression)o).translate();
00183                      }
00184                      else if (o instanceof EquationExpressionString)
00185                      {      ((EquationExpressionString)o).translate();
00186                      }
00187                      else if (o instanceof NameExpressionString)
00188                      {      ((NameExpressionString)o).translate();
00189                      }
00190               }
00191        }
00192        
00197        public void addDep (ConstructionObject oc)
00198        {      Enumeration e=v.elements();
00199               while (e.hasMoreElements())
00200               {      Object o=e.nextElement();
00201                      if (o instanceof Expression)
00202                      {      ((Expression)o).addDep(oc);
00203                      }
00204                      else if (o instanceof EquationExpressionString)
00205                      {      ((EquationExpressionString)o).addDep(oc);
00206                      }
00207                      else if (o instanceof NameExpressionString)
00208                      {      ((NameExpressionString)o).addDep(oc);
00209                      }
00210               }
00211        }
00212 }