1

我是初学者,我正在用 Liferay 和 Drools 做一个项目。我以速度和执行后动态创建 Drools 规则。它适用于地址,我认为这应该是因为我对插入有一些问题。这是我的代码:

dialect "java"

rule "Initialize Rules"
    salience 1000
    when
        user : User();
    then
        Serializable value;
        List<Address> params_0 = AddressLocalServiceUtil.getAddresses(user.getCompanyId(), Contact.class.getName(), user.getContactId());
        for(Address param_0 : params_0) {
            insertLogical(param_0);
        }
        List<Address> params_1 = AddressLocalServiceUtil.getAddresses(user.getCompanyId(), Contact.class.getName(), user.getContactId());
        for(Address param_1 : params_1) {
            insertLogical(param_1);
        }
        List<Address> params_2 = AddressLocalServiceUtil.getAddresses(user.getCompanyId(), Contact.class.getName(), user.getContactId());
        for(Address param_2 : params_2) {
            insertLogical(param_2);
        }
        List<Address> params_3 = AddressLocalServiceUtil.getAddresses(user.getCompanyId(), Contact.class.getName(), user.getContactId());
        for(Address param_3 : params_3) {
            insertLogical(param_3);
        }
end

rule "Rule_0"
    when 
        user: User();
        param_0: Address(country.name == "Andorra")
    then
        System.out.println("lalalalalala!");
        classification(user,"Andorra", 7501);
        retract(param_0);   
end

rule "Rule_1"
    when 
        user: User();
        param_1: Address(zip == "00000")
    then
        System.out.println("lalalalalala!");
        classification(user,"ZIP0", 7502);
        retract(param_1);   
end

rule "Rule_2"
    when 
        user: User();
        param_2: Address(city == "Andorra")
    then
        System.out.println("lalalalalala!");
        classification(user,"Andorra", 7503);
        retract(param_2);   
end

rule "Rule_3"
    when 
        user: User();
        param_3: Address(region.name == "Catalonia")
    then
        System.out.println("lalalalalala!");
        classification(user,"Catalonia", 7504);
        retract(param_3);   
end

这是我用来执行它的代码:

String domainName = "User segmentation";
facts.add(new Fact<User> ("user", user));
RulesResourceRetriever rulesResourceRetriever = new RulesResourceRetriever(new StringResourceRetriever(rule), String.valueOf(RulesLanguage.DROOLS_RULE_LANGUAGE));
RulesEngineUtil.update(domainName, rulesResourceRetriever, PortalClassLoaderUtil.getClassLoader());
RulesEngineUtil.execute(domainName, facts, Query.createStandardQuery(), PortalClassLoaderUtil.getClassLoader());

我通过的用户有一个地址是: - 国家:安道尔 - 邮编:00000 - 城市:安道尔

输出必须是:lalalalalala!拉拉拉拉拉!拉拉拉拉拉!

但只是一个“lalalalalala!” 因为唯一像 true 一样验证的规则是第一条。

我不知道我做错了什么。任何想法?“插入”和“插入逻辑”有什么区别?是否存在任何方式来显示我插入了哪些元素?我使用 insertLogical 是因为用户可以拥有多个地址,但这是正确的方式或者是最好的方式。

谢谢。

4

1 回答 1

2

使用 insertLogical 插入的事实将保留在会话中,直到插入它的规则的 LHS 变为假(或者您使用retract() 显式收回它们)。可以在这里找到更好的解释。

这就是你的情况发生的事情:

当您为 Andorra/00000 插入地址时,规则 Rule_0、Rule_1 和 Rule_2 将被激活。当其中一个激活被触发时(根据您的评论,Rule_0 首先被触发),Rule_0 的 RHS 收回地址。这将导致 Rule_1 和 Rule_2 的激活被取消,因为不再有与它们的模式匹配的地址。更多信息可以在这里找到。

希望能帮助到你,

于 2013-09-30T12:06:54.550 回答