流口水中的三个连续物体

时间:2013-01-21 12:56:49

标签: drools drools-fusion

我一直试图找到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但这对三个连续的对象不起作用。 请给我一些建议。谢谢你

1 个答案:

答案 0 :(得分:0)

如果您查看已发布的“检查三熊”规则,您会发现第二个条件元素与第三个条件元素之间存在矛盾:

  

勾选(此后$ t ...)
      $ t1:勾选(此后$ t ...)

这是我过去用来从流中获取最新3个事件的内容:

$t1:Tick() //the newest
not Tick(this after $t1)
$t2:Tick(this before $t1)
not Tick(this before $t1, this after $t2) //no events between $t1 and $t2
$t3:Tick(this before $t2)
not Tick(this before $t2, this after $t3) //no events between $t2 and $t3

另一种解决方案可能是使用滑动窗口:

List(size == 3) from collect( Tick(...) over window:length(3) )

在这种情况下, size == 3 条件是为了防止在窗口中只有1个或2个元素时激活规则。

希望它有所帮助,