Dear All,
I need to accumulate the all values obtained by the rules in the same
variable, I mean, sum the variable value to another value and storing in the
variable.
Below you can see my code. However it ovewrites the value and in the end
only presents the last value. Could you please help me?
import jess.*;
public class questionnaire {
public static void main(String[] unused) throws JessException {
//Creates the Jess object
Rete engine = new Rete();
//Defines the templates
Deftemplate d = new Deftemplate("Questionnaire1", "QuestionarioAvaliacao
Perfil", engine);
d.addSlot("age", Funcall.NIL, "INTEGER");
d.addSlot("totalTimeDriving", Funcall.NIL, "INTEGER");
d.addSlot("criticalTicketsLastYear", Funcall.NIL, "INTEGER");
d.addSlot("lastYearAccidents", Funcall.NIL, "INTEGER");
d.addSlot("VehicleType", Funcall.NIL, "STRING");
d.addSlot("VehicleActivity", Funcall.NIL, "STRING");
d.addSlot("ShipmentType", Funcall.NIL, "STRING");
engine.addDeftemplate(d);
//Defines a array with the data to be analyzed
String[] data = {"10", "24","15","10","police","fireman","ignitable"};
//Defines a fact
Fact f = new Fact("Questionnaire1", engine);
//Populates the facts into the templates
f.setSlotValue("age", new Value(Integer.parseInt(data[0]),
RU.INTEGER));
f.setSlotValue("totalTimeDriving",new Value(Integer.parseInt(data[1]),
RU.INTEGER));
f.setSlotValue("criticalTicketsLastYear",new
Value(Integer.parseInt(data[2]), RU.INTEGER));
f.setSlotValue("lastYearAccidents",new
Value(Integer.parseInt(data[3]), RU.INTEGER));
f.setSlotValue("VehicleType",new Value(data[4], RU.STRING));
f.setSlotValue("VehicleActivity",new Value(data[5], RU.STRING));
f.setSlotValue("ShipmentType",new Value(data[6], RU.STRING));
//Inserts the facts into the knowledge base
engine.assertFact(f);
engine.executeCommand("(facts)");
//Creates the variable to return the results
engine.executeCommand("(assert(risk 1))");
//Create the rules
String rule1 =" (defrule ruleage";
rule1 +="(risk ?r1)";
rule1 +="(Questionnaire1 {age < 25})";
rule1 +="=>";
rule1 +="(store HighRisk(+ ?r1)))";
String rule2 =" (defrule ruletotalTimeDriving";
rule2 +="(risk ?r1)";
rule2 +="(Questionnaire1 {totalTimeDriving > 8})";
rule2 +="=>";
rule2 +="(store HighRisk(+ ?r1)))";
String rule3 =" (defrule rulecriticalTicketsLastYear";
rule3 +="(risk ?r1)";
rule3 +="(Questionnaire1 {criticalTicketsLastYear > 2})";
rule3 +="=>";
rule3 +="(store HighRisk(+ ?r1)))";
String rule4 =" (defrule rulelastYearAccidents";
rule4 +="(risk ?r1)";
rule4 +="(Questionnaire1 {lastYearAccidents > 1})";
rule4 +="=>";
rule4 +="(store HighRisk(+ ?r1)))";
String rule5 =" (defrule ruleVehicleType";
rule5 +="(risk ?r1)";
rule5 +="(Questionnaire1 {VehicleType == 'police' || VehicleType ==
'fireman' || VehicleType == 'ambulance' })";
rule5 +="=>";
rule5 +="(store HighRisk(+ ?r1)))";
String rule6 =" (defrule ruleShipmentType";
rule6 +="(risk ?r1)";
rule6 +="(Questionnaire1 {ShipmentType == 'grains' || ShipmentType ==
'ignitable' || ShipmentType == 'liquid' })";
rule6 +="=>";
rule6 +="(store HighRisk(+ ?r1)))";
//Executes the rules
engine.executeCommand(rule1);
engine.executeCommand(rule2);
engine.executeCommand(rule3);
engine.executeCommand(rule4);
engine.executeCommand(rule5);
engine.executeCommand(rule6);
engine.executeCommand("(run)");
//Stores the results in a variable
Value riskValue = engine.fetch("HighRisk");
int values = 0;
if(riskValue != null){
values = riskValue.intValue(engine.getGlobalContext());
}
//Prints the variable
System.out.println(values);
}
}
Best regards,
Marcelo Pedrosa