我们使用规则模板从数据库生成规则文件,它正在工作,但我们面临一个问题,即规则中的规则生成的数量与表中的记录数量相同。因此,根据我的要求,它不应基于表中的记录生成规则文件。
规则模板:
template header
alert_playload
package com.trinity;
import com.trinity.DroolsBolt;
import com.trinity.Tuplebean;
dialect "mvel"
template "sensorRule"
rule "Safety Alert"
when
t : Tuplebean(@{alert_playload})
then
DroolsBolt.insertToAlertLog(t.getSmokeKey().toString(),t.getMacAddress().toString());
end
end template
drools螺栓逻辑:
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql://52.172.14.126:5432/iotdb",
"postgres", "track@123");
Statement sta = conn.createStatement();
String sql="SELECT min_threshould , max_threshould , sensor_name , alert_playload FROM iot.alert_configuration WHERE drools_boolean=true and alert_configuration_id=94";
ResultSet rs = sta.executeQuery(sql);
final ResultSetGenerator converter = new ResultSetGenerator();
final String drl = converter.compile(rs, getRulesStream());
System.out.println(drl);
sta.close();
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()),
ResourceType.DRL);
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession kSession =
kbase.newStatefulKnowledgeSession();
/*// now create some test data
Sensor a = new Sensor("A", 100);
Sensor b = new Sensor("B", 520);
Sensor c = new Sensor("C", 610);
kSession.insert(a);
kSession.insert(b);
kSession.insert(c);
kSession.fireAllRules();
System.out.println(a.getName() + "," + a.getThreshould());
System.out.println(b.getName() + "," + b.getThreshould());
System.out.println(c.getName() + "," + c.getThreshould());
kSession.destroy();
kSession.dispose();
kSession = kbase.newStatefulKnowledgeSession();
Sensor aa = new Sensor("AA", 820);
Sensor bb = new Sensor("BB", 200);
Sensor cc = new Sensor("CC", 141);
kSession.insert(aa);
kSession.insert(bb);
kSession.insert(cc);*/
// kSession.destroy();
// kSession.dispose();
// kSession.fireAllRules();
/*System.out.println(aa.getName() + "," + aa.getName());
System.out.println(bb.getName() + "," + bb.getName());
System.out.println(cc.getName() + "," + cc.getName());*/
//kSession.fireAllRules();
try {
BufferedWriter out = new BufferedWriter(new FileWriter("C:/IotHub/conf/alert.drl"));
out.write(drl.toString());
out.close();
}
catch (IOException e){
System.out.println("Exception ");
}
kSession.destroy();
kSession.dispose();
kSession = kbase.newStatefulKnowledgeSession();
kSession.fireAllRules();
生成的规则文件是:
rule "sensorRule_2"
when
$sensor : Sensor(getThreshould()>=0)
then
$sensor.sensor_name=":" + "Generator Fuel Level";
end
rule "sensorRule_1"
when
$sensor : Sensor(getThreshould()>=0)
then
$sensor.sensor_name=":" + "motion";
end
rule "sensorRule_0"
when
$sensor : Sensor(getThreshould()>=0)
then
$sensor.sensor_name=":" + "smke";
end
暂无答案!
目前还没有任何答案,快来回答吧!