使用dsl,dslr在Drools中检查列表中的特定元素

问题描述 投票:1回答:1

我刚刚开始使用Drools Workbench 6.5.0

我有两堂课

class Client{
    String name;
    String age;
    List<Products> products; 
}

class Product {
    String code;
    String description;
}

有一种方法可以使用dsl和dslr触发与确定的客户产品列表中的元素匹配的规则?

我使用了以下dsl

[keyword][]regla=rule
[keyword][]cuando=when
[keyword][]entonces=then
[keyword][]fin=end

[when][]es menor o igual que=<=
[when][]es menor que=<
[when][]es mayor o igual que=>=
[when][]es mayor que=>
[when][]es igual que===
[when][]igual===
[when][]- {campo:\w*} {operador} {valor:\d*}={campo} {operador} {valor} 

[when][]Hay un cliente =$c : Client($products: products)
[when][]nombre = name
[when][]edad = age
[when][]codigo = code
[when][]Hay producto = $p : Product() from $products

和以下条件

cuando 
        Hay un cliente
            - edad es mayor o igual que 12
            - nombre igual "John"
        Hay producto
            - codigo es igual que  "4"
drools dsl
1个回答
0
投票

您的代码对我有用。下面的测试将打印正确的客户名称以及相应的产品代码。

Test

@DroolsSession(resources = { "classpath:/test.rdslr", "classpath:/business.dsl" })
public class PlaygroundTest {

    @Rule
    public DroolsAssert drools = new DroolsAssert();

    @Test
    public void testIt() {
        drools.insertAndFire(new Client("client1", 40, asList(new Product("1", "product 1"), new Product("2", "product 2"))));
        drools.insertAndFire(new Client("John", 50, asList(new Product("3", "product 3"), new Product("4", "product 4"))));
    }
}

规则

regla X
cuando 
    Hay un cliente
        - edad es mayor o igual que 12
        - nombre igual "John"
    Hay producto
        - codigo es igual que  "4"
entonces
    print client name
fin

dsl

[keyword][]regla=rule
[keyword][]cuando=when
[keyword][]entonces=then
[keyword][]fin=end

[when][]es menor o igual que=<=
[when][]es menor que=<
[when][]es mayor o igual que=>=
[when][]es mayor que=>
[when][]es igual que===
[when][]igual===
[when][]- {campo:\w*} {operador} {valor:\d*}={campo} {operador} {valor} 

[when][]Hay un cliente =$c : Client($products: products)
[when][]nombre = name
[when][]edad = age
[when][]codigo = code
[when][]Hay producto = $p : Product() from $products

[then]print client name = System.out.println($c.name);

模型

public class Client {
    public String name;
    public int age;
    public List<Product> products;

    public Client(String name, int age, List<Product> products) {
        this.name = name;
        this.age = age;
        this.products = products;
    }

    public List<Product> getProducts() {
        return products;
    }
}

public class Product {
    public String code;
    public String description;

    public Product(String code, String description) {
        this.code = code;
        this.description = description;
    }
}

测试输出

00:00:00 --> inserted: Client[name=client1,age=40,products=[org.droolsassert.Product@4fbdc0f0, org.droolsassert.Product@2ad3a1bb]]
00:00:00 --> fireAllRules
00:00:00 --> inserted: Client[name=John,age=50,products=[org.droolsassert.Product@71e9a896, org.droolsassert.Product@6b9267b]]
00:00:00 --> fireAllRules
00:00:00 <-- 'X' has been activated by the tuple [Client, Product]
John

[插入第一个客户后,没有规则触发,插入约翰规则后,并打印了客户名称。

您得到了什么意外的结果?

© www.soinside.com 2019 - 2024. All rights reserved.