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

📄 testresultset.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 java.util.Enumeration;
import java.util.Vector;

import org.mandarax.kernel.InferenceEngine;
import org.mandarax.kernel.InferenceException;
import org.mandarax.kernel.KnowledgeBase;
import org.mandarax.kernel.Query;
import org.mandarax.kernel.Replacement;
import org.mandarax.kernel.Result;
import org.mandarax.kernel.ResultSet;

/**
 * An abstract test case class to test the result set query interface of the inference engine.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 1.7
 */
public abstract class TestResultSet extends MandaraxTestCase {

    protected KnowledgeBase   kb = null;
    protected InferenceEngine ie =
        new org.mandarax.reference.ResolutionInferenceEngine ();

    /**
     * Constructor.
     * @param aKnowledgeBase a new, uninitialized knowledge base that will be used
     * @param anInferenceEngine the inference engine that will be tested
     */
    public TestResultSet(KnowledgeBase aKnowledgeBase,InferenceEngine anInferenceEngine) {
        super ("testResultSet");
        kb = aKnowledgeBase;
        ie = anInferenceEngine;
        
    }

    /**
     * Compare two sets of results. Note that the order matters!
     * @return boolean
     * @param r1 the expected results
     * @param r2 the computed results
     */
    private boolean compare(Replacement[][] r1, Result[] r2) {
        int l = r1.length;

        if(l != r2.length) {
            return false;
        }

        Replacement[] rp1 = null;
        Replacement[] rp2 = null;

        // compare results one by one
        for(int i = 0; i < l; i++) {
            rp1 = r1[i];
            rp2 = r2[i].getReplacements ();

            if( !compare (rp1, rp2)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Compare two sets of replacements. Note that the order of the elements does not matter!
     * @return boolean
     * @param r1 the first array of replacements
     * @param r2 the second array of replacements
     */
    private boolean compare(Replacement[] r1, Replacement[] r2) {

        // handle null
        if(r1 == null) {
            return r2 == null;
        }

        if(r2 == null) {
            return r1 == null;
        }

        // if the length is different, the arrays do not match, return false
        if(r1.length != r2.length) {
            return false;
        }

        // work with vectors, that is more convinient
        Vector v1 = new Vector ();

        for(int i = 0; i < r1.length; i++) {
            v1.addElement (r1[i]);
        }

        Vector v2 = new Vector ();

        for(int j = 0; j < r2.length; j++) {
            v2.addElement (r2[j]);
        }

        return compare (v1, v2);
    }

    /**
     * Compare two sets of replacements. Note that the order of the elements does not matter!
     * @return boolean
     * @param v1 the first list of replacements
     * @param v2 the second list of replacements
     */
    private boolean compare(java.util.Vector v1, java.util.Vector v2) {
        if(v1.isEmpty () && v2.isEmpty ()) {
            return true;
        }

        if(v1.size () != v2.size ()) {
            return false;
        }

        for(Enumeration e = v1.elements (); e.hasMoreElements (); ) {
            Object obj = e.nextElement ();

            if(v2.contains (obj)) {
                v1.removeElement (obj);
                v2.removeElement (obj);

                return compare (v1, v2);
            }
        }

        return false;
    }

    /**
     * Add facts and rules to the knowledge base.
     * @param knowledge org.mandarax.kernel.KnowledgeBase
     */
    public void feedKnowledgeBase(KnowledgeBase knowledge) {
        knowledge.removeAll ();
        knowledge.add (Person.getRule ("isGrandFatherOf", "x", "z",
                                       "isFatherOf", "y", "z", "isFatherOf",
                                       "x", "y"));
        knowledge.add (Person.getRule ("isOncleOf", "x", "z", "isFatherOf",
                                       "y", "z", "isBrotherOf", "x", "y"));
        knowledge.add (Person.getRule ("isOncleOf", "x", "z",
                                       "isGrandFatherOf", "y", "z",
                                       "isBrotherOf", "x", "y"));
        knowledge.add (Person.getRule ("isGrandFatherOf", "z", "x",
                                       "isFatherOf", "y", "x", "isFatherOf",
                                       "z", "y"));
        knowledge.add (Person.getFact ("isFatherOf", "Jens", "Max"));
        knowledge.add (Person.getFact ("isFatherOf", "Klaus", "Jens"));
        knowledge.add (Person.getFact ("isBrotherOf", "Lutz", "Klaus"));
    }

    /**
     * Get the cardinality constraint (how many solutions
     * should be compouted). Default is <code>InferenceEngine.ONE</code>
     * @return the number of expected results
     */
    public int getCardinalityConstraint() {
        return InferenceEngine.ONE;
    }
    /**
     * Get the exception handling policy.
     * @return an integer indicating the exception handling policy
     */
    public int getExceptionHandlingPolicy() {
    	return InferenceEngine.BUBBLE_EXCEPTIONS;
    }


    /**
     * Get a description of this test case.
     * This is used by the <code>org.mandarax.demo</code>
     * package to display the test cases.
     * @return a brief description of the test case
     */
    public String getDescription() {
        return "Abstract test for the ResultSet interface";
    }

    /**
     * Get the name of the person we are looking for.
     * More precisely, this is the name of the person expected to
     * replace the query variable as a result of the query.
     * @return the name of the person
     */
    String getPersonName() {
        return "Klaus";
    }

    /**
     * Get the query.
     * @return a query
     */
    public Query getQuery() {
        return Person.getQuery("isGrandFatherOf", "x", "Max");
    }

    /**
     * Sets up the fixture.
     */
    protected void setUp() {
        feedKnowledgeBase (kb);
    }

    /**
     * Run the test.
     */
    public void testResultSet() {
        LOG_TEST.info ("Start Testcase " + getClass().getName() + " , test method: " + "testResultSet()");
		try {
        	ResultSet rs = this.ie.query(getQuery(),kb,getCardinalityConstraint(),getExceptionHandlingPolicy());
        	assertTrue(testResultSet(rs));
		}
		catch (InferenceException x) {
			assertTrue(false);
		}
        LOG_TEST.info ("Finish Testcase " + getClass().getName() + " , test method: " + "testInferenceEngine()");
    }
    /**
     * Test the result set. Note that the result set is uninitialized (nothing has been fetched).
     * @param rs the result set
     * @return false if the test fails and true otherwise
     */
     public abstract boolean testResultSet(ResultSet rs);
}

⌨️ 快捷键说明

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