Exemplo n.º 1
0
  public static void main(String[] args) {
    boolean all_x = false, do_bind = false;
    String bn_name = "", context_name = "";
    int i;

    for (i = 0; i < args.length; i++) {
      if (args[i].charAt(0) != '-') continue;

      switch (args[i].charAt(1)) {
        case 'b':
          bn_name = args[++i];
          break;
        case 'c':
          context_name = args[++i];
          break;
        case 'r':
          do_bind = true;
          break;
      }
    }

    AbstractBeliefNetworkContext bnc = null;
    AbstractBeliefNetwork bn = null;
    AbstractVariable x = null, e_var = null;

    try {
      if ("".equals(context_name)) {
        BeliefNetworkContext local_bnc = new BeliefNetworkContext(null);
        bnc = local_bnc;
      } else {
        String url = "rmi://" + context_name;
        System.err.println("Informativeness: url: " + url);
        long t0 = System.currentTimeMillis();
        bnc = (AbstractBeliefNetworkContext) Naming.lookup(url);
        long tf = System.currentTimeMillis();
        System.err.println(
            "Informativeness: Naming.lookup complete (for belief net context), elapsed time: "
                + ((tf - t0) / 1000.0)
                + " [s]");
      }

      bn = (AbstractBeliefNetwork) bnc.load_network(bn_name);
      if (do_bind) {
        System.err.println("Informativeness: bind belief net.");
        bnc.bind(bn);
      }

      String e_name, x_name;
      double e_value;

      for (i = 0; i < args.length; i++) {
        if (args[i].charAt(0) != '-') continue;

        switch (args[i].charAt(1)) {
          case 'x':
            if ("-xall".equals(args[i])) {
              AbstractVariable[] u = bn.get_variables();
              for (int j = 0; j < u.length; j++) {
                Distribution xposterior = bn.get_posterior(u[j]);
                System.out.println("Informativeness: posterior for " + u[j].get_name() + ":");
                System.out.print("  " + xposterior.format_string("  "));
              }
            } else {
              x_name = args[++i];
              long t0 = System.currentTimeMillis();
              x = (AbstractVariable) bn.name_lookup(x_name);
              long tf = System.currentTimeMillis();
              System.err.println(
                  "Informativeness: Naming.lookup complete (for variable ref), elapsed time: "
                      + ((tf - t0) / 1000.0)
                      + " [s]");
              if (x == null) throw new Exception("name_lookup failed: x: " + x_name);
              Distribution xposterior = bn.get_posterior(x);
              System.out.println("Informativeness: posterior for " + x.get_name() + ":");
              System.out.print("  " + xposterior.format_string("  "));
            }
            break;
          case 'e':
            if (args[i].length() > 2 && args[i].charAt(2) == '-') {
              e_name = args[++i];
              System.err.println("Informativeness.main: evidence: clear " + e_name);
              e_var = (AbstractVariable) bn.name_lookup(e_name);
              bn.clear_posterior(e_var);
            } else {
              e_name = args[++i];
              e_value = Double.parseDouble(args[++i]);
              System.err.println(
                  "Informativeness.main: evidence: set " + e_name + " to " + e_value);

              long t0 = System.currentTimeMillis();
              e_var = (AbstractVariable) bn.name_lookup(e_name);
              long tf = System.currentTimeMillis();
              System.err.println(
                  "Informativeness: Naming.lookup complete (for variable ref), elapsed time: "
                      + ((tf - t0) / 1000.0)
                      + " [s]");
              bn.assign_evidence(e_var, e_value);
            }
            break;
          default:
            continue;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    System.exit(0);
  }
Exemplo n.º 2
0
  /**
   * Contact a belief network context, get the helper list, and search the list to see if there's a
   * helper which matches the type sequence specified. If there's more than one helper which
   * matches, find the ``best fit.''
   *
   * <p>The class and count scores of the best-fitting helper class are written into
   * <tt>max_class_score[0]</tt> and <tt>max_count_score[0]</tt>, respectively.
   */
  public static Class find_helper_class0(
      Vector seq, String helper_type, int[] max_class_score, int[] max_count_score)
      throws ClassNotFoundException {
    long t0 = System.currentTimeMillis();
    if (bnc != null) // make sure the reference is still alive
    try {
        bnc.get_name();
      } catch (RemoteException e) {
        bnc = null;
      }

    if (bnc == null) // need to locate a context
    {
      String cb = System.getProperty("java.rmi.server.codebase", "http://localhost");
      long tt0 = System.currentTimeMillis();
      try {
        bnc = BeliefNetworkContext.locate_context(new URL(cb).getHost());
      } catch (Exception e) {
        throw new ClassNotFoundException("nested: " + e);
      }
    }

    String[] helperlist;
    try {
      helperlist = bnc.get_helper_names(helper_type);
    } catch (RemoteException e) {
      throw new ClassNotFoundException("bnc.get_helper_names failed");
    }

    int[] class_score1 = new int[1], count_score1 = new int[1];
    max_class_score[0] = -1;
    max_count_score[0] = -1;
    Class cmax_score = null;

    for (int i = 0; i < helperlist.length; i++) {
      try {
        Class c = RMIClassLoader.loadClass(helperlist[i]);
        SeqTriple[] sm = (SeqTriple[]) invoke_description(c);
        if (sm == null) continue; // apparently not a helper class
        if (MatchClassPattern.matches(sm, seq, class_score1, count_score1)) {
          if (class_score1[0] > max_class_score[0]
              || (class_score1[0] == max_class_score[0] && count_score1[0] > max_count_score[0])) {
            cmax_score = c;
            max_class_score[0] = class_score1[0];
            max_count_score[0] = count_score1[0];
          }
        }
      } catch (Exception e2) {
        System.err.println("PiHelperLoader: attempt to load " + helperlist[i] + " failed; " + e2);
      }
    }

    if (cmax_score == null) {
      System.err.println("find_helper_class0: failed; helper list:");
      for (int i = 0; i < helperlist.length; i++) System.err.println("\t" + helperlist[i]);

      String s = "";
      for (Enumeration e = seq.elements(); e.hasMoreElements(); ) {
        try {
          Class c = (Class) e.nextElement();
          s += c.getName() + ",";
        } catch (NoSuchElementException ee) {
          s += "???" + ",";
        }
      }

      throw new ClassNotFoundException("no " + helper_type + " helper for sequence [" + s + "]");
    }

    // FOR NOW IGNORE THE POSSIBILITY OF TWO OR MORE MATCHES !!!
    return cmax_score;
  }