【问题标题】:Limit OPA Rego to a single rule solution将 OPA Rego 限制为单一规则解决方案
【发布时间】:2019-11-16 21:44:54
【问题描述】:

如果我有这样的规则:(playground)

package play

rule[message] {
    max_index := count(input)-1

    some i

    index_a = i
    index_b = (max_index-i)
    point_a := input[index_a]
    point_b := input[index_b]

    point_a != point_b

    message := sprintf("%d (%s) and %d (%s)", [index_a, point_a, index_b, point_b])
}

并且有这个输入:

["a", "b", "c"]

我的规则有多种解决方案,例如

"0 (a) and 2 (c)",
"2 (c) and 0 (a)"

有什么方法可以在找到任何解决方案后停止 OPA 搜索?

【问题讨论】:

    标签: open-policy-agent


    【解决方案1】:

    今天没有办法让 OPA 在一个答案后停止。算法支持它,但这并不是人们似乎需要的东西。

    您当然可以通过将您的集合转换为数组并获取第一个元素来询问 OPA 找到的第一个答案。 Playground

    array_rule = [message | rule[message]]
    first = array_rule[0]
    

    或者,如果顺序很重要,您可以将逻辑编写为数组推导式,然后再次抓取第一个元素。 Playground

    array_rule = [message |
        max_index := count(input)-1
    
        some i
    
        index_a = i
        index_b = (max_index-i)
        point_a := input[index_a]
        point_b := input[index_b]
    
        point_a != point_b
    
        message := sprintf("%d (%s) and %d (%s)", [index_a, point_a, index_b, point_b])
    ]
    
    first = array_rule[0]
    

    【讨论】:

    • 感谢您回复我。这也不是我真正需要的东西,我能够使用一组来删除我收到的一些重复消息。只是想知道这样的事情是否存在。干杯。
    猜你喜欢
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多