Logo Search packages:      
Sourcecode: wims version File versions  Download package

TableFunction GenericGraphApplet::parseTableFuncDef ( String  def  )  [inline, protected]

Create a TableFunction from a string. The string can start with the word "table", which is ignored. The next item can optionally be one of the table styles "smooth", "linear", or "step". The default is "smooth". Then values must be specified. If the next word is "intervals", it can be followed by numbers giving the number of intervals, the minumum x, and the maximum x, and the y-values for up to (intervals+1) points evenly distributed between xmin and xmax (unassigned y-values will be zero). If no data is specified, a table function with 6 intervals between xmin = -5 and xmax = 5 and all y-values zero is created. If "intervals" is not specified, the remaining items are numbers giving pairs of (x,y)-values. Items can be separated spaces, tabs, and commas. A ParseError will be thrown if the data is illegal.

Definition at line 697 of file GenericGraphApplet.java.

References edu::hws::jcm::functions::TableFunction::addIntervals(), edu::hws::jcm::functions::TableFunction::addPoint(), edu::hws::jcm::functions::TableFunction::setStyle(), and edu::hws::jcm::functions::TableFunction::setY().

                                                         {
      try {
         TableFunction func = new TableFunction();
         StringTokenizer toks = new StringTokenizer(def, " \t,");
         String tok = null;
         if (toks.hasMoreTokens()) {
            tok = toks.nextToken();
            if (tok.equalsIgnoreCase("table") && toks.hasMoreTokens())
               tok = toks.nextToken();
         }
         if ("step".equalsIgnoreCase(tok)) {
            func.setStyle(TableFunction.STEP);
            if (toks.hasMoreTokens())
               tok = toks.nextToken();
         }
         else if ("linear".equalsIgnoreCase(tok)) {
            func.setStyle(TableFunction.PIECEWISE_LINEAR);
            if (toks.hasMoreTokens())
               tok = toks.nextToken();
         }
         else if ("smooth".equalsIgnoreCase(tok) && toks.hasMoreTokens()) {
            if (toks.hasMoreTokens())
               tok = toks.nextToken();
         }
         boolean useIntervals = "intervals".equalsIgnoreCase(tok);
         if (useIntervals && toks.hasMoreTokens())
            tok = toks.nextToken();
         double[] nums = new double[toks.countTokens() + 1];
         try {
            nums[0] = (new Double(tok)).doubleValue();
         }
         catch (NumberFormatException e) {
            throw new ParseError("Unexpected token \"" + tok + "\".",null);
         }
         try {
            for (int i = 1; i < nums.length; i++)
                nums[i] = (new Double(toks.nextToken())).doubleValue();
         }
         catch (NumberFormatException e) {
            throw new ParseError("Illegal number.", null);
         }
         if (useIntervals) {
             int ct = (nums.length == 0)? 6 : (int)Math.round(nums[0]);
             if (ct < 1 || ct > 500)
                ct = 6;
             double xmin = (nums.length < 2)? -5 : nums[1];
             double xmax = (nums.length < 3)? xmin + 10 : nums[2];
             if (xmax <= xmin)
                throw new ParseError("xmax in table must be greater than xmin", null);
             func.addIntervals(ct,xmin,xmax);
             for (int i = 3; i < nums.length && (i-3) <= ct; i++) {
                if (i-3 < ct)
                   func.setY(i-3,nums[i]);
             }
         }
         else {
            if (nums.length < 4)
               throw new ParseError("At least two points must be provided for table function.", null);
            if (nums.length % 2 == 1)
               throw new ParseError("Can't define an table function with an odd number of values.", null);
            for (int i = 0; i < nums.length/2; i++) {
               func.addPoint(nums[2*i],nums[2*i+1]);
            }
         }
         return func;
      }
      catch (Exception e) {
         throw new ParseError("Error while parsing table function: " + e.getMessage(), null);
      }
   }


Generated by  Doxygen 1.6.0   Back to index