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.Predicate;
024: import org.mandarax.kernel.Query;
025: import org.mandarax.kernel.ResultSet;
026: import org.mandarax.kernel.SimplePredicate;
027: import org.mandarax.util.LogicFactorySupport;
028:
029: /**
030: * An abstract test case class to test NAF (negation of failure) related features of an inference engine.
031: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
032: * @version 3.4 <7 March 05>
033: * @since 2.0
034: */
035: public abstract class TestInferenceEngineNAF extends MandaraxTestCase {
036: public static final String QUERY_VARIABLE = "x";
037:
038: protected KnowledgeBase kb = null;
039: protected InferenceEngine ie = null;
040: protected LogicFactorySupport lfs = new LogicFactorySupport();
041: protected static final Class[] struct = { String.class };
042: protected static final Predicate P = new SimplePredicate("P",
043: struct);
044: protected static final Predicate Q = new SimplePredicate("Q",
045: struct);
046: protected static final Predicate R = new SimplePredicate("R",
047: struct);
048: protected static final Predicate S = new SimplePredicate("S",
049: struct);
050: protected static final Predicate T = new SimplePredicate("T",
051: struct);
052:
053: /**
054: * Constructor.
055: * @param aKnowledgeBase a new, uninitialized knowledge base that will be used
056: * @param anInferenceEngine the inference engine that will be tested
057: */
058: public TestInferenceEngineNAF(KnowledgeBase aKnowledgeBase,
059: InferenceEngine anInferenceEngine) {
060: super ("testInferenceEngine");
061: kb = aKnowledgeBase;
062: ie = anInferenceEngine;
063: }
064:
065: /**
066: * Populate the knowledge base.
067: * @param knowledge the knowledge base
068: */
069: public abstract void feedKnowledgeBase(KnowledgeBase knowledge);
070:
071: /**
072: * Get the cardinality constraint (how many solutions
073: * should be compouted). Default is <code>InferenceEngine.ONE</code>
074: * @return the number of expected results
075: */
076: public int getCardinalityConstraint() {
077: return InferenceEngine.ONE;
078: }
079:
080: /**
081: * Get a description of this test case.
082: * This is used by the <code>org.mandarax.demo</code>
083: * package to display the test cases.
084: * @return java.lang.String
085: */
086: public String getDescription() {
087: return "test case " + getClass().getName();
088: }
089:
090: /**
091: * Get a query. The query should use the query variable QUERY_VARIABLE!
092: * @return a query
093: */
094: public abstract Query getQuery();
095:
096: /**
097: * Get the expected object, or null if no result is expected
098: * @return a query
099: */
100: public abstract String getExpected();
101:
102: /**
103: * Sets up the fixture.
104: */
105: protected void setUp() {
106: feedKnowledgeBase(kb);
107: }
108:
109: /**
110: * Run the test.
111: */
112: public void testInferenceEngine() {
113: LOG_TEST.info("Start Testcase " + getClass().getName()
114: + " , test method: " + "testInferenceEngine()");
115: try {
116: ResultSet rs = ie.query(getQuery(), kb,
117: getCardinalityConstraint(),
118: InferenceEngine.BUBBLE_EXCEPTIONS);
119: String expected = getExpected();
120: boolean succ = rs.next();
121: if (expected == null) {
122: assertTrue(!succ);
123: return;
124: }
125: String computed = (String) rs.getResult(String.class,
126: QUERY_VARIABLE);
127: assertTrue(expected.equals(computed));
128: } catch (InferenceException x) {
129: assertTrue(false);
130: }
131: LOG_TEST.info("Finish Testcase " + getClass().getName()
132: + " , test method: " + "testInferenceEngine()");
133: }
134:
135: /**
136: * Convert this object to a string.
137: * @return the string representation of the object
138: */
139: public String toString() {
140: StringBuffer buf = new StringBuffer();
141: buf.append(super .toString());
142: buf.append(" (");
143: buf.append(kb.getClass().getName());
144: buf.append(" / ");
145: buf.append(ie.getClass().getName());
146: buf.append(")");
147: return buf.toString();
148: }
149: }
|