View Javadoc

1   /**
2    * 
3    */
4   package unbbayes.prs.bn;
5   
6   import java.util.ResourceBundle;
7   
8   import unbbayes.prs.Graph;
9   import unbbayes.util.extension.bn.inference.IInferenceAlgorithm;
10  
11  /**
12   * Class for junction tree compiling algorithm.
13   * It includes basic consistency check for single entity networks.
14   * 
15   * By now, this is still a wrapper for {@link ProbabilisticNetwork#compile()}.
16   * 
17   * TODO gradually migrate every compilation routine from ProbabilisticNetwork to here.
18   * 
19   * @author Shou Matsumoto
20   *
21   */
22  public class JunctionTreeAlgorithm implements IInferenceAlgorithm {
23  	
24  	private ProbabilisticNetwork net;
25  	
26  
27  	/** Load resource file from util */
28    	private static ResourceBundle utilResource = unbbayes.util.ResourceController.newInstance().getBundle(
29    			unbbayes.util.resources.UtilResources.class.getName());
30  	
31  
32    	/** Load resource file from this package */
33    	protected static ResourceBundle resource = unbbayes.util.ResourceController.newInstance().getBundle(
34    			unbbayes.prs.bn.resources.BnResources.class.getName());
35    	
36    	
37  	/**
38  	 * Default constructor for plugin support
39  	 */
40  	public JunctionTreeAlgorithm() {
41  		super();
42  	}
43  
44  	/* (non-Javadoc)
45  	 * @see unbbayes.util.extension.bn.inference.IInferenceAlgorithm#getDescription()
46  	 */
47  	public String getDescription() {
48  		return this.utilResource.getString("junctionTreeAlgorithmDescription");
49  	}
50  
51  	/* (non-Javadoc)
52  	 * @see unbbayes.util.extension.bn.inference.IInferenceAlgorithm#getName()
53  	 */
54  	public String getName() {
55  		return this.utilResource.getString("junctionTreeAlgorithmName");
56  	}
57  
58  	/* (non-Javadoc)
59  	 * @see unbbayes.util.extension.bn.inference.IInferenceAlgorithm#run()
60  	 */
61  	public void run() throws IllegalStateException {
62  		if (this.getNet() == null
63  				|| this.getNet().getNodes().size() == 0) {
64  			throw new IllegalStateException(resource.getString("EmptyNetException"));
65  		}
66  		try {
67  			// TODO gradually migrate all compile routines to here
68  			this.getNet().compile();
69  		} catch (Exception e) {
70  			throw new IllegalStateException(e);
71  		}
72  	}
73  	
74      
75      
76  
77  	/* (non-Javadoc)
78  	 * @see unbbayes.util.extension.bn.inference.IInferenceAlgorithm#setNetwork(unbbayes.prs.Graph)
79  	 */
80  	public void setNetwork(Graph g) throws IllegalArgumentException {
81  		this.setNet((ProbabilisticNetwork)g);
82  	}
83  	
84  	/*
85  	 * (non-Javadoc)
86  	 * @see unbbayes.util.extension.bn.inference.IInferenceAlgorithm#getNetwork()
87  	 */
88  	public Graph getNetwork() {
89  		return this.getNet();
90  	}
91  
92  	/**
93  	 * @return the net
94  	 */
95  	public ProbabilisticNetwork getNet() {
96  		return net;
97  	}
98  
99  	/**
100 	 * @param net the net to set
101 	 */
102 	public void setNet(ProbabilisticNetwork net) {
103 		this.net = net;
104 	}
105 
106 	/*
107 	 * (non-Javadoc)
108 	 * @see unbbayes.util.extension.bn.inference.IInferenceAlgorithm#reset()
109 	 */
110 	public void reset() {
111 		try {
112 			this.getNet().initialize();
113 		} catch (Exception e) {
114 			throw new IllegalStateException(e);
115 		}
116 	}
117 
118 	/* (non-Javadoc)
119 	 * @see unbbayes.util.extension.bn.inference.IInferenceAlgorithm#propagate()
120 	 */
121 	public void propagate() {
122 		try {
123 			this.getNet().updateEvidences();
124 		} catch (Exception e) {
125 			throw new RuntimeException(e);
126 		}
127 	}
128 
129 	
130 	
131 }