Skip to content
This repository was archived by the owner on Jul 17, 2024. It is now read-only.

feat: Improve ScoreAnalysis debug information #105

Merged
merged 16 commits into from
Jul 5, 2024
Prev Previous commit
Next Next commit
feat: sync ConstraintMatch and ConstraintRef API
  • Loading branch information
zepfred committed Jul 3, 2024
commit f77d4e51a4479ffbbdd8b8a765d0ae3bd5ae71bd
12 changes: 11 additions & 1 deletion tests/test_solution_manager.py
Original file line number Diff line number Diff line change
@@ -240,7 +240,16 @@ def test_score_manager_constraint_ref():

ignored_java_functions_per_class = {
'Indictment': {'getJustification'}, # deprecated
'ConstraintRef': {'of', 'packageName', 'constraintName'} # built-in constructor and properties with @dataclass
'ConstraintRef': {'of', 'packageName', 'constraintName'}, # built-in constructor and properties with @dataclass
'ConstraintMatch': {
'getConstraintRef', # built-in constructor and properties with @dataclass
'getConstraintPackage', # deprecated
'getConstraintName', # deprecated
'getConstraintId', # deprecated
'getJustificationList', # deprecated
'getJustification', # built-in constructor and properties with @dataclass
'getScore', # built-in constructor and properties with @dataclass
}
}


@@ -250,6 +259,7 @@ def test_has_all_methods():
(ScoreAnalysis, JavaScoreAnalysis),
(ConstraintAnalysis, JavaConstraintAnalysis),
(ScoreExplanation, JavaScoreExplanation),
(ConstraintMatch, JavaConstraintMatch),
(ConstraintRef, JavaConstraintRef),
(Indictment, JavaIndictment)):
type_name = python_type.__name__

This file was deleted.

Original file line number Diff line number Diff line change
@@ -98,14 +98,13 @@ def update_log_level() -> None:
PythonLoggingToLogbackAdapter.setLevel(logger.getEffectiveLevel())


def register_python_java_type_mappings():
def register_score_python_java_type_mappings():
global _scores_registered, _java_score_mapping_dict, _python_score_mapping_dict
if _scores_registered:
return

_scores_registered = True

# score types
from .score._score import SimpleScore, HardSoftScore, HardMediumSoftScore, BendableScore
from ai.timefold.solver.core.api.score.buildin.simplelong import SimpleLongScore as _SimpleScore
from ai.timefold.solver.core.api.score.buildin.hardsoftlong import HardSoftLongScore as _HardSoftScore
@@ -138,20 +137,6 @@ def register_python_java_type_mappings():
add_python_java_type_mapping(HardMediumSoftScorePythonJavaTypeMapping(HardMediumSoftScoreType))
add_python_java_type_mapping(BendableScorePythonJavaTypeMapping(BendableScoreType))

# score analysis types
from .score._score_analysis import ConstraintRef
from ai.timefold.solver.core.api.score.constraint import ConstraintRef as _ConstraintRef

from ai.timefold.solver.python.score.constraint import ConstraintRefPythonJavaTypeMapping

_python_score_mapping_dict['ConstraintRef'] = ConstraintRef

_java_score_mapping_dict['ConstraintRef'] = _ConstraintRef

ConstraintRefType = translate_python_class_to_java_class(ConstraintRef)

add_python_java_type_mapping(ConstraintRefPythonJavaTypeMapping(ConstraintRefType))


def forward_logging_events(event: 'PythonLoggingEvent') -> None:
logger.log(event.level().getPythonLevelNumber(),
@@ -316,7 +301,7 @@ def _add_to_compilation_queue(python_class: type | PythonSupplier) -> None:
def _process_compilation_queue() -> None:
global _compilation_queue

register_python_java_type_mappings()
register_score_python_java_type_mappings()
while len(_compilation_queue) > 0:
python_class = _compilation_queue.pop(0)

@@ -339,7 +324,7 @@ def _generate_constraint_provider_class(original_function: Callable[['_Constrain
wrapped_constraint_provider: Callable[['_ConstraintFactory'],
list['_Constraint']]) -> JClass:
ensure_init()
register_python_java_type_mappings()
register_score_python_java_type_mappings()
from ai.timefold.solver.python import PythonWrapperGenerator # noqa
from ai.timefold.solver.core.api.score.stream import ConstraintProvider
class_identifier = _get_class_identifier_for_object(original_function)
Original file line number Diff line number Diff line change
@@ -90,7 +90,8 @@ def compose_constraint_id(solution_type_or_package: Union[type, str], constraint
constraint_name=constraint_name).constraint_id

def _to_java(self):
return _java_score_mapping_dict['ConstraintRef'].of(self.package_name, self.constraint_name)
from ai.timefold.solver.core.api.score.constraint import ConstraintRef as JavaConstraintRef
return JavaConstraintRef.of(self.package_name, self.constraint_name)


def _safe_hash(obj: Any) -> int:
@@ -118,6 +119,10 @@ class ConstraintMatch(Generic[Score_]):
def identification_string(self) -> str:
return self.constraint_ref.constraint_id

@property
def get_indicted_object_list(self):
return self.indicted_objects

def __hash__(self) -> int:
combined_hash = hash(self.constraint_ref)
combined_hash ^= _safe_hash(self.justification)
Loading
Oops, something went wrong.