我的项目使用drools expert means(DRL)文件。在规则文件中,如果用户想要删除和更新规则,应该怎么做?
规则文件:
package com.sample;
import com.sample.Tuplebean;
import com.sample.DroolsBolt;
dialect "mvel"
rule "SafetyAlert-Critical"
when
t:Tuplebean(t.getSmoke() == true && t.getSmoke_density() == true && t.getTemperature() > 25.0)
then
DroolsBolt.insertToAlertLog("alert generated");
end
例如,用户想要将温度值更改为30.0。
答案 0 :(得分:1)
在我的应用程序中,我经历了同样的问题,我通过以下方式修复了这些问题,我正在维护所有规则文件数据库。
我将所有规则文件保存在表中,并且我正在根据IMEI号加载每个规则文件(在我的情况下,我为一个适配器生成一个规则,即具有唯一IMEI号的适配器)。
在下面的代码中,首先从datbase加载规则文件,然后将该字符串转换为Resource beacuse Resource Factory,仅接受Resource作为构造函数参数。并将其存入ksession并解除规则。
对于编辑和删除操作,我在数据库中创建一个标志,如果该标志为真,则再次将规则文件更新到数据库表。 如果是假加载相同的旧规则文件。
在这里生成规则文件并保存到数据库表中,如果flag为true,则再次重新生成规则文件。
public void generateDrrols(String macAddress) throws SQLException, ClassNotFoundException, IOException{
logger.error("++++Generate drools file started++++");
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql://10.0.0.5:5432/iotdb",
"postgres", "track@123");
//String sql="SELECT alert_playload FROM iot.alert_configuration WHERE drools_boolean=true and alert_configuration_id=94 && 95";
String sql = ""
+ "select drools_condtion , iot.alert_configuration.alert_status , iot.alert_configuration.websocket_key , device_id , iot.alert_configuration.device_configuration_id , "
+ "email , iot.alert_configuration.mac_address , phone , sensor_id , iot.alert_configuration.sensor_configuration_id , alert_configuration_id , site_id , "
+ "startdatetime , enddatetime , exludedays , timetoexclude , log_update_time ,iot.sensor_configuration.sensor_name "
+ "from iot.alert_configuration "
+ "join iot.sensor_configuration on iot.sensor_configuration.sensor_configuration_id=iot.alert_configuration.sensor_configuration_id "
+ "where iot.alert_configuration.mac_address=? and soft_delete_flag=false";
PreparedStatement prepareStatement = conn.prepareStatement(sql);
prepareStatement.setString(1, macAddress);
ResultSet rs = prepareStatement.executeQuery();
//ResultSet rs = sta.executeQuery(sql);
final ResultSetGenerator converter = new ResultSetGenerator();
InputStream in = getClass().getResourceAsStream("/alert.drt");
final String drl = converter.compile(rs, in);
System.out.println(drl);
rs.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();
logger.error("++++Reading rule file ie., rule.drl started++++");
logger.error("++++Making drool table status false++++");
String sql2="update iot.drool_rules set drool_rules=? where mac_address=?";
PreparedStatement prepareStatement2 = conn.prepareStatement(sql2);
prepareStatement2.setString(1, drl.toString());
prepareStatement2.setString(2, macAddress);
boolean execute2 = prepareStatement2.execute();
String sql1="update iot.alert_configuration set storm_drool_status=false where mac_address=?";
//Statement createStatement = conn.createStatement();
PreparedStatement prepareStatement3 = conn.prepareStatement(sql1);
prepareStatement3.setString(1, macAddress);
boolean execute = prepareStatement3.execute();
}
在这里,您从数据库中获取规则文件并触发所有规则。
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
try{
String sql="select drool_rules from iot.drool_rules where mac_address=?";
PreparedStatement prepareStatement = connectPostgres.prepareStatement(hql);
prepareStatement.setString(1, topicId);
ResultSet executeQuery1 = prepareStatement.executeQuery();
String rules=null;
while(executeQuery1.next()){
rules=executeQuery1.getString(1);
}
String i=null;
String replace = rules.replace('"', '\"');
System.out.println(replace);
Reader reader=(Reader)new StringReader(replace);
Resource myResource = ResourceFactory.newReaderResource(reader);
kbuilder.add(myResource, ResourceType.DRL);
}
catch(Exception e)
{
e.printStackTrace();
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
ksession = kbase.newStatefulKnowledgeSession();