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