[pypy-svn] r22580 - pypy/dist/pypy/lib/logic

auc at codespeak.net auc at codespeak.net
Tue Jan 24 12:04:46 CET 2006


Author: auc
Date: Tue Jan 24 12:04:44 2006
New Revision: 22580

Modified:
   pypy/dist/pypy/lib/logic/constraint.py
   pypy/dist/pypy/lib/logic/test_unification.py
Log:
(auc, ale)
* update constraints to cope with our vars
* check that narrow work on n-ary expressions


Modified: pypy/dist/pypy/lib/logic/constraint.py
==============================================================================
--- pypy/dist/pypy/lib/logic/constraint.py	(original)
+++ pypy/dist/pypy/lib/logic/constraint.py	Tue Jan 24 12:04:44 2006
@@ -91,8 +91,9 @@
     __len__ = size
     
     def get_values(self):
-        """return all the values in the domain"""
-        return self._values
+        """return all the values in the domain
+           in an indexable sequence"""
+        return list(self._values)
 
     def __iter__(self):
         return iter(self._values)
@@ -118,9 +119,11 @@
     
     def __init__(self, variables):
         """variables is a list of variables which appear in the formula"""
+        self._names_to_vars = {}
         for var in variables:
             if var.dom is None:
                 raise DomainlessVariables
+            self._names_to_vars[var.name] = var
         self._variables = variables
 
     def affectedVariables(self):
@@ -207,18 +210,18 @@
         """key = (variable,value), value = [has_success,has_failure]"""
         result_cache = {}
         for var_name in self._variables:
-            result_cache[var_name] = {}
+            result_cache[var_name.name] = {}
         return result_cache
 
 
-    def _assign_values(self, domains):
+    def _assign_values(self):
         variables = []
         kwargs = {}
         for variable in self._variables:
-            domain = domains[variable]
+            domain = variable.dom
             values = domain.get_values()
             variables.append((domain.size(), [variable, values, 0, len(values)]))
-            kwargs[variable] = values[0]
+            kwargs[variable.name] = values[0]
         # sort variables to instanciate those with fewer possible values first
         variables.sort()
 
@@ -229,22 +232,23 @@
             for size, curr in variables:
                 if (curr[2] + 1) < curr[-1]:
                     curr[2] += 1
-                    kwargs[curr[0]] = curr[1][curr[2]]
+                    kwargs[curr[0].name] = curr[1][curr[2]]
                     break
                 else:
                     curr[2] = 0
-                    kwargs[curr[0]] = curr[1][0]
+                    kwargs[curr[0].name] = curr[1][0]
             else:
                 # it's over
                 go_on = 0
             
         
-    def narrow(self, domains):
+    def narrow(self):
+        # removed domain arg. (auc, ale)
         """generic narrowing algorithm for n-ary expressions"""
         maybe_entailed = 1
         ffunc = self.filterFunc
         result_cache = self._init_result_cache()
-        for kwargs in self._assign_values(domains):
+        for kwargs in self._assign_values():
             if maybe_entailed:
                 for var, val in kwargs.iteritems():
                     if val not in result_cache[var]:
@@ -259,7 +263,7 @@
 
         try:
             for var, keep in result_cache.iteritems():
-                domain = domains[var]
+                domain = self._names_to_vars[var].dom
                 domain.remove_values([val for val in domain if val not in keep])
                 
         except ConsistencyFailure:

Modified: pypy/dist/pypy/lib/logic/test_unification.py
==============================================================================
--- pypy/dist/pypy/lib/logic/test_unification.py	(original)
+++ pypy/dist/pypy/lib/logic/test_unification.py	Tue Jan 24 12:04:44 2006
@@ -233,3 +233,12 @@
         k = c.Expression([x, y, z], 'x == y + z')
         u.add_constraint(k)
         assert k in u._store.constraints
+
+    def test_narrowing_domains(self):
+        x,y,z = u.var('x'), u.var('y'), u.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')
+        raises(c.ConsistencyFailure, k.narrow)
+        



More information about the Pypy-commit mailing list