FileDocCategorySizeDatePackage
Algorithm.javaAPI DocApache Lucene 2.2.09025Sat Jun 16 22:20:58 BST 2007org.apache.lucene.benchmark.byTask.utils

Algorithm

public class Algorithm extends Object
Test algorithm, as read from file

Fields Summary
private org.apache.lucene.benchmark.byTask.tasks.TaskSequence
sequence
Constructors Summary
public Algorithm(org.apache.lucene.benchmark.byTask.PerfRunData runData)
Read algorithm from file

param
runData perf-run-data used at running the tasks.
throws
Exception if errors while parsing the algorithm

    String algTxt = runData.getConfig().getAlgorithmText();
    sequence = new TaskSequence(runData,null,null,false);
    TaskSequence currSequence = sequence;
    PerfTask prevTask = null;
    StreamTokenizer stok = new StreamTokenizer(new StringReader(algTxt));
    stok.commentChar('#");
    stok.eolIsSignificant(false);
    stok.ordinaryChar('"");
    stok.ordinaryChar('/");
    stok.ordinaryChar('(");
    stok.ordinaryChar(')");
    boolean colonOk = false; 
    currSequence.setDepth(0);
    String taskPackage = PerfTask.class.getPackage().getName() + ".";
    
    Class paramClass[] = {PerfRunData.class};
    PerfRunData paramObj[] = {runData};
    
    while (stok.nextToken() != StreamTokenizer.TT_EOF) { 
      switch(stok.ttype) {
  
        case StreamTokenizer.TT_WORD:
          String s = stok.sval;
          Constructor cnstr = Class.forName(taskPackage+s+"Task").getConstructor(paramClass);
          PerfTask task = (PerfTask) cnstr.newInstance(paramObj);
          currSequence.addTask(task);
          if (task instanceof RepSumByPrefTask) {
            stok.nextToken();
            String prefix = stok.sval;
            if (prefix==null || prefix.length()==0) { 
              throw new Exception("named report prefix problem - "+stok.toString()); 
            }
            ((RepSumByPrefTask) task).setPrefix(prefix);
          }
          // check for task param: '(' someParam ')'
          stok.nextToken();
          if (stok.ttype!='(") {
            stok.pushBack();
          } else {
            // get params, for tasks that supports them, - anything until next ')'
            StringBuffer params = new StringBuffer();
            stok.nextToken();
            while (stok.ttype!=')") { 
              switch (stok.ttype) {
                case StreamTokenizer.TT_NUMBER:  
                  params.append(stok.nval);
                  break;
                case StreamTokenizer.TT_WORD:    
                  params.append(stok.sval);             
                  break;
                case StreamTokenizer.TT_EOF:     
                  throw new Exception("unexpexted EOF: - "+stok.toString());
                default:
                  params.append((char)stok.ttype);
              }
              stok.nextToken();
            }
            String prm = params.toString().trim();
            if (prm.length()>0) {
              task.setParams(prm);
            }
          }

          // ---------------------------------------
          colonOk = false; prevTask = task;
          break;
  
        default:
          char c = (char)stok.ttype;
          
          switch(c) {
          
            case ':" :
              if (!colonOk) throw new Exception("colon unexpexted: - "+stok.toString());
              colonOk = false;
              // get repetitions number
              stok.nextToken();
              if ((char)stok.ttype == '*") {
                ((TaskSequence)prevTask).setRepetitions(TaskSequence.REPEAT_EXHAUST);
              } else {
                if (stok.ttype!=StreamTokenizer.TT_NUMBER) throw new Exception("expexted repetitions number: - "+stok.toString());
                ((TaskSequence)prevTask).setRepetitions((int)stok.nval);
              }
              // check for rate specification (ops/min)
              stok.nextToken();
              if (stok.ttype!=':") {
                stok.pushBack();
              } else {
                // get rate number
                stok.nextToken();
                if (stok.ttype!=StreamTokenizer.TT_NUMBER) throw new Exception("expexted rate number: - "+stok.toString());
                // check for unit - min or sec, sec is default
                stok.nextToken();
                if (stok.ttype!='/") {
                  stok.pushBack();
                  ((TaskSequence)prevTask).setRate((int)stok.nval,false); // set rate per sec
                } else {
                  stok.nextToken();
                  if (stok.ttype!=StreamTokenizer.TT_WORD) throw new Exception("expexted rate unit: 'min' or 'sec' - "+stok.toString());
                  String unit = stok.sval.toLowerCase();
                  if ("min".equals(unit)) {
                    ((TaskSequence)prevTask).setRate((int)stok.nval,true); // set rate per min
                  } else if ("sec".equals(unit)) {
                    ((TaskSequence)prevTask).setRate((int)stok.nval,false); // set rate per sec
                  } else {
                    throw new Exception("expexted rate unit: 'min' or 'sec' - "+stok.toString());
                  }
                }
              }
              colonOk = false;
              break;
    
            case '{" : 
            case '[" :  
              // a sequence
              // check for sequence name
              String name = null;
              stok.nextToken();
              if (stok.ttype!='"") {
                stok.pushBack();
              } else {
                stok.nextToken();
                name = stok.sval;
                stok.nextToken();
                if (stok.ttype!='"" || name==null || name.length()==0) { 
                  throw new Exception("sequence name problem - "+stok.toString()); 
                }
              }
              // start the sequence
              TaskSequence seq2 = new TaskSequence(runData, name, currSequence, c=='[");
              currSequence.addTask(seq2);
              currSequence = seq2;
              colonOk = false;
              break;
    
            case '>" :
              currSequence.setNoChildReport();
            case '}" : 
            case ']" : 
              // end sequence
              colonOk = true; prevTask = currSequence;
              currSequence = currSequence.getParent();
              break;
          
          } //switch(c)
          break;
          
      } //switch(stok.ttype)
      
    }
    
    if (sequence != currSequence) {
      throw new Exception("Unmatched sequences");
    }
    
    // remove redundant top level enclosing sequences
    while (sequence.getRepetitions()==1 && sequence.getRate()==0) {
      ArrayList t = sequence.getTasks();
      if (t!=null && t.size()==1) {
        PerfTask p = (PerfTask) t.get(0);
        if (p instanceof TaskSequence) {
          sequence = (TaskSequence) p;
          continue;
        }
      }
      break;
    }
  
Methods Summary
public voidexecute()
Execute this algorithm

throws
Exception

    sequence.doLogic();
  
public java.util.ArrayListextractTasks()
Expert: for test purposes, return all tasks participating in this algorithm.

return
all tasks participating in this algorithm.

    ArrayList res = new ArrayList();
    extractTasks(res, sequence);
    return res;
  
private voidextractTasks(java.util.ArrayList extrct, org.apache.lucene.benchmark.byTask.tasks.TaskSequence seq)

    if (seq==null) 
      return;
    extrct.add(seq);
    ArrayList t = sequence.getTasks();
    if (t==null) 
      return;
    for (int i = 0; i < t.size(); i++) {
      PerfTask p = (PerfTask) t.get(0);
      if (p instanceof TaskSequence) {
        extractTasks(extrct, (TaskSequence)p);
      } else {
        extrct.add(p);
      }
    }
  
public java.lang.StringtoString()

    String newline = System.getProperty("line.separator");
    StringBuffer sb = new StringBuffer();
    sb.append(sequence.toString());
    sb.append(newline);
    return sb.toString();