performancetest1.java

来自「Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规」· Java 代码 · 共 225 行

JAVA
225
字号
/*
 * 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.performance;


import java.io.PrintStream;

import org.apache.log4j.Level;
import org.mandarax.kernel.Fact;
import org.mandarax.kernel.InferenceEngine;
import org.mandarax.kernel.InferenceException;
import org.mandarax.kernel.Predicate;
import org.mandarax.kernel.Query;
import org.mandarax.kernel.ResultSet;
import org.mandarax.kernel.SimplePredicate;
import org.mandarax.kernel.Term;
import org.mandarax.reference.AdvancedKnowledgeBase;
import org.mandarax.reference.ResolutionInferenceEngine;
import org.mandarax.util.LogicFactorySupport;

/**
 * Performance test based on a binary tree of a certain depth.
 * The number of clauses in the kb is SUM(i=1..DEPTH)2^i + 2^N
 * @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 implements PerformanceTest  {
	public static final String QUERY_VARIABLE = "query_var"; 
	private int depth = 5; 
	private int numberOfResults = -1;
	private long time = -1;
    protected org.mandarax.kernel.KnowledgeBase   kb = new AdvancedKnowledgeBase();
    protected static LogicFactorySupport lfs = new LogicFactorySupport();
    protected static Class[] STRUCTURE = {String.class};
    protected static final Term VAR = lfs.variable("x",String.class);
    /**
     * Constructor.
     */
    public PerformanceTest1() {
        super ();
        initialize();
    }
    /**
     * Constructor.
     * @param depth the kb tree depth
     */
    public PerformanceTest1(int depth) {
        super ();
        this.depth = depth;
        initialize();
    }
    /**
     * Run a performance test.
     */
    public static void main(String args[] ) {
    	PerformanceTest1 test = new PerformanceTest1();
    	test.run(new ResolutionInferenceEngine(),true,true,System.out);
    }

    /**
     * Initialize the knowledge base.
     */
    private void initialize() {
    	initialize(0,getRootPredicate());
    	System.out.println("Performance test initialized");
    }
    /**
     * Initialize the knowledge base on a certain hierarchy level.
     * @param level the hierarchy level
     * @param p a predicate
     */
    private void initialize(int level,Predicate p) {
    	if (level<depth) {
    		Predicate p1 = buildNextPredicate(p,true);
    		Predicate p2 = buildNextPredicate(p,true);
    		// left branch
    		kb.add(
    			lfs.rule(
    				lfs.prereq(p1,VAR),
    				lfs.fact(p,VAR)
    			)
    		);
    		initialize(level+1,p1);
    		// right branch
    		kb.add(
    			lfs.rule(
    				lfs.prereq(p2,VAR),
    				lfs.fact(p,VAR)
    			)
    		);    
    		initialize(level+1,p2);	
    	}
    	else {
    		// terminate tree with fact
    		kb.add(
    			lfs.fact(p,lfs.cons(p.getName().replace('p','c'),String.class))
    		);
    	}
    }
    /**
     * Get the next predicate.
     * @param p the previous predicate
     * @param boolean true for the first child node, false for the second one
     * @return the next child predicate
     */
    private Predicate buildNextPredicate(Predicate p,boolean first) {
    	String name = p.getName() + (first?"0":"1");
    	return new SimplePredicate(name,STRUCTURE);
    } 
    /**
     * Get the root predicate.
     * @return the root predicate
     */
    private Predicate getRootPredicate() {
    	return new SimplePredicate("p",STRUCTURE);
    } 

    /**
     * Run the test.
     * Run the test.
     * @param ie the inference engine to be tested
     * @param one whether to return only one result, or all results
     * @param logOn whether to log or not
     * @param out print stream used to output a test summary
     */
    public synchronized void run(InferenceEngine ie,boolean one, boolean logOn, PrintStream out) {
        
        Fact queryFact = lfs.fact(getRootPredicate(),lfs.variable(QUERY_VARIABLE,String.class));
        Query query = lfs.query(queryFact,"?");
        if (ie instanceof ResolutionInferenceEngine) ((ResolutionInferenceEngine)ie).setMaxSteps(100000);
        // logs
        if (logOn) {
			org.apache.log4j.Category.getRoot().setLevel (Level.INFO);
        }
        else {
			org.apache.log4j.Category.getRoot().setLevel (Level.WARN);
    	} 
        
        // query
		try {
			long before = System.currentTimeMillis();
        	ResultSet rs    = ie.query (query,kb,one?InferenceEngine.ONE:InferenceEngine.ALL,InferenceEngine.BUBBLE_EXCEPTIONS);
        	long after = System.currentTimeMillis();
        	// count results
        	int solutions = 0;
        	while (rs.next()) {
        		solutions = solutions+1;
        	}
        	numberOfResults = solutions;
        	
        	// report
        	out.println("Inference engine performance test");
        	out.println("ie tested: " + ie);
        	out.println("kb tree depth: " + depth);
        	out.println("kb size: " + kb.getClauseSets().size());
        	out.println("results wanted: " + (one?"one":"all"));
        	out.println("logs on: " + logOn);
        	out.println("solutions found: " + solutions);
        	out.println("time (in millis): " + (after-before));
        	out.println();
        	
        	time = after-before;
        	
		}
		catch (InferenceException x) {
			x.printStackTrace();
			time = -1;
		}
		
    }
    /**
     * Get the size of the knowledge base used.
     * @return the kb size
     */
    public synchronized int getKBSize() {
    	return kb.getClauseSets().size();
    }
    /**
     * Release the test.
     */
    public synchronized void release() {
    	time = 0;
    	numberOfResults = -1;    	
    }
    /**
     * Get the time needed to run the test.
     * @param a long value (the millis)
     */
    public synchronized long getTime() {
    	return time;
    }
    /**
     * Get the number of results computed.
     * @param the number of results
     */
    public synchronized int getNumberOfResults() {
    	return numberOfResults;
    }
    /**
     * Get the tree depth (if kb is organized as tree, otherwise just return -1).
     * @return the depth of the kb tree
     */
    public synchronized int getDepth() {
    	return depth;
    }


}

⌨️ 快捷键说明

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