package org.drools.examples;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.audit.WorkingMemoryFileLogger;
import org.drools.compiler.PackageBuilder;
import org.drools.event.DebugAgendaEventListener;
import org.drools.event.DebugWorkingMemoryEventListener;
import org.drools.rule.Package;
import org.wyang.beans.IntBean;

/**
 * This is a sample file to launch a rule package from a rule source file.
 */
public class HelloWorldExample {
    public static final void main(final String[] args) throws Exception {

        //read in the source
        final Reader source = new InputStreamReader(new FileInputStream("test.drl"));

        final PackageBuilder builder = new PackageBuilder();

        //this wil parse and compile in one step
        builder.addPackageFromDrl(source);

        // Check the builder for errors
        if (builder.hasErrors()) {
            System.out.println(builder.getErrors().toString());
            throw new RuntimeException("Unable to compile \"test.drl\".");
        }

        //get the compiled package (which is serializable)
        final Package pkg = builder.getPackage();

        //add the package to a rulebase (deploy the rule package).
        final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
        ruleBase.addPackage(pkg);

        final StatefulSession session = ruleBase.newStatefulSession();

        session.addEventListener(new DebugAgendaEventListener());
        session.addEventListener(new DebugWorkingMemoryEventListener());

        final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger(session);
        logger.setFileName("log/helloworld");

        final Message message = new Message();
        message.setMessage("a");
        message.setStatus(Message.HELLO);
        message.setD((double)99.9);
        message.setF((float)99.9);
        first x = new first();
        x.setI(1);
        x.setD((double)99.9);
        x.setF((float)99.9);
        second y = new second();
        y.setFir(x);
        message.setSec(y);
        
        final IntBean ib = new IntBean();
        ib.setI(1);
        
        session.insert(message);
        session.insert(ib);

        session.fireAllRules();

//        logger.writeToDisk();

        session.dispose();
    }

    public static class first {
        private int i;
        private Float f;
        private Double d;

        public int getI() {
            return i;
        }

        public void setI(int i) {
            this.i = i;
        }

        public Float getF() {
            return f;
        }

        public void setF(Float f) {
            this.f = f;
        }

        public Double getD() {
            return d;
        }

        public void setD(Double d) {
            this.d = d;
        }
        
    }

    public static class second {
        private first fir;

        public first getFir() {
            return fir;
        }

        public void setFir(first fir) {
            this.fir = fir;
        }

    }

    public static class Message {
        public static final int HELLO = 0;
        public static final int GOODBYE = 1;

        private String message;
        private int status;
        private second sec;
        private float f;
        private double d;
        
        public float getF() {
            return f;
        }

        public void setF(float f) {
            this.f = f;
        }

        public double getD() {
            return d;
        }

        public void setD(double d) {
            this.d = d;
        }

        public Message() {

        }

        public String getMessage() {
            return this.message;
        }

        public void setMessage(final String message) {
            this.message = message;
        }

        public int getStatus() {
            return this.status;
        }

        public void setStatus(final int status) {
            this.status = status;
        }

        public second getSec() {
            return sec;
        }

        public void setSec(second sec) {
            this.sec = sec;
        }
    }

}
