package com.sample;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.DecisionTableConfiguration;
import org.drools.builder.DecisionTableInputType;
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.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;




public class ClearanceTest {
    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            System.out.println("Knowledgebase read");
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            //KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
            // go !
            Weather weather = new Weather();
            weather.setTemperature(-3);
            weather.setPrecipitation(Weather.SNOW);
            weather.setIntensity(Weather.HEAVY);
            Assignment assignment = new Assignment();
            ksession.insert(weather);
            ksession.insert(assignment);
            ksession.fireAllRules();
            System.out.println("All rules fired");
            //logger.close();
            System.out.println("Status: " + assignment.status);
            System.out.println("Message: " + assignment.message);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        
        SpreadsheetCompiler sc = new SpreadsheetCompiler();
        String drlstr = sc.compile( ResourceFactory.newClassPathResource("clearance.xls").getInputStream() , InputType.XLS);
        System.out.println("****\n" + drlstr + "\n*****");
        
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        DecisionTableConfiguration dtableconfiguration = KnowledgeBuilderFactory.newDecisionTableConfiguration();
        dtableconfiguration.setInputType( DecisionTableInputType.XLS );
        
        kbuilder.add(ResourceFactory.newClassPathResource("clearance.xls"), ResourceType.DTABLE, dtableconfiguration);
        
        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 Assignment {
        
        public static final int INACTIVE = 0;
        public static final int ACTIVE = 1;
        
        public static final int ALL = 0;
        public static final int MAINROADSANDCITYCENTERS = 1;
        public static final int MAINROADS = 2;
        public static final int HIGHWAYS = 3;
        public static final int HIGHROADS = 4;
        
        public static final int PREVENTIVE = 0;
        public static final int MEDIUM = 1;
        public static final int INTENSIVE = 2;

        private String message;
        private int status = INACTIVE;
        private int level;
        private int intensity;
        private int vehicleCount;

        public String getMessage() {
            return this.message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getStatus() {
            return this.status;
        }

        public void setStatus(int status) {
            this.status = status;
        }
        
        public int getLevel() {
            return this.level;
        }
        
        public void setLevel(int level) {
            this.level = level;
        }
        
        public int getIntensity() {
            return this.intensity;
        }
        
        public void setIntensity(int level) {
            this.intensity = level;
        }
        public int getVehicleCount() {
            return this.vehicleCount;
        }
        
        public void setVehicleCount(int vehicleCount) {
            this.vehicleCount = vehicleCount;
        }
        
    }
    
public static class Weather {
        
        public static final int CLEAR = 0;
        public static final int RAIN = 1;
        public static final int SNOW = 2;
        
        public static final int NONE = 0;
        public static final int LIGHT = 1;
        public static final int MEDIUM = 2;
        public static final int HEAVY = 3;

        private String message;
        private double temperature;
        private int precipitation;
        private int intensity;

        public String getMessage() {
            return this.message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public double getTemperature() {
            return this.temperature;
        }

        public void setTemperature(double temp) {
            this.temperature = temp;
        }
        
        public int getPrecipitation() {
            return this.precipitation;
        }
        
        public void setPrecipitation(int precipitation) {
            this.precipitation = precipitation;
        }
        
        public int getIntensity() {
            return this.intensity;
        }
        
        public void setIntensity(int level) {
            this.intensity = level;
        }
                
    }

}
