import org.drools.*; import org.drools.io.RuleBaseLoader; import org.xml.sax.SAXException; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.io.IOException; public class DroolsPerformanceExample { private RuleBase ruleBase; private List facts; private void initializeFacts(int n) { facts = new ArrayList(n); for(int i = 0; i < n; i++) { facts.add(new Integer(i)); } } private void initializeRuleBase() throws IntegrationException, IOException, SAXException { ruleBase = RuleBaseLoader.loadFromUrl(DroolsPerformanceExample.class.getResource("odds.java.drl")); } private void run(int loopCount, int factCount, int printCount) throws IntegrationException, IOException, SAXException, FactException { initializeRuleBase(); initializeFacts(factCount); int count = 0; long start = System.currentTimeMillis(); long stop; for(int i = 0; i < loopCount; i++) { WorkingMemory workingMemory = ruleBase.newWorkingMemory(); for (Iterator iter = facts.iterator(); iter.hasNext();) { Integer fact = (Integer) iter.next(); workingMemory.assertObject(fact); } workingMemory.fireAllRules(); count++; if (count == printCount) { stop = System.currentTimeMillis(); System.out.println((i - count)+" - "+i+": "+(stop-start)); count = 0; start = System.currentTimeMillis(); } } if (count > 0) { stop = System.currentTimeMillis(); System.out.println((loopCount - count)+" - "+loopCount+": "+(stop-start)); } } public static void main(String[] args) throws IntegrationException, IOException, FactException, SAXException { DroolsPerformanceExample example = new DroolsPerformanceExample(); example.run(1000000, 10, 1000); } }