使用同一类的2个对象

发布于 2025-01-22 14:48:32 字数 191 浏览 1 评论 0原文

我是流口水的新手,必须比较同一类的两个对象的价值。 即,可以说课是这样的,

public class Person() {
    public string Name;
}

所以我想比较Person1的名字和Person2的名字,并在它们相等的情况下引发例外。 如果有人可以给出同样的例子,那就太好了。

I am new to Drools and have to compare value of 2 objects of same class.
i.e. let's say the class is

public class Person() {
    public string Name;
}

So I want to compare person1's name and person2's name and throw an exception if they are equal.
It would be great if someone can give an example of the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2025-01-29 14:48:32

可以轻松地使用类似的规则来完成:

rule 'Unique Names'
when
    $p1: Person()
    Person(this != $p1, name == $p1.name)
then
    //error
end

我会阻止您简单地在中抛出一个例外,然后一部分规则。最好的主意是在某个地方收集结果,然后在流口水之外验证这些结果。

That can easily be done with a rule similar to this:

rule 'Unique Names'
when
    $p1: Person()
    Person(this != $p1, name == $p1.name)
then
    //error
end

I would discourage you to simply throw an exception in the then part of the rule though. It is a better idea to collect your results somewhere and then validate those results outside Drools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文