I kind of start with JAVA and Drools in particular, so don’t ask why I do it: D The task was to implement a simple system that would calculate tariffs and prices for 2 elements (different colors). I made a simple class with getters and setters:
package com.sample;
public class Pen {
private String color;
private int quantity;
private double tariff;
private double subTotal;
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
public int getQuantity(){
return quantity;
}
public void setQuantity(int quantity){
this.quantity=quantity;
}
public double getTariff(){
return tariff;
}
public void setTariff(double tariff){
this.tariff=tariff;
}
public double getSubTotal(){
return subTotal;
}
public void setSubTotal(double subTotal){
this.subTotal=subTotal;
}
}
The task was to read some predefined data from the CSV file and write it to another CSV (writing has not yet been implemented). The class that processes the input and the calling session is as follows:
public class CSVReader {
public void CSVToJava() {
String CSVFile = "csvExample.csv";
BufferedReader buffer = null;
String line = "";
String delimiter = ",";
List<Pen> penList = new ArrayList<Pen>();
try {
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
buffer = new BufferedReader(new FileReader(CSVFile));
while ((line = buffer.readLine()) != null) {
String[] pens = line.split(delimiter);
Pen redPen = new Pen();
Pen bluePen = new Pen();
if (pens[0].equalsIgnoreCase("Blue Pen")) {
bluePen.setColor(pens[0]);
bluePen.setQuantity(Integer.parseInt(pens[1].trim()));
penList.add(bluePen);
ksession.insert(bluePen);
} else {
redPen.setColor(pens[0]);
redPen.setQuantity(Integer.parseInt(pens[1].trim()));
penList.add(redPen);
ksession.insert(redPen);
}
}
ksession.fireAllRules();
printPenList(penList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
} finally {
if (buffer != null) {
try {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void printPenList(List<Pen> penListToPrint) {
for (int i = 0; i < penListToPrint.size(); i++) {
System.out.println(penListToPrint.get(i).getColor() + "," + penListToPrint.get(i).getQuantity() + ","
+ penListToPrint.get(i).getTariff() + "," + penListToPrint.get(i).getSubTotal());
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("com/Drools/BluePen.drl"), ResourceType.DRL);
kbuilder.add(ResourceFactory.newClassPathResource("com/Drools/RedPen.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;
}
}
I also have two separate DRL files for each color of the element. Based on the calculation of the rules, I need to print 2 additional properties, simple multiplications (subTotal). Each file has 2 rules:
import com.sample.Pen; //FIRST ONE
rule "less or equal to 10"
when
item : Pen(item.getColor().equalsIgnoreCase("BLUE PEN"), (item.getQuantity()) <= 10.0)
then
item.setTariff(3.0);
item.setSubTotal(3.0 * ((double) item.getQuantity()));
end
rule "more than 10"
when
item : Pen(item.getColor().equalsIgnoreCase("BLUE PEN"), (item.getQuantity()) > 10.0)
then
item.setTariff(2.5);
item.setSubTotal(2.5 * ((double) item.getQuantity()));
end
import com.sample.Pen; //SECOND ONE
rule "less or equal to 10"
when
item : Pen(item.getColor().equalsIgnoreCase("RED PEN"), (item.getQuantity()) <= 10.0)
then
item.setTariff(3.5);
item.setSubTotal(3.5 * ((double) item.getQuantity()));
end
rule "more than 10"
when
item : Pen(item.getColor().equalsIgnoreCase("RED PEN"), (item.getQuantity()) > 10.0)
then
item.setTariff(3.0);
item.setSubTotal(3.0 * ((double) item.getQuantity()));
end
In addition, there is a class in which I call the entire method to start the process:
public class App {
public static void main(String[] args) {
CSVReader csvReader = new CSVReader();
csvReader.CSVToJava();
}
}
: packageExplorer
, : >
Red Pen,6,3.5,21.0
Blue Pen,12,0.0,0.0
4 , CSVReader , (3,5 21,0 ) Drools. , , ... - , :)
EDIT: 3- , DRL:
package com.drools //First.drl
import com.sample.Pen;
rule "Subtotal for blue pen" salience 2
when
item : Pen(item.getColor().equalsIgnoreCase("BLUE PEN"))
then
item.setTotal(item.getTotal() + item.getSubTotal());
end
rule "Subtotal for red pen" salience 2
when
item : Pen(item.getColor().equalsIgnoreCase("RED PEN"))
then
item.setTotal(item.getTotal() + item.getSubTotal());
end
package com.drools //Second.drl
import com.sample.Pen;
rule "Discounted total price" salience 1
when
item : Pen((item.getTotal()) > 100.0)
then
item.setTotal(0.85 * item.getTotal());
end
, "". fireAll, drl , 0,85 (total - , Pen, suTotal ), → , 140 * 0.85 = 119.
:
Red Pen,30,3.0,90.0
Blue Pen,20,2.5,50.0
140
, - , , , :)