14
14
15
15
package org .eclipse .edc .policy .engine .plan ;
16
16
17
- import org .eclipse .edc .policy .engine .spi .AtomicConstraintFunction ;
18
- import org .eclipse .edc .policy .engine .spi .DynamicAtomicConstraintFunction ;
17
+ import org .eclipse .edc .policy .engine .spi .AtomicConstraintRuleFunction ;
18
+ import org .eclipse .edc .policy .engine .spi .DynamicAtomicConstraintRuleFunction ;
19
19
import org .eclipse .edc .policy .engine .spi .PolicyContext ;
20
- import org .eclipse .edc .policy .engine .spi .PolicyValidatorFunction ;
21
- import org .eclipse .edc .policy .engine .spi .RuleFunction ;
20
+ import org .eclipse .edc .policy .engine .spi .PolicyRuleFunction ;
21
+ import org .eclipse .edc .policy .engine .spi .PolicyValidatorRule ;
22
22
import org .eclipse .edc .policy .engine .spi .plan .PolicyEvaluationPlan ;
23
23
import org .eclipse .edc .policy .engine .spi .plan .step .AndConstraintStep ;
24
24
import org .eclipse .edc .policy .engine .spi .plan .step .AtomicConstraintStep ;
37
37
import org .eclipse .edc .policy .model .Constraint ;
38
38
import org .eclipse .edc .policy .model .Duty ;
39
39
import org .eclipse .edc .policy .model .MultiplicityConstraint ;
40
- import org .eclipse .edc .policy .model .Operator ;
41
40
import org .eclipse .edc .policy .model .OrConstraint ;
42
41
import org .eclipse .edc .policy .model .Permission ;
43
42
import org .eclipse .edc .policy .model .Policy ;
44
43
import org .eclipse .edc .policy .model .Prohibition ;
45
44
import org .eclipse .edc .policy .model .Rule ;
46
45
import org .eclipse .edc .policy .model .XoneConstraint ;
47
- import org .eclipse .edc .spi .result .Result ;
48
46
49
47
import java .util .ArrayList ;
50
48
import java .util .List ;
54
52
import java .util .TreeMap ;
55
53
import java .util .stream .Collectors ;
56
54
57
- import static org .eclipse .edc .policy .engine .PolicyEngineImpl .scopeFilter ;
58
55
import static org .eclipse .edc .policy .engine .spi .PolicyEngine .DELIMITER ;
59
56
60
57
public class PolicyEvaluationPlanner implements Policy .Visitor <PolicyEvaluationPlan >, Rule .Visitor <RuleStep <? extends Rule >>, Constraint .Visitor <ConstraintStep > {
61
58
62
59
private final Stack <Rule > ruleContext = new Stack <>();
63
- private final List <PolicyValidatorFunction > preValidators = new ArrayList <>();
64
- private final List <PolicyValidatorFunction > postValidators = new ArrayList <>();
65
- private final Map <String , List <ConstraintFunctionEntry <Rule >>> constraintFunctions = new TreeMap <>();
66
- private final List <DynamicAtomicConstraintFunctionEntry <Rule >> dynamicConstraintFunctions = new ArrayList <>();
67
- private final List <RuleFunctionFunctionEntry <Rule >> ruleFunctions = new ArrayList <>();
60
+ private final List <PolicyValidatorRule <? extends PolicyContext > > preValidators = new ArrayList <>();
61
+ private final List <PolicyValidatorRule <? extends PolicyContext > > postValidators = new ArrayList <>();
62
+ private final Map <String , List <ConstraintFunctionEntry <Rule , ? extends PolicyContext >>> constraintFunctions = new TreeMap <>();
63
+ private final List <DynamicAtomicConstraintFunctionEntry <Rule , ? extends PolicyContext >> dynamicConstraintFunctions = new ArrayList <>();
64
+ private final List <RuleFunctionFunctionEntry <Rule , ? extends PolicyContext >> ruleFunctions = new ArrayList <>();
68
65
private final String delimitedScope ;
69
66
private final String scope ;
70
67
@@ -98,19 +95,19 @@ public XoneConstraintStep visitXoneConstraint(XoneConstraint constraint) {
98
95
public AtomicConstraintStep visitAtomicConstraint (AtomicConstraint constraint ) {
99
96
var currentRule = currentRule ();
100
97
var leftValue = constraint .getLeftExpression ().accept (s -> s .getValue ().toString ());
101
- var function = getFunctions (leftValue , currentRule .getClass ());
98
+ var functionName = getFunctionName (leftValue , currentRule .getClass ());
102
99
103
100
var filteringReasons = new ArrayList <String >();
104
101
105
102
if (!ruleValidator .isInScope (leftValue , delimitedScope )) {
106
103
filteringReasons .add ("leftOperand '%s' is not bound to scope '%s'" .formatted (leftValue , scope ));
107
104
}
108
105
109
- if (function == null ) {
106
+ if (functionName == null ) {
110
107
filteringReasons .add ("leftOperand '%s' is not bound to any function within scope '%s'" .formatted (leftValue , scope ));
111
108
}
112
109
113
- return new AtomicConstraintStep (constraint , filteringReasons , currentRule , function );
110
+ return new AtomicConstraintStep (constraint , filteringReasons , currentRule , functionName );
114
111
}
115
112
116
113
@ Override
@@ -161,17 +158,17 @@ public DutyStep visitDuty(Duty duty) {
161
158
return prohibitionStepBuilder .build ();
162
159
}
163
160
164
- private AtomicConstraintFunction < Rule > getFunctions (String key , Class <? extends Rule > ruleKind ) {
161
+ private String getFunctionName (String key , Class <? extends Rule > ruleKind ) {
165
162
return constraintFunctions .getOrDefault (key , new ArrayList <>())
166
163
.stream ()
167
164
.filter (entry -> ruleKind .isAssignableFrom (entry .type ()))
168
- .map (entry -> entry .function )
165
+ .map (entry -> entry .function . name () )
169
166
.findFirst ()
170
167
.or (() -> dynamicConstraintFunctions
171
168
.stream ()
172
169
.filter (f -> ruleKind .isAssignableFrom (f .type ))
173
170
.filter (f -> f .function .canHandle (key ))
174
- .map (entry -> wrapDynamicFunction ( key , entry .function ))
171
+ .map (f -> f .function . name ( ))
175
172
.findFirst ())
176
173
.orElse (null );
177
174
}
@@ -215,43 +212,19 @@ private List<ConstraintStep> validateMultiplicityConstraint(MultiplicityConstrai
215
212
.collect (Collectors .toList ());
216
213
}
217
214
218
- private <R extends Rule > AtomicConstraintFunction <R > wrapDynamicFunction (String key , DynamicAtomicConstraintFunction <R > function ) {
219
- return new AtomicConstraintFunctionWrapper <>(key , function );
220
- }
221
-
222
- private record ConstraintFunctionEntry <R extends Rule >(
215
+ private record ConstraintFunctionEntry <R extends Rule , C extends PolicyContext >(
223
216
Class <R > type ,
224
- AtomicConstraintFunction < R > function ) {
217
+ AtomicConstraintRuleFunction < R , C > function ) {
225
218
}
226
219
227
- private record DynamicAtomicConstraintFunctionEntry <R extends Rule >(
220
+ private record DynamicAtomicConstraintFunctionEntry <R extends Rule , C extends PolicyContext >(
228
221
Class <R > type ,
229
- DynamicAtomicConstraintFunction < R > function ) {
222
+ DynamicAtomicConstraintRuleFunction < R , C > function ) {
230
223
}
231
224
232
- private record RuleFunctionFunctionEntry <R extends Rule >(
225
+ private record RuleFunctionFunctionEntry <R extends Rule , C extends PolicyContext >(
233
226
Class <R > type ,
234
- RuleFunction <R > function ) {
235
- }
236
-
237
- private record AtomicConstraintFunctionWrapper <R extends Rule >(
238
- String leftOperand ,
239
- DynamicAtomicConstraintFunction <R > inner ) implements AtomicConstraintFunction <R > {
240
-
241
- @ Override
242
- public boolean evaluate (Operator operator , Object rightValue , R rule , PolicyContext context ) {
243
- return inner .evaluate (leftOperand , operator , rightValue , rule , context );
244
- }
245
-
246
- @ Override
247
- public Result <Void > validate (Operator operator , Object rightValue , R rule ) {
248
- return inner .validate (leftOperand , operator , rightValue , rule );
249
- }
250
-
251
- @ Override
252
- public String name () {
253
- return inner .name ();
254
- }
227
+ PolicyRuleFunction <R , C > function ) {
255
228
}
256
229
257
230
public static class Builder {
@@ -270,55 +243,33 @@ public Builder ruleValidator(RuleValidator ruleValidator) {
270
243
return this ;
271
244
}
272
245
273
- public Builder preValidator (String scope , PolicyValidatorFunction validator ) {
274
-
275
- if (scopeFilter (scope , planner .delimitedScope )) {
276
- planner .preValidators .add (validator );
277
- }
246
+ public <C extends PolicyContext > Builder preValidator (PolicyValidatorRule <C > validator ) {
247
+ planner .preValidators .add (validator );
278
248
279
249
return this ;
280
250
}
281
251
282
- public Builder preValidators (String scope , List <PolicyValidatorFunction > validators ) {
283
- validators .forEach (validator -> preValidator (scope , validator ));
284
- return this ;
285
- }
286
-
287
- public Builder postValidator (String scope , PolicyValidatorFunction validator ) {
288
- if (scopeFilter (scope , planner .delimitedScope )) {
289
- planner .postValidators .add (validator );
290
- }
291
- return this ;
292
- }
293
-
294
- public Builder postValidators (String scope , List <PolicyValidatorFunction > validators ) {
295
- validators .forEach (validator -> postValidator (scope , validator ));
252
+ public <C extends PolicyContext > Builder postValidator (PolicyValidatorRule <C > validator ) {
253
+ planner .postValidators .add (validator );
296
254
return this ;
297
255
}
298
256
299
257
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
300
- public <R extends Rule > Builder evaluationFunction (String scope , String key , Class <R > ruleKind , AtomicConstraintFunction <R > function ) {
301
-
302
- if (scopeFilter (scope , planner .delimitedScope )) {
303
- planner .constraintFunctions .computeIfAbsent (key , k -> new ArrayList <>())
304
- .add (new ConstraintFunctionEntry (ruleKind , function ));
305
- }
258
+ public <R extends Rule , C extends PolicyContext > Builder evaluationFunction (String key , Class <R > ruleKind , AtomicConstraintRuleFunction <R , C > function ) {
259
+ planner .constraintFunctions .computeIfAbsent (key , k -> new ArrayList <>())
260
+ .add (new ConstraintFunctionEntry (ruleKind , function ));
306
261
return this ;
307
262
}
308
263
309
264
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
310
- public <R extends Rule > Builder evaluationFunction (String scope , Class <R > ruleKind , DynamicAtomicConstraintFunction <R > function ) {
311
- if (scopeFilter (scope , planner .delimitedScope )) {
312
- planner .dynamicConstraintFunctions .add (new DynamicAtomicConstraintFunctionEntry (ruleKind , function ));
313
- }
265
+ public <R extends Rule , C extends PolicyContext > Builder evaluationFunction (Class <R > ruleKind , DynamicAtomicConstraintRuleFunction <R , C > function ) {
266
+ planner .dynamicConstraintFunctions .add (new DynamicAtomicConstraintFunctionEntry (ruleKind , function ));
314
267
return this ;
315
268
}
316
269
317
270
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
318
- public <R extends Rule > Builder evaluationFunction (String scope , Class <R > ruleKind , RuleFunction <R > function ) {
319
- if (scopeFilter (scope , planner .delimitedScope )) {
320
- planner .ruleFunctions .add (new RuleFunctionFunctionEntry (ruleKind , function ));
321
- }
271
+ public <R extends Rule , C extends PolicyContext > Builder evaluationFunction (Class <R > ruleKind , PolicyRuleFunction <R , C > function ) {
272
+ planner .ruleFunctions .add (new RuleFunctionFunctionEntry (ruleKind , function ));
322
273
return this ;
323
274
}
324
275
0 commit comments