标签:
一、定义日志模型
public class Logger { // 日志编号 private String id; // 目标IP private String targetIp; // 目标端口 private String targetPort; // 源IP private String sourceIp; // 源端口 private String sourcePort; // 创建时间 private long createTime; // 资产IP private String assetsId; public List<String> getTags() { return tags; } public void setTags(List<String> tags) { this.tags = tags; } private List<String> tags = new ArrayList<String>(); public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTargetIp() { return targetIp; } public void setTargetIp(String targetIp) { this.targetIp = targetIp; } public String getTargetPort() { return targetPort; } public void setTargetPort(String targetPort) { this.targetPort = targetPort; } public String getSourceIp() { return sourceIp; } public void setSourceIp(String sourceIp) { this.sourceIp = sourceIp; } public String getSourcePort() { return sourcePort; } public void setSourcePort(String sourcePort) { this.sourcePort = sourcePort; } public long getCreateTime() { return createTime; } public void setCreateTime(long createTime) { this.createTime = createTime; } public String getAssetsId() { return assetsId; } public void setAssetsId(String assetsId) { this.assetsId = assetsId; } }
二、编写规则算法文件
import com.xxx.drools.logger.Logger; rule "logger1" when $logger : Logger(targetIp=="192.168.26.108") then $logger.getTags().add("测试机"); System.out.println("Logger:"+$logger.getTargetIp()); end
三、编写drools加载代码
import java.util.Collection; import org.drools.core.event.DebugAgendaEventListener; import org.drools.core.event.DebugRuleRuntimeEventListener; import org.kie.api.KieServices; import org.kie.api.io.ResourceType; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import org.kie.internal.KnowledgeBase; import org.kie.internal.KnowledgeBaseFactory; import org.kie.internal.builder.KnowledgeBuilder; import org.kie.internal.builder.KnowledgeBuilderFactory; import org.kie.internal.definition.KnowledgePackage; import org.kie.internal.io.ResourceFactory; import org.kie.internal.runtime.StatefulKnowledgeSession; public class LoggerTag { public void runRules(String[] rules, Object[] facts) throws Exception { KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory .newKnowledgeBuilder(); for (int i = 0; i < rules.length; i++) { String ruleFile = rules[i]; System.out.println("Loading file: " + ruleFile); kbuilder.add(ResourceFactory.newClassPathResource(ruleFile, LoggerTag.class), ResourceType.DRL); } Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages(); kbase.addKnowledgePackages(pkgs); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); for (int i = 0; i < facts.length; i++) { Object fact = facts[i]; System.out.println("Inserting fact: " + fact); ksession.insert(fact); } ksession.fireAllRules(); } }
四、测试代码
public class Example1 { public static void main(String[] args) throws Exception { List<Logger> loggerList = LoggerData.getLogger(); new LoggerTag().runRules(new String[] { "logger1.drl" }, loggerList.toArray()); if (loggerList != null && loggerList.size() > 0) { for (Logger logger : loggerList) { for (String tag : logger.getTags()) { System.out.println("tag:"+tag); } } } } }
标签:
原文地址:http://www.cnblogs.com/qq27271609/p/4686713.html