📄 testrecursion1.java
字号:
/*
* Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package test.org.mandarax.reference;
import java.util.Iterator;
import org.mandarax.kernel.*;
import org.mandarax.lib.math.IntArithmetic;
import org.mandarax.reference.AbstractResolutionInferenceEngine;
import org.mandarax.util.LogicFactorySupport;
/**
* Test recursion. Uses the factorial example.
* In particular, we test whether terms are simplified using semantic evaluation
* within the scope of a simple predicate.
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 3.3
*/
public class TestRecursion1 extends MandaraxTestCase {
private KnowledgeBase kb = null;
private InferenceEngine ie = null;
private Predicate p = null;
/**
* Constructor.
* @param testName the name of the test
*/
public TestRecursion1(String testName, KnowledgeBase aKnowledgeBase, InferenceEngine anInferenceEngine) {
super(testName);
kb = aKnowledgeBase;
ie = anInferenceEngine;
}
/**
* Set up the test case.
*/
public void setUp() throws Exception {
super.setUp();
//BasicConfigurator.configure();
//Logger.getRoot().setLevel(Level.DEBUG);
// init kb
// the order does matter, but problems can be avoided by setting the appropriate kb comparator,
// or by adding another prereq to the rule
LogicFactorySupport lfs = new LogicFactorySupport();
p = new SimplePredicate("factorial",new Class[]{Integer.class,Integer.class});
// fact factorial(1,1)
Fact f = lfs.fact(p,new Integer(1),new Integer(1));
kb.add(f);
// rule if factorial(n-1,k) then factorial(n,n*k)
Rule r = lfs.rule(
lfs.prereq(
p,
lfs.cplx(IntArithmetic.MINUS,lfs.variable("n",Integer.class),new Integer(1)),
lfs.variable("k",Integer.class)
),
lfs.fact(
p,
lfs.variable("n",Integer.class),
lfs.cplx(IntArithmetic.TIMES,lfs.variable("n",Integer.class),lfs.variable("k",Integer.class))
)
);
kb.add(r);
LOG_TEST.debug("Setup test case " + this);
LOG_TEST.debug("kb contains:");
for (Iterator iter = kb.getClauseSets().iterator();iter.hasNext();) {
LOG_TEST.debug(iter.next().toString());
}
}
/**
* Build a query for a certain number.
* @param i
* @return a query
*/
private Query buildQuery(int i) {
LogicFactorySupport lfs = new LogicFactorySupport();
// query ? factorial(i,i)
Query q = lfs.query(
lfs.fact(p,new Integer(i),lfs.variable("x",Integer.class)),
"?factorial(" + i + ")"
);
LOG_TEST.debug("Query is : " + q.getFacts()[0]);
return q;
}
/**
* Private test method.
* @param i a number
*/
private void test(int i) throws Exception {
// this number of steps should be sufficient
if (ie instanceof AbstractResolutionInferenceEngine) ((AbstractResolutionInferenceEngine)ie).setMaxSteps(100);
Query q = buildQuery(i);
ResultSet rs = ie.query(q,kb,InferenceEngine.ONE,InferenceEngine.BUBBLE_EXCEPTIONS);
rs.next();
int computed = ((Integer)rs.getResult(Integer.class,"x")).intValue();
int expected = factorial(i);
rs.close();
assertTrue(computed==expected);
}
/**
* Compute the factorial for a certain number.
* @param i a number
* @return the factorial of this number
*/
private int factorial(int i) {
if (i==0) return 1;
else return i*factorial(i-1);
}
/**
* Test method.
*/
public void test1() throws Exception {
test(1);
}
/**
* Test method.
*/
public void test2() throws Exception {
test(2);
}
/**
* Test method.
*/
public void test3() throws Exception {
test(3);
}
/**
* Test method.
*/
public void test4() throws Exception {
test(4);
}
/**
* Test method.
*/
public void test5() throws Exception {
test(5);
}
/**
* Test method.
*/
public void test6() throws Exception {
test(10);
}
/**
* Test method.
*/
public void test7() throws Exception {
test(30);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -