autofactstest.java
来自「Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规」· Java 代码 · 共 185 行
JAVA
185 行
/*
* 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.util;
import java.util.*;
import junit.framework.TestCase;
import org.mandarax.kernel.*;
import org.mandarax.util.*;
/**
* Abstract test case for various auto facts test cases where we test <code>clauses()</code>.
* Note that plenty of test cases for the <code>clauses(Clause,Object)</code> method are contained
* in the package <code>test.org.mandarax.math</code>!
* @see org.mandarax.util.AutoFacts
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 1.2
*/
public abstract class AutoFactsTest extends TestCase {
/**
* Constructor.
*/
public AutoFactsTest() {
super ("test");
}
/**
* Get the auto facts.
* @return auto facts
*/
protected AutoFacts getAutoFacts() {
// build object wrappers for extensions (for convenience, extensions are defined
// as primitives in test cases)
double[] dd = getDoubleTestSet ();
final Vector doubles = new Vector ();
for(int i = 0; i < dd.length; i++) {
doubles.add (new Double (dd[i]));
}
int[] ii = getIntTestSet ();
final Vector integers = new Vector ();
for(int i = 0; i < ii.length; i++) {
integers.add (new Integer (ii[i]));
}
// create auto facts
AutoFacts af = new AutoFacts () {
public Collection getExtension(Class type) {
if(type == Double.class) {
return doubles;
}
if(type == Integer.class) {
return integers;
}
return super.getExtension (type);
}
};
// register predicates
Predicate[] p = { getPredicate1 (), getPredicate2 () };
af.setPredicates (p);
return af;
}
/**
* Get the set of double numbers used for testing.
* @return an array of doubles
*/
protected abstract double[] getDoubleTestSet();
/**
* Get the expected number of generated facts.
* @return the number of facts expected
*/
protected abstract int getExpected();
/**
* Get the set of int numbers used for testing.
* @return an array of integers
*/
protected abstract int[] getIntTestSet();
/**
* Get the first predicate we use for testing.
* This predicate means "an integer equals a rounded double".
* @return a predicate
*/
private Predicate getPredicate1() {
Class[] struct = { Integer.class, Double.class };
Predicate p = new SimplePredicate ("= (round)", struct) {
public Object perform(Term[] t,Session session) {
int i;
double d;
try {
i = ((Number) (t[0].resolve (null))).intValue ();
d = ((Number) (t[1].resolve (null))).doubleValue ();
} catch(Exception x) {
throw new IllegalArgumentException ();
}
return(Math.round (d) == i)
? Boolean.TRUE
: Boolean.FALSE;
}
};
return p;
}
/**
* Get the second predicate we use for testing.
* This predicate means "an integer equals a truncated double".
* @return a predicate
*/
private Predicate getPredicate2() {
Class[] struct = { Integer.class, Double.class };
Predicate p = new SimplePredicate ("= (floor)", struct) {
public Object perform(Term[] t,Session session) {
int i;
double d;
try {
i = ((Number) (t[0].resolve (null))).intValue ();
d = ((Number) (t[1].resolve (null))).doubleValue ();
} catch(Exception x) {
throw new IllegalArgumentException ();
}
return(Math.floor (d) == i)
? Boolean.TRUE
: Boolean.FALSE;
}
};
return p;
}
/**
* Perform the test.
*/
public void test() {
AutoFacts af = getAutoFacts ();
System.out.println ("PERFORMING TEST " + getClass ());
int counter = 0;
for(Iterator it = af.clauses (); it.hasNext (); ) {
counter = counter + 1;
System.out.println ("Next generated fact is: " + it.next ());
}
System.out.println ();
assertTrue (getExpected () == counter);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?