[rules-users] Is there a faster way of doing this in Drools ?

soumya_sd soumya_sd at yahoo.com
Wed May 9 07:55:57 EDT 2012


I've a use case where I need to create a rule dynamically every time the user
sends a request. 
My current understanding of Drools is that you need to create a
KnowledgeBuilder and then add the rules 

I'm creating the KnowledgeBase as follows. 

    private static KnowledgeBase readKnowledgeBase() throws Exception {
    	long t1 = System.currentTimeMillis();
        KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();  
    	long t11 = System.currentTimeMillis();


        kbuilder.add(
org.drools.io.ResourceFactory.newByteArrayResource(getRule()),
ResourceType.DRL);

    	long t2 = System.currentTimeMillis();

        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse
knowledge.");
        }
    	long t3 = System.currentTimeMillis();

        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    	long t4 = System.currentTimeMillis();

        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    	long t5 = System.currentTimeMillis();

        
        Collection<KnowledgePackage> kpackages =
kbase.getKnowledgePackages();
		for (KnowledgePackage knowledgePackage : kpackages) {
			System.out.println("Package -------- " + knowledgePackage.getName());
			Collection<Rule> rules = knowledgePackage.getRules();
			for (Rule rule : rules) {
				System.out.println("****" + rule.getName());
			}
		}
    	long t6 = System.currentTimeMillis();
    	
    	System.out.println( (t11-t1) + " " + (t2-t11) + " " + ( t3-t2) + " " +
( t4-t3) + " " + ( t5-t4) + " " + ( t6-t5)+ " " );

        return kbase;
    }

Based on the timing logs the code take majority (more than 80%) of time in
only these two operations. Is there a way to make it faster ? 

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();  
kbuilder.add( org.drools.io.ResourceFactory.newByteArrayResource(getRule()),
ResourceType.DRL);

thanks. 



--
View this message in context: http://drools.46999.n3.nabble.com/Is-there-a-faster-way-of-doing-this-in-Drools-tp3973888.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list