[pypy-svn] r22732 - pypy/dist/pypy/lib/logic
auc at codespeak.net
auc at codespeak.net
Fri Jan 27 14:10:04 CET 2006
Author: auc
Date: Fri Jan 27 14:10:00 2006
New Revision: 22732
Modified:
pypy/dist/pypy/lib/logic/computationspace.py
pypy/dist/pypy/lib/logic/constraint.py
pypy/dist/pypy/lib/logic/distributor.py
pypy/dist/pypy/lib/logic/problems.py
pypy/dist/pypy/lib/logic/test_computationspace.py
pypy/dist/pypy/lib/logic/test_variable.py
pypy/dist/pypy/lib/logic/variable.py
Log:
(ale, auc)
* add clone
* API change on variables/domains
Modified: pypy/dist/pypy/lib/logic/computationspace.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computationspace.py (original)
+++ pypy/dist/pypy/lib/logic/computationspace.py Fri Jan 27 14:10:00 2006
@@ -213,6 +213,10 @@
if other is None: return False
return self._nbalt == other._nbalt
+def NoProblem():
+ """the empty problem, used by clone()"""
+ pass
+
#----------- Store Exceptions ----------------------------
class UnboundVariable(VariableException):
@@ -247,9 +251,6 @@
def __str__(self):
return "%s %s have incompatible domains" % \
(self.var1, self.var2)
-
-class Foo(object):
- pass
#---- ComputationSpace -------------------------------
class ComputationSpace(object):
@@ -263,13 +264,6 @@
(also called determined variables)."""
def __init__(self, problem, parent=None):
- self.parent = parent
-
- # current/working computation space, per thread
- # self.TLS = local()
- self.TLS = Foo()
- self.TLS.current_cs = self
-
# consistency-preserving stuff
self.in_transaction = False
self.bind_lock = RLock()
@@ -285,16 +279,18 @@
# set of all constraints
self.constraints = set()
self.root = self.var('__root__')
+ # set up the problem
self.bind(self.root, problem(self))
+ # check satisfiability of the space
+ self._process()
else:
self.vars = parent.vars
self.names = parent.names
self.var_const_map = parent.var_const_map
self.constraints = parent.constraints
self.root = parent.root
-
- # run ...
- self._process()
+
+#-- Store ------------------------------------------------
#-- Variables ----------------------------
@@ -320,7 +316,7 @@
assert(isinstance(var, Var) and (var in self.vars))
if var.is_bound():
raise AlreadyBound
- var.dom = FiniteDomain(dom)
+ var.cs_set_dom(self, FiniteDomain(dom))
def get_var_by_name(self, name):
try:
@@ -339,7 +335,7 @@
def get_variables_with_a_domain(self):
varset = set()
for var in self.vars:
- if var.dom != EmptyDom: varset.add(var)
+ if var.cs_get_dom(self) != EmptyDom: varset.add(var)
return varset
def satisfiable(self, constraint):
@@ -361,15 +357,15 @@
varset = set()
constset = set()
self._compute_dependant_vars(constraint, varset, constset)
- old_domains = collect_domains(varset)
+ old_domains = self.collect_domains(varset)
for const in constset:
try:
const.narrow()
except ConsistencyFailure:
- restore_domains(old_domains)
+ self.restore_domains(old_domains)
return False
- restore_domains(old_domains)
+ self.restore_domains(old_domains)
return True
@@ -379,16 +375,16 @@
constset = set()
self._compute_dependant_vars(constraint, varset,
constset)
- old_domains = collect_domains(varset)
-
+ old_domains = self.collect_domains(varset)
+
for const in constset:
try:
const.narrow()
except ConsistencyFailure:
- restore_domains(old_domains)
+ self.restore_domains(old_domains)
return {}
- narrowed_domains = collect_domains(varset)
- restore_domains(old_domains)
+ narrowed_domains = self.collect_domains(varset)
+ self.restore_domains(old_domains)
return narrowed_domains
def satisfy(self, constraint):
@@ -396,22 +392,22 @@
varset = set()
constset = set()
self._compute_dependant_vars(constraint, varset, constset)
- old_domains = collect_domains(varset)
+ old_domains = self.collect_domains(varset)
for const in constset:
try:
const.narrow()
except ConsistencyFailure:
- restore_domains(old_domains)
+ self.restore_domains(old_domains)
raise
def satisfy_all(self):
- old_domains = collect_domains(self.vars)
+ old_domains = self.collect_domains(self.vars)
for const in self.constraints:
try:
const.narrow()
except ConsistencyFailure:
- restore_domains(old_domains)
+ self.restore_domains(old_domains)
raise
def _compute_dependant_vars(self, constraint, varset,
@@ -426,6 +422,38 @@
continue
self._compute_dependant_vars(const, varset,
constset)
+
+ def _compatible_domains(self, var, eqs):
+ """check that the domain of var is compatible
+ with the domains of the vars in the eqs
+ """
+ if var.cs_get_dom(self) == EmptyDom: return True
+ empty = set()
+ for v in eqs:
+ if v.cs_get_dom(self) == EmptyDom: continue
+ if v.cs_get_dom(self).intersection(var.cs_get_dom(self)) == empty:
+ return False
+ return True
+
+ #-- collect / restore utilities for domains
+
+ def collect_domains(self, varset):
+ """makes a copy of domains of a set of vars
+ into a var -> dom mapping
+ """
+ dom = {}
+ for var in varset:
+ if var.cs_get_dom(self) != EmptyDom:
+ dom[var] = var.cs_get_dom(self).copy()
+ return dom
+
+ def restore_domains(self, domains):
+ """sets the domain of the vars in the domains mapping
+ to their (previous) value
+ """
+ for var, dom in domains.items():
+ var.cs_set_dom(self, dom)
+
#-- BIND -------------------------------------------
@@ -461,9 +489,8 @@
# print "variable - value binding : %s %s" % (eqs, val)
# bind all vars in the eqset to val
for var in eqs:
- if var.dom != EmptyDom:
- if val not in var.dom.get_values():
- print val, var.dom, var.dom.get_values()
+ if var.cs_get_dom(self) != EmptyDom:
+ if val not in var.cs_get_dom(self).get_values():
# undo the half-done binding
for v in eqs:
v.val = eqs
@@ -472,7 +499,7 @@
def _merge(self, v1, v2):
for v in v1.val:
- if not _compatible_domains(v, v2.val):
+ if not self._compatible_domains(v, v2.val):
raise IncompatibleDomains(v1, v2)
self._really_merge(v1.val, v2.val)
@@ -595,7 +622,7 @@
def _distributable_domains(self):
for var in self.vars:
- if var.dom.size() > 1 :
+ if var.cs_get_dom(self).size() > 1 :
return True
return False
@@ -614,6 +641,14 @@
finally:
self.status_condition.release()
+ def clone(self):
+ spc = ComputationSpace(NoProblem, parent=self)
+ for var in spc.vars:
+ var.cs_set_dom(spc, var.cs_get_dom(self).copy())
+ return spc
+
+ def inject(self, restricting_problem):
+ pass
def make_children(self):
for dommap in self.distributor.distribute():
@@ -654,44 +689,8 @@
# (a) might be more Oz-ish, maybe allowing some very fancy
# stuff with distribution or whatever
self.do_something_with(result)
-
-
-
-
-
-#-------------------------------------------------------
-
-
-def _compatible_domains(var, eqs):
- """check that the domain of var is compatible
- with the domains of the vars in the eqs
- """
- if var.dom == EmptyDom: return True
- empty = set()
- for v in eqs:
- if v.dom == EmptyDom: continue
- if v.dom.intersection(var.dom) == empty:
- return False
- return True
-#-- collect / restore utilities for domains
-def collect_domains(varset):
- """makes a copy of domains of a set of vars
- into a var -> dom mapping
- """
- dom = {}
- for var in varset:
- if var.dom != EmptyDom:
- dom[var] = FiniteDomain(var.dom)
- return dom
-
-def restore_domains(domains):
- """sets the domain of the vars in the domains mapping
- to their (previous) value
- """
- for var, dom in domains.items():
- var.dom = dom
#-- Unifiability checks---------------------------------------
@@ -762,31 +761,31 @@
#--
#-- the global store
from problems import dummy_problem
-_store = ComputationSpace(dummy_problem)
+_cs = ComputationSpace(dummy_problem)
#-- global accessor functions
def var(name):
- v = Var(name, _store)
- _store.add_unbound(v)
+ v = Var(name, _cs)
+ _cs.add_unbound(v)
return v
def set_domain(var, dom):
- return _store.set_domain(var, dom)
+ return _cs.set_domain(var, dom)
def add_constraint(constraint):
- return _store.add_constraint(constraint)
+ return _cs.add_constraint(constraint)
def satisfiable(constraint):
- return _store.satisfiable(constraint)
+ return _cs.satisfiable(constraint)
def get_satisfying_domains(constraint):
- return _store.get_satisfying_domains(constraint)
+ return _cs.get_satisfying_domains(constraint)
def satisfy(constraint):
- return _store.satisfy(constraint)
+ return _cs.satisfy(constraint)
def bind(var, val):
- return _store.bind(var, val)
+ return _cs.bind(var, val)
def unify(var1, var2):
- return _store.unify(var1, var2)
+ return _cs.unify(var1, var2)
Modified: pypy/dist/pypy/lib/logic/constraint.py
==============================================================================
--- pypy/dist/pypy/lib/logic/constraint.py (original)
+++ pypy/dist/pypy/lib/logic/constraint.py Fri Jan 27 14:10:00 2006
@@ -123,11 +123,12 @@
class AbstractConstraint(object):
- def __init__(self, variables):
+ def __init__(self, c_space, variables):
"""variables is a list of variables which appear in the formula"""
+ self.cs = c_space
self._names_to_vars = {}
for var in variables:
- if var.dom == EmptyDom:
+ if var.cs_get_dom(self.cs) == EmptyDom:
raise DomainlessVariables
self._names_to_vars[var.name] = var
self._variables = variables
@@ -199,12 +200,12 @@
"""A constraint represented as a python expression."""
_FILTER_CACHE = {}
- def __init__(self, variables, formula, typ='fd.Expression'):
+ def __init__(self, c_space, variables, formula, typ='fd.Expression'):
"""variables is a list of variables which appear in the formula
formula is a python expression that will be evaluated as a boolean"""
self.formula = formula
self.type = typ
- AbstractConstraint.__init__(self, variables)
+ AbstractConstraint.__init__(self, c_space, variables)
try:
self.filterFunc = Expression._FILTER_CACHE[formula]
except KeyError:
@@ -224,7 +225,7 @@
variables = []
kwargs = {}
for variable in self._variables:
- domain = variable.dom
+ domain = variable.cs_get_dom(self.cs)
values = domain.get_values()
variables.append((domain.size(), [variable, values, 0, len(values)]))
kwargs[variable.name] = values[0]
@@ -269,7 +270,7 @@
try:
for var, keep in result_cache.iteritems():
- domain = self._names_to_vars[var].dom
+ domain = self._names_to_vars[var].cs_get_dom(self.cs)
domain.remove_values([val for val in domain if val not in keep])
except ConsistencyFailure:
Modified: pypy/dist/pypy/lib/logic/distributor.py
==============================================================================
--- pypy/dist/pypy/lib/logic/distributor.py (original)
+++ pypy/dist/pypy/lib/logic/distributor.py Fri Jan 27 14:10:00 2006
@@ -1,10 +1,10 @@
import math, random
-def make_new_domains(variables):
+def make_new_domains(cs, variables):
"""updates variables with copied domains indexed by computation space"""
new_doms = {}
for var in variables:
- new_doms[var] = var.dom.copy()
+ new_doms[var] = var.cs_get_dom(cs).copy()
return new_doms
class AbstractDistributor(object):
@@ -12,19 +12,19 @@
def __init__(self, c_space, nb_subspaces=2):
self.nb_subspaces = nb_subspaces
- self.c_space = c_space
+ self.cs = c_space
self.verbose = 0
def findSmallestDomain(self):
"""returns the variable having the smallest domain.
(or one of such varibles if there is a tie)
"""
- vars_ = [var for var in self.c_space.get_variables_with_a_domain()
- if var.dom.size() > 1]
+ vars_ = [var for var in self.cs.get_variables_with_a_domain()
+ if var.cs_get_dom(self.cs).size() > 1]
best = vars_[0]
for var in vars_:
- if var.dom.size() < best.dom.size():
+ if var.cs_get_dom(self.cs).size() < best.cs_get_dom(self.cs).size():
best = var
return best
@@ -33,12 +33,12 @@
"""returns the variable having the largest domain.
(or one of such variables if there is a tie)
"""
- vars_ = [var for var in self.c_space.get_variables_with_a_domain()
- if var.dom.size() > 1]
+ vars_ = [var for var in self.cs.get_variables_with_a_domain()
+ if var.cs_get_dom(self.cs).size() > 1]
best = vars_[0]
for var in vars_:
- if var.dom.size() > best.dom.size():
+ if var.cs_get_dom(self.cs).size() > best.cs_get_dom(self.cs).size():
best = var
return best
@@ -52,10 +52,10 @@
"""do the minimal job and let concrete class distribute variables
"""
self.verbose = verbose
- variables = self.c_space.get_variables_with_a_domain()
+ variables = self.cs.get_variables_with_a_domain()
replicas = []
for i in range(self.nb_subdomains()):
- replicas.append(make_new_domains(variables))
+ replicas.append(make_new_domains(self.cs, variables))
modified_domains = self._distribute(*replicas)
for domain in modified_domains:
domain.reset_flags()
@@ -119,9 +119,9 @@
"""See AbstractDistributor"""
self.__to_split = self.findSmallestDomain()
if self.nb_subspaces:
- return min(self.nb_subspaces, self.__to_split.dom.size()) #domains[self.__to_split].size())
+ return min(self.nb_subspaces, self.__to_split.cs_get_dom(self.cs).size()) #domains[self.__to_split].size())
else:
- return self.__to_split.dom.size() # domains[self.__to_split].size()
+ return self.__to_split.cs_get_dom(self.cs).size() # domains[self.__to_split].size()
def _distribute(self, *args):
"""See AbstractDistributor"""
Modified: pypy/dist/pypy/lib/logic/problems.py
==============================================================================
--- pypy/dist/pypy/lib/logic/problems.py (original)
+++ pypy/dist/pypy/lib/logic/problems.py Fri Jan 27 14:10:00 2006
@@ -11,8 +11,8 @@
y.cs_set_dom(cs, c.FiniteDomain([2, 3]))
z.cs_set_dom(cs, c.FiniteDomain([4, 5]))
w.cs_set_dom(cs, c.FiniteDomain([1, 4, 5, 6, 7]))
- cs.add_constraint(c.Expression([x, y, z], 'x == y + z'))
- cs.add_constraint(c.Expression([z, w], 'z < w'))
+ cs.add_constraint(c.Expression(cs, [x, y, z], 'x == y + z'))
+ cs.add_constraint(c.Expression(cs, [z, w], 'z < w'))
# set up a distribution strategy
cs.set_distributor(di.DichotomyDistributor(cs))
return (x, w, y)
@@ -25,8 +25,8 @@
y.cs_set_dom(cs, c.FiniteDomain([2, 3]))
z.cs_set_dom(cs, c.FiniteDomain([4, 5]))
w.cs_set_dom(cs, c.FiniteDomain([1, 4, 5]))
- cs.add_constraint(c.Expression([x, y, z], 'x == y + z'))
- cs.add_constraint(c.Expression([z, w], 'z < w'))
+ cs.add_constraint(c.Expression(cs, [x, y, z], 'x == y + z'))
+ cs.add_constraint(c.Expression(cs, [z, w], 'z < w'))
# set up a distribution strategy
cs.set_distributor(di.DichotomyDistributor(cs))
return (x, w, y)
@@ -40,8 +40,8 @@
y.cs_set_dom(cs, c.FiniteDomain([2, 3]))
z.cs_set_dom(cs, c.FiniteDomain([4, 5]))
w.cs_set_dom(cs, c.FiniteDomain([1]))
- cs.add_constraint(c.Expression([x, y, z], 'x == y + z'))
- cs.add_constraint(c.Expression([z, w], 'z < w'))
+ cs.add_constraint(c.Expression(cs, [x, y, z], 'x == y + z'))
+ cs.add_constraint(c.Expression(cs, [z, w], 'z < w'))
# set up a distribution strategy
cs.set_distributor(di.DichotomyDistributor(cs))
return (x, w, y)
Modified: pypy/dist/pypy/lib/logic/test_computationspace.py
==============================================================================
--- pypy/dist/pypy/lib/logic/test_computationspace.py (original)
+++ pypy/dist/pypy/lib/logic/test_computationspace.py Fri Jan 27 14:10:00 2006
@@ -7,8 +7,6 @@
from problems import *
from py.test import raises
-#-- helpers -----------------
-
#-- meat ------------------------
@@ -25,7 +23,7 @@
class TestStoreUnification:
def setup_method(self, meth):
- cs._store = cs.ComputationSpace(dummy_problem)
+ cs._cs = cs.ComputationSpace(dummy_problem)
def test_already_in_store(self):
x = cs.var('x')
@@ -121,9 +119,9 @@
cs.bind(x, [42, z])
cs.bind(y, [w, 44])
cs.bind(z, w)
- assert cs._store.in_transaction == False
+ assert cs._cs.in_transaction == False
raises(cs.UnificationFailure, cs.unify, x, y)
- assert cs._store.in_transaction == False
+ assert cs._cs.in_transaction == False
# check store consistency
assert x.val == [42, z]
assert y.val == [w, 44]
@@ -186,7 +184,7 @@
vars_.append(v)
for var in vars_:
- assert var in cs._store.vars
+ assert var in cs._cs.vars
assert var.val == x.val
t1, t2 = (FunThread(do_stuff, x, 42),
@@ -197,7 +195,7 @@
t2.join()
#check that every var is really bound to 42 or 43
for var in vars_:
- assert var in cs._store.vars
+ assert var in cs._cs.vars
assert var.val == x.val
assert (t2.raised and not t1.raised) or \
(t1.raised and not t2.raised)
@@ -206,7 +204,7 @@
def test_set_var_domain(self):
x = cs.var('x')
cs.set_domain(x, [1, 3, 5])
- assert x.dom == c.FiniteDomain([1, 3, 5])
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 3, 5])
def test_bind_with_domain(self):
x = cs.var('x')
@@ -235,100 +233,100 @@
cs.set_domain(z, [41, 42, 43])
cs.unify(x, y)
assert z.val == 42
- assert z.dom == c.FiniteDomain([41, 42, 43])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([41, 42, 43])
def test_add_constraint(self):
x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
raises(c.DomainlessVariables,
- c.Expression, [x, y, z], 'x == y + z')
- x.dom = c.FiniteDomain([1, 2])
- y.dom = c.FiniteDomain([2, 3])
- z.dom = c.FiniteDomain([3, 4])
- k = c.Expression([x, y, z], 'x == y + z')
+ c.Expression, cs._cs, [x, y, z], 'x == y + z')
+ x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2]))
+ y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
+ z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
+ k = c.Expression(cs._cs, [x, y, z], 'x == y + z')
cs.add_constraint(k)
- assert k in cs._store.constraints
+ assert k in cs._cs.constraints
def test_narrowing_domains_failure(self):
x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
- x.dom = c.FiniteDomain([1, 2])
- y.dom = c.FiniteDomain([2, 3])
- z.dom = c.FiniteDomain([3, 4])
- k = c.Expression([x, y, z], 'x == y + z')
+ x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2]))
+ y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
+ z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
+ k = c.Expression(cs._cs, [x, y, z], 'x == y + z')
raises(c.ConsistencyFailure, k.narrow)
def test_narrowing_domains_success(self):
x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
- x.dom = c.FiniteDomain([1, 2, 5])
- y.dom = c.FiniteDomain([2, 3])
- z.dom = c.FiniteDomain([3, 4])
- k = c.Expression([x, y, z], 'x == y + z')
+ x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
+ y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
+ z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
+ k = c.Expression(cs._cs, [x, y, z], 'x == y + z')
k.narrow()
- assert x.dom == c.FiniteDomain([5])
- assert y.dom == c.FiniteDomain([2])
- assert z.dom == c.FiniteDomain([3])
-
- def test_store_satisfiable_success(self):
- x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
- x.dom = c.FiniteDomain([1, 2, 5])
- y.dom = c.FiniteDomain([2, 3])
- z.dom = c.FiniteDomain([3, 4])
- k = c.Expression([x, y, z], 'x == y + z')
- cs.add_constraint(k)
- assert cs.satisfiable(k) == True
- assert x.dom == c.FiniteDomain([1, 2, 5])
- assert y.dom == c.FiniteDomain([2, 3])
- assert z.dom == c.FiniteDomain([3, 4])
-
- def test_store_satisfiable_failure(self):
- x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
- x.dom = c.FiniteDomain([1, 2])
- y.dom = c.FiniteDomain([2, 3])
- z.dom = c.FiniteDomain([3, 4])
- k = c.Expression([x, y, z], 'x == y + z')
- cs.add_constraint(k)
- assert cs.satisfiable(k) == False
- assert x.dom == c.FiniteDomain([1, 2])
- assert y.dom == c.FiniteDomain([2, 3])
- assert z.dom == c.FiniteDomain([3, 4])
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([5])
+ assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3])
def test_compute_dependant_vars(self):
x,y,z,w = (cs.var('x'), cs.var('y'),
cs.var('z'), cs.var('w'))
- x.dom = c.FiniteDomain([1, 2, 5])
- y.dom = c.FiniteDomain([2, 3])
- z.dom = c.FiniteDomain([3, 4])
- w.dom = c.FiniteDomain([1, 4, 5])
- k1 = c.Expression([x, y, z], 'x == y + z')
- k2 = c.Expression([z, w], 'z < w')
+ x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
+ y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
+ z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
+ w.cs_set_dom(cs._cs, c.FiniteDomain([1, 4, 5]))
+ k1 = c.Expression(cs._cs, [x, y, z], 'x == y + z')
+ k2 = c.Expression(cs._cs, [z, w], 'z < w')
cs.add_constraint(k1)
cs.add_constraint(k2)
varset = set()
constset = set()
- cs._store._compute_dependant_vars(k1, varset, constset)
+ cs._cs._compute_dependant_vars(k1, varset, constset)
assert varset == set([x, y, z, w])
assert constset == set([k1, k2])
+ def test_store_satisfiable_success(self):
+ x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
+ x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
+ y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
+ z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
+ k = c.Expression(cs._cs, [x, y, z], 'x == y + z')
+ cs.add_constraint(k)
+ assert cs.satisfiable(k) == True
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
+ assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
+
+ def test_store_satisfiable_failure(self):
+ x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
+ x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2]))
+ y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
+ z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
+ k = c.Expression(cs._cs, [x, y, z], 'x == y + z')
+ cs.add_constraint(k)
+ assert cs.satisfiable(k) == False
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2])
+ assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
+
def test_satisfiable_many_const_success(self):
x,y,z,w = (cs.var('x'), cs.var('y'),
cs.var('z'), cs.var('w'))
- x.dom = c.FiniteDomain([1, 2, 5])
- y.dom = c.FiniteDomain([2, 3])
- z.dom = c.FiniteDomain([3, 4])
- w.dom = c.FiniteDomain([1, 4, 5])
- k1 = c.Expression([x, y, z], 'x == y + z')
- k2 = c.Expression([z, w], 'z < w')
+ x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
+ y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
+ z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
+ w.cs_set_dom(cs._cs, c.FiniteDomain([1, 4, 5]))
+ k1 = c.Expression(cs._cs, [x, y, z], 'x == y + z')
+ k2 = c.Expression(cs._cs, [z, w], 'z < w')
cs.add_constraint(k1)
cs.add_constraint(k2)
assert cs.satisfiable(k1) == True
- assert x.dom == c.FiniteDomain([1, 2, 5])
- assert y.dom == c.FiniteDomain([2, 3])
- assert z.dom == c.FiniteDomain([3, 4])
- assert w.dom == c.FiniteDomain([1, 4, 5])
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
+ assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
+ assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1, 4, 5])
assert cs.satisfiable(k2) == True
- assert x.dom == c.FiniteDomain([1, 2, 5])
- assert y.dom == c.FiniteDomain([2, 3])
- assert z.dom == c.FiniteDomain([3, 4])
- assert w.dom == c.FiniteDomain([1, 4, 5])
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
+ assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
+ assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1, 4, 5])
narrowed_doms = cs.get_satisfying_domains(k1)
assert narrowed_doms == {x:c.FiniteDomain([5]),
y:c.FiniteDomain([2]),
@@ -344,24 +342,24 @@
def test_satisfiable_many_const_failure(self):
x,y,z,w = (cs.var('x'), cs.var('y'),
cs.var('z'), cs.var('w'))
- x.dom = c.FiniteDomain([1, 2, 5])
- y.dom = c.FiniteDomain([2, 3])
- z.dom = c.FiniteDomain([3, 4])
- w.dom = c.FiniteDomain([1])
- k1 = c.Expression([x, y, z], 'x == y + z')
- k2 = c.Expression([z, w], 'z < w')
+ x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
+ y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
+ z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
+ w.cs_set_dom(cs._cs, c.FiniteDomain([1]))
+ k1 = c.Expression(cs._cs, [x, y, z], 'x == y + z')
+ k2 = c.Expression(cs._cs, [z, w], 'z < w')
cs.add_constraint(k1)
cs.add_constraint(k2)
assert cs.satisfiable(k1) == False
- assert x.dom == c.FiniteDomain([1, 2, 5])
- assert y.dom == c.FiniteDomain([2, 3])
- assert z.dom == c.FiniteDomain([3, 4])
- assert w.dom == c.FiniteDomain([1])
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
+ assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
+ assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1])
assert cs.satisfiable(k2) == False
- assert x.dom == c.FiniteDomain([1, 2, 5])
- assert y.dom == c.FiniteDomain([2, 3])
- assert z.dom == c.FiniteDomain([3, 4])
- assert w.dom == c.FiniteDomain([1])
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
+ assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
+ assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1])
narrowed_doms = cs.get_satisfying_domains(k1)
assert narrowed_doms == {}
narrowed_doms = cs.get_satisfying_domains(k2)
@@ -370,42 +368,42 @@
def test_satisfy_many_const_failure(self):
x,y,z,w = (cs.var('x'), cs.var('y'),
cs.var('z'), cs.var('w'))
- x.dom = c.FiniteDomain([1, 2, 5])
- y.dom = c.FiniteDomain([2, 3])
- z.dom = c.FiniteDomain([3, 4])
- w.dom = c.FiniteDomain([1])
- k1 = c.Expression([x, y, z], 'x == y + z')
- k2 = c.Expression([z, w], 'z < w')
+ x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
+ y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
+ z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
+ w.cs_set_dom(cs._cs, c.FiniteDomain([1]))
+ k1 = c.Expression(cs._cs, [x, y, z], 'x == y + z')
+ k2 = c.Expression(cs._cs, [z, w], 'z < w')
cs.add_constraint(k1)
cs.add_constraint(k2)
raises(cs.ConsistencyFailure, cs.satisfy, k1)
- assert x.dom == c.FiniteDomain([1, 2, 5])
- assert y.dom == c.FiniteDomain([2, 3])
- assert z.dom == c.FiniteDomain([3, 4])
- assert w.dom == c.FiniteDomain([1])
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
+ assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
+ assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1])
raises(cs.ConsistencyFailure, cs.satisfy, k2)
- assert x.dom == c.FiniteDomain([1, 2, 5])
- assert y.dom == c.FiniteDomain([2, 3])
- assert z.dom == c.FiniteDomain([3, 4])
- assert w.dom == c.FiniteDomain([1])
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
+ assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
+ assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1])
def test_satisfy_many_const_success(self):
x,y,z,w = (cs.var('x'), cs.var('y'),
cs.var('z'), cs.var('w'))
- x.dom = c.FiniteDomain([1, 2, 5])
- y.dom = c.FiniteDomain([2, 3])
- z.dom = c.FiniteDomain([3, 4])
- w.dom = c.FiniteDomain([1, 4, 5])
- k1 = c.Expression([x, y, z], 'x == y + z')
- k2 = c.Expression([z, w], 'z < w')
+ x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
+ y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
+ z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
+ w.cs_set_dom(cs._cs, c.FiniteDomain([1, 4, 5]))
+ k1 = c.Expression(cs._cs, [x, y, z], 'x == y + z')
+ k2 = c.Expression(cs._cs, [z, w], 'z < w')
cs.add_constraint(k1)
cs.add_constraint(k2)
cs.satisfy(k2)
- print x.dom
- assert x.dom == c.FiniteDomain([5])
- assert y.dom == c.FiniteDomain([2])
- assert z.dom == c.FiniteDomain([3])
- assert w.dom == c.FiniteDomain([4, 5])
+ print x.cs_get_dom(cs._cs)
+ assert x.cs_get_dom(cs._cs) == c.FiniteDomain([5])
+ assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2])
+ assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3])
+ assert w.cs_get_dom(cs._cs) == c.FiniteDomain([4, 5])
class TestComputationSpace:
@@ -422,6 +420,11 @@
def test_ask_success(self):
spc = cs.ComputationSpace(one_solution_problem)
assert spc.ask() == cs.Succeeded
+
+ def test_double_ask(self):
+ spc = cs.ComputationSpace(one_solution_problem)
+ assert spc.ask() == cs.Succeeded
+ assert spc.ask() == cs.Succeeded
def test_ask_failure(self):
spc = cs.ComputationSpace(unsatisfiable_problem)
@@ -460,4 +463,19 @@
#print hash(expected_domains[0]), hash(expected_domains[1])
#assert set(new_domains) == set(expected_domains)
-
+ def test_clone(self):
+ spc = cs.ComputationSpace(satisfiable_problem)
+ w = spc.get_var_by_name('w')
+ assert spc.ask() == cs.Alternatives(2)
+ new_spc = spc.clone()
+ new_spc.add_constraint(c.Expression(new_spc, [w], 'w == 5'))
+ new_spc._process()
+ assert spc.ask() == cs.Alternatives(2)
+ assert new_spc.ask() == cs.Succeeded
+ assert w.cs_get_dom(spc) == c.FiniteDomain([5, 6, 7])
+ assert w.cs_get_dom(new_spc) == c.FiniteDomain([5])
+
+## with-space(spc):
+## assert w.dom == ... [5, 6, 7]
+## with-space(new_spc):
+## assert w.dom == ... [5]
Modified: pypy/dist/pypy/lib/logic/test_variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/test_variable.py (original)
+++ pypy/dist/pypy/lib/logic/test_variable.py Fri Jan 27 14:10:00 2006
@@ -25,7 +25,7 @@
class TestVariable:
def setup_method(self, meth):
- u._store = u.ComputationSpace(dummy_problem)
+ u._cs = u.ComputationSpace(dummy_problem)
def test_no_same_name(self):
x = u.var('x')
@@ -33,8 +33,8 @@
def test_get_by_name(self):
x = u.var('x')
- assert x == u._store.get_var_by_name('x')
- raises(u.NotInStore, u._store.get_var_by_name, 'y')
+ assert x == u._cs.get_var_by_name('x')
+ raises(u.NotInStore, u._cs.get_var_by_name, 'y')
def test_one_thread_reading_one_var(self):
cons = Consumer()
Modified: pypy/dist/pypy/lib/logic/variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/variable.py (original)
+++ pypy/dist/pypy/lib/logic/variable.py Fri Jan 27 14:10:00 2006
@@ -109,13 +109,13 @@
# should be used by threads that want to block on
# unbound variables
- def set_dom(self, dom):
- self.cs_set_dom(self.cs.TLS.current_cs, dom)
+## def set_dom(self, dom):
+## self.cs_set_dom(self.cs, dom)
- def get_dom(self):
- return self.cs_get_dom(self.cs.TLS.current_cs)
+## def get_dom(self):
+## return self.cs_get_dom(self.cs)
- dom = property(get_dom, set_dom)
+## dom = property(get_dom, set_dom)
def get(self):
"""Make threads wait on the variable
More information about the Pypy-commit
mailing list