ASP-based solver: use a unique ID counter (#41290)

* solver: use a unique counter for condition, triggers and effects

* Do not reset counters when re-running setup

  What we need is just a unique ID, it doesn't need
  to start from zero every time.
This commit is contained in:
Massimiliano Culpo 2023-11-28 16:28:54 +01:00 committed by Harmen Stoppels
parent e29049d9c0
commit df2a3bd531

View file

@ -1117,11 +1117,8 @@ def __init__(self, tests=False):
self.reusable_and_possible = ConcreteSpecsByHash() self.reusable_and_possible = ConcreteSpecsByHash()
# id for dummy variables self._id_counter = itertools.count()
self._condition_id_counter = itertools.count()
self._trigger_id_counter = itertools.count()
self._trigger_cache = collections.defaultdict(dict) self._trigger_cache = collections.defaultdict(dict)
self._effect_id_counter = itertools.count()
self._effect_cache = collections.defaultdict(dict) self._effect_cache = collections.defaultdict(dict)
# Caches to optimize the setup phase of the solver # Caches to optimize the setup phase of the solver
@ -1517,7 +1514,7 @@ def condition(
# In this way, if a condition can't be emitted but the exception is handled in the caller, # In this way, if a condition can't be emitted but the exception is handled in the caller,
# we won't emit partial facts. # we won't emit partial facts.
condition_id = next(self._condition_id_counter) condition_id = next(self._id_counter)
self.gen.fact(fn.pkg_fact(named_cond.name, fn.condition(condition_id))) self.gen.fact(fn.pkg_fact(named_cond.name, fn.condition(condition_id)))
self.gen.fact(fn.condition_reason(condition_id, msg)) self.gen.fact(fn.condition_reason(condition_id, msg))
@ -1525,7 +1522,7 @@ def condition(
named_cond_key = (str(named_cond), transform_required) named_cond_key = (str(named_cond), transform_required)
if named_cond_key not in cache: if named_cond_key not in cache:
trigger_id = next(self._trigger_id_counter) trigger_id = next(self._id_counter)
requirements = self.spec_clauses(named_cond, body=True, required_from=name) requirements = self.spec_clauses(named_cond, body=True, required_from=name)
if transform_required: if transform_required:
@ -1541,7 +1538,7 @@ def condition(
cache = self._effect_cache[named_cond.name] cache = self._effect_cache[named_cond.name]
imposed_spec_key = (str(imposed_spec), transform_imposed) imposed_spec_key = (str(imposed_spec), transform_imposed)
if imposed_spec_key not in cache: if imposed_spec_key not in cache:
effect_id = next(self._effect_id_counter) effect_id = next(self._id_counter)
requirements = self.spec_clauses(imposed_spec, body=False, required_from=name) requirements = self.spec_clauses(imposed_spec, body=False, required_from=name)
if transform_imposed: if transform_imposed:
@ -2549,12 +2546,8 @@ def setup(
reuse: list of concrete specs that can be reused reuse: list of concrete specs that can be reused
allow_deprecated: if True adds deprecated versions into the solve allow_deprecated: if True adds deprecated versions into the solve
""" """
self._condition_id_counter = itertools.count()
# preliminary checks
check_packages_exist(specs) check_packages_exist(specs)
# get list of all possible dependencies
self.possible_virtuals = set(x.name for x in specs if x.virtual) self.possible_virtuals = set(x.name for x in specs if x.virtual)
node_counter = _create_counter(specs, tests=self.tests) node_counter = _create_counter(specs, tests=self.tests)
@ -2674,8 +2667,8 @@ def setup(
def literal_specs(self, specs): def literal_specs(self, specs):
for spec in specs: for spec in specs:
self.gen.h2("Spec: %s" % str(spec)) self.gen.h2("Spec: %s" % str(spec))
condition_id = next(self._condition_id_counter) condition_id = next(self._id_counter)
trigger_id = next(self._trigger_id_counter) trigger_id = next(self._id_counter)
# Special condition triggered by "literal_solved" # Special condition triggered by "literal_solved"
self.gen.fact(fn.literal(trigger_id)) self.gen.fact(fn.literal(trigger_id))
@ -2689,7 +2682,7 @@ def literal_specs(self, specs):
"literal specs have different requirements. clear cache before computing literals" "literal specs have different requirements. clear cache before computing literals"
) )
assert imposed_spec_key not in cache, msg assert imposed_spec_key not in cache, msg
effect_id = next(self._effect_id_counter) effect_id = next(self._id_counter)
requirements = self.spec_clauses(spec) requirements = self.spec_clauses(spec)
root_name = spec.name root_name = spec.name
for clause in requirements: for clause in requirements: