Java-通用Drools规则 [英] Java - Generic Drools rule

查看:363
本文介绍了Java-通用Drools规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以更通用的格式创建规则,这意味着应该验证字段和值。按照我的POJO结构。

I would like to create my rule more Generic format which mean it should be validate the fields and values. Following my POJO structure.

 public class RulesModel
private String field;
private List<String> values;
// other stuff  

我的规则

rule "sample"
when
    $rule : RulesModel( field == "source", values contains "facebook", values contains "youtube", value not contains "instagram" )  
then
    // actions
end  

验证单个字段工作正常,但是我想验证RulesModel的多个字段。
所以我希望创建POJO

It's working fine to validate single field, but i want to validate the multiple fields of RulesModel. So i desire to create the POJO

public class RulesModelList {

    private List<RulesModel> ruleList;
    // Getter & Setter
}

现在,当我使用RulesModelList类传递RulesModel列表时,规则应该如下所示进行验证

So now when i pass the list of RulesModel by using RulesModelList class the rule should be validate like below

$rule : RulesModel( field == "source", values contains "facebook") && $rule : RulesModel( field == "createdBy", values contains "admin")

如何验证

预先感谢。

推荐答案

如果您只想验证列表中是否有这两个(或三个或三个或多个)RuleModels,非常简单:

If all you want to do is verify that those two (or three or however many) RulesModels are in the list, it's quite simple:

// Example from question
rule "Verify Facebook source and admin createdBy"
when
  RulesModelList( $list: ruleList)
  exists(RulesModel( field == "source", values contains "facebook") from $list)
  exists(RulesModel( field == "createdBy", values contains "admin") from $list)
then
  // do something
end

基本上,我从RulesModelList输入中获取RulesModel的列表,并将其分配给 $ list 。然后,我确认存在一个满足我的源条件的RulesModel实例,然后存在一个与 createdBy条件相匹配的实例。

Basically I get the list of RulesModel out of the RulesModelList input and assign to $list. Then I verify that there exists a RulesModel instance that matches my 'source' criteria, and then one that matches the 'createdBy' criteria.

在该示例中,我使用了 exists()而不是分配变量,因为我的示例规则不需要利用右侧的RulesModel实例。如果要做需要对RulesModel实例做一些事情,只需将 exists()替换为这样的赋值:

In the example I used exists() instead of assigning a variable because my sample rule doesn't need to make use of the RulesModel instance on the right hand side. If you do need to do something with the RulesModel instance, simply replace exists() with an assignment like this:

$source: RulesModel( field == "source", values contains "facebook") from $list
$createdBy: RulesModel( field == "createdBy", values contains "admin") from $list

除非您要遍历列表并找到符合特定条件的内容。例如,假设字段值之一是 updatedBy,并且可以有多个具有该字段名的RulesModel实例。要获取字段名称为 updatedBy的RulesModel实例的子集,请使用累加,如Mykhaylo在他的回答中所示。

There is no need to do an accumulate unless you're trying to iterate over the list and find things that match a certain criteria. For example, let's say that one of the "field" values is "updatedBy", and there can be multiple RulesModel instances with that field name. To get the subset of the RulesModel instances with field name "updatedBy", you'd use accumulate, as Mykhaylo demonstrated in his answer.

但是,如果您要做的只是验证列表中是否包含符合您条件的对象,那么累积就太过分了,这是合适的解决方案。

But if all you want to do is verify that a list contains an object that meets your criteria, accumulate is overkill and this here is the appropriate solution.

这篇关于Java-通用Drools规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆