package org.drools.examples;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.StatelessSession;
import org.drools.StatelessSessionResult;
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 {
        System.setProperty("drools.defaultlanguage", "en");
        System.setProperty("drools.defaultcountry", "US");
        System.setProperty("drools.dateformat", "MMM-dd-yyyy HH:mm:ss Z");

        //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 StatelessSession session = ruleBase.newStatelessSession();

        session.addEventListener(new DebugAgendaEventListener());
        session.addEventListener(new DebugWorkingMemoryEventListener());

        final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger(session);
        logger.setFileName("log/helloworld");

        final Message message = new Message();
        MyDate date = new MyDate("Aug-03-2009");
        date.setHours(10);
        message.setDate(date);
        message.setMessage("msg");
        message.setStatus(2147483647);
        message.setD((double) 99.9);
        message.setF((float) 99.9);
        message.setArr(new int[] { 1 });
        message.setB((byte) 1);
        message.setDb(new BigDecimal("101.1"));
        message.set_workspace_Service_Function_js01("rule1");
        first x = new first();
        x.setI(1000000001);
        x.setL(9223372036854775807L);
        x.setD((double) 99.9);
        x.setF((float) 99.9);
        x.setB((byte) 1);
        x.setDb(new BigDecimal("101.1"));
        x.setBi(new BigInteger("2147483647"));
        int[] ia = { 1, 2, 3 };
        x.setIa(ia);
        x.setA_b("ab");
        second y = new second();
        y.setFir(x);
        message.setSec(y);

        final IntBean ib = new IntBean();
        ib.setI(1);
        List objects = new ArrayList();
        objects.add(message);
        StatelessSessionResult result = session.executeWithResults(objects.toArray());
        Iterator iter = result.iterateObjects();
        while (iter.hasNext()) {
            Object obj = iter.next();
            if (obj instanceof Message)
                System.out.println(((Message) obj).getMessage());
        }
    }

    public static class first {
        private int i;
        private long l;
        private Float f;
        private Double d;
        private byte b;
        private BigDecimal db;
        private BigInteger bi;
        private int[] ia;
        private String a_b;

        public String getA_b() {
            return a_b;
        }

        public void setA_b(String aB) {
            a_b = aB;
        }

        public int[] getIa() {
            return ia;
        }

        public void setIa(int[] ia) {
            this.ia = ia;
        }

        public BigInteger getBi() {
            return bi;
        }

        public void setBi(BigInteger bi) {
            this.bi = bi;
        }

        public BigDecimal getDb() {
            return db;
        }

        public void setDb(BigDecimal db) {
            this.db = db;
        }

        public byte getB() {
            return b;
        }

        public void setB(byte b) {
            this.b = b;
        }

        public int getI() {
            return i;
        }

        public void setI(int i) {
            this.i = i;
        }

        public long getL() {
            return l;
        }

        public void setL(long l) {
            this.l = l;
        }

        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 MyDate date;
        private int status;
        private second sec;
        private float f;
        private double d;
        private int[] arr;
        private Byte b;
        private BigDecimal db;
        private String _workspace_Service_Function_js01;

        public String get_workspace_Service_Function_js01() {
            return _workspace_Service_Function_js01;
        }

        public void set_workspace_Service_Function_js01(String workspaceServiceFunctionJs01) {
            _workspace_Service_Function_js01 = workspaceServiceFunctionJs01;
        }

        public BigDecimal getDb() {
            return db;
        }

        public void setDb(BigDecimal db) {
            this.db = db;
        }

        public Byte getB() {
            return b;
        }

        public void setB(Byte b) {
            this.b = b;
        }

        public int[] getArr() {
            return arr;
        }

        public void setArr(int[] arr) {
            this.arr = arr;
        }

        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;
        }

        public MyDate getDate() {
            return date;
        }

        public void setDate(MyDate date) {
            this.date = date;
        }
    }

    public static class MyDate extends Date {
        private long time;

        public MyDate() {
            super();
            // TODO Auto-generated constructor stub
        }

        public MyDate(long date) {
            super(date);
            // TODO Auto-generated constructor stub
        }

        public MyDate(int year, int month, int date, int hrs, int min, int sec) {
            super(year, month, date, hrs, min, sec);
            // TODO Auto-generated constructor stub
        }

        public MyDate(int year, int month, int date, int hrs, int min) {
            super(year, month, date, hrs, min);
            // TODO Auto-generated constructor stub
        }

        public MyDate(int year, int month, int date) {
            super(year, month, date);
            // TODO Auto-generated constructor stub
        }

        public MyDate(String s) {
            super(s);
            // TODO Auto-generated constructor stub
        }

        public long getTime() {
            return super.getTime();
        }

        public void setTime(long time) {
            super.setTime(time);
        }

    }

}
