1

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

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

4

1 回答 1

1

今天没有办法让 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]
于 2019-11-17T01:41:02.740 回答