在Drools的lhs中如何获取已定义的变量?

huangapple go评论52阅读模式
英文:

How can I get a variable already defined in drools lhs?

问题

我使用 drools API 来定义一个规则,如下所示:

PackageDescr pkg = DescrFactory.newPackage()
.name("org.drools.example")
.newRule().name("Xyz")
.attribute("ruleflow-grou","bla")
.lhs()
.and()
.pattern("Student").id("$stu", false ).constraint("name==miko").constraint("age > 6").end()
.not().pattern("Bar").constraint("a+b==c").end().end()
.end()
.end()

最终它将转化为一个 DRL 文件,如下所示:

$stu: Student(name == miko, age > 6)
not Bar(a + b == c)

我的问题是,如果我想要获取之前定义的 `$stu` 变量,并添加一些额外的约束条件,比如:
```$stu(grade != 2)```
我该如何获取并重用它呢?
英文:

I'm using drools api to define a rule like this:

PackageDescr pkg = DescrFactory.newPackage()
       .name("org.drools.example")
       .newRule().name("Xyz")
           .attribute("ruleflow-grou","bla")
       .lhs()
           .and()
               .pattern("Student").id( "$stu", false ).constraint("name==miko").constraint("age > 6").end()
               .not().pattern("Bar").constraint("a+b==c").end().end()
           .end()
       .end()

Finnally it will transfer into drl file like:

$stu: Studeng(name == miko, age > 6)
not Bar(a + b == c)

My question is if I wanna to get the '$stu' variable defined befor and to add some more constaints, like:
$stu(grade != 2)
how can I get it and reuse it?

答案1

得分: 0

为了向Student添加额外的约束条件,只需在你已经拥有的约束条件后面链接它。

.pattern("Student")
.id( "$stu", false )
.constraint("name == "miko"")
.constraint("age > 6")
.constraint("grade != 2 ")
.end()


这将生成:

$stu: Student( name == "miko", age > 6, grade != 2)


(我假设这是你想要做的,因为`$stu( grade != 2 )`不是有效的语法。)

此规则将识别任何名字为“miko”的学生(请注意这是区分大小写的!),年龄大于6,并且成绩不等于2。
英文:

To add an additional constraint to Student, just chain it after the constraints you already have.

.pattern("Student")
  .id( "$stu", false )
  .constraint("name == \"miko\"")
  .constraint("age > 6")
  .constraint("grade != 2 ")
.end()

Which would generate:

$stu: Student( name == "miko", age > 6, grade != 2)

(I'm presuming that's what you're trying to do, because $stu( grade != 2 ) is not valid syntax.)

This rule would identify any students with the name "miko" (note this is case sensitive!), have an age greater than 6, and a grade not equal to 2.

huangapple
  • 本文由 发表于 2023年4月6日 20:45:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定