简体   繁体   English

当事实不再匹配时,从Drools获取一些事件

[英]Getting some event from Drools when the fact does not match anymore

Please have a look at the two code settings below. 请查看下面的两个代码设置。

Setting 1: 设置1:

public class DroolsAnotherTest {
    private static KieSession kSession;
    private static Building building;
    private static FactHandle buildingFact;
    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KieServices ks = KieServices.Factory.get();
            KieContainer kContainer = ks.getKieClasspathContainer();
            kSession = kContainer.newKieSession("ksession-rules");
            building = new Building();
            building.setRooms(2);
            buildingFact = kSession.insert(building);
            kSession.fireAllRules();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static void fireAgain(){
        System.out.println("inside fireAgain method");
        kSession.fireAllRules(); // firing the rules second time
    }
public static class Building {

        public int rooms;
        public int getRooms() {
            return this.rooms;
        }
        public void setRooms(int rooms) {
            this.rooms = rooms;
        }
    }
}

And the .drl file 和.drl文件

package com.sample

import com.sample.DroolsAnotherTest.Building;
import com.sample.DroolsAnotherTest;

rule "Building"
    when
        Building( rooms <3 )
    then
        System.out.println( "Building rule fired" );
        DroolsAnotherTest.firingAgain();
end

Upon running, the output is : Building rule fired 运行后,输出为:已触发构建规则

inside fireAgain method 内火再次使用方法


Setting 2: Here I just changed the fireAgain() method to: 设置2:在这里,我只是将fireAgain()方法更改为:

public static void fireAgain(){
                System.out.println("inside fireAgain method");
        kSession.delete(buildingFact);
        building.setRooms(4);
        kSession.insert(building);
        kSession.fireAllRules(); // firing the rule second time
}

everything else is same. 其他一切都一样。 Upon running, the output is same again : Building rule fired 运行后,输出再次相同:已触发构建规则

inside fireAgain method 内火再次使用方法


As per my understanding, 据我了解,

in Setting 1, the rule did not get fired second time because the fact has not changed in the working memory. 在设置1中,该规则没有第二次被触发,因为事实在工作存储器中没有发生变化。

in Setting 2, the rule did not get fired second time because now the fact has updated and it does not match with the rule condition. 在设置2中,该规则没有第二次被触发,因为现在事实已更新并且与规则条件不匹配。

My question is, does Drools generate any event for the setting 2 ( the rule fired once but now is not fired again because fact has updated ?) In that way I can distinguish between : a rule did not get fired because the fact are unchanged and it did not get fired because now the fact does not match with rule condition ? 我的问题是,Drools是否会为设置2生成任何事件(规则被触发一次,但由于事实已更新而现在不再触发?)以这种方式我可以区分:规则没有被触发,因为事实未更改, 并且它没有被解雇,因为现在事实与规则条件不匹配?

There is no way you can determine why a rule isn't put on the agenda. 您无法确定为什么未将规则列入议程。

You know when you update a fact, so if this interesting for some reason, register it in the fact or elsewhere. 您知道何时更新事实,因此,如果出于某种原因对此很感兴趣,请在事实或其他地方进行注册。

And DO NOT, repeat: DO NOT, call fireAllRules while another such call on the same session is still executing. 并且不要重复:不要在同一会话上的另一个此类调用仍在执行时调用fireAllRules。 There may be all kind of unexpected effects. 可能会有各种意想不到的影响。

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

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