testkb.java

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

JAVA
117
字号
/*
 * 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 test.org.mandarax.util.resultsetfilters;

import java.text.DateFormat;
import java.util.*;
import org.mandarax.kernel.*;
import org.mandarax.reference.AdvancedKnowledgeBase;
import org.mandarax.reference.DefaultInferenceEngine;

/**
 * Utility class to create test knowledge bases.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 3.0
 */



public class TestKB implements ExampleConstants {

	
	static Term[] queryVariables = new Term[] {FNAME,NAME,DOB,WEIGHT,SIZE};
	private static KnowledgeBase kb = null;
	static boolean initialized = false;
	private static Predicate predicate1 = new SimplePredicate("people", new Class[] { String.class, String.class, java.sql.Date.class, Integer.class, Double.class});
	static DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);

	/**
	 * Query all facts.
	 * @return a result set
	 * @throws an Exception
	 */
	public static ResultSet queryAll() throws Exception {
		Query query = lfs.query(
			lfs.fact(predicate1,queryVariables), 
			"query for all facts");
		InferenceEngine ie = new DefaultInferenceEngine();
		return ie.query(query,getKB(),InferenceEngine.ALL,InferenceEngine.BUBBLE_EXCEPTIONS);
	}
	
	/**
	 * Get the knowledge base.
	 * @return a knowledge base
	 */
	public static KnowledgeBase getKB() throws Exception {
		createKB();
		return kb;
	}
	private static KnowledgeBase createKB() throws Exception {
		if (kb != null) return kb;
		predicate1.setSlotNames(new String[] {"first_name","name","dob","weight","size"});
		kb = new AdvancedKnowledgeBase();
		addFact("Tom", "Meier", "01/01/1960", 80, 1.80);
		addFact("John", "Meier", "01/01/1960", 90, 1.90);
		addFact("Jim", "Smith", "01/01/1970", 100, 1.90);
		addFact("Jack", "Smith", "01/01/1970", 60, 1.60);
		addFact("Tom", "Taylor", "01/01/1980", 70, 1.70);
		addFact("John", "Meijer", "01/01/1980", 80, 1.80);
		addFact("Jim", "Mejer", "01/01/1990", 50, 1.50);
		return kb;
	}
	/**
	 * Create a fact in the kb.
	 */
	private static void addFact(String fname, String name, String dob, int weight, double size) throws Exception {
		addFact(fname, name, df.parse(dob), weight, size);
	}
	/**
	 * Create a fact in the kb.
	 */
	private static void addFact(String fname, String name, Date dob, int weight, double size) {
		// convert util date to sql date
		java.sql.Date sqlDOB = new java.sql.Date(dob.getTime());
		kb.add(
			lfs.fact(
				predicate1,
				new Term[] {
					lfs.cons(fname, String.class),
					lfs.cons(name, String.class),
					lfs.cons(sqlDOB, java.sql.Date.class),
					lfs.cons(new Integer(weight), Integer.class),
					lfs.cons(new Double(size), Double.class)}));
	}
	/**
	 * Parse a string and return it as SQL date.
	 * @param txt a data string
	 * @return a sql date instance
	 */
	public static java.sql.Date parse(String txt) {
		try {
			java.util.Date d = df.parse(txt);
			return new java.sql.Date(d.getTime());
		}
		catch (Exception x) {
			return null;
		}
	}

}

⌨️ 快捷键说明

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