public static PiHelper load_pi_helper( PiHelper pi_helper_cache, ConditionalDistribution pxu, Distribution[] pi_messages) throws Exception { if (pi_messages.length == 0) return new TrivialPiHelper(); Vector seq = new Vector(); seq.addElement(pxu.getClass()); for (int i = 0; i < pi_messages.length; i++) seq.addElement(pi_messages[i].getClass()); if (pi_helper_cache != null && MatchClassPattern.matches(pi_helper_cache.description(), seq, new int[1], new int[1])) return pi_helper_cache; Class c = find_helper_class(seq, "pi"); return (PiHelper) c.newInstance(); }
/** * 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; }
/** * 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; }