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

📄 xkbtestcase.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.xkb;


import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.input.DOMBuilder;
import org.mandarax.kernel.Clause;
import org.mandarax.kernel.ClauseSetException;
import org.mandarax.kernel.Fact;
import org.mandarax.kernel.KnowledgeBase;
import org.mandarax.kernel.Rule;
import org.mandarax.util.LogicFactorySupport;
import org.mandarax.util.logging.LogCategories;
import org.mandarax.xkb.XKBDriver;
import org.mandarax.xkb.XKBManager;
import org.mandarax.xkb.framework.XKBDriver_1_0;
import org.mandarax.xkb.framework.XKBDriver_1_1;

import test.org.mandarax.testsupport.TestUtils;
/**
 * Abstract test case for XKB drivers.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 1.6
 */
public abstract class XKBTestCase extends junit.framework.TestCase implements LogCategories {

    protected XKBManager xkbMgr      = new XKBManager ();
    protected XKBDriver  driver      = null;
    protected DOMBuilder domBuilder  = new DOMBuilder ();
    protected String     description = null;

    // the file name used to output the xml
    protected String xml = null;

    /**
     * Construct a test case with the name for an XKB driver.
     * @param aDriver the driver to be tested
     */
    public XKBTestCase(XKBDriver aDriver) {
        super ("test");
        xkbMgr.setDriver (aDriver);
        driver = aDriver;
    }

    /**
     * Construct a test case with the name for an XKB driver.
     * @param aDriver the driver to be tested
     */
    public XKBTestCase(XKBDriver aDriver, String xmlFileName) {
        super ("test");
        xml = xmlFileName;
        xkbMgr.setDriver (aDriver);
        driver = aDriver;
    }

    /**
     * Get the knowledge base used as test data.
     * @return a knowledge base
     */
    public abstract KnowledgeBase getKB();

    /**
     * Compare knowledge bases.
     * @return a boolean
     * @param kb1 the first kb
     * @param kb2 the second kb
     */
    public boolean compare(KnowledgeBase kb1, KnowledgeBase kb2) {
    	boolean result = true;
    	try {
	    	// compare clauses
	        Iterator iter1 = kb1.clauses();
	        Iterator iter2 = kb2.clauses();
	        while (result && iter1.hasNext() && iter2.hasNext()) {
	        	result = result && compare((Clause)iter1.next(),(Clause)iter2.next());
	        }
	    	// compare queries
	        iter1 = kb1.queries();
	        iter2 = kb2.queries();
	        while (result && iter1.hasNext() && iter2.hasNext()) {
	        	result = result && (iter1.next().equals(iter2.next()));
	        }        
	        
	        return result;
    	}
    	catch (ClauseSetException x) {
    		x.printStackTrace();
    	}
    	return false;
    }
    /**
     * Compare clauses.
     * @return a boolean
     * @param c1 the first clause
     * @param c2 the second clause
     */
    public boolean compare(Clause c1,Clause c2) {
    	if (c1 instanceof Rule && c2 instanceof Rule) return compare((Rule)c1,(Rule)c2);
    	else return c1.equals(c2);
    }
    /**
     * Compare rules. Special handling for old drivers: the prerequisite interface has only been introduced
     * in version 2.0 !!
     * @return a boolean
     * @param r1 the first rule
     * @param r2 the second rule
     */
    public boolean compare(Rule r1,Rule r2) {
    	if (driver instanceof XKBDriver_1_0 || driver instanceof XKBDriver_1_1) {
			boolean result = true;
			result = result && r1.getHead().equals(r2.getHead());
			List body1 = r1.getBody();
			List body2 = r2.getBody();
			result = result && body1.size()==body2.size();
			LogicFactorySupport lfs = new LogicFactorySupport();
			if (result) {
				int i = 0;
				while ((i<body1.size()) && result) {
					Fact pr11 = (Fact)body1.get(i);
					Fact pr21 = (Fact)body2.get(i);
					Fact pr12 = lfs.fact(pr11.getPredicate(),pr11.getTerms());
					Fact pr22 = lfs.fact(pr21.getPredicate(),pr21.getTerms());
					result = result && pr12.equals(pr22);
					i=i+1;
				}
			}
			return result;
    	}
    	else return r1.equals(r2);
    }
    /**
     * Run the test.
     */
    public void test() throws Exception {
        KnowledgeBase original = getKB ();
        Document      doc      = driver.exportKnowledgeBase (original);
        KnowledgeBase imported = driver.importKnowledgeBase (doc);
        if(xml != null) {
            // write the xml file
            FileWriter out = new FileWriter (TestUtils.getFile(xml));
            xkbMgr.exportKnowledgeBase (out, original);
            out.close ();
        }
        assertTrue (compare (original, imported));
    }

    /**
     * Convert the object to a string.
     * @return a string
     */
    public String toString() {
        String name = description==null?super.toString():description;
        name = name + " (tested driver: " + driver.getClass() + ")";
        return name;
    }
}

⌨️ 快捷键说明

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