📄 derivationeventlistenertest.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.derivationeventlisteners;
import java.util.Map;
import junit.framework.TestCase;
import org.mandarax.kernel.*;
/**
* Test case that is also a simple derivation listener implementation.
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 3.2
*/
public class DerivationEventListenerTest extends TestCase implements DerivationEventListener {
private boolean beforeDerivationHandlerCalled = false;
private boolean resultsOK = true;
private InferenceEngine ie = null;
private int counter = -1;
/**
* Constructor.
* @param ie an inference engine
*/
public DerivationEventListenerTest(InferenceEngine ie) {
super("test");
this.ie = ie;
}
/**
* Set up the test case.
*/
public void setUp() throws Exception {
super.setUp();
counter = -1;
beforeDerivationHandlerCalled = false;
resultsOK = true;
}
/**
* This method is executed before a result set is added to the result set.
* @param session a session object
*/
public void onResultDo(Session session) throws InferenceException {
counter = counter+1;
Map replacements = session.getVariableReplacements();
ConstantTerm t1 = (ConstantTerm)replacements.get(TestKB.QUERY_VAR_1_1);
ConstantTerm t2 = (ConstantTerm)replacements.get(TestKB.QUERY_VAR_1_2);
resultsOK = resultsOK && t1!=null && t1.getObject().equals(TestKB.EXPECTED_VALUES_1_1[counter]);
resultsOK = resultsOK && t2!=null && t2.getObject().equals(TestKB.EXPECTED_VALUES_1_2[counter]);
}
/**
* This method is executed before the IE starts the derivation.
* @param session a session object
*/
public void beforeDerivationDo(Session session) throws InferenceException {
beforeDerivationHandlerCalled = true;
}
/**
* Run the test.
*/
public void test() throws Exception {
KnowledgeBase kb = TestKB.createKB();
Query query = kb.getQuery(TestKB.QUERY_NAME1);
query.setDerivationEventListeners(new DerivationEventListener[]{this});
ResultSet rs = ie.query(query,kb,InferenceEngine.ALL,InferenceEngine.BUBBLE_EXCEPTIONS);
assertTrue(beforeDerivationHandlerCalled && resultsOK && (counter==TestKB.EXPECTED_VALUES_1_1.length-1));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -