将 OPA Rego 限制为单一规则解决方案
Limit OPA Rego to a single rule solution
如果我有这样的规则:(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 搜索吗?
今天没有办法要求 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]
如果我有这样的规则:(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 搜索吗?
今天没有办法要求 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]