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

📄 factimpl.java

📁 Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规则
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.mandarax.reference;

/*
 * 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
 */
import java.util.*;
import org.apache.commons.collections.iterators.SingletonIterator;
import org.mandarax.kernel.*;

/**
 * Reference implementation for (simple) facts. In older versions (prior to 1.9)
 * there was no separate Query interface/class - facts were used instead.
 * In order to facilitate maintanance of older applications, this class still implements
 * Query. Some newer features, in particular DerivationEventListeners, are not supported.
 * For queries, QueryImpl should be used instead.
 * @see QueryImpl
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 1.1
 * Prova re-integration modifications
 * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
 * @version 3.4 <7 March 05>
 */
public class FactImpl extends Complex implements Fact,Query {

	private java.util.List posLiterals = null;
	private static java.util.List negLiterals = new java.util.Vector();
	private Predicate predicate = null;
	private transient Vector clauseSetChangeListener = new Vector();
	private ClauseSet container = this;
	// utility vars for the query interface
	private Fact[] facts = {this};
	private String name = null;
	private Properties delegate = null;
	private int containsVariables = -1;

	/**
	 * Constructor.
	 */
	public FactImpl() {
		super ();
	}
	/**
	 * Constructor.
	 * @param p the predicate
	 */
	FactImpl(org.mandarax.kernel.Predicate p) {
		super ();
		setPredicate (p);
	}
	/**
	 * Constructor.
	 * @param p the predicate
	 * @param t the terms
	 */
	FactImpl(org.mandarax.kernel.Predicate p, Term[] t) {
		super ();
		setPredicate (p);
		for(int i = 0; i < t.length; i++) {
			setTerm (t[i], i);
		}
	}
	/**
	 * Add a clause set listener.
	 * @param l a listener
	 */
	public void addClauseSetChangeListener(ClauseSetChangeListener l) {
		clauseSetChangeListener.add (l);
	}
	/**
	 * Apply a set of replacements to a clause. Returns a new clause!
	 * @return the fact (clause) resulting from the application of the replacements
	 * @param r a collection of replacements
	 */
	public Clause apply(java.util.Collection r) {
		return applyToFact (r);
	}
	/**
	 * Apply a single replacement to a fact. Returns a new clause!
	 * @return the fact (clause) resulting from the application of the replacemen
	 * @param r the replacement
	 */
	public Clause apply(Replacement r) {
		return applyToFact (r);
	}
	/**
	 * Apply a set of replacements to a fact. Returns a new fact!
	 * @return the fact resulting from the application of the replacement
	 * @param r a collection of replacement
	 */
	public Fact applyToFact(java.util.Collection r) {
		Term[] t       = getTerms ();
		Term   newTerm = null;

		// create a new fact
		FactImpl f = new FactImpl ();
		copyStructure (f);
		for(int i = 0; i < t.length; i++) {
			newTerm = t[i];
			for(java.util.Iterator it = r.iterator (); it.hasNext (); ) {
				newTerm = newTerm.apply ((Replacement) it.next ());
			}
			f.setTerm (newTerm, i);
		}

		return f;
	}
	/**
	 * Apply a single replacement to a fact. Returns a new fact!
	 * @return the fact (clause) resulting from the application of the replacement
	 * @param r a replacement
	 */
	public Fact applyToFact(Replacement r) {
		Term[] t = getTerms ();
		// create a new fact
		FactImpl f = new FactImpl ();
		copyStructure (f);
		for(int i = 0; i < t.length; i++) {
			f.setTerm (t[i].apply (r), i);
		}
		return f;
	}

	/**
	 * Whether the fact contains variables
	 * @return boolean
	 */
	public boolean isBound() {
		if( containsVariables >= 0 )
			return containsVariables==0;

		Term[] tt = this.getTerms();
		for( int i=0; i<tt.length; i++ ) {
			if( tt[i].containsVariables() ) {
				containsVariables = 1;
				return false;
			}
		}
		containsVariables = 0;
		return true;
	}

	/**
	 * Get an iterator for clauses.
	 * @return a clause iterator
	 */
	public org.mandarax.util.ClauseIterator clauses() {
		return new org.mandarax.util.SingleClauseIterator (this);
	}
	/**
	 * Get an iterator for clauses. The parameters are ignored and <code>clauses()</code> is called!
	 * @return a clause iterator
	 * @param query a query
	 * @param additionalParameter an additional parameter
	 */
	public org.mandarax.util.ClauseIterator clauses(Clause query,Object additionalParameter) {
		return clauses ();
	}
	/**
	 * Copy the structure, i.e. length of the terms, types of the terms and methods,
	 * @param f the fact that takes the structure from this object
	 */
	protected void copyStructure(FactImpl f) {
		f.types     = types;
		f.terms     = new Term[terms.length];
		f.predicate = predicate;
	}
	/**
	 * Check whether objects are equal.
	 * @param obj the object to compare this object with
	 * @return true if the objects are equal, false otherwise
	 */
	public boolean equals(Object obj) {
		if((obj != null) && (obj instanceof FactImpl)) {
			FactImpl f      = (FactImpl) obj;
			boolean  result = (predicate == null)
							  ? f.predicate == null
							  : predicate.equals (f.predicate);
			Term[]   terms1 = terms;
			Term[]   terms2 = f.terms;
			if (terms1==null) result = result && terms2==null;
			else {
				for(int i = 0; i < terms.length; i++) {
					result = result
							 && (((terms[i] == null) && (terms2[i] == null))
								 || (terms1[i].equals (terms2[i])));

					if( !result) {
						return false;
					}
				}
			}
			return result;
		}
		return false;
	}
	/**
	 * Fire a clause set change event
	 * @param e the event
	 */
	protected void fireClauseSetChangeEvent(ClauseSetChangeEvent e) {
		ClauseSetChangeListener l;

		for(Iterator it = clauseSetChangeListener.iterator (); it.hasNext (); ) {
			l = (ClauseSetChangeListener) it.next ();
			l.clauseSetChanged (e);
		}
	}
	/**
	 * Get the object constructing the complex.
	 * @return the predicate of this fact
	 */
	protected org.mandarax.kernel.Constructor getConstructor() {
		return getPredicate ();
	}
	/**
	 * Get the clause set containing the clause. This method is in particular useful for analyzing derivations,
	 * since derivations show only the clauses used, not the clause set generating this clauses (e.g., AutoFacts). On the other
	 * hand, knowledge bases contain often clause sets. So it could be hard to find the clause sets in the knowledge base
	 * that caused a certain result. For some clauses such as facts, the container is just the fact itself.
	 * @return a clause set
	 */
	public ClauseSet getContainer() {
		return container;
	}
	/**
	 * Get a key for fast access. The key is usually the predicate, but subclasses
	 * might want to use different keys. Note that keys are mainly used by inference engines and
	 * knowledge bases to improve performance and inference engine and knowledge bases
	 * must know about the semantics of keys to use them correctly!
	 * @return the key object
	 */
	public Object getKey() {
		return getPredicate ();
	}
	/**
	 * Get the negative literals.
	 * @return the collection of negative literals, this is an empty collection!
	 */
	public java.util.List getNegativeLiterals() {
		return negLiterals;
	}

⌨️ 快捷键说明

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