简体   繁体   English

使用 Drools 规则进行实体验证

[英]Entity validation using Drools rule

Can i validate an object for primary key values using Drools rule?我可以使用 Drools 规则验证 object 的主键值吗? Suppose i have one Object called Person with two primary keys personId and PersonNumber and many other fields like age,occupation etc. I need to create a Drools rule for validate the object Person for primary key validation ( Primary key shouldn't be not null).假设我有一个名为 Person 的 Object 具有两个主键 personId 和 PersonNumber 以及许多其他字段,如年龄、职业等。我需要创建一个 Drools 规则来验证 object Person 用于主键验证(主键不应为空) .

if it is possible i have one more doubt.如果可能的话,我还有一个疑问。 Can i make the the Drools rule as generic rule for validating other objects also ( it should be dynamic since all fields will different with respect to other object).我可以将 Drools 规则作为验证其他对象的通用规则吗(它应该是动态的,因为所有字段相对于其他对象都会有所不同)。

If anyone have suggestion on this please make a comment here.如果有人对此有任何建议,请在此处发表评论。

Happy Coding!!编码快乐!!

You can do a null check of course.当然,您可以进行 null 检查。 But if this is something being inserted into a database that should really be taken care of by database constraints.但是,如果这是插入到数据库中的东西,则应该由数据库约束真正处理。

Anyway, using your example with an object Person and two fields personId and PersonNumber (ignoring the other fields because they're not immediately relevant.) I will assume the class and its getters look like this:无论如何,使用 object Person 和两个字段 personId 和 PersonNumber 的示例(忽略其他字段,因为它们不是立即相关的。)我将假设 class 及其吸气剂看起来像这样:

class Person {
  private Integer personId, PersonNumber;
  public Integer getPersonId() {
    return personId;
  }
  public Integer getPersonNumber() {
    return PersonNumber;
  }
}

Then you can write a rule checking that there is a Person with either personId or PersonNumber null:然后你可以编写一个规则来检查是否有一个 PersonId 或 PersonNumber null 的人:

rule "Person with either personId or PersonNumber null"
when
  exists( Person( personId == null || personNumber == null ))
then
  System.out.println("Person has either null personId or PersonNumber");
end

Note that if you wanted to check that both personId and PersonNumber are null, the rule would be a little different;请注意,如果您想检查 personId 和 PersonNumber 是否都是null,则规则会有所不同; you'd not use the ||你不会使用|| operator, but instead use the comma and:运算符,而是使用逗号和:

rule "Person with both personId and PersonNumber null"
when
  exists( Person( personId == null, personNumber == null ))
then
  System.out.println("Person has null personId and null PersonNumber");
end

Alternatively, if I misunderstood what you means by Primary key shouldn't be not null such that you want the fields to be null so they can be auto-set by the database, then you could just invert the null check to a not null check either by replacing exists with not : Alternatively, if I misunderstood what you means by Primary key shouldn't be not null such that you want the fields to be null so they can be auto-set by the database, then you could just invert the null check to a not null check通过用not替换exists

not( Person( personId == null || personNumber == null ))
not( Person( personId == null, personNumber == null ))

... or by swapping the == to !== . ...或通过将==交换为!==

As to your question about making a generic rule, that's not possible with rules unless all of the objects you're validating share a base class or an interface, and the method to get the primary key(s) is part of the base class/interface.至于您关于制定通用规则的问题,除非您要验证的所有对象都共享一个基本 class 或接口,并且获取主键的方法是基类的一部分,否则这是不可能的。界面。 In theory you could use reflection and create a Drools function to map a set of requested fields to a null check, but this seems like an incredibly bad idea and extremely fragile.理论上讲,您可以使用反射并创建Drools function到 map 一组请求字段到 null 检查,但这似乎是一个非常糟糕的主意并且非常脆弱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM