Drools rule engine - if it is determined whether or not the collection of an object contains the specified value

A collection of related rules engine processing

In the actual production process, there are many scenes on a set of processing, such as a Fact object comprises a set, and the need to determine whether the set contains a value. Drools rules engine and also provides a variety of approaches, such as by operating from, contains, exists other, more.

Function may of course be done by comparing the corresponding function, in a too mentioned in other sections, will not be described here. Here the focus was to explain to a few examples, to use depending on the circumstances in a particular practice.

Examples

Omitted basic configuration, look directly at the calling code and the rules of the code.

Test calling code:

public class ContainsDemo extends BaseDemo {

    public static void main(String[] args) {

        KieSession kieSession = getKieSession("containsVar");

        Corporation corporation = new Corporation();
        Set<Scope> scopes = new HashSet<>();
        scopes.add(new Scope("P2P"));
        scopes.add(new Scope("金融"));
        scopes.add(new Scope("区块链"));
        corporation.setScopes(scopes);

        Scope scope = new Scope("区块链");

        kieSession.insert(corporation);
        kieSession.insert(scope);

        kieSession.fireAllRules();

    }
}

Related entity classes:

@Data
public class Corporation {

    private Set<Scope> scopes;

}
@Data
public class Scope {


    public Scope(String scope){
        this.scope = scope;
    }

    private String scope;

}

Then look at the rules:

package com.containsVar

import com.secbro2.drools.entity.Corporation
import com.secbro2.drools.entity.Scope
import java.util.List

rule "containsVar1"

when

    $c: Corporation($scopes:scopes);
    $s: Scope(scope == "P2P") from $scopes;

then

    System.out.println("containsVar1行业类型为:P2P");
end

rule "containsVar2"

when

    $c: Corporation($scopes:scopes);
    exists (Scope(scope == "P2P") from $scopes);
then

    System.out.println("containsVar2行业类型为:P2P");

end


rule "containsVar3"

when
    $s: Scope(scope == "区块链")
    $c: Corporation(scopes contains $s);
then

    System.out.println("containsVar3行业类型为:区块链");

end

rule "containsVar4"

when
    $s: Scope(scope == "区块链")
    exists (Corporation(scopes contains $s));
then

    System.out.println("containsVar4行业类型为:区块链");

end

4 lists the method used in the above example:

  • First, the object is first acquired Fact Corporation, and redefined its properties $ scopes. Then, to traverse in value by $ scopes from keywords, get qualified. In this case it does not need to pass an object Scope corresponding fact.
  • The second, with the first half of the first approach, but is not get the results of the screening, the direct use to determine whether there exists.
  • The third, first met the conditions of the Scope of the Fact objects, and then use this fact to fact objects Corporation were screened, only to meet the conditions before they can continue.
  • The fourth, and the third the same effect, the same principle exists to use the second way.

Related video technology

CSDN Institute: "Drools7 Rules Engine Advanced Course"

CSDN Institute: "Drools7 Rule Engine Guide"

CSDN Institute: "Drools7 Series Package"

Original link: http: //www.choupangxia.com/2019/07/31/drools%e8%a7%84%e5%88%99%e5%bc%95%e6%93%8e-%e5%a6% 82% e6% 9e% 9c% e5% 88% a4% e6% 96% ad% e6% 9f% 90% e4% b8% aa% e5% af% b9% e8% b1% a1% e4% b8% ad% e7% 9a% 84% e9% 9b% 86% e5% 90% 88% e6% 98% af% e5% 90% a6% e5% 8c% 85% e5% 90% ab% e6% 8c% 87 /

Guess you like

Origin www.cnblogs.com/secbro/p/11279098.html