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;
}
}