コード例 #1
0
  /**
   * procedure GetBestExplForFunc
   *
   * <p>最低でもRootのみのTreeは取得できる
   *
   * @param f
   * @param h_max
   * @return
   */
  public static FunctionTree GetBestExplTreeForFunc(Function f, int h_max) {

    //		if(f.getName().equals("getFruitList")){
    //			System.out.println("aaa");
    //		}

    /*
     * 親クラスが"this"かつ、非staticの場合は、レシーバ無しでも起動できるようにする。
     */
    boolean this_flg = false;
    if (f.getParentClass().equals("this") && f.isStatic() == false) {
      this_flg = true;
    }

    FunctionTree best_tree = new FunctionTree(f, input_keywords, numOfSameWords); // RootのみのTree
    ExplanationVector e_cumulative =
        new ExplanationVector(best_tree.e_vec); // best_treeのe_vecをコピーしてnew

    // for each p in params(f)

    if (f.getParameters() == null) return best_tree; // 関数に引数がないとき、rootのみを返す
    else if (h_max == 0) return null; // 関数に引数はあるが、高さが0のとき、nullを返す

    /*
     * this_flg=trueのときは、高さ0、引数レシーバのみのときがある。
     */
    if (this_flg == true && f.getParameters().length == 1) return best_tree;

    for (String param : f.getParameters()) { // 引数の順番通りにループする
      // e_best = (-∞, 0, 0, 0, ...)
      ExplanationVector e_best =
          new ExplanationVector(input_keywords.size(), -ExplanationVector.INFINITE_VALUE); // 空っぽ
      FunctionTree param_tree = null;
      // for each 1 <= i <= h
      for (int i = 0; i < h_max; i++) { // h_max=0の場合はこのループは実行されない
        // for each (e', f') in bestRoots(p, i)
        FunctionTree[] trees = bestRoots.getRoots(param, i);
        if (trees != null) {
          for (FunctionTree t : trees) {
            if (t != null) {
              // if e_cumulative + e' > e_best
              //    e_best = e_cumulative + e'

              ExplanationVector tmp = ExplanationVector.add(e_cumulative, t.e_vec);
              if (tmp.compareTo(e_best) == 1) {
                e_best.substitution(tmp); // 代入
                param_tree = t;
              }
            }
          }
        }
      }
      // e_cumulative = e_best
      e_cumulative.substitution(e_best);

      if (param_tree == null) return null; // 引数が1つでも埋まらなければ,そのroot関数は選択しない。nullを返す。
      else best_tree.addChild(param_tree);
    }

    return best_tree;
  }