View Javadoc

1   /*
2    *  UnBBayes
3    *  Copyright (C) 2002, 2009 Universidade de Brasilia - http://www.unb.br
4    *
5    *  This file is part of UnBBayes.
6    *
7    *  UnBBayes is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  UnBBayes is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with UnBBayes.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  package unbbayes.evaluation.controller;
22  
23  import java.awt.Cursor;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import javax.swing.JOptionPane;
32  import javax.swing.JPanel;
33  
34  import unbbayes.evaluation.EvidenceEvaluation;
35  import unbbayes.evaluation.FastLWApproximateEvaluation;
36  import unbbayes.evaluation.IEvaluation;
37  import unbbayes.evaluation.exception.EvaluationException;
38  import unbbayes.evaluation.gui.EvaluationPane;
39  import unbbayes.gui.LongTaskProgressBar;
40  import unbbayes.prs.Node;
41  import unbbayes.prs.bn.ProbabilisticNetwork;
42  import unbbayes.prs.bn.ProbabilisticNode;
43  
44  public class EvaluationController {
45  	
46  	private EvaluationPane evaluationPane;
47  	private ProbabilisticNetwork network;
48  	private IEvaluation evaluation;
49  	
50  	public EvaluationController(ProbabilisticNetwork network) {
51  		this.network = network;
52  		this.evaluationPane = new EvaluationPane();
53  		setUpEvaluation();
54  	}
55  	
56  	public void setUpEvaluation() {
57  		List<Node> nodeList = network.getNodes();
58  		
59  		Map<String, List<String>> nodeFindingMap = new HashMap<String, List<String>>();
60  		List<String> findingList;
61  		for (Node node : nodeList) {
62  			findingList = new ArrayList<String>(node.getStatesSize());
63  			for (int i = 0; i < node.getStatesSize(); i++) {
64  				findingList.add(node.getStateAt(i));
65  			}
66  			nodeFindingMap.put(node.getName(), findingList);
67  		}
68  		
69  		evaluationPane.addInputValues(nodeFindingMap);
70  		
71  		evaluationPane.setRunButtonActionListener(new ActionListener() {
72  
73  			public void actionPerformed(ActionEvent arg0) {
74  				new EvaluationThread(EvaluationController.this);  // thread is called here
75  			}
76  
77  		});
78  	}
79  	
80  	private void validateData() throws EvaluationException {
81  		StringBuffer errorMsg = new StringBuffer();
82  		
83  		try {
84  			Integer sampleSize = evaluationPane.getSampleSizeValue();
85  			if (sampleSize <= 0) {
86  				errorMsg.append("Sample size has to be greater than 0. \n");
87  			}
88  		} catch (NumberFormatException e) {
89  			errorMsg.append("Sample size has to be greater than 0. \n");
90  		}
91  		
92  		if (evaluationPane.getTargetNodeNameList().size() == 0) {
93  			errorMsg.append("There must be at least one target node selected. \n");
94  		}
95  		
96  		if (evaluationPane.getEvidenceNodeNameList().size() == 0) {
97  			errorMsg.append("There must be at least one evidence node selected. \n");
98  		}
99  		
100 		if (errorMsg.toString().length() > 0) {
101 			throw new EvaluationException(errorMsg.toString());
102 		}
103 			
104 	}
105 	
106 	void runEvaluation() {
107 		evaluationPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
108 		
109 		try {
110 			validateData();
111 		} catch (EvaluationException e) {
112 			JOptionPane.showMessageDialog(evaluationPane, e.getMessage(), "Validation Error", JOptionPane.ERROR_MESSAGE);
113 			return;
114 		}
115 		
116 		List<String> targetNodeNameList = evaluationPane.getTargetNodeNameList();
117 		List<String> evidenceNodeNameList = evaluationPane.getEvidenceNodeNameList();
118 		int sampleSize = evaluationPane.getSampleSizeValue();
119 		
120 		evaluation = new FastLWApproximateEvaluation(sampleSize);
121 		// Start progress bar
122 		LongTaskProgressBar progress = new LongTaskProgressBar(false);
123 		// Set up what to do once the long task is canceled
124 		progress.getCancelButton().addActionListener(new ActionListener() {
125 			public void actionPerformed(ActionEvent e) {
126 				finishEvaluation();
127 			}
128 		});
129 		// Register progress bar as observer of the long task
130 		evaluation.registerObserver(progress);
131 		// Add thread to progress bar to allow canceling the operation
132 		progress.setThread(EvaluationThread.t);
133 		// Update the pane with the progress bar just created
134 		evaluationPane.updateProgressBarPane(progress);
135 
136 		Map<String, String> nodeFindingMap = evaluationPane.getNodeConditionMap();
137 		ProbabilisticNode node;
138 		for (String nodeName : nodeFindingMap.keySet()) {
139 			node = (ProbabilisticNode)network.getNode(nodeName);
140 			for (int i = 0; i < node.getStatesSize(); i++) {
141 				if (nodeFindingMap.get(nodeName).equals(node.getStateAt(i))) {
142 					node.initMarginalList();
143 					node.addFinding(i);
144 				}
145 			}
146 		}
147 		
148 		try {
149 			// Run the long task
150 			evaluation.evaluate(network, targetNodeNameList, evidenceNodeNameList, false);
151 			// Set long task as done on the progress bar (100%)
152 			progress.setProgressBar(10000);
153 			
154 			List<EvidenceEvaluation> evidenceEvaluationList = evaluation.getBestMarginalImprovement();
155 			
156 			for (EvidenceEvaluation evidenceEvaluation : evidenceEvaluationList) {
157 				evidenceEvaluation.setCost(evaluationPane.getCost(evidenceEvaluation.getName()));
158 			}
159 			
160 			evaluationPane.setPccValue(evaluation.getEvidenceSetPCC());
161 			
162 			evaluationPane.setErrorValue(evaluation.getError());
163 			
164 			evaluationPane.addOutputValues(evidenceEvaluationList);
165 			
166 			node = (ProbabilisticNode)network.getNode(targetNodeNameList.get(0));
167 			
168 			String[] header = new String[node.getStatesSize()];
169 			for (int i = 0; i < node.getStatesSize(); i++) {
170 				header[i] = node.getStateAt(i);
171 			}
172 			
173 			float[][] data = evaluation.getEvidenceSetCM();
174 			Float[][] rowData = new Float[data.length][data[0].length];
175 			for (int i = 0; i < rowData.length; i++) {
176 				for (int j = 0; j < rowData[0].length; j++) {
177 					rowData[i][j] = data[i][j];
178 				}
179 			}
180 			
181 			evaluationPane.setUpOutputCM(rowData, header);
182 			
183 			evaluationPane.revalidate();
184 			evaluationPane.repaint();
185 		} catch (Exception e) {
186 			JOptionPane.showMessageDialog(evaluationPane, e.getMessage(), "Evaluation Error", JOptionPane.ERROR_MESSAGE);
187 			return;
188 		}
189 		
190 		// Reset evidences to all finding/conditional nodes, which means they do not have findings any more.  
191 		network.resetEvidences();
192 		
193 		finishEvaluation();
194 	}
195 	
196 	private void finishEvaluation() {
197 		evaluationPane.resetProgressBar();
198 		evaluationPane.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
199 	}
200 	
201 	public JPanel getView() {
202 		return evaluationPane;
203 	}
204 
205 }