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

📄 performancetest1.java

📁 Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规则
💻 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 org.mandarax.examples.family.performance;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Category;
import org.apache.log4j.Level;
import org.mandarax.kernel.InferenceEngine;
import org.mandarax.kernel.KnowledgeBase;
import org.mandarax.kernel.Predicate;
import org.mandarax.kernel.Query;
import org.mandarax.kernel.ResultSet;
import org.mandarax.kernel.Rule;
import org.mandarax.kernel.SimplePredicate;
import org.mandarax.reference.AdvancedKnowledgeBase;
import org.mandarax.reference.ResolutionInferenceEngine3;
import org.mandarax.util.LogicFactorySupport;

/**
 * Performance test based on a tree model with one root, NUMBER_OF_SONS children 
 * and NUMBER_OF_GRANDCHILDREN_PER_SON children for each child node. 
 * The tree is described by father facts, and there is one rule describing 
 * what it means to be sibling (brother). The number of expected results is:
 * (using N for NUMBER_OF_SONS and G for NUMBER_OF_GRANDCHILDREN_PER_SON)
 * <tt>N*(N-1) + N*G+(G-1)</tt>. 
 * E.g., in this class we use N=25 and G=3, this yields 750.
 * <br>
 * We run the computation 3 times to give JIT compilers time to optimize code execution.
 * <br. 
 * This version uses 'optimized' constant and variable names that are easy to match (see String#equals). 
 * Here is the result obtained using a Pentium 4 1.8, 512 MB RAM, Win XP Pro, SUN JDK 1.4.2, (version 3.3, 19 Feb 2004):<p>
 * 1st run solutions found: 750 time (in millis): 511<br>
 * 2nd run solutions found: 750 time (in millis): 320<br>
 * 3nd run solutions found: 750 time (in millis): 280<p>
 * <p>
 * Same as before, but uses BEA's jrockit 81 sp2_141_05 JVM:<p> 
 * 1st run solutions found: 750 time (in millis): 420<br>
 * 2nd run solutions found: 750 time (in millis): 231<br>
 * 3nd run solutions found: 750 time (in millis): 360<p>
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 2.1
 */
public class PerformanceTest1 {
	public static final String FAMILY_ROOT = "root";
	public static final int NUMBER_OF_SONS = 25;
	public static final int NUMBER_OF_GRANDCHILDREN_PER_SON = 3;
	
	public static void main(String[] args) {
		
        // disable deep level logging
        BasicConfigurator.configure();
        Category.getRoot().setLevel(Level.INFO);
		
		System.out.println("Run performance test " + PerformanceTest1.class.getName());
		
		// get kb 
		KnowledgeBase kb = createKB();
		System.out.println("kb created");
		
		// issue query
		InferenceEngine ie = new ResolutionInferenceEngine3();
		Query query = kb.getQuery("query");
		
		System.out.println("Inference engine performance test");
        System.out.println("ie tested: " + ie);
        System.out.println("kb size: " + kb.getClauseSets().size());
        	
        System.out.println("1st run");	
		run(ie,query,kb,false);
		System.out.println("2nd run");
		run(ie,query,kb,false);
		System.out.println("3nd run");
		run(ie,query,kb,false);
		
		System.out.println("Computation finished !");
		System.exit(0);
		
	}
	/**
	 * Run and time the computation.
	 */
	private static void run(InferenceEngine ie,Query q,KnowledgeBase kb,boolean logResults) {
		try {
			long before = System.currentTimeMillis();
			ResultSet rs = ie.query(q,kb,InferenceEngine.ALL,InferenceEngine.BUBBLE_EXCEPTIONS);
			long after = System.currentTimeMillis();
			
			// count results
	        int solutions = 0;
	        while (rs.next()) {
	        	solutions = solutions+1;
	        	if (logResults) {
	        		System.out.print(rs.getResult(String.class,"x"));
	        		System.out.print(" is brother of ");
	        		System.out.println(rs.getResult(String.class,"y"));
	        	}
	        }
	        
	        // report
        	System.out.println("solutions found: " + solutions);
        	System.out.println("time (in millis): " + (after-before));
		}
		catch (Exception x) {
			x.printStackTrace();
		}
	}
	/**
	 * Set up the knowledge base.
	 * @return the knowledge base.
	 */
	private static KnowledgeBase createKB() {
		LogicFactorySupport lfs = new LogicFactorySupport();
		
		// init predicates 
		Class[] struct = { String.class, String.class };
		Predicate predicate_is_father = new SimplePredicate("father", struct);
		Predicate predicate_is_brother = new SimplePredicate("brother", struct);
		
		// add knowledge - rule
		KnowledgeBase kb = new AdvancedKnowledgeBase();
		Rule rule =
			lfs.rule(
				lfs.prereq(predicate_is_father, lfs.variable("s"), lfs.variable("f")),
				lfs.prereq(predicate_is_father, lfs.variable("t"), lfs.variable("f")),
				lfs.prereq(org.mandarax.lib.text.StringArithmetic.NOT_EQUAL, lfs.variable("s"), lfs.variable("t")),
				lfs.fact(predicate_is_brother, lfs.variable("s"), lfs.variable("t")));
		kb.add(rule);
		
		
		// add facts for family tree
		for (int i=0;i<NUMBER_OF_SONS;i++) {
			kb.add(
				lfs.fact(
					predicate_is_father,
					lfs.cons(""+i,String.class),
					lfs.cons(FAMILY_ROOT,String.class)
				)
			);
			for (int j=0;j<NUMBER_OF_GRANDCHILDREN_PER_SON;j++) {
				kb.add(
					lfs.fact(
						predicate_is_father,
						lfs.cons(""+i+"."+j,String.class),
						lfs.cons(""+i,String.class)
					)
				);	
			}
		}
		// add query
		kb.addQuery(
			lfs.query(
				lfs.fact(
					predicate_is_brother,
					lfs.variable("x",String.class),
					lfs.variable("y",String.class)
				),
				"query"
			)
		);
		
		// return
		return kb;
	}
}

⌨️ 快捷键说明

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