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

📄 testcut.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 test.org.mandarax.reference;


import junit.framework.TestCase;

import org.mandarax.kernel.Fact;
import org.mandarax.kernel.InferenceEngine;
import org.mandarax.kernel.InferenceException;
import org.mandarax.kernel.Prerequisite;
import org.mandarax.kernel.Query;
import org.mandarax.kernel.ResultSet;
import org.mandarax.kernel.SimplePredicate;
import org.mandarax.reference.AdvancedKnowledgeBase;
import org.mandarax.reference.ResolutionInferenceEngine4;
import org.mandarax.util.LogicFactorySupport;
import org.mandarax.util.logging.LogCategories;

/**
 * An abstract test case class for testing cut.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 2.1
 */
public abstract class TestCut extends TestCase implements LogCategories {
	public static final String QUERY_VARIABLE = "query_var"; 
	
    protected org.mandarax.kernel.KnowledgeBase   kb = new AdvancedKnowledgeBase();
    protected InferenceEngine ie = new ResolutionInferenceEngine4();
    protected LogicFactorySupport lfs = new LogicFactorySupport();
    /**
     * Constructor.
     */
    public TestCut() {
        super ("testInferenceEngine");
    }

    /**
     * Populate the knowledge base.
     * @param knowledge the knowledge base
     */
    public abstract void feedKnowledgeBase(org.mandarax.kernel.KnowledgeBase knowledge);


    /**
     * Get a description of this test case.
     * This is used by the <code>org.mandarax.demo</code>
     * package to display the test cases.
     * @return java.lang.String
     */
    public String getDescription() {
        return "test case " + getClass ().getName ();
    }
    /**
     * Sets up the fixture.
     */
    protected void setUp() {
        feedKnowledgeBase (kb);
    }
	/**
	 * Get the expected results.
	 * @return an array of strings
	 */
	protected abstract String[] getExpectedResults() ;
    /**
     * Run the test.
     */
    public void testInferenceEngine() {
        LOG_TEST.info ("Start Testcase " + getClass ().getName () + " , test method: " + "testInferenceEngine()");
        Fact queryFact = lfs.fact(new SimplePredicate("R",new Class[]{String.class}),lfs.variable(QUERY_VARIABLE,String.class));
        Query query = lfs.query(queryFact,"?");
		try {
        	ResultSet rs    = ie.query (query,kb,InferenceEngine.ALL,InferenceEngine.BUBBLE_EXCEPTIONS);
        	
        	String[] expected = getExpectedResults();
        	boolean success = true;
        	int i=0;
        	while (i<expected.length && success) {
        		success = success && rs.next();
        		success = success && expected[i].equals(rs.getResult(String.class,QUERY_VARIABLE));
        		i = i+1;
        	}
        	// there should be no more computed results:
        	success = success && !rs.next();
        	
	        assertTrue (success);
		}
		catch (InferenceException x) {
			assertTrue(false);
		}
        LOG_TEST.info ("Finish Testcase " + getClass ().getName () + " , test method: " + "testInferenceEngine()");
    }
    /**
     * Convert this object to a string.
     * @return the string representation of the object
     */
    public String toString() {        
        StringBuffer buf = new StringBuffer();
        buf.append(super.toString());
        buf.append(" (");
        buf.append(kb.getClass().getName());
        buf.append(" / ");
        buf.append(ie.getClass().getName());
        buf.append(")");
        return buf.toString();        
    }
    /**
     * Create a prerequisite with a simple predicate and a constant. 
     * @param p a predicate name
     * @param c a constant name
     * @return a prerequisite
     */
    protected Prerequisite prereq1(String p, String c) {
    	return lfs.prereq(new SimplePredicate(p,new Class[]{String.class}),lfs.cons(c,String.class));
    } 
    /**
     * Create a prerequisite with a simple predicate and a variable. 
     * @param p a predicate name
     * @param v a variable name
     * @return a prerequisite
     */
    protected Prerequisite prereq2(String p, String v) {
    	return lfs.prereq(new SimplePredicate(p,new Class[]{String.class}),lfs.variable(v,String.class));
    } 
    /**
     * Create a cut prerequisite. 
     * @return a prerequisite
     */
    protected Prerequisite cut() {
    	return lfs.cut();
    } 
    /**
     * Create a fact with a simple predicate and a constant. 
     * @param p a predicate name
     * @param c a constant name
     * @return a prerequisite
     */
    protected Fact fact1(String p, String c) {
    	return lfs.fact(new SimplePredicate(p,new Class[]{String.class}),lfs.cons(c,String.class));
    } 
    /**
     * Create a fact with a simple predicate and a variable. 
     * @param p a predicate name
     * @param v a variable name
     * @return a fact
     */
    protected Fact fact2(String p, String v) {
    	return lfs.fact(new SimplePredicate(p,new Class[]{String.class}),lfs.variable(v,String.class));
    } 
    
}

⌨️ 快捷键说明

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