001: package test.org.mandarax.reference;
002:
003: /*
004: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
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 org.mandarax.kernel.ComplexTerm;
022: import org.mandarax.kernel.ConstantTerm;
023: import org.mandarax.kernel.InferenceEngine;
024: import org.mandarax.kernel.InferenceException;
025: import org.mandarax.kernel.KnowledgeBase;
026: import org.mandarax.kernel.Query;
027: import org.mandarax.kernel.ResultSet;
028: import org.mandarax.kernel.Term;
029: import org.mandarax.kernel.VariableTerm;
030: import org.mandarax.lib.math.IntArithmetic;
031:
032: /**
033: * Tests queries returning results still containing variables (in complex terms).
034: * Hint : read the clauses in the test kb as P1 as 'is number' and P2 as 'is even number'.
035: * We then query for even numbers and must get: any 2*x as result.
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 1.9.2
039: */
040: public class TestInferenceEngine16 extends TestInferenceEngineUseMath {
041:
042: /**
043: * Constructor.
044: * @param aKnowledgeBase a new, uninitialized knowledge base that will be used
045: * @param anInferenceEngine the inference engine that will be tested
046: */
047: public TestInferenceEngine16(KnowledgeBase aKnowledgeBase,
048: InferenceEngine anInferenceEngine) {
049: super (aKnowledgeBase, anInferenceEngine);
050: }
051:
052: /**
053: * Add facts and rules to the knowledge base.
054: * @param knowledge org.mandarax.kernel.KnowledgeBase
055: */
056: public void feedKnowledgeBase(KnowledgeBase knowledge) {
057: knowledge.removeAll();
058:
059: // hint : read P1 as 'is number' and P2 as 'is even number'
060: knowledge.add(lfs.fact(P1, "x"));
061: knowledge.add(lfs.rule(lfs.fact(P1, "x"), lfs.fact(P2, lfs
062: .cplx(IntArithmetic.TIMES, new Integer(2), "x"))));
063: }
064:
065: /**
066: * Get the expected number.
067: * @return the name of the person
068: */
069: int getExpectedNumber() {
070: return 42;
071: }
072:
073: /**
074: * Get a description of this test case.
075: * This is used by the <code>org.mandarax.demo</code>
076: * package to display the test cases.
077: * @return a brief description of the test case
078: */
079: public String getDescription() {
080: return "Testing complex terms in result sets";
081: }
082:
083: /**
084: * Get the query.
085: * @return a query
086: */
087: public Query getQuery() {
088: return lfs.query(lfs.fact(P2, QUERY_VARIABLE), "a query");
089: }
090:
091: /**
092: * Run the test.
093: */
094: public void testInferenceEngine() {
095: LOG_TEST.info("Start Testcase " + getClass().getName()
096: + " , test method: " + "testInferenceEngine()");
097: try {
098: ResultSet rs = ie.query(getQuery(), kb,
099: getCardinalityConstraint(),
100: InferenceEngine.BUBBLE_EXCEPTIONS);
101: boolean result = rs.next();
102: ComplexTerm t = (ComplexTerm) rs.getResult(Integer.class,
103: QUERY_VARIABLE);
104: result = result
105: && t.getFunction().equals(IntArithmetic.TIMES);
106: Term[] terms = t.getTerms();
107: result = result
108: && (terms[0] instanceof ConstantTerm)
109: && (((ConstantTerm) terms[0]).getObject()
110: .equals(new Integer(2)));
111: result = result && (terms[1] instanceof VariableTerm)
112: && (terms[1].getType() == Integer.class);
113: assertTrue(result);
114: } catch (InferenceException x) {
115: assertTrue(false);
116: }
117: LOG_TEST.info("Finish Testcase " + getClass().getName()
118: + " , test method: " + "testInferenceEngine()");
119: }
120:
121: }
|