multipleclausesetiterator.java

来自「Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规」· Java 代码 · 共 139 行

JAVA
139
字号
/*
 * 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.util;

import java.util.Iterator;

import org.mandarax.kernel.Clause;
import org.mandarax.kernel.ClauseSet;
import org.mandarax.kernel.ClauseSetException;

/**
 * Iterator for a collection containing clause sets and clauses.
 * New version - no explicit merged collection is needed. This hould result
 * in performance improvements for bot the knowledge base and the inference engine.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 1.0
 */
public final class MultipleClauseSetIterator extends AbstractClauseIterator {

	private Clause nextClause = null;
	private boolean useParameters = false;
	private ClauseIterator nextClauseIterator = null;
	private Iterator iterator = null;
	private Clause par1 = null;
	private Object par2 = null;

	/**
	 * Constructor.
	 * @param coll a collection of clause sets
	 */
	public MultipleClauseSetIterator(java.util.Collection coll) {
		super();

		iterator = coll.iterator();
		useParameters = false;
	}

	/**
	 * Constructor.
	 * @param coll a collection of clause sets
	 * @param query org.mandarax.kernel.Clause
	 * @param additionalParameter java.lang.Object
	 */
	public MultipleClauseSetIterator(
		java.util.Collection coll,
		Clause query,
		Object additionalParameter) {
		super();

		iterator = coll.iterator();
		useParameters = true;
		par1 = query;
		par2 = additionalParameter;
	}

	/**
	 * Indicates whether there are more clauses.
	 * For performance, clauses get special treatment
	 * @return true if there are more clauses, false otherwise
	 * @throws ClauseSetException
	 */
	public boolean hasMoreClauses() throws ClauseSetException {

		// initialize - first step (or after there was a clause)
		if (nextClauseIterator == null) {

			// set the first clause set clause set
			if (iterator.hasNext()) {
				ClauseSet cs = (ClauseSet) iterator.next();

				if ((cs instanceof Clause) && ((Clause) cs).isAtomic()) {
					nextClause = (Clause) cs;
					nextClauseIterator = null;

					return true;
				}

				nextClauseIterator = useParameters ? cs.clauses(par1, par2) : cs.clauses();

				return hasMoreClauses();
			}
			else {
				return false;
			}
		}

		// regular step
		if (nextClauseIterator.hasMoreClauses()) {
			nextClause = nextClauseIterator.nextClause();

			return true;
		}
		else {
			if (iterator.hasNext()) {
				ClauseSet cs = (ClauseSet) iterator.next();
				// bugfix 26 Oct 04 by Jens Dietrich : test for atomic clauses here as well
				// this addresses bug reported by gabriel.quennesson@seam.fr 20 Oct 04
				if ((cs instanceof Clause) && ((Clause) cs).isAtomic()) {
					nextClause = (Clause) cs;
					nextClauseIterator = null;

					return true;
				}

				nextClauseIterator = useParameters ? cs.clauses(par1, par2) : cs.clauses();

				return hasMoreClauses();
			}
			else {
				return false;
			}
		}
	}

	/**
	 * Get the next clause.
	 * @return the next clause
	 */
	public org.mandarax.kernel.Clause nextClause() {
		return nextClause;
	}
}

⌨️ 快捷键说明

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