0

我一直在尝试查找 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 但这不适用于三个连续的对象。请给我一些建议。谢谢

4

1 回答 1

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 个元素时激活规则。

希望能帮助到你,

于 2013-01-22T10:10:29.220 回答