17
17
MultiConstraintVerification as JavaMultiConstraintVerification )
18
18
19
19
def verifier_suite (verifier : ConstraintVerifier , same_value , is_value_one ,
20
+ EntityValueIndictment , EntityValueJustification , EntityValuePairJustification ,
20
21
solution , e1 , e2 , e3 , v1 , v2 , v3 ):
21
22
verifier .verify_that (same_value ) \
22
23
.given (e1 , e2 ) \
@@ -37,6 +38,11 @@ def verifier_suite(verifier: ConstraintVerifier, same_value, is_value_one,
37
38
.given (e1 , e2 ) \
38
39
.penalizes (1 )
39
40
41
+ with pytest .raises (AssertionError ):
42
+ verifier .verify_that (same_value ) \
43
+ .given (e1 , e2 ) \
44
+ .indicts_with_exactly (EntityValueIndictment (e1 , v1 ))
45
+
40
46
e1 .value = v1
41
47
e2 .value = v1
42
48
e3 .value = v1
@@ -47,7 +53,47 @@ def verifier_suite(verifier: ConstraintVerifier, same_value, is_value_one,
47
53
48
54
verifier .verify_that (same_value ) \
49
55
.given (e1 , e2 ) \
50
- .penalizes ()
56
+ .penalizes (1 )
57
+
58
+ verifier .verify_that (same_value ) \
59
+ .given (e1 , e2 ) \
60
+ .indicts_with (EntityValueIndictment (e1 , e1 .value ), EntityValueIndictment (e2 , v1 )) \
61
+ .penalizes_by (1 )
62
+
63
+ verifier .verify_that (same_value ) \
64
+ .given (e1 , e2 ) \
65
+ .indicts_with_exactly (EntityValueIndictment (e1 , e1 .value ), EntityValueIndictment (e2 , v1 )) \
66
+ .penalizes_by (1 )
67
+
68
+ verifier .verify_that (same_value ) \
69
+ .given (e1 , e2 ) \
70
+ .indicts_with (EntityValueIndictment (e1 , v1 ))
71
+
72
+ verifier .verify_that (same_value ) \
73
+ .given (e1 , e2 ) \
74
+ .justifies_with (EntityValuePairJustification ((e1 , e2 ), v1 , SimpleScore (- 1 ))) \
75
+ .penalizes_by (1 )
76
+
77
+ verifier .verify_that (same_value ) \
78
+ .given (e1 , e2 ) \
79
+ .justifies_with_exactly (EntityValuePairJustification ((e1 , e2 ), v1 , SimpleScore (- 1 ))) \
80
+ .penalizes_by (1 )
81
+
82
+ with pytest .raises (AssertionError ):
83
+ verifier .verify_that (same_value ) \
84
+ .given (e1 , e2 ) \
85
+ .indicts_with_exactly (EntityValueIndictment (e1 , v1 ))
86
+
87
+
88
+ with pytest .raises (AssertionError ):
89
+ verifier .verify_that (same_value ) \
90
+ .given (e1 , e2 ) \
91
+ .justifies_with (EntityValuePairJustification ((e1 , e2 ), v1 , SimpleScore (1 )))
92
+
93
+ with pytest .raises (AssertionError ):
94
+ verifier .verify_that (same_value ) \
95
+ .given (e1 , e2 , e3 ) \
96
+ .justifies_with_exactly (EntityValuePairJustification ((e1 , e2 ), v1 , SimpleScore (1 )))
51
97
52
98
with pytest .raises (AssertionError ):
53
99
verifier .verify_that (same_value ) \
@@ -68,10 +114,28 @@ def verifier_suite(verifier: ConstraintVerifier, same_value, is_value_one,
68
114
.given (e1 , e2 , e3 ) \
69
115
.penalizes (3 )
70
116
117
+ verifier .verify_that (same_value ) \
118
+ .given (e1 , e2 , e3 ) \
119
+ .penalizes_more_than (2 )
120
+
121
+ verifier .verify_that (same_value ) \
122
+ .given (e1 , e2 , e3 ) \
123
+ .penalizes_less_than (4 )
124
+
71
125
verifier .verify_that (same_value ) \
72
126
.given (e1 , e2 , e3 ) \
73
127
.penalizes ()
74
128
129
+ with pytest .raises (AssertionError ):
130
+ verifier .verify_that (same_value ) \
131
+ .given (e1 , e2 , e3 ) \
132
+ .penalizes_more_than (3 )
133
+
134
+ with pytest .raises (AssertionError ):
135
+ verifier .verify_that (same_value ) \
136
+ .given (e1 , e2 , e3 ) \
137
+ .penalizes_less_than (3 )
138
+
75
139
with pytest .raises (AssertionError ):
76
140
verifier .verify_that (same_value ) \
77
141
.given (e1 , e2 , e3 ) \
@@ -199,28 +263,64 @@ def verifier_suite(verifier: ConstraintVerifier, same_value, is_value_one,
199
263
200
264
201
265
def test_constraint_verifier_create ():
202
- @dataclass
266
+ @dataclass ( unsafe_hash = True )
203
267
class Value :
204
268
code : str
205
269
270
+ def __str__ (self ):
271
+ return f'Value({ self .code } )'
272
+
206
273
@planning_entity
207
- @dataclass
274
+ @dataclass ( unsafe_hash = True )
208
275
class Entity :
209
276
code : str
210
- value : Annotated [Value , PlanningVariable ] = field (default = None )
277
+ value : Annotated [Value | None , PlanningVariable ] = field (default = None )
278
+
279
+ def __str__ (self ):
280
+ return f'Entity({ self .code } , { self .value } )'
281
+
282
+ @dataclass (unsafe_hash = True )
283
+ class EntityValueIndictment :
284
+ entity : Entity
285
+ value : Value
286
+
287
+ def __str__ (self ):
288
+ return f'EntityValueIndictment({ self .entity } , { self .value } )'
289
+
290
+ @dataclass (unsafe_hash = True )
291
+ class EntityValueJustification (ConstraintJustification ):
292
+ entity : Entity
293
+ value : Value
294
+ score : SimpleScore
295
+
296
+ def __str__ (self ):
297
+ return f'EntityValueJustification({ self .entity } , { self .value } , { self .score } )'
298
+
299
+ @dataclass (unsafe_hash = True )
300
+ class EntityValuePairJustification (ConstraintJustification ):
301
+ entities : tuple [Entity ]
302
+ value : Value
303
+ score : SimpleScore
304
+
305
+ def __str__ (self ):
306
+ return f'EntityValuePairJustification({ self .entities } , { self .value } , { self .score } )'
211
307
212
308
def same_value (constraint_factory : ConstraintFactory ):
213
309
return (constraint_factory .for_each (Entity )
214
310
.join (Entity , Joiners .less_than (lambda e : e .code ),
215
311
Joiners .equal (lambda e : e .value ))
216
312
.penalize (SimpleScore .ONE )
313
+ .indict_with (lambda e1 , e2 : [EntityValueIndictment (e1 , e1 .value ), EntityValueIndictment (e2 , e2 .value )])
314
+ .justify_with (lambda e1 , e2 , score : EntityValuePairJustification ((e1 , e2 ), e1 .value , score ))
217
315
.as_constraint ('Same Value' )
218
316
)
219
317
220
318
def is_value_one (constraint_factory : ConstraintFactory ):
221
319
return (constraint_factory .for_each (Entity )
222
320
.filter (lambda e : e .value .code == 'v1' )
223
321
.reward (SimpleScore .ONE )
322
+ .indict_with (lambda e : [EntityValueIndictment (e , e .value )])
323
+ .justify_with (lambda e , score : EntityValueJustification (e , e .value , score ))
224
324
.as_constraint ('Value 1' )
225
325
)
226
326
@@ -259,6 +359,7 @@ class Solution:
259
359
solution = Solution ([e1 , e2 , e3 ], [v1 , v2 , v3 ])
260
360
261
361
verifier_suite (verifier , same_value , is_value_one ,
362
+ EntityValueIndictment , EntityValueJustification , EntityValuePairJustification ,
262
363
solution , e1 , e2 , e3 , v1 , v2 , v3 )
263
364
264
365
verifier = ConstraintVerifier .build (my_constraints , Solution , Entity )
@@ -274,6 +375,7 @@ class Solution:
274
375
solution = Solution ([e1 , e2 , e3 ], [v1 , v2 , v3 ])
275
376
276
377
verifier_suite (verifier , same_value , is_value_one ,
378
+ EntityValueIndictment , EntityValueJustification , EntityValuePairJustification ,
277
379
solution , e1 , e2 , e3 , v1 , v2 , v3 )
278
380
279
381
0 commit comments