在该条款中无法访问流口变量

发布于 2025-02-11 04:54:49 字数 1035 浏览 3 评论 0原文

我有一个具有几个字段和方法的对象类,我可以在wher句子中成功访问:

public final class ObjectWrapper {

   private Object object;
   private Date approvalDate;
   
   public getObject() {
       return object;
   }
   
   <remaining getters and setters>

}

这类似于我的流口水规则的结构:

rule "IS_ELIGIBLE"
when
    ObjectWrapper(
                  $object : getObject(),
                  <remaining conditions>
                 )
then
    System.out.println($object);
end

Intellij立即抱怨说,它无法解析符号 $ object 。该变量似乎只有在我执行以下操作时才能访问,但是 getObject()方法无法从包装器上下文外部访问。

rule "IS_ELIGIBLE"
when
    $object : getObject(),
    ObjectWrapper(
                  <remaining conditions>
                 )
then
    System.out.println($object);
end

在使用Drools框架时处理变量的正确方法是什么?是否可以在外部声明变量,然后在包装器内更改其值?

项目中还有其他Drools文件可以在其中有效,但是对于该文件,该变量仅在 中可以访问,然后在包装器类外声明 然后才能访问

I have an Object Class with a couple of fields and methods that I can successfully access in the when clause:

public final class ObjectWrapper {

   private Object object;
   private Date approvalDate;
   
   public getObject() {
       return object;
   }
   
   <remaining getters and setters>

}

This is similar to the structure of my drools rule:

rule "IS_ELIGIBLE"
when
    ObjectWrapper(
                  $object : getObject(),
                  <remaining conditions>
                 )
then
    System.out.println($object);
end

IntelliJ complains right away telling me that it cannot resolve the symbol $object. The variable only seems to be accessible if I do the following, but the getObject() method cannot be accessed from outside the Wrapper context.

rule "IS_ELIGIBLE"
when
    $object : getObject(),
    ObjectWrapper(
                  <remaining conditions>
                 )
then
    System.out.println($object);
end

What's the proper way of dealing with variables in using the drools framework? Would it be possible to declare the variable outside and then change its value inside the wrapper?

There are other drools files in the project where the above code works, but for that one, the variable is only accessible in the then clause if it's declared outside the wrapper class.

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

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

发布评论

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

评论(1

英雄似剑 2025-02-18 04:54:49

Intellij的代码格式非常好,但是它也有一些已知的错误,并且发布者对修复它并不特别感兴趣。仅仅因为代码格式说这是错误的,并不意味着它实际上是(尽管通常是正确的)。

根据您所显示的内容,适用于规则的正确语法应该是:

rule "IS_ELIGIBLE"
when
    ObjectWrapper( $object: object,
                  <remaining conditions>
                 )
then
    System.out.println($object);
end

当您具有Objectwrapper($ O:Object)之类的内容时,Drools将首先寻找一个称为>的公共变量对象,然后查找一种称为getObject的方法(基本上是遵循bean命名约定。)通常,您唯一一次调用方法的方法是适用于不符合该的东西命名约定...例如,如果您有dofoo()返回要分配结果的布尔值。

如图所示的规则(ObjectWrapper($ object:getObject()))也可以正常工作。代码格式器只是困惑的。我提供的版本在句法上等效,并且更像是“流口水”,但是您的尝试显示的内容并没有错。

IntelliJ's code formatting is pretty good, but it's also got some known bugs and the publisher isn't particularly interested in fixing it. Just because the code formatter says it's wrong doesn't mean that it actually is (though it's usually right.) Take its recommendations with a grain of salt.

Based on what you've shown, the correct syntax for your rule should be this:

rule "IS_ELIGIBLE"
when
    ObjectWrapper( $object: object,
                  <remaining conditions>
                 )
then
    System.out.println($object);
end

When you have something like ObjectWrapper( $o: object ), Drools will first look for a public variable called object, and then look for a method called getObject (basically following the bean naming conventions.) Generally the only time you'd invoke a method would be for something that doesn't meet the naming conventions ... eg if you have a doFoo() that returns a boolean that you want to assign the results of.

Your rule as shown (ObjectWrapper( $object: getObject() )) also works fine. The code formatter is just confused. The version I provide is syntactically equivalent, and is more "Drools-like", but there's nothing wrong with what you've shown as your attempt.

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