⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demoview.java

📁 Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规则
💻 JAVA
字号:
/*
 * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.mandarax.examples.family;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.UIManager;

import org.mandarax.kernel.ClauseSetException;
import org.mandarax.kernel.InferenceEngine;
import org.mandarax.kernel.InferenceException;
import org.mandarax.kernel.Query;
import org.mandarax.kernel.ResultSet;
import org.mandarax.kernel.VariableTerm;
import org.mandarax.reference.DefaultLogicFactory;
import org.mandarax.reference.DefaultLoopCheckingAlgorithm;
import org.mandarax.reference.ResolutionInferenceEngine;
import org.mandarax.util.ClauseIterator;

import test.org.mandarax.reference.TestInferenceEngine;
import test.org.mandarax.reference.TestInferenceEngine1;
import test.org.mandarax.reference.TestInferenceEngine10;
import test.org.mandarax.reference.TestInferenceEngine2;
import test.org.mandarax.reference.TestInferenceEngine3;
import test.org.mandarax.reference.TestInferenceEngine4;
import test.org.mandarax.reference.TestInferenceEngine5;
import test.org.mandarax.reference.TestInferenceEngine6;
import test.org.mandarax.reference.TestInferenceEngine7;
import test.org.mandarax.reference.TestInferenceEngine8;
import test.org.mandarax.reference.TestInferenceEngine9;

/**
 * Simple interface to display some demos. The demos are the test cases defined in the
 * mandarax test package.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 1.2
 */
public class DemoView extends javax.swing.JPanel {

	// model
	private Vector testCases = null;
	private TestInferenceEngine selectedTestCase = null;

	// widgets
	private JComboBox cbxTestCases = null;
	private JTextArea txtDescription = new JTextArea();
	private JList listKnowledge = new JList();
	private JTree treeProof = new JTree();
	private JLabel labQuery = new JLabel();
	private JLabel labExpectedResult = new JLabel();
	private JLabel labResult = new JLabel();
	private JButton butRun = new JButton("run:");
	private Action actRun = null;
	private JTabbedPane notebook = null;

	// private class to handle item events from the combobox containing the test cases
	private class SelectTestCaseAdapter implements ItemListener {

		// handle an item event
		public void itemStateChanged(ItemEvent e) {
			Object selected = e.getItem();

			if (selected == null) {
				setSelectedTestCase(null);
				butRun.setEnabled(false);
			}
			else {
				setSelectedTestCase((TestInferenceEngine) selected);
				butRun.setEnabled(true);
			}
		}
	}

	// renderer for the proof tree view
	private class ProofTreeRenderer extends javax.swing.tree.DefaultTreeCellRenderer {

		public Component getTreeCellRendererComponent(
			JTree tree,
			Object value,
			boolean selected,
			boolean expanded,
			boolean leaf,
			int row,
			boolean hasFocus) {
			boolean failed =
				((value != null)
					&& (value instanceof org.mandarax.kernel.DerivationNode)
					&& ((org.mandarax.kernel.DerivationNode) value).getState() == org.mandarax.kernel.DerivationNode.FAILED);

			setTextNonSelectionColor(failed ? Color.red : UIManager.getColor("Tree.selectionForeground"));
			setTextSelectionColor(failed ? Color.red : UIManager.getColor("Tree.textForeground"));

			return super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
		}

		public Icon getClosedIcon() {
			return null;
		}

		public Icon getOpenIcon() {
			return null;
		}

		public Icon getLeafIcon() {
			return null;
		}
	}

	/**
	 * Constructor.
	 */
	public DemoView() {
		super();

		initialize();
	}

	/**
	 * Display the result.
	 * @param result the result of the derivation
	 */
	private void displayResult(ResultSet result) {
			
		try {
			result.next();
			VariableTerm queryVar = (VariableTerm)result.getQueryVariables().get(0);
			labResult.setText(result.getResult(queryVar).toString());
			ProofStructure model = new ProofStructure(result.getProof().getRoot());
			treeProof.setModel(model);
		}
		catch (Throwable t) {
			treeProof.setModel(null);
		}
	}

	/**
	 * Get a new inference engine to run a test.
	 * @return an inference engine
	 */
	public static InferenceEngine getNewInferenceEngine() {
		ResolutionInferenceEngine ie = new ResolutionInferenceEngine();

		ie.setLoopCheckingAlgorithm(new DefaultLoopCheckingAlgorithm(10, 5, 5));

		return ie;
	}

	/**
	 * Get a new knowledge base to run a test.
	 * @return a knowledge base
	 */
	public static org.mandarax.kernel.KnowledgeBase getNewKnowledgeBase() {
		return new org.mandarax.reference.KnowledgeBase();
	}

	/**
	 * Get a collection of test cases.
	 * @return a collection of test cases (taken from the mandarax test package)
	 */
	protected Vector getTestCases() {
		if (testCases == null) {
			testCases = new Vector(8);

			testCases.add(new TestInferenceEngine1(getNewKnowledgeBase(), getNewInferenceEngine()));
			testCases.add(new TestInferenceEngine2(getNewKnowledgeBase(), getNewInferenceEngine()));
			testCases.add(new TestInferenceEngine3(getNewKnowledgeBase(), getNewInferenceEngine()));
			testCases.add(new TestInferenceEngine4(getNewKnowledgeBase(), getNewInferenceEngine()));
			testCases.add(new TestInferenceEngine5(getNewKnowledgeBase(), getNewInferenceEngine()));
			testCases.add(new TestInferenceEngine6(getNewKnowledgeBase(), getNewInferenceEngine()));
			testCases.add(new TestInferenceEngine7(getNewKnowledgeBase(), getNewInferenceEngine()));
			testCases.add(new TestInferenceEngine8(getNewKnowledgeBase(), getNewInferenceEngine()));
			testCases.add(new TestInferenceEngine9(getNewKnowledgeBase(), getNewInferenceEngine()));
			testCases.add(new TestInferenceEngine10(getNewKnowledgeBase(), getNewInferenceEngine()));
		}

		return testCases;
	}

	/**
	 * Initialize the view.
	 */
	protected void initialize() {

		// initialize logic : set a default logic factory
		 (new DefaultLogicFactory()).install();

		// set up the action to run a test case
		actRun = new AbstractAction() {

			public void actionPerformed(ActionEvent e) {
				if (selectedTestCase != null) {
					org.mandarax.kernel.KnowledgeBase kb = getNewKnowledgeBase();

					selectedTestCase.feedKnowledgeBase(kb);
					ResultSet result = null;
					Query query = selectedTestCase.getQuery();
					try {
						result = (getNewInferenceEngine()).query(query, kb,InferenceEngine.ONE,InferenceEngine.BUBBLE_EXCEPTIONS);
					}
					catch (InferenceException x) {
						JOptionPane.showMessageDialog(
							DemoView.this,
							"Inference Error, see console for details!",
							"",
							JOptionPane.WARNING_MESSAGE);
						x.printStackTrace(System.err);
					}

					displayResult(result);
					notebook.setSelectedIndex(1);
				}
			}
		};

		butRun.addActionListener(actRun);

		// list of all test cases
		cbxTestCases = new JComboBox(getTestCases());

		cbxTestCases.addItemListener(new SelectTestCaseAdapter());

		// initialize the layout
		initializeLayout();

		// preselect the first test case
		setSelectedTestCase((TestInferenceEngine) getTestCases().get(0));

		// initially disable the run button
		butRun.setEnabled(true);

		// set up tree
		treeProof.setModel(null);
		treeProof.setCellRenderer(new ProofTreeRenderer());
	}

	/**
	 * Initialize the layout - arrange all components and add them to the container.
	 */
	protected void initializeLayout() {
		setLayout(new BorderLayout(2, 2));

		notebook = new JTabbedPane(JTabbedPane.BOTTOM);

		// configure button bar
		JPanel buttonBar = new JPanel(new FlowLayout(FlowLayout.LEFT));

		buttonBar.add(butRun);
		buttonBar.add(cbxTestCases);

		// configure page 1
		JPanel page1 = new JPanel(new BorderLayout(2, 2));
		JPanel top1 = new JPanel(new FlowLayout(FlowLayout.LEFT));

		top1.add(new JLabel("  query:"));
		top1.add(labQuery);

		JPanel center1 = new JPanel(new BorderLayout(5, 5));

		center1.add(new JLabel("knowledge base:"), "North");
		center1.add(new JScrollPane(listKnowledge));
		page1.add(new JPanel(), "South");
		page1.add(new JPanel(), "East");
		page1.add(new JPanel(), "West");
		page1.add(center1, "Center");
		page1.add(top1, "North");
		notebook.add(" knowledge ", page1);

		// configure page 2
		JPanel page2 = new JPanel(new BorderLayout(2, 2));
		JPanel top2 = new JPanel(new FlowLayout(FlowLayout.LEFT));

		top2.add(new JLabel("  result:"));
		top2.add(labResult);

		JPanel center2 = new JPanel(new BorderLayout(5, 5));

		center2.add(new JLabel("proof:"), "North");
		center2.add(new JScrollPane(treeProof));
		page2.add(center2, "Center");
		page2.add(new JPanel(), "South");
		page2.add(new JPanel(), "East");
		page2.add(new JPanel(), "West");
		page2.add(top2, "North");
		notebook.add("  result  ", page2);

		// configute page 3
		JPanel page3 = new JPanel(new BorderLayout(2, 2));

		page3.add(new JPanel(), "North");
		page3.add(new JPanel(), "South");
		page3.add(new JPanel(), "East");
		page3.add(new JPanel(), "West");
		txtDescription.setEditable(false);
		page3.add(new JScrollPane(txtDescription), "Center");
		notebook.add("description", page3);

		// add the notebook itself to the panel
		add(notebook, "Center");
		add(buttonBar, "North");
		add(new JPanel(), "South");
		add(new JPanel(), "West");
		add(new JPanel(), "East");
	}

	/**
	 * Set the selected test case.
	 * @param aTestCase the new selected test case
	 */
	private void setSelectedTestCase(TestInferenceEngine aTestCase) {
		selectedTestCase = aTestCase;

		// update view settings here
		if (selectedTestCase == null) {
			txtDescription.setText("");
			listKnowledge.setListData(new Vector());
			labQuery.setText("");
			labResult.setText("");
			treeProof.setModel(null);
		}
		else {
			txtDescription.setText(selectedTestCase.getDescription());

			// fetch knowledge, represent as list and display it
			org.mandarax.kernel.KnowledgeBase kb = getNewKnowledgeBase();

			selectedTestCase.feedKnowledgeBase(kb);

			Vector collect = new Vector();

			try {
				for (ClauseIterator it = kb.clauses(); it.hasMoreClauses();) {
					collect.add(it.nextClause());
				}
			}
			catch (ClauseSetException x) {

				JOptionPane.showMessageDialog(
					DemoView.this,
					"Error initializing knowledge base, see console for details!",
					"",
					JOptionPane.WARNING_MESSAGE);
				x.printStackTrace(System.err);
			}

			listKnowledge.setListData(collect);

			// set query
			labQuery.setText(selectedTestCase.getQuery().toString());

			// set result (set dummies, see actRun !)
			labResult.setText("<press run button!>");
			treeProof.setModel(null);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -