Skip to content

Commit 31ae549

Browse files
committed
Added checks for ambiguity.
1 parent 724eadc commit 31ae549

File tree

1 file changed

+20
-0
lines changed
  • extra/modules/pb-rule-engine/src/main/java/org/prebid/server/hooks/modules/rule/engine/core/rules/tree

1 file changed

+20
-0
lines changed

extra/modules/pb-rule-engine/src/main/java/org/prebid/server/hooks/modules/rule/engine/core/rules/tree/RuleTreeFactory.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,33 @@ public static <T, C> RuleTree<RuleConfig<T, C>> buildTree(List<RuleConfig<T, C>>
1717
final List<ParsingContext<RuleConfig<T, C>>> parsingContexts = toParsingContexts(rules);
1818
final int depth = getDepth(parsingContexts);
1919

20+
if (depth == 0) {
21+
throw new InvalidMatcherConfiguration("Rule with no matchers");
22+
}
23+
2024
if (!parsingContexts.stream().allMatch(context -> context.argumentMatchers().size() == depth)) {
2125
throw new InvalidMatcherConfiguration("Mismatched arguments count");
2226
}
2327

28+
validateRules(parsingContexts);
29+
2430
return new RuleTree<>(parseRuleNode(parsingContexts), depth);
2531
}
2632

33+
private static <T, C> void validateRules(List<ParsingContext<RuleConfig<T, C>>> parsingContexts) {
34+
final List<String> ambiguousRules = parsingContexts.stream()
35+
.collect(Collectors.groupingBy(context -> context.value().getCondition(), Collectors.counting()))
36+
.entrySet()
37+
.stream()
38+
.filter(entry -> entry.getValue() > 1)
39+
.map(Map.Entry::getKey)
40+
.toList();
41+
42+
if (!ambiguousRules.isEmpty()) {
43+
throw new InvalidMatcherConfiguration("Ambiguous matchers: " + String.join(", ", ambiguousRules));
44+
}
45+
}
46+
2747
private static <T, C> List<ParsingContext<RuleConfig<T, C>>> toParsingContexts(List<RuleConfig<T, C>> rules) {
2848
return rules.stream()
2949
.map(rule -> new ParsingContext<>(

0 commit comments

Comments
 (0)