我一直在尝试查找 Drools Fusion 中是否存在三个相同类的对象。我有一个名为 Tick 的 POJO,如下所示
public class Tick
{
CrossOverPrice crossoverprice;
CrossOverType crossovertype;
public Tick(CrossOverPrice crossoverprice, CrossOverType crossovertype) {
super();
this.crossoverprice = crossoverprice;
this.crossovertype = crossovertype;
}
public CrossOverPrice getCrossoverprice() {
return crossoverprice;
}
public void setCrossoverprice(CrossOverPrice crossoverprice) {
this.crossoverprice = crossoverprice;
}
public CrossOverType getCrossovertype() {
return crossovertype;
}
public void setCrossovertype(CrossOverType crossovertype) {
this.crossovertype = crossovertype;
}
}
我试图找出是否有三个连续的 Tick 对象具有 crossovertype ==CrossOverType.bear
以下是规则和主要课程
public class DroolsTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
// go !
Tick t1=new Tick(CrossOverPrice.p12,CrossOverType.bear);
Tick t2=new Tick(CrossOverPrice.p23,CrossOverType.bear);
Tick t5=new Tick(CrossOverPrice.p34,CrossOverType.bear);
ksession.insert(t1);
ksession.fireAllRules();
/*ksession.insert(t3);
ksession.fireAllRules();*/
ksession.insert(t2);
ksession.fireAllRules();
ksession.insert(t5);
ksession.fireAllRules();
logger.close();
} 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.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
}
规则是
declare Tick
@role(event)
end
rule "Check three bear"
when
$t:Tick(crossovertype:CrossOverType.bear ,crossoverprice : CrossOverPrice.p12)
not Tick(this after $t,crossovertype == CrossOverType.bull)
$t1 : Tick(this after $t ,crossovertype == CrossOverType.bear ,crossoverprice ==CrossOverPrice.p23)
not Tick(this after $t1,crossovertype == CrossOverType.bull)
$t3 : Tick(this after $t1 ,crossovertype == CrossOverType.bear ,crossoverprice ==CrossOverPrice.p34)
then
System.out.println("great");
end
我能够找到是否有两个连续的 Tick 对象具有 crossovertype==CrossOverType.bear 但这不适用于三个连续的对象。请给我一些建议。谢谢