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! らららららら!らららららら!

でも「らららららら!」はただひとつ。true のように検証される唯一のルールは最初のものだからです。

何が間違っているのかわかりません。何か案が?「挿入」と「挿入論理」の違いはどれですか? 挿入した要素を表示する方法はありますか? ユーザーは複数のアドレスを持つことができるため、insertLogical を実行しますが、これが正しい方法であり、最善の方法です。

ありがとう。

4

1 に答える 1

2

Facts inserted using insertLogical will remain in the session until the LHS of the rule that iserted it becomes false (or you explicitly retract them using retract()). A better explanation can be found here.

This is what is happening in your situation:

When you insert an Address for Andorra/00000, rules Rule_0, Rule_1 and Rule_2 get activated. When one of this activations gets fired (according to your comments, Rule_0 gets fired first), the RHS of Rule_0 retracts the Address. This will cause the cancellation of the activations for Rule_1 and Rule_2 because there is no longer an Address matching their patterns. More information can be found here.

Hope it helps,

于 2013-09-30T12:06:54.550 に答える