gpt4 book ai didi

drools - 如何编辑和更新 Drools 规则文件?

转载 作者:行者123 更新时间:2023-12-02 03:08:20 27 4
gpt4 key购买 nike

我的项目使用 drools 专家手段 (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。

最佳答案

在我的应用程序中,我遇到了同样的问题,我通过休闲方式解决了这些问题,我正在维护所有规则文件数据库。

我将所有规则文件保存在表中,并根据 IMEI 编号加载每个规则文件(在我的例子中,我为一个适配器生成一个规则,即具有唯一 IMEI 编号的适配器)。

在下面的代码中,首先从数据库加载规则文件并将该字符串转换为资源,因为资源工厂只接受资源作为构造函数参数。并将其存储到 ksession 中并触发规则。

对于编辑和删除操作,我在数据库中创建一个标志,如果该标志再次为真,则将规则文件更新到数据库表中。如果为假,则加载相同的旧规则文件。

在这里您生成规则文件并保存到数据库表中,如果标志为真,您将再次生成规则文件。

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();

关于drools - 如何编辑和更新 Drools 规则文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41239770/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com