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: import org.mandarax.kernel.SimplePredicate;
026: import org.mandarax.util.LogicFactorySupport;
027:
028: /**
029: * An abstract test case class to test features of an inference engine.
030: * Similar to TestInferenceEngine, but tests are based on (integer) arithmetics.
031: * @see TestInferenceEngin
032: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
033: * @version 3.4 <7 March 05>
034: * @since 1.9.1
035: */
036: public abstract class TestInferenceEngineUseMath extends
037: MandaraxTestCase {
038: public static final String QUERY_VARIABLE = "?x";
039:
040: protected KnowledgeBase kb = null;
041: protected InferenceEngine ie = null;
042:
043: protected LogicFactorySupport lfs = new LogicFactorySupport();
044: protected Class[] types = { Integer.class };
045: protected SimplePredicate P1 = new SimplePredicate("P1", types);
046: protected SimplePredicate P2 = new SimplePredicate("P2", types);
047: protected SimplePredicate P3 = new SimplePredicate("P3", types);
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 TestInferenceEngineUseMath(KnowledgeBase aKnowledgeBase,
055: InferenceEngine anInferenceEngine) {
056: super ("testInferenceEngine");
057:
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.ONE;
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 expected number.
089: * @return the name of the person
090: */
091: abstract int getExpectedNumber();
092:
093: /**
094: * Get a query. The query should use the query variable QUERY_VARIABLE!
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: Integer expected = new Integer(getExpectedNumber());
117: boolean succ = rs.next();
118: Integer computed = (Integer) rs.getResult(Integer.class,
119: QUERY_VARIABLE);
120: succ = succ && expected.equals(computed);
121: assertTrue(succ);
122: } catch (InferenceException x) {
123: assertTrue(false);
124: }
125: LOG_TEST.info("Finish Testcase " + getClass().getName()
126: + " , test method: " + "testInferenceEngine()");
127: }
128:
129: /**
130: * Convert this object to a string.
131: * @return the string representation of the object
132: */
133: public String toString() {
134: StringBuffer buf = new StringBuffer();
135: buf.append(super .toString());
136: buf.append(" (");
137: buf.append(kb.getClass().getName());
138: buf.append(" / ");
139: buf.append(ie.getClass().getName());
140: buf.append(")");
141: return buf.toString();
142: }
143: }
|