是Drools的新手并尝试滑动窗口概念。
我的问题是检测在2个销售活动中是否发生了至少一次成功交易。即使发生了这样的事件,我总是得到“销售已发生超过2件事”的输出。
------------- ---------- DRL
declare Sale
@role( event )
end
rule "Identify more than a single valid sale event"
//The variable saleHappened would be set to Y if a successful transaction
when sale: Sale ( saleHappened == "Y") over window:length(2) from entry-point MyEntryPoint
then
System.out.println("A sale has happened over 2 events" );
end
------------- ----------代码
public class DroolsTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
WorkingMemoryEntryPoint entryPoint = ksession.getWorkingMemoryEntryPoint("MyEntryPoint");
Sale sale = new Sale();
sale.setSaleHappened("N");
entryPoint.insert(sale);
Sale sale2 = new Sale();
sale2.setSaleHappened("N");
entryPoint.insert(sale2);
ksession.fireAllRules();
ksession.dispose();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error: errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
final KnowledgeBaseConfiguration kbConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbConfig.setOption(EventProcessingOption.STREAM);
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbConfig);
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
public static class Sale {
public Sale () {
}
public String getSaleHappened() {
return saleHappened;
}
public void setSaleHappened(String saleHappened) {
this.saleHappened = saleHappened;
}
private String saleHappened;
}
}
答案 0 :(得分:2)
这与Bug 787118 - Length sliding window keeps all events有关。在bugzilla条目中解释了滑动窗口事件触发的概念以及事件如何不会从窗口中掉落。
可以修改如下:
--------------drl--------------------
declare Sale
@role( event )
end
declare window Ticks
Sale()
over window:length( 5 )
from entry-point MyEntryPoint
end
rule "More than 2 sale suceess in 5 events"
when Number($cnt : intValue,intValue > 2)
from accumulate( Sale ( saleHappened == "Y" ) from window Ticks, count(1) )
then
System.out.println("A sale has happened over " + $cnt +" events" );
end