well. */
/* make a copy of potentials */
HashSet allPhi = new HashSet();
for (Iterator i = model.factorsIterator (); i.hasNext(); ){
Factor factor = (Factor) i.next ();
allPhi.add(factor.duplicate());
}
Set nodes = model.variablesSet ();
/* Eliminate each node in turn */
for (Iterator i = nodes.iterator(); i.hasNext(); ) {
Variable node = (Variable) i.next();
if (node == query) continue; // Eliminate the query variable last!
Factor newCPF = eliminate (allPhi, node);
/* Extract (marginalize) over this variables */
Factor singleCPF;
if(newCPF.varSet().size() == 1) {
singleCPF = newCPF;
} else {
singleCPF = newCPF.marginalizeOut (node);
}
/* add it back to the list of potentials */
allPhi.add(singleCPF);
}
/* Now, all the potentials that are left should contain only the
* query variable.... UNLESS the graph is disconnected. So just
* eliminate the query var.
*/
Factor marginal = eliminate (allPhi, query);
assert marginal.containsVar (query);
assert marginal.varSet().size() == 1;
return marginal;
}