001: package test.org.mandarax.reference;
002:
003: /**
004: * Copyright (C) 1999-2004 Jens Dietrich (mailto:mandarax@jbdietrich.com)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.util.List;
022: import org.mandarax.kernel.InferenceEngine;
023: import org.mandarax.kernel.InferenceException;
024: import org.mandarax.kernel.KnowledgeBase;
025: import org.mandarax.kernel.Query;
026: import org.mandarax.kernel.ResultSet;
027:
028: /**
029: * An abstract test case class to test features of an inference engines supporting multiple results.
030: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
031: * @version 3.4 <7 March 05>
032: * @since 1.1
033: */
034: public abstract class TestInferenceEngine4MultipleResults extends
035: MandaraxTestCase {
036:
037: protected KnowledgeBase kb = null;
038: protected InferenceEngine ie = new org.mandarax.reference.ResolutionInferenceEngine2();
039: public static final String QUERY_VARIABLE = "x";
040:
041: /**
042: * Constructor.
043: * @param testName the name of the test method
044: */
045: public TestInferenceEngine4MultipleResults(String testName) {
046: super (testName);
047: }
048:
049: /**
050: * Constructor.
051: * @param aKnowledgeBase a new, uninitialized knowledge base that will be used
052: * @param anInferenceEngine the inference engine that will be tested
053: */
054: public TestInferenceEngine4MultipleResults(
055: KnowledgeBase aKnowledgeBase,
056: InferenceEngine anInferenceEngine) {
057: super ("testInferenceEngine");
058: kb = aKnowledgeBase;
059: ie = anInferenceEngine;
060: }
061:
062: /**
063: * Populate the knowledge base.
064: * @param knowledge the knowledge base
065: */
066: public abstract void feedKnowledgeBase(KnowledgeBase knowledge);
067:
068: /**
069: * Get the cardinality constraint (how many solutions
070: * should be compouted). Default is <code>InferenceEngine.ONE</code>
071: * @return the number of expected results
072: */
073: public int getCardinalityConstraint() {
074: return InferenceEngine.ALL;
075: }
076:
077: /**
078: * Get a description of this test case.
079: * This is used by the <code>org.mandarax.demo</code>
080: * package to display the test cases.
081: * @return java.lang.String
082: */
083: public String getDescription() {
084: return "test case " + getClass().getName();
085: }
086:
087: /**
088: * Get the persons we are looking for.
089: * @return the name of the person
090: */
091: public abstract List getExpectedResults();
092:
093: /**
094: * Get a query.
095: * @return a query
096: */
097: public abstract Query getQuery();
098:
099: /**
100: * Sets up the fixture.
101: */
102: protected void setUp() {
103: feedKnowledgeBase(kb);
104: }
105:
106: /**
107: * Run the test.
108: */
109: public void testInferenceEngine() {
110: LOG_TEST.info("Start Testcase " + getClass().getName()
111: + " , test method: " + "testInferenceEngine()");
112: try {
113: ResultSet rs = ie.query(getQuery(), kb,
114: getCardinalityConstraint(),
115: InferenceEngine.BUBBLE_EXCEPTIONS);
116: List expected = getExpectedResults();
117: boolean succ = true;
118: for (int i = 0; i < expected.size(); i++) {
119: succ = succ && rs.next();
120: Object computed = rs.getResult(Person.class,
121: QUERY_VARIABLE);
122: succ = succ && expected.get(i).equals(computed);
123: }
124: // this ensures that there are no more results!
125: succ = succ && !rs.next();
126: // compare
127: assertTrue(succ);
128: } catch (InferenceException x) {
129: assertTrue(false);
130: }
131: LOG_TEST.info("Finish Testcase " + getClass().getName()
132: + " , test method: " + "testInferenceEngine()");
133: }
134: }
|