📄 zkbtestcase.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.zkb;
import java.io.File;
import java.util.Comparator;
import java.util.Iterator;
import org.mandarax.kernel.Clause;
import org.mandarax.kernel.ClauseSet;
import org.mandarax.kernel.ClauseSetException;
import org.mandarax.kernel.ExtendedKnowledgeBase;
import org.mandarax.kernel.KnowledgeBase;
import org.mandarax.kernel.Rule;
import org.mandarax.kernel.validation.TestCase;
import org.mandarax.util.logging.LogCategories;
import org.mandarax.zkb.BinarySerializationOPS;
import org.mandarax.zkb.KnowledgeBasePlusAttachment;
import org.mandarax.zkb.ObjectPersistencyService;
import org.mandarax.zkb.ZKBDriver;
import org.mandarax.zkb.ZKBManager;
import test.org.mandarax.testsupport.TestUtils;
/**
* Abstract test case for ZKB drivers / OPSs.
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 2.2
*/
public abstract class ZKBTestCase extends junit.framework.TestCase implements LogCategories {
protected ZKBManager zkbMgr = new ZKBManager();
//protected DOMBuilder domBuilder = new DOMBuilder();
protected String name = null;
// the file name used to output / temp storage
protected File file = null;
protected boolean zipOrFolder = true;
/**
* Construct a ZKB test case.
* @param driver the zkb driver
* @param ops the object persistency service
* @param name the name of the test case
* @param zipOrFolder whether the zkb is stored in a zip file or folder true - zip, false - folder
*/
public ZKBTestCase(
ZKBDriver aDriver,
ObjectPersistencyService ops,
String name,
boolean zipOrFolder) {
super("test");
zkbMgr.setDriver(aDriver);
zkbMgr.setOps(ops);
setName(name);
this.zipOrFolder = zipOrFolder;
// build filename
StringBuffer buf = new StringBuffer();
buf.append("_test_zkb_");
buf.append(getName());
buf.append("_");
buf.append(ops instanceof BinarySerializationOPS ? "bin" : "xml");
buf.append("_4driver_");
buf.append(aDriver.getName());
if (zipOrFolder) buf.append(".zip");
file = zipOrFolder?TestUtils.getFile(buf.toString()):TestUtils.getFolder(buf.toString());
}
/**
* Get the knowledge base used as test data.
* @return a knowledge base
*/
public abstract KnowledgeBase getKB();
/**
* Get the attachment to be saved with the knowledge base.
* @return a knowledge base
*/
public Object getAttachment() {
return null;
}
/**
* Compare knowledge bases.
* @return a boolean
* @param kb1 the first kb
* @param kb2 the second kb
*/
public boolean compare(KnowledgeBase kb1, KnowledgeBase kb2) {
boolean result = true;
try {
// compare clauses
Iterator iter1 = kb1.clauses();
Iterator iter2 = kb2.clauses();
while (result && iter1.hasNext() && iter2.hasNext()) {
Clause c1 = (Clause) iter1.next();
Clause c2 = (Clause) iter2.next();
result = result && compare(c1,c2) && (c1.getProperties()==null?c2.getProperties()==null:c1.getProperties().equals(c2.getProperties()));
}
// compare queries
iter1 = kb1.queries();
iter2 = kb2.queries();
while (result && iter1.hasNext() && iter2.hasNext()) {
result = result && (iter1.next().equals(iter2.next()));
}
// compare test cases
iter1 = kb1.testcases();
iter2 = kb2.testcases();
while (result && iter1.hasNext() && iter2.hasNext()) {
TestCase tc1 = (TestCase) iter1.next();
TestCase tc2 = (TestCase) iter2.next();
result = result && compare(tc1,tc2) && (tc1.getProperties()==null?tc2.getProperties()==null:tc1.getProperties().equals(tc2.getProperties()));
}
// compare comparators
if (kb1 instanceof ExtendedKnowledgeBase
&& kb2 instanceof ExtendedKnowledgeBase) {
Comparator comp1 =
((ExtendedKnowledgeBase) kb1).getComparator();
Comparator comp2 =
((ExtendedKnowledgeBase) kb2).getComparator();
result = result && (comp1 == null ? comp2 == null : comp1.equals(comp2));
}
return result;
} catch (ClauseSetException x) {
x.printStackTrace();
}
return false;
}
/**
* Compare clauses.
* @return a boolean
* @param c1 the first clause
* @param c2 the second clause
*/
public boolean compare(Clause c1, Clause c2) {
if (c1 instanceof Rule && c2 instanceof Rule)
return compare((Rule) c1, (Rule) c2);
else
return c1.equals(c2);
}
/**
* Compare rules. Special handling for old drivers: the prerequisite interface has only been introduced
* in version 2.0 !!
* @return a boolean
* @param r1 the first rule
* @param r2 the second rule
*/
public boolean compare(Rule r1, Rule r2) {
return r1.equals(r2);
}
/**
* Compare test cases.
* @return a boolean
* @param tc1 the first test case
* @param tc2 the second test case
*/
public boolean compare(TestCase tc1, TestCase tc2) {
return tc1.equals(tc2);
}
/**
* Run the test.
*/
public void test() throws Exception {
KnowledgeBase original = getKB();
// attach some properties to the clause sets !
for (Iterator iter = original.getClauseSets().iterator();iter.hasNext();) {
ClauseSet cs = (ClauseSet)iter.next();
cs.setProperty("key1","value1");
cs.setProperty("key2","value2");
}
Object attachment = getAttachment();
try {
zkbMgr.exportKnowledgeBase(file, original, attachment);
KnowledgeBasePlusAttachment imported = zkbMgr.importKnowledgeBaseAndAttachment(file);
assertTrue(compare(original, imported.getKB()) && compareAttachments(attachment, imported.getAttachment()));
}
catch (Exception x) {
LOG_TEST.error("Error running test " + this,x);
assertTrue(false);
}
}
/**
* Compare attachments
* @return a boolean
* @param a1 the first attachment
* @param a2 the second attachment
*/
public boolean compareAttachments(Object a1, Object a2) {
return a1 == null ? a2 == null : a1.equals(a2);
}
/**
* Convert the object to a string.
* @return a string
*/
public String toString() {
return name + " (zkb driver- " + zkbMgr.getDriver().getClass() + ", ops- " + zkbMgr.getOps().getClass() + ")";
}
/**
* Returns the name.
* @return String
*/
public String getName() {
return toString();
}
/**
* Sets the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -