使用 dsl、dslr 检查 Drools 列表中的特定元素
Check for specific element in a list in Drools using dsl,dslr
我刚开始使用 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"
你的代码对我有用。下面的测试打印出正确的客户端名称,其中包含相应的产品代码。
测试
@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
插入第一个客户端后没有触发规则,插入 John 规则后触发并打印客户端名称。
你得到了什么意想不到的结果?
我刚开始使用 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"
你的代码对我有用。下面的测试打印出正确的客户端名称,其中包含相应的产品代码。
测试
@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
插入第一个客户端后没有触发规则,插入 John 规则后触发并打印客户端名称。
你得到了什么意想不到的结果?