英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论