001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.examples.family;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Color;
022: import java.awt.Component;
023: import java.awt.FlowLayout;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ItemEvent;
026: import java.awt.event.ItemListener;
027: import java.util.Vector;
028:
029: import javax.swing.AbstractAction;
030: import javax.swing.Action;
031: import javax.swing.Icon;
032: import javax.swing.JButton;
033: import javax.swing.JComboBox;
034: import javax.swing.JLabel;
035: import javax.swing.JList;
036: import javax.swing.JOptionPane;
037: import javax.swing.JPanel;
038: import javax.swing.JScrollPane;
039: import javax.swing.JTabbedPane;
040: import javax.swing.JTextArea;
041: import javax.swing.JTree;
042: import javax.swing.UIManager;
043:
044: import org.mandarax.kernel.ClauseSetException;
045: import org.mandarax.kernel.InferenceEngine;
046: import org.mandarax.kernel.InferenceException;
047: import org.mandarax.kernel.Query;
048: import org.mandarax.kernel.ResultSet;
049: import org.mandarax.kernel.VariableTerm;
050: import org.mandarax.reference.DefaultLogicFactory;
051: import org.mandarax.reference.DefaultLoopCheckingAlgorithm;
052: import org.mandarax.reference.ResolutionInferenceEngine;
053: import org.mandarax.util.ClauseIterator;
054:
055: import test.org.mandarax.reference.TestInferenceEngine;
056: import test.org.mandarax.reference.TestInferenceEngine1;
057: import test.org.mandarax.reference.TestInferenceEngine10;
058: import test.org.mandarax.reference.TestInferenceEngine2;
059: import test.org.mandarax.reference.TestInferenceEngine3;
060: import test.org.mandarax.reference.TestInferenceEngine4;
061: import test.org.mandarax.reference.TestInferenceEngine5;
062: import test.org.mandarax.reference.TestInferenceEngine6;
063: import test.org.mandarax.reference.TestInferenceEngine7;
064: import test.org.mandarax.reference.TestInferenceEngine8;
065: import test.org.mandarax.reference.TestInferenceEngine9;
066:
067: /**
068: * Simple interface to display some demos. The demos are the test cases defined in the
069: * mandarax test package.
070: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
071: * @version 3.4 <7 March 05>
072: * @since 1.2
073: */
074: public class DemoView extends javax.swing.JPanel {
075:
076: // model
077: private Vector testCases = null;
078: private TestInferenceEngine selectedTestCase = null;
079:
080: // widgets
081: private JComboBox cbxTestCases = null;
082: private JTextArea txtDescription = new JTextArea();
083: private JList listKnowledge = new JList();
084: private JTree treeProof = new JTree();
085: private JLabel labQuery = new JLabel();
086: private JLabel labExpectedResult = new JLabel();
087: private JLabel labResult = new JLabel();
088: private JButton butRun = new JButton("run:");
089: private Action actRun = null;
090: private JTabbedPane notebook = null;
091:
092: // private class to handle item events from the combobox containing the test cases
093: private class SelectTestCaseAdapter implements ItemListener {
094:
095: // handle an item event
096: public void itemStateChanged(ItemEvent e) {
097: Object selected = e.getItem();
098:
099: if (selected == null) {
100: setSelectedTestCase(null);
101: butRun.setEnabled(false);
102: } else {
103: setSelectedTestCase((TestInferenceEngine) selected);
104: butRun.setEnabled(true);
105: }
106: }
107: }
108:
109: // renderer for the proof tree view
110: private class ProofTreeRenderer extends
111: javax.swing.tree.DefaultTreeCellRenderer {
112:
113: public Component getTreeCellRendererComponent(JTree tree,
114: Object value, boolean selected, boolean expanded,
115: boolean leaf, int row, boolean hasFocus) {
116: boolean failed = ((value != null)
117: && (value instanceof org.mandarax.kernel.DerivationNode) && ((org.mandarax.kernel.DerivationNode) value)
118: .getState() == org.mandarax.kernel.DerivationNode.FAILED);
119:
120: setTextNonSelectionColor(failed ? Color.red : UIManager
121: .getColor("Tree.selectionForeground"));
122: setTextSelectionColor(failed ? Color.red : UIManager
123: .getColor("Tree.textForeground"));
124:
125: return super .getTreeCellRendererComponent(tree, value,
126: selected, expanded, leaf, row, hasFocus);
127: }
128:
129: public Icon getClosedIcon() {
130: return null;
131: }
132:
133: public Icon getOpenIcon() {
134: return null;
135: }
136:
137: public Icon getLeafIcon() {
138: return null;
139: }
140: }
141:
142: /**
143: * Constructor.
144: */
145: public DemoView() {
146: super ();
147:
148: initialize();
149: }
150:
151: /**
152: * Display the result.
153: * @param result the result of the derivation
154: */
155: private void displayResult(ResultSet result) {
156:
157: try {
158: result.next();
159: VariableTerm queryVar = (VariableTerm) result
160: .getQueryVariables().get(0);
161: labResult.setText(result.getResult(queryVar).toString());
162: ProofStructure model = new ProofStructure(result.getProof()
163: .getRoot());
164: treeProof.setModel(model);
165: } catch (Throwable t) {
166: treeProof.setModel(null);
167: }
168: }
169:
170: /**
171: * Get a new inference engine to run a test.
172: * @return an inference engine
173: */
174: public static InferenceEngine getNewInferenceEngine() {
175: ResolutionInferenceEngine ie = new ResolutionInferenceEngine();
176:
177: ie.setLoopCheckingAlgorithm(new DefaultLoopCheckingAlgorithm(
178: 10, 5, 5));
179:
180: return ie;
181: }
182:
183: /**
184: * Get a new knowledge base to run a test.
185: * @return a knowledge base
186: */
187: public static org.mandarax.kernel.KnowledgeBase getNewKnowledgeBase() {
188: return new org.mandarax.reference.KnowledgeBase();
189: }
190:
191: /**
192: * Get a collection of test cases.
193: * @return a collection of test cases (taken from the mandarax test package)
194: */
195: protected Vector getTestCases() {
196: if (testCases == null) {
197: testCases = new Vector(8);
198:
199: testCases.add(new TestInferenceEngine1(
200: getNewKnowledgeBase(), getNewInferenceEngine()));
201: testCases.add(new TestInferenceEngine2(
202: getNewKnowledgeBase(), getNewInferenceEngine()));
203: testCases.add(new TestInferenceEngine3(
204: getNewKnowledgeBase(), getNewInferenceEngine()));
205: testCases.add(new TestInferenceEngine4(
206: getNewKnowledgeBase(), getNewInferenceEngine()));
207: testCases.add(new TestInferenceEngine5(
208: getNewKnowledgeBase(), getNewInferenceEngine()));
209: testCases.add(new TestInferenceEngine6(
210: getNewKnowledgeBase(), getNewInferenceEngine()));
211: testCases.add(new TestInferenceEngine7(
212: getNewKnowledgeBase(), getNewInferenceEngine()));
213: testCases.add(new TestInferenceEngine8(
214: getNewKnowledgeBase(), getNewInferenceEngine()));
215: testCases.add(new TestInferenceEngine9(
216: getNewKnowledgeBase(), getNewInferenceEngine()));
217: testCases.add(new TestInferenceEngine10(
218: getNewKnowledgeBase(), getNewInferenceEngine()));
219: }
220:
221: return testCases;
222: }
223:
224: /**
225: * Initialize the view.
226: */
227: protected void initialize() {
228:
229: // initialize logic : set a default logic factory
230: (new DefaultLogicFactory()).install();
231:
232: // set up the action to run a test case
233: actRun = new AbstractAction() {
234:
235: public void actionPerformed(ActionEvent e) {
236: if (selectedTestCase != null) {
237: org.mandarax.kernel.KnowledgeBase kb = getNewKnowledgeBase();
238:
239: selectedTestCase.feedKnowledgeBase(kb);
240: ResultSet result = null;
241: Query query = selectedTestCase.getQuery();
242: try {
243: result = (getNewInferenceEngine()).query(query,
244: kb, InferenceEngine.ONE,
245: InferenceEngine.BUBBLE_EXCEPTIONS);
246: } catch (InferenceException x) {
247: JOptionPane
248: .showMessageDialog(
249: DemoView.this ,
250: "Inference Error, see console for details!",
251: "", JOptionPane.WARNING_MESSAGE);
252: x.printStackTrace(System.err);
253: }
254:
255: displayResult(result);
256: notebook.setSelectedIndex(1);
257: }
258: }
259: };
260:
261: butRun.addActionListener(actRun);
262:
263: // list of all test cases
264: cbxTestCases = new JComboBox(getTestCases());
265:
266: cbxTestCases.addItemListener(new SelectTestCaseAdapter());
267:
268: // initialize the layout
269: initializeLayout();
270:
271: // preselect the first test case
272: setSelectedTestCase((TestInferenceEngine) getTestCases().get(0));
273:
274: // initially disable the run button
275: butRun.setEnabled(true);
276:
277: // set up tree
278: treeProof.setModel(null);
279: treeProof.setCellRenderer(new ProofTreeRenderer());
280: }
281:
282: /**
283: * Initialize the layout - arrange all components and add them to the container.
284: */
285: protected void initializeLayout() {
286: setLayout(new BorderLayout(2, 2));
287:
288: notebook = new JTabbedPane(JTabbedPane.BOTTOM);
289:
290: // configure button bar
291: JPanel buttonBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
292:
293: buttonBar.add(butRun);
294: buttonBar.add(cbxTestCases);
295:
296: // configure page 1
297: JPanel page1 = new JPanel(new BorderLayout(2, 2));
298: JPanel top1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
299:
300: top1.add(new JLabel(" query:"));
301: top1.add(labQuery);
302:
303: JPanel center1 = new JPanel(new BorderLayout(5, 5));
304:
305: center1.add(new JLabel("knowledge base:"), "North");
306: center1.add(new JScrollPane(listKnowledge));
307: page1.add(new JPanel(), "South");
308: page1.add(new JPanel(), "East");
309: page1.add(new JPanel(), "West");
310: page1.add(center1, "Center");
311: page1.add(top1, "North");
312: notebook.add(" knowledge ", page1);
313:
314: // configure page 2
315: JPanel page2 = new JPanel(new BorderLayout(2, 2));
316: JPanel top2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
317:
318: top2.add(new JLabel(" result:"));
319: top2.add(labResult);
320:
321: JPanel center2 = new JPanel(new BorderLayout(5, 5));
322:
323: center2.add(new JLabel("proof:"), "North");
324: center2.add(new JScrollPane(treeProof));
325: page2.add(center2, "Center");
326: page2.add(new JPanel(), "South");
327: page2.add(new JPanel(), "East");
328: page2.add(new JPanel(), "West");
329: page2.add(top2, "North");
330: notebook.add(" result ", page2);
331:
332: // configute page 3
333: JPanel page3 = new JPanel(new BorderLayout(2, 2));
334:
335: page3.add(new JPanel(), "North");
336: page3.add(new JPanel(), "South");
337: page3.add(new JPanel(), "East");
338: page3.add(new JPanel(), "West");
339: txtDescription.setEditable(false);
340: page3.add(new JScrollPane(txtDescription), "Center");
341: notebook.add("description", page3);
342:
343: // add the notebook itself to the panel
344: add(notebook, "Center");
345: add(buttonBar, "North");
346: add(new JPanel(), "South");
347: add(new JPanel(), "West");
348: add(new JPanel(), "East");
349: }
350:
351: /**
352: * Set the selected test case.
353: * @param aTestCase the new selected test case
354: */
355: private void setSelectedTestCase(TestInferenceEngine aTestCase) {
356: selectedTestCase = aTestCase;
357:
358: // update view settings here
359: if (selectedTestCase == null) {
360: txtDescription.setText("");
361: listKnowledge.setListData(new Vector());
362: labQuery.setText("");
363: labResult.setText("");
364: treeProof.setModel(null);
365: } else {
366: txtDescription.setText(selectedTestCase.getDescription());
367:
368: // fetch knowledge, represent as list and display it
369: org.mandarax.kernel.KnowledgeBase kb = getNewKnowledgeBase();
370:
371: selectedTestCase.feedKnowledgeBase(kb);
372:
373: Vector collect = new Vector();
374:
375: try {
376: for (ClauseIterator it = kb.clauses(); it
377: .hasMoreClauses();) {
378: collect.add(it.nextClause());
379: }
380: } catch (ClauseSetException x) {
381:
382: JOptionPane
383: .showMessageDialog(
384: DemoView.this ,
385: "Error initializing knowledge base, see console for details!",
386: "", JOptionPane.WARNING_MESSAGE);
387: x.printStackTrace(System.err);
388: }
389:
390: listKnowledge.setListData(collect);
391:
392: // set query
393: labQuery.setText(selectedTestCase.getQuery().toString());
394:
395: // set result (set dummies, see actRun !)
396: labResult.setText("<press run button!>");
397: treeProof.setModel(null);
398: }
399: }
400: }
|