⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcomplexterm.java

📁 Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规则
💻 JAVA
字号:
/*
 * Copyright (C) 1999-2004 <a href="mailto:paschke@in.tum.de">Adrian Paschke</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.AdvancedKnowledgeBase;
import org.mandarax.reference.ResolutionInferenceEngine4;
import org.mandarax.util.LogicFactorySupport;
import org.apache.log4j.*;

/**
 * Test complex terms.  
 * @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
 * @version 3.4 <7 March 05>
 * @since 3.3.1
 */
public class TestComplexTerm extends MandaraxTestCase {
	private KnowledgeBase kb = null;
	private InferenceEngine ie = null;
	private Predicate result1 = null;
	private Predicate result2 = null;
	
	/**
	 * Constructor.
	 * @param testName the name of the test
	 */
	public TestComplexTerm(String testName, KnowledgeBase aKnowledgeBase, InferenceEngine anInferenceEngine) {
		super(testName);
        kb = aKnowledgeBase;
        ie = anInferenceEngine;
	}
	/**
	 * Constructor.
	 * @param testName the name of the test
	 */
	public TestComplexTerm(String testName) {
		this(testName,new AdvancedKnowledgeBase(),new ResolutionInferenceEngine4());
	}

	/**
	 * Set up the test case.
	 */
	public void setUp() throws Exception {
		super.setUp();

		
		// init kb		
		LogicFactorySupport lfs = new LogicFactorySupport();
		
		
		Predicate input = new SimplePredicate("input", new Class [] { Integer.class });
		result1 = new SimplePredicate("result1", new Class [] {Integer.class});
		result2 = new SimplePredicate("result2", new Class [] {Integer.class, Integer.class});
		
		// input(1)
		Fact f = lfs.fact(input,new Integer(1));
		kb.add(f);
				
		// result1(plus(N,N)) <-- input(N)
		Rule rule1 = 	lfs.rule(			
			lfs.prereq(input, lfs.variable("N",Integer.class)),				
			// -->					
			lfs.fact(result1,lfs.cplx(IntArithmetic.PLUS, lfs.variable("N",Integer.class), lfs.variable("N", Integer.class)))
		);
		kb.add(rule1);
		
		// result2(N,plus(N,N)) <-- input(N)
		Rule rule2 = 	lfs.rule(			
			lfs.prereq(input, lfs.variable("N",Integer.class)),				
			// -->					
			lfs.fact(result2,lfs.variable("N",Integer.class), lfs.cplx(IntArithmetic.PLUS, lfs.variable("N",Integer.class), lfs.variable("N", Integer.class)))
		);
		kb.add(rule2);
		
		
		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());
		}
	}


	/**
	 * Test method 1 - WARNING: this is not supported by current IE implementations (Jens, March 05) !!	 
	 */
	public void testComplexTerm1() throws Exception {
		LogicFactorySupport lfs = new LogicFactorySupport();
		// query ? result1(2)
		Query q = lfs.query(lfs.fact(result1,new Integer(2)),"?result1(2)");
		LOG_TEST.debug("Query is : " + q.getFacts()[0]);
		ResultSet rs = ie.query(q,kb,InferenceEngine.ONE,InferenceEngine.BUBBLE_EXCEPTIONS);
		if (!rs.next()) assertTrue("Query "+q.getName()+" failed ",false);
		assertTrue(true);
	}
	
	/**
	 * Test method 2.
	 */
	public void testComplexTerm2() throws Exception {
		LogicFactorySupport lfs = new LogicFactorySupport();
		// query ? result1(N)
		Query q = lfs.query(lfs.fact(result1,lfs.variable("N",Integer.class)),"?result1(N)");
		LOG_TEST.debug("Query is : " + q.getFacts()[0]);
		ResultSet rs = ie.query(q,kb,InferenceEngine.ONE,InferenceEngine.BUBBLE_EXCEPTIONS);
		if (!rs.next()) assertTrue("Query "+q.getName()+" failed ",false);
		assertTrue(((Integer)rs.getResult(Integer.class,"N")).intValue()==2);
	}
	
	/**
	 * Test method 3.
	 */
	public void testComplexTerm3() throws Exception {
		LogicFactorySupport lfs = new LogicFactorySupport();
		// query ? result2(1,N)
		Query q = lfs.query(lfs.fact(result2,new Integer(1),lfs.variable("N",Integer.class)),"?result2(1,N)");
		LOG_TEST.debug("Query is : " + q.getFacts()[0]);
		ResultSet rs = ie.query(q,kb,InferenceEngine.ONE,InferenceEngine.BUBBLE_EXCEPTIONS);
		if (!rs.next()) assertTrue("Query "+q.getName()+" failed ",false);
		assertTrue(((Integer)rs.getResult(Integer.class,"N")).intValue()==2);
	}
		
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -