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

📄 ieutils.java

📁 Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规则
💻 JAVA
字号:
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.mandarax.kernel.*;
import org.mandarax.util.TermIterator;

/**
 * Class containing some useful utilities for inference engine implementations.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 1.8
 */
public class IEUtils {
	/**
	 * Get an array of replacements for the variables passed in the query.
	 * @return an array of replacements
	 * @param node a derivation node
	 * @param query a query
	 */
	public static Replacement[] getReplacements(DerivationNodeImpl node, Clause query) {
		List vars = getVars(query);
		return getReplacements(node, vars);
	}
	/**
	 * Get an array of replacements for the variables.
	 * @return an array of replacements
	 * @param node a derivation node
	 * @param vars a list of variables
	 */
	public static Replacement[] getReplacements(DerivationNodeImpl node, List vars) {
		Replacement[] replacements = new Replacement[vars.size()];
		for (int i = 0; i < replacements.length; i++) {
			VariableTerm vt = (VariableTerm) vars.get(i);
			replacements[i] = new Replacement(vt, node.replace(vt));
		}
		return replacements;
	}
	/**
	* Retrieve the variables from a query.
	* @return a list of variables
	* @param q a query
	*/
	public static List getVars(Query q) {
		return getVars(getGoal(q));
	}
	/**
	* Retrieve the variables from a clause.
	* @return a list of variables
	* @param c a clause
	*/
	public static List getVars(Clause c) {
		List vars = new ArrayList();

		// can we assume a fact here .. this could be critical when working with
		// negation ! (perhaps introduce an interface Literal)
		Fact nextLiteral = null;
		Term next = null;

		for (Iterator itNeg = c.getNegativeLiterals().iterator(); itNeg.hasNext();) {
			nextLiteral = (Fact) itNeg.next();
			for (TermIterator e = nextLiteral.terms(); e.hasMoreTerms();) {
				next = e.nextTerm();
				addVars(vars, next);
			}
		}

		for (Iterator itPos = c.getPositiveLiterals().iterator(); itPos.hasNext();) {
			nextLiteral = (Fact) itPos.next();

			for (TermIterator e = nextLiteral.terms(); e.hasMoreTerms();) {
				next = e.nextTerm();
				addVars(vars, next);
			}
		}

		return vars;
	}
	/**
	* Retrieve the variables from a clause.
	* @return a list of variables
	* @param c a clause
	*/
	public static boolean containsVars(Fact f) {
		Term[] terms = f.getTerms();
		for (int i=0;i<terms.length;i++) {
			if (terms[i].containsVariables()) return true;
		}
		
		return false;
	}
	/**
	 * Retrieve the variables from a term and add them to the collection passed.
	 * @param vars a collection of variables
	 * @param term the term
	 */
	public static void addVars(List vars, Term term) {
		if(term.isVariable ()) {
			vars.add(term);
		}
		if(term.isCompound ()) {
			// next line can be improved .. we assume that any compound term is an instance of CompleTerm !!
			// perhaps add terms() to the interface Term
			ComplexTerm ct = (ComplexTerm) term;
			for(TermIterator e = ct.terms (); e.hasMoreTerms (); ) {
				addVars (vars, e.nextTerm ());
			}
		}
	}
	/**
	 * Build a goal from the query.
	 * @return a goal
	 * @param query a query
	 */
	public static Clause getGoal(Query query) {
		TmpClause nextClause = new TmpClause();
		Fact[] facts = query.getFacts();
		List lit = new ArrayList(facts.length);
		for (int i = 0; i < facts.length; i++)
			lit.add(facts[i]);
		nextClause.negativeLiterals = lit;
		nextClause.positiveLiterals = new ArrayList();
		return nextClause;
	}
}

⌨️ 快捷键说明

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