【问题标题】:How to set different event @expires for sub-classes in drools如何为drools中的子类设置不同的事件@expires
【发布时间】:2020-03-26 09:29:39
【问题描述】:

假设我有一个代表事件的类(流模式下流口水),我像这样设置该事件的到期时间:

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

效果很好。插入此事件 1 分钟后,它会自动从工作记忆中收回。 现在假设我有另一个类(继承自上面的类):

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

所以我预计 EventWithLongExpiration 事件会在 10m 后收回。或者,我也可以使用一个解决方案,其中子类根本没有到期,或者超类根本没有到期,所有组合都经过测试并且不起作用。

实际发生的是超级@expires 定义总是优先于子类@expires 定义。如果超类根本没有过期时间,而只有子类有过期时间,那么任何一个类都没有过期时间,并且对象永远不会收回。

有没有什么办法可以让这个工作和子类有 2 种不同的过期策略?

明确地说,我只想在 DRL 查询中查询超类型,并在同一语句中获取超类和子类的实例,但我希望它们在不同的持续时间后被收回。

【问题讨论】:

    标签: java jboss drools rules kie


    【解决方案1】:

    规则内的声明按预期工作。

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

    测试

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

    域名

    public class EventWithShortExpiration {
        public int ordinal;
    
        public EventWithShortExpiration(int ordinal) {
            this.ordinal = ordinal;
        }
    }
    
    public class EventWithLongExpiration extends EventWithShortExpiration {
    
        public EventWithLongExpiration(int ordinal) {
            super(ordinal);
        }
    }
    

    规则

    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
    

    测试输出

    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:没有运气让注释按预期工作,很可能是因为注释的读取和应用方式。我会将其视为流口水错误。在 7.28.0.Final 测试。

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 2021-07-30
      • 2019-03-26
      • 2018-08-30
      相关资源
      最近更新 更多