Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
make CachePicker work with solver
Browse files Browse the repository at this point in the history
  • Loading branch information
NoR8quoh1r committed Nov 15, 2024
1 parent 07124ad commit 6d3719e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
30 changes: 19 additions & 11 deletions src/faebryk/libs/picker/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import faebryk.library._F as F
from faebryk.core.module import Module
from faebryk.core.parameter import Parameter
from faebryk.core.parameter import And, Is, Parameter, ParameterOperatable, Predicate
from faebryk.core.solver import Solver
from faebryk.libs.picker.jlcpcb.jlcpcb import Component
from faebryk.libs.picker.lcsc import attach
Expand Down Expand Up @@ -82,8 +82,9 @@ def pick(self, module: Module):
) from e


def _try_merge_params(target: Module, source: Module, solver: Solver) -> bool:
raise NotImplementedError
def _build_compatible_constraint(
target: Module, source: Module
) -> ParameterOperatable.BooleanLike:
assert type(target) is type(source)

# Override module parameters with picked component parameters
Expand All @@ -92,14 +93,23 @@ def _try_merge_params(target: Module, source: Module, solver: Solver) -> bool:
)

# sort by type to avoid merge conflicts
predicates: list[Predicate] = []
it = module_params.values()
for p, value in it:
if not value.is_subset_of(p):
return False
for p, value in it:
p.alias_is(value)
predicates.append(Is(p, value))

return And(*predicates)

return True

def _get_compatible_modules(
module: Module, cache: list[Module], solver: Solver
) -> Iterable[Module]:
compatible_constraints = [_build_compatible_constraint(module, m) for m in cache]
solve_result = solver.assert_any_predicate(
list(zip(compatible_constraints, cache)), lock=True
)
for _, m in solve_result.true_predicates:
yield m


class CachePicker(F.has_multi_picker.Picker):
Expand All @@ -109,9 +119,7 @@ def __init__(self):

def pick(self, module: Module, solver: Solver):
mcache = [m for m in self.cache[type(module)] if m.has_trait(has_part_picked)]
for m in mcache:
if not _try_merge_params(module, m, solver):
continue
for m in _get_compatible_modules(module, mcache, solver):
logger.debug(f"Found compatible part in cache: {module} with {m}")
module.add(
F.has_descriptive_properties_defined(
Expand Down
8 changes: 2 additions & 6 deletions src/faebryk/libs/picker/picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import faebryk.library._F as F
from faebryk.core.module import Module
from faebryk.core.moduleinterface import ModuleInterface
from faebryk.core.parameter import Is, Parameter, ParameterOperatable, Predicate
from faebryk.core.parameter import And, Is, Parameter, ParameterOperatable, Predicate
from faebryk.core.solver import Solver
from faebryk.libs.util import flatten, not_none

Expand Down Expand Up @@ -183,11 +183,7 @@ def pick_module_by_params(
predicates[o] = True
continue

anded = predicate_list[0]
for p in predicate_list[1:]:
anded = anded.operation_and(p)

predicates[o] = anded
predicates[o] = And(*predicate_list)

if len(predicates) == 0:
raise PickErrorParams(module, list(options))
Expand Down

0 comments on commit 6d3719e

Please sign in to comment.