【问题标题】:groovy dsl optional keywordgroovy dsl 可选关键字
【发布时间】:2020-02-02 14:49:34
【问题描述】:

我正在使用 groovy 开发 DSL,由于我是这种语言的新手,所以我正在努力解决一些问题。

我有这个代码

def from(state1) {
        def closure
        closure = { sensor ->
            [becomes: { signal ->
                // someProcess;
                [and:  closure]
            }]
        }
        [to: { state2 ->
            // someProcess
            [when: closure]
        }]
    };

我所做的就是读这种句子

from "on" to "off" when "button" becomes "high" and "button2" becomes "high"

and "button2" becomes "high",是可选的。所以从语法上讲,一切都按照我的意愿进行。

问题是我想增加治疗or 的可能性,像这样:

from "on" to "off" when "button" becomes "high" or "button2" becomes "high"

但我不知道该怎么做。我该怎么做才能添加它,还有可能知道是and 还是or 有一个控制语句和每个不同的进程

问题已经两天了..

谢谢:)

【问题讨论】:

    标签: groovy closures dsl


    【解决方案1】:

    首先,我喜欢你的问题!

    不知道您的 //someProcess 应该做什么以及整个用例的全部内容(物联网?),您可以像这样实现 or 运算符:

    ...
        [to: { state2 ->
            [when: closure, whenEither:{ Map conds -> conds.any{ sensor, signal -> 'high' == signal } } ]
        }]
    

    那么你可以写:

    from "on" to "off" whenEither button:"high", button2:"high"
    

    【讨论】:

      猜你喜欢
      • 2020-12-19
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 2018-05-22
      • 2012-10-28
      • 2017-06-08
      相关资源
      最近更新 更多