1
1
from .._timefold_java_interop import get_class
2
2
from .._jpype_type_conversions import to_python_score
3
+ from .._timefold_java_interop import _java_score_mapping_dict
3
4
from _jpyinterpreter import unwrap_python_like_object , add_java_interface
4
5
from dataclasses import dataclass
5
6
from multipledispatch import dispatch
@@ -42,17 +43,26 @@ class ConstraintRef:
42
43
The constraint name.
43
44
It might not be unique, but `constraint_id` is unique.
44
45
When using a `constraint_configuration`, it is equal to the `ConstraintWeight.constraint_name`.
46
+
47
+ constraint_id : str
48
+ Always derived from `packageName` and `constraintName`.
45
49
"""
46
50
package_name : str
47
51
constraint_name : str
48
52
49
53
@property
50
54
def constraint_id (self ) -> str :
51
- """
52
- Always derived from packageName and constraintName.
53
- """
54
55
return f'{ self .package_name } /{ self .constraint_name } '
55
56
57
+ @staticmethod
58
+ def parse_id (constraint_id : str ):
59
+ slash_index = constraint_id .rfind ('/' )
60
+ if slash_index == - 1 : raise RuntimeError (
61
+ f'The constraint_id { constraint_id } is invalid as it does not contain a package separator \' /\' .' )
62
+ package_name = constraint_id [:slash_index ]
63
+ constraint_name = constraint_id [slash_index + 1 :]
64
+ return ConstraintRef (package_name , constraint_name )
65
+
56
66
@staticmethod
57
67
def compose_constraint_id (solution_type_or_package : Union [type , str ], constraint_name : str ) -> str :
58
68
"""
@@ -78,6 +88,9 @@ def compose_constraint_id(solution_type_or_package: Union[type, str], constraint
78
88
return ConstraintRef (package_name = package ,
79
89
constraint_name = constraint_name ).constraint_id
80
90
91
+ def _to_java (self ):
92
+ return _java_score_mapping_dict ['ConstraintRef' ].of (self .package_name , self .constraint_name )
93
+
81
94
82
95
def _safe_hash (obj : Any ) -> int :
83
96
try :
@@ -201,7 +214,7 @@ def _map_constraint_match_set(constraint_match_set: set['_JavaConstraintMatch'])
201
214
.getConstraintRef ().constraintName ()),
202
215
justification = _unwrap_justification (constraint_match .getJustification ()),
203
216
indicted_objects = tuple ([unwrap_python_like_object (indicted )
204
- for indicted in cast (list , constraint_match .getIndictedObjectList ())]),
217
+ for indicted in cast (list , constraint_match .getIndictedObjectList ())]),
205
218
score = to_python_score (constraint_match .getScore ())
206
219
)
207
220
for constraint_match in constraint_match_set
@@ -214,7 +227,7 @@ def _unwrap_justification(justification: Any) -> ConstraintJustification:
214
227
if isinstance (justification , _JavaDefaultConstraintJustification ):
215
228
fact_list = justification .getFacts ()
216
229
return DefaultConstraintJustification (facts = tuple ([unwrap_python_like_object (fact )
217
- for fact in cast (list , fact_list )]),
230
+ for fact in cast (list , fact_list )]),
218
231
impact = to_python_score (justification .getImpact ()))
219
232
else :
220
233
return unwrap_python_like_object (justification )
@@ -245,6 +258,7 @@ class Indictment(Generic[Score_]):
245
258
in `constraint_match_set`.
246
259
247
260
"""
261
+
248
262
def __init__ (self , delegate : '_JavaIndictment[Score_]' ):
249
263
self ._delegate = delegate
250
264
@@ -496,6 +510,7 @@ def score(self) -> Score_:
496
510
def summarize (self ) -> str :
497
511
return self ._delegate .summarize ()
498
512
513
+
499
514
class ScoreAnalysis :
500
515
"""
501
516
Represents the breakdown of a `Score` into individual `ConstraintAnalysis` instances,
@@ -592,7 +607,7 @@ def constraint_analysis(self, constraint_package: str, constraint_name: str) ->
592
607
return ConstraintAnalysis (self ._delegate .getConstraintAnalysis (constraint_package , constraint_name ))
593
608
594
609
@dispatch (ConstraintRef )
595
- def constraint_analysis (self , constraint_ref : ConstraintRef ) -> ConstraintAnalysis :
610
+ def constraint_analysis (self , constraint_ref : ' ConstraintRef' ) -> ConstraintAnalysis :
596
611
"""
597
612
Performs a lookup on `constraint_map`.
598
613
@@ -605,7 +620,7 @@ def constraint_analysis(self, constraint_ref: ConstraintRef) -> ConstraintAnalys
605
620
ConstraintAnalysis
606
621
None if no constraint matches of such constraint are present
607
622
"""
608
- return self .constraint_analysis (constraint_ref .package_name , constraint_ref . constraint_name )
623
+ return ConstraintAnalysis ( self ._delegate . getConstraintAnalysis (constraint_ref ._to_java ()) )
609
624
610
625
@property
611
626
def summarize (self ) -> str :
@@ -615,7 +630,6 @@ def summarize(self) -> str:
615
630
def is_solution_initialized (self ) -> bool :
616
631
return self ._delegate .isSolutionInitialized ()
617
632
618
-
619
633
def diff (self , other : 'ScoreAnalysis' ) -> 'ScoreAnalysis' :
620
634
"""
621
635
Compare this `ScoreAnalysis to another `ScoreAnalysis`
@@ -647,9 +661,6 @@ def diff(self, other: 'ScoreAnalysis') -> 'ScoreAnalysis':
647
661
return ScoreAnalysis (self ._delegate .diff (other ._delegate ))
648
662
649
663
650
-
651
-
652
-
653
664
__all__ = ['ScoreExplanation' ,
654
665
'ConstraintRef' , 'ConstraintMatch' , 'ConstraintMatchTotal' ,
655
666
'ConstraintJustification' , 'DefaultConstraintJustification' , 'Indictment' ,
0 commit comments