Esempio n. 1
0
  public static boolean are_d_connected(AbstractVariable x1, AbstractVariable x2, Vector evidence)
      throws RemoteException {
    // Find all paths between x1 and x2, then see if there is some path which is
    // d-connecting given the evidence. If so, return true, otherwise false.

    Hashtable path_sets = new Hashtable(); // HEY !!! THIS OUGHT TO BE CACHED SOMEWHERE !!!
    PathAnalysis.compile_paths(x1, x2, path_sets);

    Vector path_set = (Vector) path_sets.get(new VariablePair(x1, x2));
    if (path_set == null)
      // No connections whatsoever.
      return false;

    Enumeration path_set_enum = path_set.elements();
    while (path_set_enum.hasMoreElements()) {
      AbstractVariable[] path = (AbstractVariable[]) path_set_enum.nextElement();
      if (is_d_connecting(path, evidence)) {
        System.err.print("PathAnalysis.are_d_connected: path ");
        int i;
        for (i = 0; i < path.length; i++) System.err.print(path[i].get_name() + " ");
        System.err.print("is d-connected given evidence ");
        for (i = 0; i < evidence.size(); i++)
          System.err.print(((AbstractVariable) evidence.elementAt(i)).get_name() + " ");
        System.err.println("");
        return true;
      }
    }

    return false;
  }
Esempio n. 2
0
  public String toString() {
    String s = "[" + helper_type + ";";
    for (Enumeration e = seq.elements(); e.hasMoreElements(); ) {
      try {
        Class c = (Class) e.nextElement();
        s += c.getName() + ",";
      } catch (NoSuchElementException ee) {
        s += "???" + ",";
      }
    }

    return s + "]";
  }
Esempio n. 3
0
  public static void find_all_paths(
      AbstractVariable x, AbstractVariable end, Vector path_set, Stack path_stack)
      throws RemoteException {
    int i;

    path_stack.push(x);

    if (x == end) {
      // Construct a path from the beginning to the end, using what's on the stack.

      AbstractVariable[] path = new AbstractVariable[path_stack.size()];

      // System.err.println( "\tFound path: " );
      Enumeration e;
      for (i = 0, e = path_stack.elements(); e.hasMoreElements(); i++) {
        path[i] = (AbstractVariable) e.nextElement();
        // System.err.print( path[i].get_name()+" " );
      }
      // System.err.println("");

      path_set.addElement(path);
      path_stack.pop();
      return;
    }

    AbstractVariable[] parents = x.get_parents();
    for (i = 0; i < parents.length; i++) {
      Enumeration e = path_stack.elements();
      boolean is_on_stack = false;
      while (e.hasMoreElements())
        if (e.nextElement() == parents[i]) {
          is_on_stack = true;
          break;
        }

      if (!is_on_stack) find_all_paths(parents[i], end, path_set, path_stack);
    }

    AbstractVariable[] children = x.get_children();
    for (i = 0; i < children.length; i++) {
      Enumeration e = path_stack.elements();
      boolean is_on_stack = false;
      while (e.hasMoreElements())
        if (e.nextElement() == children[i]) {
          is_on_stack = true;
          break;
        }

      if (!is_on_stack) find_all_paths(children[i], end, path_set, path_stack);
    }

    path_stack.pop();
  }
Esempio n. 4
0
  public boolean equals(Object another) {
    if (another instanceof HelperCacheKey) {
      HelperCacheKey another_key = (HelperCacheKey) another;

      if (!this.helper_type.equals(another_key.helper_type)) return false;

      Enumeration e1, e2;
      for (e1 = this.seq.elements(), e2 = another_key.seq.elements(); e1.hasMoreElements(); ) {
        Object o1, o2;
        try {
          o1 = e1.nextElement();
        } catch (NoSuchElementException ex) {
          throw new RuntimeException("HelperCacheKey.equals: should never happen, " + ex);
        }
        try {
          o2 = e2.nextElement();
        } catch (NoSuchElementException ex) {
          return false;
        } // e2 has fewer elements than e2, no match

        if (!o1.equals(o2)) return false;
      }

      if (e2.hasMoreElements()) return false;
      else return true;
    } else return false;
  }
Esempio n. 5
0
  /**
   * Check the helpers currently stored in the helper cache to see if any of them can handle the
   * sequence we've just been given. This avoids pinging the belief network context to get a helper
   * list.
   */
  public static Class find_helper_class1(
      Vector seq, String helper_type, int[] max_class_score, int[] max_count_score)
      throws ClassNotFoundException {
    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 (Enumeration e = helper_cache.keys(); e.hasMoreElements(); ) {
      try {
        HelperCacheKey key = (HelperCacheKey) e.nextElement();
        if (!key.helper_type.equals(helper_type)) continue;
        Class c = (Class) helper_cache.get(key);
        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) {
      } // eat it; stagger forward
    }

    if (Global.debug > 1)
      System.err.println(
          "PiHelperLoader.find_helper_class1: helper "
              + (cmax_score == null ? "is NOT" : "is")
              + " in cache.");
    if (cmax_score == null) // no luck; try to get a helper list from the bnc & plunge ahead
    return find_helper_class0(seq, helper_type, max_class_score, max_count_score);
    else // success!
    return cmax_score;
  }
Esempio n. 6
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;
  }
Esempio n. 7
0
  public static void main(String[] args) {
    boolean do_compile_all = false;
    String bn_name = "", x1_name = "", x2_name = "";
    Vector evidence_names = new Vector();

    for (int 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 'a':
          do_compile_all = true;
          break;
        case 'x':
          if (args[i].charAt(2) == '1') x1_name = args[++i];
          else if (args[i].charAt(2) == '2') x2_name = args[++i];
          else System.err.println("PathAnalysis.main: " + args[i] + " -- huh???");
          break;
        case 'e':
          evidence_names.addElement(args[++i]);
          break;
        default:
          System.err.println("PathAnalysis.main: " + args[i] + " -- huh???");
      }
    }

    try {
      BeliefNetworkContext bnc = new BeliefNetworkContext(null);
      bnc.add_path("/bechtel/users10/krarti/dodier/belief-nets/assorted");
      AbstractBeliefNetwork bn = bnc.load_network(bn_name);
      Hashtable path_sets;
      Enumeration p;

      if ((p = PathAnalysis.has_directed_cycle(bn)) == null)
        System.err.println("PathAnalysis: no directed cycles found in " + bn_name);
      else {
        System.err.println("PathAnalysis.main: " + bn_name + " has a directed cycle; quit.");
        System.err.print(" cycle is: ");
        while (p.hasMoreElements()) {
          System.err.print(((AbstractVariable) p.nextElement()).get_name());
          if (p.hasMoreElements()) System.err.print(" -> ");
          else System.err.println("");
        }

        System.exit(1);
      }

      Vector evidence = new Vector();
      if (evidence_names.size() > 0) {
        for (int i = 0; i < evidence_names.size(); i++)
          evidence.addElement(bn.name_lookup((String) (evidence_names.elementAt(i))));
      }

      if (do_compile_all) {
        path_sets = PathAnalysis.compile_all_paths(bn);
      } else {
        AbstractVariable x1 = (AbstractVariable) bn.name_lookup(x1_name);
        AbstractVariable x2 = (AbstractVariable) bn.name_lookup(x2_name);
        path_sets = new Hashtable();
        PathAnalysis.compile_paths(x1, x2, path_sets);

        if (PathAnalysis.are_d_connected(x1, x2, evidence))
          System.err.print(
              x1.get_name() + " and " + x2.get_name() + " are d-connected given evidence ");
        else
          System.err.print(
              x1.get_name() + " and " + x2.get_name() + " are NOT d-connected given evidence ");

        for (int i = 0; i < evidence.size(); i++)
          System.err.print(((AbstractVariable) evidence.elementAt(i)).get_name() + " ");
        System.err.println("");
      }

      System.err.println("PathAnalysis.main: results of path finding:");

      AbstractVariable[] u = bn.get_variables();
      for (int i = 0; i < u.length; i++) {
        System.err.println(" --- paths from: " + u[i].get_name() + " ---");

        for (int j = i + 1; j < u.length; j++) {
          VariablePair vp = new VariablePair(u[i], u[j]);
          Vector path_set = (Vector) path_sets.get(vp);
          if (path_set == null) continue;

          Enumeration path_set_enum = path_set.elements();
          while (path_set_enum.hasMoreElements()) {
            AbstractVariable[] path = (AbstractVariable[]) path_set_enum.nextElement();
            System.err.print(" path: ");
            for (int k = 0; k < path.length; k++) System.err.print(path[k].get_name() + " ");
            System.err.println("");
          }
        }
      }

      System.exit(0);
    } catch (Exception e) {
      System.err.println("PathAnalysis.main:");
      e.printStackTrace();
      System.exit(1);
    }
  }