DSL中的逻辑或

时间:2015-01-28 18:09:37

标签: drools dsl rules

我有一个关于如何做或在DSLR中的问题,我有这个规则:

rule "Test"
    when
        There is AThing
        - with attribute1 is equal to something1
        - attribute2 is higher than or equal to somethingelse1

        There is AThing
        - with attribute1  is equal to something2
        - attribute2 is higher than or equal to somethingelse2
    then
        something is valid
end

重写为dlr,如:

rule "Test"
    when
        AThing(attribute1 ==  something1, attribute2  >=  somethingelse1)
        AThing(attribute1 ==  something2, attribute2  >= somethingelse2)
    then
        something is valid
end

在DSLR中将2条件置于OR中的最佳方法是什么?我想写一些类似的东西:

rule "Test"
    when
        (There is AThing
        - with attribute1 is equal to something1
        - attribute2 is higher than or equal to somethingelse1)
        or
        (There is AThing
        - with attribute1  is equal to something2
        - attribute2 is higher than or equal to somethingelse2)
    then
        something is valid
end

但是Drools编译器抱怨,我尝试了许多括号组合。

在这种情况下,我可以编写两个单独的规则,但实际上它们是一个最复杂的规则的一部分,我想避免重复两个大规则只是为了一个或。

有办法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

最好把它写成两条规则 - 无论如何都会发生。

语法要求您编写中缀或在LHS上编写

when
( Type(...)
or
  Type(...) )
then

和前缀或

when
(or Type(...)
    Type(...))
then

使用DSL都不容易实现。你能做的最好的事情就是写上括号和或者在一个前缀为更大符号(>)的行上,它只是将余数传递给DRL输出。

when
> (
  Type(...)
> or
  Type(...)
> )

但是像你的例子这样的条件也可以这样组合:

when
    AThing(attribute1 == something1 && attribute2 >=  somethingelse1
           ||
           attribute1 == something2 && attribute2 >= somethingelse2)
then

但使用DSL很难实现。 (正如我在SO的另一个角落写的那样,......)