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 junit.framework.TestCase;
021:
022: import org.mandarax.kernel.Fact;
023: import org.mandarax.kernel.InferenceEngine;
024: import org.mandarax.kernel.InferenceException;
025: import org.mandarax.kernel.Prerequisite;
026: import org.mandarax.kernel.Query;
027: import org.mandarax.kernel.ResultSet;
028: import org.mandarax.kernel.SimplePredicate;
029: import org.mandarax.reference.AdvancedKnowledgeBase;
030: import org.mandarax.reference.ResolutionInferenceEngine4;
031: import org.mandarax.util.LogicFactorySupport;
032: import org.mandarax.util.logging.LogCategories;
033:
034: /**
035: * An abstract test case class for testing cut.
036: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
037: * @version 3.4 <7 March 05>
038: * @since 2.1
039: */
040: public abstract class TestCut extends TestCase implements LogCategories {
041: public static final String QUERY_VARIABLE = "query_var";
042:
043: protected org.mandarax.kernel.KnowledgeBase kb = new AdvancedKnowledgeBase();
044: protected InferenceEngine ie = new ResolutionInferenceEngine4();
045: protected LogicFactorySupport lfs = new LogicFactorySupport();
046:
047: /**
048: * Constructor.
049: */
050: public TestCut() {
051: super ("testInferenceEngine");
052: }
053:
054: /**
055: * Populate the knowledge base.
056: * @param knowledge the knowledge base
057: */
058: public abstract void feedKnowledgeBase(
059: org.mandarax.kernel.KnowledgeBase knowledge);
060:
061: /**
062: * Get a description of this test case.
063: * This is used by the <code>org.mandarax.demo</code>
064: * package to display the test cases.
065: * @return java.lang.String
066: */
067: public String getDescription() {
068: return "test case " + getClass().getName();
069: }
070:
071: /**
072: * Sets up the fixture.
073: */
074: protected void setUp() {
075: feedKnowledgeBase(kb);
076: }
077:
078: /**
079: * Get the expected results.
080: * @return an array of strings
081: */
082: protected abstract String[] getExpectedResults();
083:
084: /**
085: * Run the test.
086: */
087: public void testInferenceEngine() {
088: LOG_TEST.info("Start Testcase " + getClass().getName()
089: + " , test method: " + "testInferenceEngine()");
090: Fact queryFact = lfs.fact(new SimplePredicate("R",
091: new Class[] { String.class }), lfs.variable(
092: QUERY_VARIABLE, String.class));
093: Query query = lfs.query(queryFact, "?");
094: try {
095: ResultSet rs = ie.query(query, kb, InferenceEngine.ALL,
096: InferenceEngine.BUBBLE_EXCEPTIONS);
097:
098: String[] expected = getExpectedResults();
099: boolean success = true;
100: int i = 0;
101: while (i < expected.length && success) {
102: success = success && rs.next();
103: success = success
104: && expected[i].equals(rs.getResult(
105: String.class, QUERY_VARIABLE));
106: i = i + 1;
107: }
108: // there should be no more computed results:
109: success = success && !rs.next();
110:
111: assertTrue(success);
112: } catch (InferenceException x) {
113: assertTrue(false);
114: }
115: LOG_TEST.info("Finish Testcase " + getClass().getName()
116: + " , test method: " + "testInferenceEngine()");
117: }
118:
119: /**
120: * Convert this object to a string.
121: * @return the string representation of the object
122: */
123: public String toString() {
124: StringBuffer buf = new StringBuffer();
125: buf.append(super .toString());
126: buf.append(" (");
127: buf.append(kb.getClass().getName());
128: buf.append(" / ");
129: buf.append(ie.getClass().getName());
130: buf.append(")");
131: return buf.toString();
132: }
133:
134: /**
135: * Create a prerequisite with a simple predicate and a constant.
136: * @param p a predicate name
137: * @param c a constant name
138: * @return a prerequisite
139: */
140: protected Prerequisite prereq1(String p, String c) {
141: return lfs.prereq(new SimplePredicate(p,
142: new Class[] { String.class }), lfs
143: .cons(c, String.class));
144: }
145:
146: /**
147: * Create a prerequisite with a simple predicate and a variable.
148: * @param p a predicate name
149: * @param v a variable name
150: * @return a prerequisite
151: */
152: protected Prerequisite prereq2(String p, String v) {
153: return lfs.prereq(new SimplePredicate(p,
154: new Class[] { String.class }), lfs.variable(v,
155: String.class));
156: }
157:
158: /**
159: * Create a cut prerequisite.
160: * @return a prerequisite
161: */
162: protected Prerequisite cut() {
163: return lfs.cut();
164: }
165:
166: /**
167: * Create a fact with a simple predicate and a constant.
168: * @param p a predicate name
169: * @param c a constant name
170: * @return a prerequisite
171: */
172: protected Fact fact1(String p, String c) {
173: return lfs.fact(new SimplePredicate(p,
174: new Class[] { String.class }), lfs
175: .cons(c, String.class));
176: }
177:
178: /**
179: * Create a fact with a simple predicate and a variable.
180: * @param p a predicate name
181: * @param v a variable name
182: * @return a fact
183: */
184: protected Fact fact2(String p, String v) {
185: return lfs.fact(new SimplePredicate(p,
186: new Class[] { String.class }), lfs.variable(v,
187: String.class));
188: }
189:
190: }
|