繁体   English   中英

将 OPA Rego 限制为单一规则解决方案

[英]Limit OPA Rego to a single rule solution

如果我有这样的规则:(操场

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 搜索?

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

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

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

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

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM