简体   繁体   English

如何为drools中的子类设置不同的事件@expires

[英]How to set different event @expires for sub-classes in drools

Let's say I have a class representing an event (drools in stream mode) and I set expiry on that event like so:假设我有一个表示事件的类(流模式下流口水),并且我在该事件上设置了到期时间,如下所示:

@Role(Role.Type.EVENT)
@Expires("1m")
public class EventWithShortExpiration {}

That works fine.这很好用。 1 minute after I insert this event, it is automatically retracted from working memory.我插入此事件后 1 分钟,它会自动从工作内存中撤回。 Now let's say I have another class (inheriting from the above class):现在假设我有另一个类(继承自上述类):

@Role(Role.Type.EVENT)
@Expires("10m")
public class EventWithLongExpiration extends EventWithShortExpiration {}

So I expect the EventWithLongExpiration events to be retracted after 10m.所以我希望 EventWithLongExpiration 事件在 10m 后被收回。 Alternatively, I could also use a solution where the sub-class has no expiry at all, or the super class has no expiry at all, all combinations tested and didn't work.或者,我也可以使用一个解决方案,其中子类根本没有到期,或者超类根本没有到期,所有组合都经过测试并且不起作用。

What actually happens is that the super @expires definition always takes precedence over the subclass @expires definition.实际发生的是超级@expires 定义总是优先于子类@expires 定义。 If the super class does not have an expiration at all and only the subclass has one, there's no expiration at all for either classes and objects are never retracted.如果超类根本没有过期,而只有子类有过期,则任何一个类都没有过期,并且永远不会收回对象。

Is there any way to make this work and to have 2 different expiration policies to sub-classes?有什么方法可以使这项工作并为子类设置 2 个不同的过期策略?

To be clear, I want to query only the super type in my DRL queries and get instances of both super and sub-class in the same statement, but I want them to get retracted after different durations.需要明确的是,我只想查询 DRL 查询中的超类型,并在同一语句中获取超类和子类的实例,但我希望它们在不同的持续时间后被收回。

Declaration inside rule works as expected.规则内的声明按预期工作。

declare EventWithShortExpiration @role (event) @expires (1m) end
declare EventWithLongExpiration @role (event) @expires (10m) end

test测试

@DroolsSession("classpath:/test.drl")
public class PlaygroundTest {

    @Rule
    public DroolsAssert drools = new DroolsAssert();

    @Test
    public void testIt() {
        drools.insertAndFire(new EventWithShortExpiration(1), new EventWithLongExpiration(2));
        drools.assertFactsCount(2);

        drools.advanceTime(1, MINUTES);
        drools.assertFactsCount(1);

        drools.advanceTime(9, MINUTES);
        drools.assertFactsCount(0);
    }
}

domain领域

public class EventWithShortExpiration {
    public int ordinal;

    public EventWithShortExpiration(int ordinal) {
        this.ordinal = ordinal;
    }
}

public class EventWithLongExpiration extends EventWithShortExpiration {

    public EventWithLongExpiration(int ordinal) {
        super(ordinal);
    }
}

rule规则

declare EventWithShortExpiration @role (event) @expires (1m) end
declare EventWithLongExpiration @role (event) @expires (10m) end

rule 'test rule'
    when
        $event : EventWithShortExpiration() 
    then
        System.out.println($event.ordinal);
end

test output测试输出

00:00:00 --> inserted: EventWithShortExpiration[ordinal=1]
00:00:00 --> fireAllRules
00:00:00 <-- 'test rule' has been activated by the tuple [EventWithShortExpiration]
1
00:00:00 --> inserted: EventWithLongExpiration[ordinal=2]
00:00:00 --> fireAllRules
00:00:00 <-- 'test rule' has been activated by the tuple [EventWithLongExpiration]
2

PS: there was no luck to make annotations work as expected, most likely because of the way how annotations are read and applied. PS:没有运气使注释按预期工作,很可能是因为注释的读取和应用方式。 I would treat this as a drools bug.我会把这当作一个流口水的错误。 Tested on 7.28.0.Final.在 7.28.0.Final 上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM