Search code examples
javadrools

How to pass data from CSV file as facts to Drools?


I am new to JAVA and Drools. I have a CSV file with a few thousand rows. I want to apply rules to this data row by row that is the same rules are to be applied to every row of the CSV. I have a ReadFile class that reads the CSV file using BufferReader. and I pass the output of this to drools as in the following code.

public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KieServices ks = KieServices.Factory.get();
            KieContainer kContainer = ks.getKieClasspathContainer();
            KieSession kSession = kContainer.newKieSession("ksession-rules");


             ReadFile data = new ReadFile();
             String line = data.readfile();
            kSession.insert(line);
            kSession.fireAllRules();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

Solution

  • Drools rule engine rule firing happens on some occurrences of an event. Every change of event is a rule trigger in drool. In your case, my recommendation is to create a model object for your CSV and then parse each line based on that model object and then insert the model object in the drool's session. For example, let's say you have a CSV with 4 columns (timestamp, name, parameter, value) you can create a model for each line as:

    Test.java

    public class Test {
        private long timepoint;
        private String name;
        private String parameter;
        private double value;
    
        public Test(long timepoint, String name, String parameter, double value) {
            this.timepoint = timepoint;
            this.name = name;
            this.parameter = parameter;
            this.value = value;
        }
    
        public long getTimepoint() {
            return timepoint;
        }
    
        public void setTimepoint(long timepoint) {
            this.timepoint = timepoint;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getParameter() {
            return parameter;
        }
    
        public void setParameter(String parameter) {
            this.parameter = parameter;
        }
    
        public double getValue() {
            return value;
        }
    
        public void setValue(double value) {
            this.value = value;
        }
    }
    

    Then you can normally follow your code :

    public static final void main(String[] args) {
            try {
                // load up the knowledge base
                KieServices ks = KieServices.Factory.get();
                KieContainer kContainer = ks.getKieClasspathContainer();
                KieSession kSession = kContainer.newKieSession("ksession-rules");
                ReadFile data = new ReadFile();
                String line = data.readfile();
                Test test = getTestObjectFromCSV(line) // a function that takes csv and returns Test class obejct
                kSession.insert(test);
                kSession.fireAllRules();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    

    After all the java stuff you can now create a rule as below:

    rule "testrule1"
    when
      Test(name == "abc")
    then
        System.out.println("Test rule is successfully executed");
    end
    

    You can read more on drools here