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; }
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); } }