gpt4 book ai didi

java - 当具有包含无限循环的函数(监视器)时如何在 Drools 中触发规则?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:42 25 4
gpt4 key购买 nike

我有一个包含无限循环的函数(MonitorLog);这是必要的,因为我想监视文件并在有修改时执行操作。但是,Drools 中的then 部分不想启动(“WS 已更新”不出现)。请帮忙?

package smartWssec ;
dialect "mvel"
rule "SmartWssec"
when
$log :PolicyUpdateWssec(flag == true ,changelog == true)
then
$log.UpdateXml("D:\\Mod\\policy.xml")// consequence
System.out.println("Ws-Policy Updated")
end

///////////////////////////////////////////////////

package smartWssec;


import java.io.IOException;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;



public class MainPolicyUpdateWssec {


public static void main(String[] args) throws IOException, InterruptedException {
//create a Session
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
try {

//create fact
PolicyUpdateWssec LogModSecurity = new PolicyUpdateWssec();
LogModSecurity.MonitorLog("D:\\Mod");
LogModSecurity.ReadLog("D:\\Mod\\Modsecurity.txt");
//insert fact and fire Rules
kSession.insert(LogModSecurity);
kSession.fireAllRules();

}
finally {
kSession.dispose();
}
}

/////////////////////////////////////////////////////////////////////////

 public  class PolicyUpdateWssec {
private boolean flag;
private boolean changelog;



public boolean getchangelog(){
return changelog;
}
public void setChangelog(boolean changelog){
this.changelog=changelog;
}

public boolean getflag(){
return flag;
}

public void setflag(boolean flag){
this.flag=flag;
}


public void Update(String filename){

try (PrintStream ps = new PrintStream("D:\\Mod\\Wspolicyupdate.txt");
Stream<String> stream = Files.lines(Paths.get(filename))) {
stream.map(line -> line.replaceAll("</wsp:Policy>","<wssp:Integrity>\r\n\r\n<wssp:SignatureAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>\r\n\r\n<wssp:CanonicalizationAlgorithm URI=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/>\r\n\r\n<!-- Require the Timestamp header to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://www.bea.com/wls90/security/policy/wsee#part\">\r\n\r\nwls:SecurityHeader(wsu:Timestamp)\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n<!-- Require the message body to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://schemas.xmlsoap.org/2002/12/wsse#part\">\r\n\r\nwsp:Body()\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n</wssp:Integrity>\r\n\r\n</wsp:Policy>")).forEach(ps::println);
System.out.println("Ws-Policy updated");
} catch (IOException e) {
e.printStackTrace();
}}

public void ReadLog(String filename) throws IOException{

String line =null;
boolean flag=false;

// FileReader reads text files in the default encoding

FileReader fileReader = new FileReader(filename);

// Always wrap FileReader in BufferedReader

BufferedReader bufferedReader = new BufferedReader(fileReader);

while((line = bufferedReader.readLine()) != null) {


flag=line.matches("(.*)sql_injection_attacks(.*)");
if(flag==true) setflag(flag);

}

// Always close files
bufferedReader.close();
}

public void MonitorLog(String path) throws InterruptedException{

boolean changelog=false;

try {
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = Paths.get(path);
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

System.out.println("Watch Service registered for : " + dir.getFileName());

WatchKey key = watcher.poll(10, TimeUnit.SECONDS);

try {

// wait for a key to be available

key = watcher.take();
} catch (InterruptedException ex) {
return;
}

for (WatchEvent<?> event : key.pollEvents()) {
// get event type
WatchEvent.Kind<?> kind = event.kind();

// get file name
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();

System.out.println(kind.name() + ": " + fileName);

if (kind == ENTRY_MODIFY ) {
changelog=true;
setChangelog(changelog);
System.out.println("WS-Policy is being Updated .......");


}

}



watcher.close();
}
catch (IOException ex) {
System.err.println(ex);
}


}




public void UpdateXml(String filename) {

ArrayList<String> lines = new ArrayList<String>();
String line = null;

File f1=null;
FileReader fr=null;

BufferedReader br=null;
FileWriter fw=null;
BufferedWriter out=null;
try {
f1 = new File(filename);
fr = new FileReader(f1);
br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
if (line.contains("</wsp:Policy>"))
line = line.replace("</wsp:Policy>", "<wssp:Integrity>\r\n\r\n<wssp:SignatureAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>\r\n\r\n<wssp:CanonicalizationAlgorithm URI=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/>\r\n\r\n<!-- Require the Timestamp header to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://www.bea.com/wls90/security/policy/wsee#part\">\r\n\r\nwls:SecurityHeader(wsu:Timestamp)\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n<!-- Require the message body to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://schemas.xmlsoap.org/2002/12/wsse#part\">\r\n\r\nwsp:Body()\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n</wssp:Integrity>\r\n\r\n</wsp:Policy>");
lines.add(line);
}

fw = new FileWriter(f1);
out = new BufferedWriter(fw);
for (String s : lines)
out.write(s);
out.flush();

} catch (Exception ex) {
ex.printStackTrace();
} finally {
try{
fr.close();
br.close();
out.close();
}catch(IOException ioe)

{
ioe.printStackTrace();
}

}
}}

最佳答案

改变类成员来触发规则是不够的。您必须通知 Drools 引擎才能做到这一点。修改您的代码,以便您拥有插入事实的事实句柄:

FactHandle fh = kSession.insert(LogModSecurity);

修改后调用update:

setChangelog(changelog);  // assuming this is code in PolicyUpdateWssec
kSession.update( fh, this );

由于缺少您的大部分代码,我无法告诉您如何集成它。

编辑 MonitorLog 中的代码似乎稍等片刻,等待对某个目录中的文件进行修改,然后尝试对被修改的文件执行某些操作。 (WatchService 方法 poll 和 take 都从键集中删除了一些东西——你在那里丢失了一个键。)如果没有任何反应,poll 返回 null,因此 NPE 是可以预料的。然后,如果它是 MODIFY,则 changelog 设置为 true。然后调用 fireAllRules。但规则不会触发,因为 flag 从未设置为 true。

据我所知,不需要规则。您希望对文件进行更改:因此等待它,然后执行需要完成的任何操作。由于该更改(可能某些文件必须包含某些特定文本),规则会触发。这里没有任何东西可以保证使用生产规则系统。

关于java - 当具有包含无限循环的函数(监视器)时如何在 Drools 中触发规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35772325/

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