[pypy-svn] r23116 - pypy/dist/pypy/lib/logic/computation_space

auc at codespeak.net auc at codespeak.net
Tue Feb 7 18:18:03 CET 2006


Author: auc
Date: Tue Feb  7 18:18:01 2006
New Revision: 23116

Modified:
   pypy/dist/pypy/lib/logic/computation_space/computationspace.py
   pypy/dist/pypy/lib/logic/computation_space/test_variable.py
   pypy/dist/pypy/lib/logic/computation_space/variable.py
Log:
(aurelien, ludovic)
* stream stuff, will be rewritten entirely tomorrow
* rename merge as alias


Modified: pypy/dist/pypy/lib/logic/computation_space/computationspace.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/computationspace.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/computationspace.py	Tue Feb  7 18:18:01 2006
@@ -321,7 +321,7 @@
                 elif val._is_bound(): # 2a.var is bound, not val
                     self._bind(var.val, val.val)
                 else: # 1. both are unbound
-                    self._merge(var, val)
+                    self._alias(var, val)
             else: # 3. val is really a value
                 if var._is_bound():
                     raise AlreadyBound(var.name)
@@ -342,13 +342,13 @@
                     raise OutOfDomain(var)
             var.val = val
 
-    def _merge(self, v1, v2):
+    def _alias(self, v1, v2):
         for v in v1.val:
             if not self._compatible_domains(v, v2.val):
                 raise IncompatibleDomains(v1, v2)
-        self._really_merge(v1.val, v2.val)
+        self._really_alias(v1.val, v2.val)
 
-    def _really_merge(self, eqs1, eqs2):
+    def _really_alias(self, eqs1, eqs2):
         # print "unbound variables binding : %s %s" % (eqs1, eqs2)
         if eqs1 == eqs2: return
         # merge two equisets into one

Modified: pypy/dist/pypy/lib/logic/computation_space/test_variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/test_variable.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/test_variable.py	Tue Feb  7 18:18:01 2006
@@ -1,4 +1,5 @@
 from threading import Thread
+import operator
 
 from py.test import raises
 
@@ -6,6 +7,18 @@
 import variable as v
 from problems import dummy_problem
 
+#-- utilities ---------------------
+
+class FunThread(Thread):
+
+    def __init__(self, fun, *args):
+        Thread.__init__(self)
+        self.fun = fun
+        self.args = args
+
+    def run(self):
+        self.fun(self, *self.args)
+
 class Consumer(Thread):
 
     def give_var(self, var):
@@ -22,6 +35,8 @@
     def run(self):
         val = [var.get() for var in self.vars]
 
+#-- meat ----------------------------
+
 class TestVariable:
 
     def test_no_same_name(self):
@@ -71,16 +86,92 @@
         for i in range(10):
             assert vars_[i].val == str(i)
 
-    def test_producer_consummer_sreams(self):
-        sp = space.ComputationSpace(dummy_problem)
+##     def test_basic_producer_consummer_sream(self):
+##         # this one is quite silly
+##         sp = space.ComputationSpace(dummy_problem)
+
+##         def generate(thread, var, n, limit):
+##             s = var.get()
+##             while n<limit:
+##                 print n
+##                 s.put(n)
+##                 n += 1
+##             s.put(None)
+        
+##         def reduc(thread, var, fun):
+##             stream = var.get()
+##             val = stream.get()
+##             while (val != None):
+##                 print val
+##                 thread.result = fun(thread.result, val)
+##                 val = stream.get()
 
-        def generate(var, n, limit):
-            if n<limit:
-                var.get().put(n)
-            else:
-                var.get().put(None)
+##         s = sp.var('s')
+##         s.bind(v.Stream())
         
-        def reduc(var, fun, a):
-            val = var.get()
+##         generator = FunThread(generate, s, 1, 5)
+##         reductor = FunThread(reduc, s, operator.mul)
+##         reductor.result = 2
+
+##         generator.start()
+##         reductor.start()
+##         generator.join()
+##         reductor.join()
+
+##         print  reductor.result
+##         assert 0
+
+    def test_daisychain_stream(self):
+        # chained stupidity
+        sp = space.ComputationSpace(dummy_problem)
+
+        s1 = sp.var('s1')
+        s2 = sp.var('s2')
+
+        stream1 = v.Stream(stuff=[1, 2, 3, s2])
+        stream2 = v.Stream(stuff=[4, 5, 6, None])
+
+        s1.bind(stream1)
+        s2.bind(stream2)
+
+        def woman_in_chains(thread, stream_variable):
+            stream = stream_variable.get()
+            val = stream.get()
             while val != None:
-                pass
+                thread.result = val
+                val = stream.get()
+                if isinstance(val, v.Var):
+                    stream = val.get()
+                    val = stream.get()
+
+        woman = FunThread(woman_in_chains, s1)
+        woman.start()
+        woman.join()
+
+        assert woman.result == 6
+                
+    def test_cyclicproducer_consummer_sream(self):
+        # infinite sillyness
+        sp = space.ComputationSpace(dummy_problem)
+
+        circular = sp.var('circular')
+        s = v.Stream(stuff=[0, 1, 2, circular])
+        circular.bind(s)
+
+        def touch10(thread, stream_variable):
+            stream = stream_variable.get()
+            val = None
+            for i in range(10):
+                val = stream.get()
+                if isinstance(val, v.Var):
+                    # at stream tail is a var
+                    stream = val.get()
+                    val = stream.get()
+                assert i % 3 == val 
+            thread.result = val
+
+        toucher = FunThread(touch10, circular)
+        toucher.start()
+        toucher.join()
+
+        assert toucher.result == 0

Modified: pypy/dist/pypy/lib/logic/computation_space/variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/variable.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/variable.py	Tue Feb  7 18:18:01 2006
@@ -127,12 +127,36 @@
 
 #-- stream stuff -----------------------------
 
-import Queue
+from Queue import Queue
 
-class Stream(Queue.Queue):
+class StreamUserBug(Exception):
+    pass
+
+class Stream(Queue):
     """a stream is potentially unbounded list
        of messages, i.e a list whose tail is
        an unbound dataflow variable
     """
 
-    
+    def __init__(self, size=5, stuff=None):
+        self.elts = stuff
+        self.idx = 0
+        Queue.__init__(self, size)
+
+    def get(self):
+        if self.elts is None:
+            Queue.get(self)
+        else:
+            try:
+                v = self.elts[self.idx]
+                self.idx += 1
+                return v
+            except IndexError:
+                self.idx = 0
+                return self.get()
+
+    def put(self, elt):
+        if self.elts is None:
+            Queue.put(self, elt)
+        else:
+            raise NoImplemented



More information about the Pypy-commit mailing list