package com.sample;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.event.rule.DebugAgendaEventListener;
import org.drools.event.rule.DebugWorkingMemoryEventListener;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

import java.sql.Timestamp;

/**
 * This is a sample class to launch a rule.
 */
public class DroolsTest {

	public static final void main(String[] args) {
		try {
			SampleEvent event1 = new SampleEvent();
			event1.setDtOperIni(Timestamp.valueOf("2011-04-02 10:37:25"));
			event1.setiResultado(0);
			SampleEvent event2 = new SampleEvent();
			event2.setDtOperIni(Timestamp.valueOf("2011-04-02 10:47:56"));
			event2.setiResultado(0);
			SampleEvent event3 = new SampleEvent();
			event3.setDtOperIni(Timestamp.valueOf("2011-04-02 10:58:25"));
			event3.setiResultado(0);
			SampleEvent event4 = new SampleEvent();
			event4.setDtOperIni(Timestamp.valueOf("2011-04-02 11:08:55"));
			event4.setiResultado(0);
			SampleEvent event5 = new SampleEvent();
			event5.setDtOperIni(Timestamp.valueOf("2011-04-02 11:19:26"));
			event5.setiResultado(0);
			
			// load up the knowledge base
			KnowledgeBase kbase = readKnowledgeBase();
			StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
	        // setup the debug listeners
	        ksession.addEventListener( new DebugAgendaEventListener() );
	        ksession.addEventListener( new DebugWorkingMemoryEventListener() );

	        KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
			// go !

	        ksession.insert(event1);
	        ksession.fireAllRules();
	        ksession.insert(event2);
	        ksession.fireAllRules();
	        ksession.insert(event3);
	        ksession.fireAllRules();
	        ksession.insert(event4);
	        ksession.fireAllRules();
	        ksession.insert(event5);
	        ksession.fireAllRules();
			logger.close();
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	private static KnowledgeBase readKnowledgeBase() throws Exception {
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("SampleAlarmRules.drl"), ResourceType.DRL);
		KnowledgeBuilderErrors errors = kbuilder.getErrors();
		if (errors.size() > 0) {
			for (KnowledgeBuilderError error: errors) {
				System.err.println(error);
			}
			throw new IllegalArgumentException("Could not parse knowledge.");
		}
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		return kbase;
	}

	public static class SampleEvent {

		int iResultado = -1;

		Timestamp dtOperIni = null;

		public int getiResultado() {
			return iResultado;
		}

		public void setiResultado(int iResultado) {
			this.iResultado = iResultado;
		}

		public Timestamp getDtOperIni() {
			return dtOperIni;
		}

		public void setDtOperIni(Timestamp dtOperIni) {
			this.dtOperIni = dtOperIni;
		}

	}

	public static class SampleAlarm {

	    public static final int INACTIVA = 0;
	    public static final int ACTIVA  = 1;
		
		Timestamp dtFechaIni = null;

		int iEstado = INACTIVA;

		public Timestamp getDtFechaIni() {
			return dtFechaIni;
		}

		public void setDtFechaIni(Timestamp dtFechaIni) {
			this.dtFechaIni = dtFechaIni;
		}

		public int getiEstado() {
			return iEstado;
		}

		public void setiEstado(int iEstado) {
			this.iEstado = iEstado;
		}

	}

	public static class SampleSetAlarmInterval {

		SampleAlarm alarma = null;
		
		Timestamp dtFechaIni = null;
		Timestamp dtFechaFin = null;
		int iEstado = SampleAlarm.INACTIVA;

		public SampleAlarm getAlarma() {
			return alarma;
		}
		public void setAlarma(SampleAlarm alarma) {
			this.alarma = alarma;
		}
		public Timestamp getDtFechaIni() {
			return dtFechaIni;
		}
		public void setDtFechaIni(Timestamp dtFechaIni) {
			this.dtFechaIni = dtFechaIni;
		}
		public Timestamp getDtFechaFin() {
			return dtFechaFin;
		}
		public void setDtFechaFin(Timestamp dtFechaFin) {
			this.dtFechaFin = dtFechaFin;
		}
		public int getiEstado() {
			return iEstado;
		}
		public void setiEstado(int iEstado) {
			this.iEstado = iEstado;
		}

	}

	public static class SampleSetAlarmTest {

	    public static final int NEGATIVO = -1;
	    public static final int NEUTRO = 0;
	    public static final int POSITIVO  = 1;

	    SampleSetAlarmInterval alarma = null;
		SampleEvent operacion = null;

		Timestamp dtFecha = null;
		int iResultado = NEUTRO;

		public SampleSetAlarmInterval getAlarma() {
			return alarma;
		}
		public void setAlarma(SampleSetAlarmInterval alarma) {
			this.alarma = alarma;
		}
		public SampleEvent getOperacion() {
			return operacion;
		}
		public void setOperacion(SampleEvent operacion) {
			this.operacion = operacion;
		}
		public Timestamp getDtFecha() {
			return dtFecha;
		}
		public void setDtFecha(Timestamp dtFecha) {
			this.dtFecha = dtFecha;
		}
		public int getiResultado() {
			return iResultado;
		}
		public void setiResultado(int iResultado) {
			this.iResultado = iResultado;
		}
	}
}