001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:paschke@in.tum.de">Adrian Paschke</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:
019: package test.org.mandarax.reference;
020:
021: import java.util.Iterator;
022: import org.mandarax.kernel.*;
023: import org.mandarax.lib.math.IntArithmetic;
024: import org.mandarax.reference.AdvancedKnowledgeBase;
025: import org.mandarax.reference.ResolutionInferenceEngine4;
026: import org.mandarax.util.LogicFactorySupport;
027: import org.apache.log4j.*;
028:
029: /**
030: * Test complex terms.
031: * @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
032: * @version 3.4 <7 March 05>
033: * @since 3.3.1
034: */
035: public class TestComplexTerm extends MandaraxTestCase {
036: private KnowledgeBase kb = null;
037: private InferenceEngine ie = null;
038: private Predicate result1 = null;
039: private Predicate result2 = null;
040:
041: /**
042: * Constructor.
043: * @param testName the name of the test
044: */
045: public TestComplexTerm(String testName,
046: KnowledgeBase aKnowledgeBase,
047: InferenceEngine anInferenceEngine) {
048: super (testName);
049: kb = aKnowledgeBase;
050: ie = anInferenceEngine;
051: }
052:
053: /**
054: * Constructor.
055: * @param testName the name of the test
056: */
057: public TestComplexTerm(String testName) {
058: this (testName, new AdvancedKnowledgeBase(),
059: new ResolutionInferenceEngine4());
060: }
061:
062: /**
063: * Set up the test case.
064: */
065: public void setUp() throws Exception {
066: super .setUp();
067:
068: // init kb
069: LogicFactorySupport lfs = new LogicFactorySupport();
070:
071: Predicate input = new SimplePredicate("input",
072: new Class[] { Integer.class });
073: result1 = new SimplePredicate("result1",
074: new Class[] { Integer.class });
075: result2 = new SimplePredicate("result2", new Class[] {
076: Integer.class, Integer.class });
077:
078: // input(1)
079: Fact f = lfs.fact(input, new Integer(1));
080: kb.add(f);
081:
082: // result1(plus(N,N)) <-- input(N)
083: Rule rule1 = lfs.rule(lfs.prereq(input, lfs.variable("N",
084: Integer.class)),
085: // -->
086: lfs.fact(result1, lfs.cplx(IntArithmetic.PLUS, lfs
087: .variable("N", Integer.class), lfs.variable(
088: "N", Integer.class))));
089: kb.add(rule1);
090:
091: // result2(N,plus(N,N)) <-- input(N)
092: Rule rule2 = lfs.rule(lfs.prereq(input, lfs.variable("N",
093: Integer.class)),
094: // -->
095: lfs.fact(result2, lfs.variable("N", Integer.class), lfs
096: .cplx(IntArithmetic.PLUS, lfs.variable("N",
097: Integer.class), lfs.variable("N",
098: Integer.class))));
099: kb.add(rule2);
100:
101: LOG_TEST.debug("Setup test case " + this );
102: LOG_TEST.debug("kb contains:");
103: for (Iterator iter = kb.getClauseSets().iterator(); iter
104: .hasNext();) {
105: LOG_TEST.debug(iter.next().toString());
106: }
107: }
108:
109: /**
110: * Test method 1 - WARNING: this is not supported by current IE implementations (Jens, March 05) !!
111: */
112: public void testComplexTerm1() throws Exception {
113: LogicFactorySupport lfs = new LogicFactorySupport();
114: // query ? result1(2)
115: Query q = lfs.query(lfs.fact(result1, new Integer(2)),
116: "?result1(2)");
117: LOG_TEST.debug("Query is : " + q.getFacts()[0]);
118: ResultSet rs = ie.query(q, kb, InferenceEngine.ONE,
119: InferenceEngine.BUBBLE_EXCEPTIONS);
120: if (!rs.next())
121: assertTrue("Query " + q.getName() + " failed ", false);
122: assertTrue(true);
123: }
124:
125: /**
126: * Test method 2.
127: */
128: public void testComplexTerm2() throws Exception {
129: LogicFactorySupport lfs = new LogicFactorySupport();
130: // query ? result1(N)
131: Query q = lfs.query(lfs.fact(result1, lfs.variable("N",
132: Integer.class)), "?result1(N)");
133: LOG_TEST.debug("Query is : " + q.getFacts()[0]);
134: ResultSet rs = ie.query(q, kb, InferenceEngine.ONE,
135: InferenceEngine.BUBBLE_EXCEPTIONS);
136: if (!rs.next())
137: assertTrue("Query " + q.getName() + " failed ", false);
138: assertTrue(((Integer) rs.getResult(Integer.class, "N"))
139: .intValue() == 2);
140: }
141:
142: /**
143: * Test method 3.
144: */
145: public void testComplexTerm3() throws Exception {
146: LogicFactorySupport lfs = new LogicFactorySupport();
147: // query ? result2(1,N)
148: Query q = lfs.query(lfs.fact(result2, new Integer(1), lfs
149: .variable("N", Integer.class)), "?result2(1,N)");
150: LOG_TEST.debug("Query is : " + q.getFacts()[0]);
151: ResultSet rs = ie.query(q, kb, InferenceEngine.ONE,
152: InferenceEngine.BUBBLE_EXCEPTIONS);
153: if (!rs.next())
154: assertTrue("Query " + q.getName() + " failed ", false);
155: assertTrue(((Integer) rs.getResult(Integer.class, "N"))
156: .intValue() == 2);
157: }
158:
159: }
|