Skip to content

Commit

Permalink
Merge pull request #367 from gaphor/no-juggle-error
Browse files Browse the repository at this point in the history
Remove JuggleError
  • Loading branch information
amolenaar authored Nov 19, 2021
2 parents b3d4a27 + 84c740d commit 69e1d01
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
2 changes: 1 addition & 1 deletion gaphas/solver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from gaphas.solver.constraint import BaseConstraint, Constraint, MultiConstraint
from gaphas.solver.solver import JuggleError, Solver
from gaphas.solver.solver import Solver
from gaphas.solver.variable import (
NORMAL,
REQUIRED,
Expand Down
17 changes: 3 additions & 14 deletions gaphas/solver/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ class Solver:
A constraint should have accompanying variables.
"""

def __init__(self) -> None:
def __init__(self, resolve_limit: int = 16) -> None:
# a dict of constraint -> name/variable mappings
self._constraints: Set[Constraint] = set()
self._marked_cons: List[Constraint] = []
self._solving = False
self._resolve_limit = resolve_limit
self._handlers: Set[Callable[[Constraint], None]] = set()

def add_handler(self, handler: Callable[[Constraint], None]) -> None:
Expand Down Expand Up @@ -129,12 +130,8 @@ def request_resolve_constraint(self, c: Constraint) -> None:
if c in self._marked_cons:
self._marked_cons.remove(c)
self._marked_cons.append(c)
else:
elif self._marked_cons.count(c) < self._resolve_limit:
self._marked_cons.append(c)
if self._marked_cons.count(c) > 100:
raise JuggleError(
f"Variable juggling detected, constraint {c} resolved {self._marked_cons.count(c)} times out of {len(self._marked_cons)}"
)

def solve(self) -> None:
"""
Expand Down Expand Up @@ -199,11 +196,3 @@ def find_containing_constraint(
):
return find_containing_constraint(cs, constraints)
return None


class JuggleError(AssertionError):
"""Variable juggling exception.
Raised when constraint's variables are marking each other dirty
forever.
"""
11 changes: 6 additions & 5 deletions tests/test_solver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Test constraint solver."""
import pytest

from gaphas.constraint import EqualsConstraint, EquationConstraint, LessThanConstraint
from gaphas.solver import REQUIRED, JuggleError, MultiConstraint, Solver, Variable
from gaphas.solver import REQUIRED, MultiConstraint, Solver, Variable
from gaphas.solver.constraint import Constraint, ContainsConstraints


Expand Down Expand Up @@ -79,7 +78,7 @@ def test_minimal_size_constraint():
assert 10 == v3


def test_juggle_error_is_raised_when_constraints_can_not_be_resolved():
def test_constraints_can_not_be_resolved():
solver = Solver()
a = Variable()
b = Variable()
Expand All @@ -90,8 +89,10 @@ def test_juggle_error_is_raised_when_constraints_can_not_be_resolved():
solver.add_constraint(EqualsConstraint(a, c))
solver.add_constraint(EqualsConstraint(b, d))

with pytest.raises(JuggleError):
solver.solve()
solver.solve()

assert a.value == 40.0
assert b.value == 30.0


def test_notify_for_nested_constraint():
Expand Down

0 comments on commit 69e1d01

Please sign in to comment.