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

📄 testcaseimpl.java

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

import org.mandarax.kernel.*;
import org.mandarax.kernel.validation.TestCase;
import org.mandarax.reference.DefaultInferenceEngine;
import java.util.*;


/**
 * Default implementation of a test case, based on the JUnit framework. 
 * @author <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 3.3.3
 */
public class TestCaseImpl extends junit.framework.TestCase implements TestCase {

	protected KnowledgeBase kb = null; // the kb to be tested (usually containing only rules)
	protected ClauseSet[] assumptions = new Fact[0]; // additional assumptions
	protected Query query = null; // the test query
	protected int expectedNumberOfResults = -1; // -1 means that this is not checked
	protected java.util.Map[] expectedReplacements = null; // the replacements expected in the first result, null means that this is not checked
	protected InferenceEngine ie = new DefaultInferenceEngine();
	private boolean mustBeTrue = false; // for ground queries
	private Properties additionalProperties = null; // generic properties
	private int policyToAddAssumptionsToKB = AT_BOTTOM; // policy to add assumptions to the kb
	
	/**
	 * Compares objects.
	 * @param obj an object
	 * @return a boolean
	 */
	public boolean equals(Object obj) {
		if (obj!=null && obj.getClass()==this.getClass()) {
			TestCaseImpl tc = (TestCaseImpl)obj;
			boolean result = true; // IMPORTANT - do not compare the kbs - this is a back reference and will lead to an infinite loop
			result = result && (query==null?tc.query==null:this.query.equals(tc.query));
			if (expectedReplacements==null) result = result&&tc.expectedReplacements==null;
			else {
				result = result && expectedReplacements.length==tc.expectedReplacements.length;
				if (result) {
					for (int i=0;i<expectedReplacements.length;i++) {
						result = result && expectedReplacements[i].equals(tc.expectedReplacements[i]);
					}
				}
			}
			result = result && (mustBeTrue==tc.mustBeTrue);
			result = result && (policyToAddAssumptionsToKB==tc.policyToAddAssumptionsToKB);
			result = result && expectedNumberOfResults==tc.expectedNumberOfResults;
			result = result && (getProperties()==null?tc.getProperties()==null:this.getProperties().equals(tc.getProperties()));
			return result;
		}
		return false;
	}
	/**
	 * Get the hash code for this object
	 * @return an integer
	 */
	public int hashCode() {
		return (query==null?0:query.hashCode()) ^ expectedNumberOfResults ^ (expectedReplacements==null?42:expectedReplacements.hashCode());
	}
	
	/**
	 * Constructor.
	 * @param assumptions
	 * @param policyToAddAssumptionsToKB
	 * @param query
	 * @param expectedNumberOfResults
	 * @param expectedReplacements
	 * @param mustBeTrue
	 */
	public TestCaseImpl(ClauseSet[] assumptions, int policyToAddAssumptionsToKB,Query query,int expectedNumberOfResults, java.util.Map[] expectedReplacements,boolean mustBeTrue) {
		super("execute");
		this.assumptions = assumptions;
		this.policyToAddAssumptionsToKB = policyToAddAssumptionsToKB;
		this.query = query;
		this.expectedNumberOfResults = expectedNumberOfResults;
		this.expectedReplacements = expectedReplacements;
		this.mustBeTrue = mustBeTrue;
	}
	/**
	 * Constructor.
	 * @param assumptions
	 * @param policyToAddAssumptionsToKB
	 * @param query
	 * @param expectedNumberOfResults
	 */
	public TestCaseImpl(ClauseSet[] assumptions, int policyToAddAssumptionsToKB, Query query,int expectedNumberOfResults) {
		super("execute");
		this.assumptions = assumptions;
		this.policyToAddAssumptionsToKB = policyToAddAssumptionsToKB;
		this.query = query;
		this.expectedNumberOfResults = expectedNumberOfResults;

	}
	/**
	 * Constructor.
	 * @param assumptions
	 * @param policyToAddAssumptionsToKB
	 * @param query
	 * @param expectedReplacements
	 */
	public TestCaseImpl(ClauseSet[] assumptions, int policyToAddAssumptionsToKB, Query query,java.util.Map[] expectedReplacements) {
		super("execute");
		this.assumptions = assumptions;
		this.policyToAddAssumptionsToKB = policyToAddAssumptionsToKB;
		this.query = query;
		this.expectedReplacements = expectedReplacements;
	}
	/**
	 * Constructor.
	 * @param assumptions
	 * @param policyToAddAssumptionsToKB
	 * @param query
	 * @param expectedNumberOfResults
	 * @param expectedReplacements
	 * @param mustBeTrue
	 */
	public TestCaseImpl(ClauseSet[] assumptions, int policyToAddAssumptionsToKB, Query query,boolean mustBeTrue) {
		super("execute");
		this.assumptions = assumptions;
		this.policyToAddAssumptionsToKB = policyToAddAssumptionsToKB;
		this.query = query;
		this.mustBeTrue = mustBeTrue;
	}
	


	/**
	 * Tears down the test case.
	 */
	protected void tearDown() throws Exception {
		for (int i=0;i<assumptions.length;i++) this.kb.remove(assumptions[i]);
		super.tearDown();
	}
	/**
	 * Sets up down the test case.
	 */
	protected void setUp() throws Exception {
		super.setUp();
		if (this.policyToAddAssumptionsToKB==AT_BOTTOM) for (int i=0;i<assumptions.length;i++) this.kb.add(assumptions[i]);
		if (this.policyToAddAssumptionsToKB==ON_TOP && kb instanceof ExtendedKnowledgeBase ) {
			ExtendedKnowledgeBase xkb = (ExtendedKnowledgeBase)kb;
			for (int i=assumptions.length-1;i>-1;i--) {
				xkb.add(assumptions[i]);
				xkb.moveToTop(assumptions[i]);
			}
		}
	}
	/**
	 * Prepares the test case.
	 */
	public void prepare() throws Exception {
		this.setUp();
	}

	/**
	 * Releases the test case.
	 */
	public void release() throws Exception {
		this.release();
	}
	/**
	 * Tests the knowledge base.
	 */
	public void execute() throws Exception {
		
		ResultSet rs = ie.query(query,kb,InferenceEngine.ALL,InferenceEngine.BUBBLE_EXCEPTIONS);
		int counter = 0;
		boolean finalResult = true;
		
		// 1. ground queries
		if (query.isGround()) {
			boolean ok = rs.next()==this.mustBeTrue;
			counter = counter + 1;
			if (ok) finalResult = finalResult&&ok;
			else assertTrue(mustBeTrue?"Ground query should be valid":"Ground query should not be valid",ok);
		}
		
		// 2. non-ground queries
		else if (this.expectedReplacements!=null && this.expectedReplacements.length>0) {
			for (int i=0;i<expectedReplacements.length;i++) {
				java.util.Map map = this.expectedReplacements[i];
				rs.next();
				counter = counter + 1;
				for (Iterator keys = map.keySet().iterator();keys.hasNext();) {
					VariableTerm var = (VariableTerm)keys.next();
					Object computed = rs.getResult(var);
					Object expected = map.get(var);
					if (!expected.equals(computed)) {
						StringBuffer buf = new StringBuffer()
							.append("In result ")
							.append(counter)
							.append(" ")
							.append(expected)
							.append(" was expected but ")
							.append(computed)
							.append(" was found as result for ")
							.append(var);
						assertTrue(buf.toString(),false);
					}
				}
			}
		}
		
		// 3. Count results
		if (this.expectedNumberOfResults>-1) {
			while (rs.next()) counter = counter+1;
			if (expectedNumberOfResults!=counter) {
				StringBuffer buf = new StringBuffer()
				.append("Expected number of results is ")
				.append(expectedNumberOfResults)
				.append(" but ")
				.append(counter)
				.append(" results found ");
				assertTrue(buf.toString(),false);
			}
		}
		rs.close();
		
		assertTrue(true);
		
	
	}
	/**
	 * Get the assumptions.
	 * @return Returns the assumptions.
	 */
	public ClauseSet[] getAssumptions() {
		return assumptions;
	}
	/**
	 * Set the assumptions.
	 * @param assumptions The assumptions to set.
	 */
	public void setAssumptions(ClauseSet[] assumptions) {
		this.assumptions = assumptions;
	}
	/**
	 * @return Returns the kb.
	 */
	public KnowledgeBase getKb() {
		return kb;
	}
	/**
	 * Set the kb. Called by the kb when adding the test case to the kb to insert a back reference.
	 * @param kb The kb to set.
	 */
	public void setKb(KnowledgeBase kb) {
		this.kb = kb;
	}
	/**
	 * Get the test query.
	 * @return Returns the query.
	 */
	public Query getQuery() {
		return query;
	}
	/**
	 * Set the test query.
	 * @param query The query to set.
	 */
	public void setQuery(Query query) {
		this.query = query;
	}
	/**
	 * @return Returns the expectedNumberOfResults.
	 */
	public int getExpectedNumberOfResults() {
		return expectedNumberOfResults;
	}
	/**
	 * @param expectedNumberOfResults The expectedNumberOfResults to set.
	 */
	public void setExpectedNumberOfResults(int expectedNumberOfResults) {
		this.expectedNumberOfResults = expectedNumberOfResults;
	}
	/**
	 * Get an array of maps containing variable term -> constant term mapping expected in the result
	 * at this position.
	 * Can be null indicating that this condition will not be checked in this test case.
	 * @return Returns the expectedReplacements.
	 */
	public java.util.Map[] getExpectedReplacements() {
		return expectedReplacements;
	}
	/**
	 * Set an array of maps containing variable term -> constant term mapping expected in the result
	 * at this position.
	 * Can be null indicating that this condition will not be checked in this test case.
	 * @param expectedReplacements The expectedReplacements to set.
	 */
	public void setExpectedReplacements(java.util.Map[] expectedReplacements) {
		this.expectedReplacements = expectedReplacements;
	}
	/**
	 * Indicates whether a test case must yield true.
	 * Only used if the query is ground (does not have variables).
	 * @return Returns the mustBeTrue.
	 */
	public boolean isMustBeTrue() {
		return mustBeTrue;
	}
	/**
	 * Sets whether a test case must yield true.
	 * Only used if the query is ground (does not have variables).
	 * @param mustBeTrue The mustBeTrue to set.
	 */
	public void setMustBeTrue(boolean mustBeTrue) {
		this.mustBeTrue = mustBeTrue;
	}
	/**
	 * Set a property.
	 * @param key the key
	 * @param value the value
	 * @return the previous value of the specified key in this property list, or null if it did not have one.
	 */
	public Object setProperty(String key,String value) {
		if (additionalProperties==null) additionalProperties = new Properties();
		return additionalProperties.setProperty(key,value);	
	}
	/**
	 * Get a property.
	 * @param key the property key
	 * @return the respective value. The method returns null if the property is not found. 
	 */
	public String getProperty(String key) {
		if (additionalProperties==null) additionalProperties = new Properties();
		return additionalProperties.getProperty(key);		
	}
	/** 
	 * Returns an enumeration of all the keys in this property list, including distinct 
	 * keys in the default property list if a key of the same name has not already been 
	 * found from the main properties list. 
	 * @return an enumeration of all the keys in this property list, including the keys in 
	 * the default property list
	 */
	public Enumeration propertyNames() {
		if (additionalProperties==null) additionalProperties = new Properties();
		return additionalProperties.propertyNames();	
	}
	/**
	 * Remove a property.
	 * @param key the property key
	 * @return the value to which the key had been mapped, or null if the key did not have a mapping.
	 */
	public Object removeProperty(String key) {
		if (additionalProperties!=null) return additionalProperties.remove(key);
		return null;
	}
	/**
	 * Get the properties as one "properties" instance.
	 * @return a properties instance
	 */
	public Properties getProperties() {
		if (additionalProperties==null) additionalProperties = new Properties();
		return additionalProperties;
	};
	/**
	 * Set the properties. Not required by the interface, but useful for bean (introspection-) based
	 * tools.
	 * @param properties the properties
	 */
	public void setProperties(Properties properties) {
		additionalProperties = properties;
	};
	/**
	 * Set the policy to add an assumption. 
	 * @param policy the (encoded) policy = one of the constants defined in TestCase
	 */
	public void setPolicyToAddAssumptionsToKB(int policy) {
		policyToAddAssumptionsToKB = policy;
	}
	/**
	 * Get the policy to add an assumption. 
	 * @return an integer, the (encoded) policy = one of the constants defined in TestCase
	 */
	public int getPolicyToAddAssumptionsToKB() {
		return policyToAddAssumptionsToKB;
	}
}

⌨️ 快捷键说明

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