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

📄 genericdriver.java

📁 Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规则
💻 JAVA
字号:
package org.mandarax.zkb.framework;

/**
 *  Copyright (C) 1999-2004  Jens Dietrich (mailto:mandarax@jbdietrich.com)
 *
 *  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
 */

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.mandarax.kernel.KnowledgeBase;
import org.mandarax.kernel.LogicFactory;
import org.mandarax.util.NullWriter;
import org.mandarax.zkb.ObjectPersistencyService;
import org.mandarax.zkb.ZKBDriver;
import org.mandarax.zkb.ZKBException;
/**
 * A generic driver that uses adapter to map objects to XML.<br>
 * The driver can be more or less fault tolerant. The level of fault tolerance
 * can be set using the respective constants. Furthermore, we can set
 * a print writer that the adapter can use to report events or problems that do not lead
 * to exceptions. The fault tolerance concepts applies in particular on the level of
 * knowledge bases. Here the following guidelines should be used:
 * <ul>
 * <li>If the fault tolerance level is medium, each problem should interrupt the export
 * or import and the error should be "bubbled up"
 * <li>If the level is medium, the export or import can continue even id it fails for some clauses.
 * But the log writer should be used to post a descriptive message, and (for export) a comment
 * should be inserted into the document where the clause set is supposed to be.
 * <li>If the level is high, problems with export or import of single clauses are just ignored.
 * </ul>
 * The default fault tolerance level is medium.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 2.2
 */
public abstract class GenericDriver implements ZKBDriver, TagAndAttributeNames {

	protected PrintWriter out = NullWriter.printWriter;
	protected int faultToleranceLevel = GenericDriver.DEFAULT_FAULT_TOLERANCE;

	// adapters by tag name - useful to locate adapters to import
	private Map adapters = new Hashtable();
	private List adaptersAsList = new ArrayList();

	// fault tolerance levels
	public static final int LOW_FAULT_TOLERANCE = 0;
	public static final int MEDIUM_FAULT_TOLERANCE = 1;
	public static final int HIGH_FAULT_TOLERANCE = 2;
	public static final int DEFAULT_FAULT_TOLERANCE = MEDIUM_FAULT_TOLERANCE;

	// dtd policy
	private int dtdRefPolicy = NO_DTD_REF;

	/**
	 * Constructor.
	 */
	public GenericDriver() {
		super();
	}
	/**
	 * Install an adapter.
	 * @param adapter an adapter
	 */
	public void install(Adapter adapter) {
		adapters.put(adapter.getTagName(), adapter);
		adaptersAsList.add(adapter);
	}

	/**
	 * Get an adapter by tag name.
	 * @param tagName a text indicating the name of an xml element
	 */
	public Adapter getAdapter(String tagName) throws ZKBException {
		Adapter adapter = (Adapter) adapters.get(tagName);
		if (adapter == null)
			throw new ZKBException(
				"Cannot find adapter for tag <" + tagName + ">");
		return adapter;
	}
	/**
	 * Get a short text describing the driver.
	 * @return a text
	 */
	public String getDescription() {
		return "Generic driver for knowledge bases";
	}
	/**
	 * Get the name of the driver.
	 * @return a text
	 */
	public String getName() {
		return "Generic Driver";
	}
	/**
	 * Get the root element type.
	 * @return the name of the root element
	 */
	protected abstract String getRootElementType();
	/**
	 * Export a knowledge base, i.e., convert it into an xml document.
	 * All encountered object references should be registered with the ops.
	 * @return an xml document
	 * @param kb a knowledge base
	 * @param ops an object persistency service
	 * @exception a ZKBException is thrown if export fails
	 */
	public Document exportKnowledgeBase(KnowledgeBase kb,ObjectPersistencyService ops) throws ZKBException {
		Adapter adapter = getAdapter(getRootElementType());
		DocType docType = null;
		if (dtdRefPolicy == EXTERNAL_DTD_REF) {
			docType = new DocType(getRootElementType(),getURL4DTD());
		}
		else if (dtdRefPolicy == INTERNAL_DTD_REF) {
			docType = new DocType(getRootElementType());
			StringBuffer buf = new StringBuffer();
			for (int i=0;i<adaptersAsList.size();i++) {
				((Adapter)adaptersAsList.get(i)).printDTD(buf);
			}
			docType.setInternalSubset(buf.toString());			
		}
		Document doc = null;
		if (docType == null) doc = new Document(adapter.exportObject(kb, this, ops));
		else doc = new Document(adapter.exportObject(kb, this, ops),docType);

		return doc;
	}
	/**
	 * Import a knowledge base, i.e., build it from an xml document.
	 * @return a knowledge base
	 * @param doc an xml document
	 * @param ops an object persistency service
	 * @exception a ZKBException is thrown if import fails
	 */
	public KnowledgeBase importKnowledgeBase(Document doc,ObjectPersistencyService ops)	throws ZKBException {
		Element root = doc.getRootElement();
		Adapter adapter = getAdapter(root.getName());
		return (KnowledgeBase) adapter.importObject(root,this,ops,	LogicFactory.getDefaultFactory());
	}
	/**
	 * Import a knowledge base.
	 * @return a knowledge base
	 * @param e an xml element
	 * @param ops an object persistency service
	 * @exception an ZKBException is thrown if import fails
	 */
	private KnowledgeBase importKnowledgeBase(Element e) throws ZKBException {
		KnowledgeBase kb = (KnowledgeBase) this.getAdapter(e.getName());
		return kb;
	}
	/**
	 * Indicates whether the driver (and the underlying xml format (=dtd))
	 * supports auto facts.
	 * @return a boolean
	 */
	public boolean supportsAutoFacts() {
		return false;
	}
	/**
	 * Indicates whether the driver (and the underlying xml format (=dtd))
	 * supports clause sets.
	 * @return a boolean
	 */
	public boolean supportsClauseSets() {
		return true;
	}
	/**
	 * Indicates whether the driver (and the underlying xml format (=dtd))
	 * supports facts. (some formats might see facts as rules without body)
	 * @return a boolean
	 */
	public boolean supportsFacts() {
		return true;
	}
	/**
	 * Indicates whether the driver (and the underlying xml format (=dtd))
	 * supports functions.
	 * @return a boolean
	 */
	public boolean supportsFunctions() {
		return true;
	}
	/**
	 * Indicates whether the driver (and the underlying xml format (=dtd))
	 * supports the java semantics (e.g. JFunctions, JPredicate).
	 * @return a boolean
	 */
	public boolean supportsJavaSemantics() {
		return true;
	}
	/**
	 * Indicates whether the driver (and the underlying xml format (=dtd))
	 * supports multiple premises.
	 * @return a boolean
	 */
	public boolean supportsMultiplePremises() {
		return true;
	}
	/**
	 * Indicates whether the driver (and the underlying xml format (=dtd))
	 * supports multiple premises connected by OR.
	 * @return a boolean
	 */
	public boolean supportsOrPremises() {
		return true;
	}
	/**
	 * Indicates whether the driver (and the underlying xml format (=dtd))
	 * supports types.
	 * @return a boolean
	 */
	public boolean supportsTypes() {
		return true;
	}
	/**
	 * Indicates whether the driver (and the underlying xml format (=dtd))
	 * supports queries.
	 * @return a boolean
	 */
	public boolean supportsQueries() {
		return adapters != null && adapters.containsKey(QUERY);
	}
	/**
	 * Get the log writer.
	 * @return a print writer
	 */
	public PrintWriter getLogWriter() {
		return out==null?NullWriter.printWriter:out;
	}
	/**
	 * Set the log writer.
	 * @param pw a print writer
	 */
	public void setLogWriter(PrintWriter pw) {
		out = pw;
	}
	/**
	 * Get the fault tolerance level.
	 * @return a number (see the constants defined in GenericDriver)
	 */
	public int getFaultToleranceLevel() {
		return faultToleranceLevel;
	}
	/**
	 * Set the fault tolerance level.
	 * @param level a number (see the constants defined in GenericDriver)
	 */
	public void setFaultToleranceLevel(int level) {
		faultToleranceLevel = level;
	}
	/**
	 * Get the adapters as a list. 
	 * @return a list.
	 */
	List getAdapters() {
		return adaptersAsList;
	}

	/**
	 * Get the dtd reference policy.
	 * @return int
	 */
	public int getDtdRefPolicy() {
		return dtdRefPolicy;
	}

	/**
	 * Sets the dtd reference policy.
	 * @param dtdRefPolicy The dtd reference policy to set
	 */
	public void setDtdRefPolicy(int dtdRefPolicy) {
		this.dtdRefPolicy = dtdRefPolicy;
	}

}

⌨️ 快捷键说明

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