package bmi;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.util.Random;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;

/**
 * This is a sample class to launch a rule.
 */
public class DroolsTest {
    
    private static Writer output = null;

	public static final void main(String[] args) throws IOException {
	    Random rand= new Random();
        double bmi = 0;
        BMIClass[] array = new BMIClass[10000];
        File file = new File("writeDroolsRules.txt");
        output = new BufferedWriter(new FileWriter(file));
        for(int i = 0; i < 10000; i++) {
           bmi = rand.nextDouble();
           bmi = bmi * 50 + 15;
           BMIClass bmiClass = new BMIClass();
           bmiClass.setBmi(bmi);
           array[i] = bmiClass;
        }
        for(int i = 0; i < 10000; i++) {
            BMIClass bmiClass = array[i];
            try {
                //load up the rulebase
                RuleBase ruleBase = readRule();
                WorkingMemory workingMemory = ruleBase.newStatefulSession();
                // go !
                workingMemory.insert(bmiClass);
                long before = System.nanoTime();
                workingMemory.fireAllRules();  
                long after = System.nanoTime();
                long total = after-before;
                output.write(total + "\t" + bmiClass.getBmi());
                ((BufferedWriter) output).newLine();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
	}
	
	private static RuleBase readRule() throws Exception {
	    //read in the source
	    Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/Sample.drl" ) );
	    
	    //optionally read in the DSL (if you are using it).
	    //Reader dsl = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/mylang.dsl" ) );

	    //Use package builder to build up a rule package.
	    //An alternative lower level class called "DrlParser" can also be used...
	    
	    PackageBuilder builder = new PackageBuilder();

	    //this will parse and compile in one step
	    //NOTE: There are 2 methods here, the one argument one is for normal DRL.
	    builder.addPackageFromDrl( source );

	    //Use the following instead of above if you are using a DSL:
	    //builder.addPackageFromDrl( source, dsl );
	    
	    //get the compiled package (which is serializable)
	    Package pkg = builder.getPackage();
	    
	    //add the package to a rulebase (deploy the rule package).
	    RuleBase ruleBase = RuleBaseFactory.newRuleBase();
	    ruleBase.addPackage( pkg );
	    return ruleBase;
	}	
	
	public static class BMIClass {
	    
	    private double bmi;

        public BMIClass(double bmi) {
            this.bmi = bmi;
        }
        
        public BMIClass() {
        }
        
        public double getBmi() {
            return bmi;
        }
        
        public void setBmi(double bmi) {
            this.bmi = bmi;
        }
	}
}