【问题标题】:Insert in Drools插入流口水
【发布时间】:2013-09-30 07:37:22
【问题描述】:

我是初学者,我正在使用 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!”因为唯一像 true 一样验证的规则是第一个。

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

谢谢。

【问题讨论】:

    标签: insert liferay drools rules


    【解决方案1】:

    使用 insertLogical 插入的事实将保留在会话中,直到插入它的规则的 LHS 变为假(或者您使用retract() 显式收回它们)。 A better explanation can be found here.

    这就是你的情况:

    当您为 Andorra/00000 插入地址时,规则 Rule_0、Rule_1 和 Rule_2 将被激活。当其中一个激活被触发时(根据您的 cmets,Rule_0 被首先触发),Rule_0 的 RHS 收回地址。这将导致 Rule_1 和 Rule_2 的激活被取消,因为不再有匹配它们的模式的地址。 More information can be found here.

    希望对你有帮助,

    【讨论】:

    • 好的,谢谢!然后,即使我执行与规则相同数量的 insertLogical,我也只有一个对象。有没有办法多次插入同一个对象?
    • 没有干净的方法来做你所要求的。一种常见的方法是在执行结束时有一个清理规则:pastebin.com/RbkQfVsf,并避免在每个规则中收回地址。
    猜你喜欢
    • 2011-06-21
    • 1970-01-01
    • 2012-05-12
    • 2011-12-06
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多