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

📄 comparatortest.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.comparators;


import java.util.Comparator;
import java.util.List;

import junit.framework.TestCase;

import org.mandarax.kernel.ClauseSet;
import org.mandarax.kernel.Fact;
import org.mandarax.kernel.Predicate;
import org.mandarax.kernel.Rule;
import org.mandarax.kernel.SimplePredicate;
import org.mandarax.reference.AdvancedKnowledgeBase;
import org.mandarax.util.LogicFactorySupport;
import org.mandarax.util.logging.LogCategories;

/**
 * An abstract test case class for testing the default comparator.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 2.2
 */
public abstract class ComparatorTest extends TestCase implements LogCategories {
	
    protected org.mandarax.kernel.ExtendedKnowledgeBase kb = new AdvancedKnowledgeBase();
    protected static LogicFactorySupport lfs = new LogicFactorySupport();
    
    // some predicates 
    private static Predicate A = new SimplePredicate("A",new Class[]{Integer.class,Integer.class});
    private static Predicate B = new SimplePredicate("B",new Class[]{Integer.class,Integer.class});
    private static Predicate C = new SimplePredicate("C",new Class[]{Integer.class,Integer.class});
    
    // facts with 0,1 and 2 variables
    protected static Fact F0 = lfs.fact(A,new Integer(1),new Integer(2));
    protected static Fact F1 = lfs.fact(A,lfs.variable("x",Integer.class),new Integer(2));
    protected static Fact F2 = lfs.fact(A,lfs.variable("x",Integer.class),lfs.variable("y",Integer.class));
    
    // some rules 
    
    // 1 prerequisite, 4 variables (occurrences), no negated prerequisite
    protected static Rule R0 = 
    	lfs.rule(
    		lfs.prereq(B,lfs.variable("x",Integer.class),lfs.variable("y",Integer.class)),
    		lfs.fact(A,lfs.variable("y",Integer.class),lfs.variable("x",Integer.class))
    	);
    // 2 prerequisites, 6 variables (occurrences), no negated prerequisite	
    protected static Rule R1 = 
    	lfs.rule(
    		lfs.prereq(B,lfs.variable("x",Integer.class),lfs.variable("y",Integer.class)),
    		lfs.prereq(C,lfs.variable("y",Integer.class),lfs.variable("z",Integer.class)),
    		lfs.fact(A,lfs.variable("x",Integer.class),lfs.variable("z",Integer.class))
    	);
    // 2 prerequisites, 6 variables (occurrences), 1 prerequisite is negated
    protected static Rule R2 = 
    	lfs.rule(
    		lfs.prereq(C,lfs.variable("x",Integer.class),lfs.variable("y",Integer.class)),
    		lfs.prereq(B,lfs.variable("y",Integer.class),lfs.variable("y",Integer.class),true),
    		lfs.fact(A,lfs.variable("x",Integer.class),lfs.variable("y",Integer.class))
    	);
    	
  	
    	
    
    /**
     * Constructor.
     * @param aKnowledgeBase a new, uninitialized knowledge base that will be used
     */
    public ComparatorTest() {
        super ("testComparator");
    }

    /**
     * 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() {
        ClauseSet[] clauses = getInputOrder();
        for (int i=0;i<clauses.length;i++) {
        	kb.add(clauses[i]);
        }
        kb.setComparator(getComparator());
    }
	/**
	 * Get the expected order.
	 * @return an array of clause sets
	 */
	protected abstract ClauseSet[] getExpectedOrder() ;
	/**
	 * Get the input order.
	 * @return an array of clause sets
	 */
	protected abstract ClauseSet[] getInputOrder() ;
	
	/**
	 * Get the comparator.
	 * @return a comparator
	 */
	protected abstract Comparator getComparator() ;
		
    /**
     * Run the test.
     */
    public void testComparator() {
        LOG_TEST.info ("Start Testcase " + getClass ().getName () + " , test method: " + "testClauses()");

		List clauses = kb.getClauseSets(A);
		ClauseSet[] expected = getExpectedOrder();
		for (int i=0;i<clauses.size();i++) {
			if (clauses.get(i)!=expected[i]) {
				LOG_TEST.info("Clause set expected at position " + i + " is " + expected[i] + " but is " + clauses.get(i));
				assertTrue(false);
			}				
		}
		assertTrue(true);
    }

    
}

⌨️ 快捷键说明

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