-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathEvaluationContext.java
More file actions
73 lines (63 loc) · 2.93 KB
/
Copy pathEvaluationContext.java
File metadata and controls
73 lines (63 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package io.split.engine.evaluator;
import io.split.client.dtos.ExcludedSegments;
import io.split.engine.experiments.ParsedCondition;
import io.split.engine.experiments.ParsedRuleBasedSegment;
import io.split.rules.engine.EvaluationResult;
import io.split.storages.RuleBasedSegmentCacheConsumer;
import io.split.storages.SegmentCacheConsumer;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class EvaluationContext implements io.split.rules.engine.EvaluationContext {
private final Evaluator _evaluator;
private final SegmentCacheConsumer _segmentCacheConsumer;
private final RuleBasedSegmentCacheConsumer _ruleBasedSegmentCacheConsumer;
public EvaluationContext(Evaluator evaluator, SegmentCacheConsumer segmentCacheConsumer,
RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer) {
_evaluator = Objects.requireNonNull(evaluator);
_segmentCacheConsumer = Objects.requireNonNull(segmentCacheConsumer);
_ruleBasedSegmentCacheConsumer = Objects.requireNonNull(ruleBasedSegmentCacheConsumer);
}
public Evaluator getEvaluator() {
return _evaluator;
}
public SegmentCacheConsumer getSegmentCache() {
return _segmentCacheConsumer;
}
public RuleBasedSegmentCacheConsumer getRuleBasedSegmentCache() {
return _ruleBasedSegmentCacheConsumer;
}
@Override
public EvaluationResult evaluate(String matchingKey, String bucketingKey, String ruleName, Map<String, Object> attributes) {
EvaluatorImp.TreatmentLabelAndChangeNumber r = _evaluator.evaluateFeature(matchingKey, bucketingKey, ruleName, attributes);
return new EvaluationResult(r.treatment, r.label);
}
@Override
public boolean isInSegment(String segmentName, String key) {
return _segmentCacheConsumer.isInSegment(segmentName, key);
}
@Override
public boolean isInRuleBasedSegment(String segmentName, String key, String bucketingKey, Map<String, Object> attributes) {
ParsedRuleBasedSegment parsedRuleBasedSegment = _ruleBasedSegmentCacheConsumer.get(segmentName);
if (parsedRuleBasedSegment == null) {
return false;
}
if (parsedRuleBasedSegment.excludedKeys().contains(key)) {
return false;
}
for (ExcludedSegments excludedSegment : parsedRuleBasedSegment.excludedSegments()) {
if (excludedSegment.isStandard() && _segmentCacheConsumer.isInSegment(excludedSegment.name, key)) {
return false;
}
if (excludedSegment.isRuleBased() && isInRuleBasedSegment(excludedSegment.name, key, bucketingKey, attributes)) {
return false;
}
}
for (ParsedCondition condition : parsedRuleBasedSegment.parsedConditions()) {
if (condition.matcher().match(key, bucketingKey, attributes, this)) {
return true;
}
}
return false;
}
}