[Checkins] SVN: Sandbox/adamg/ocql/trunk/src/ocql/ merge of branch alg-compiler

Adam Groszer agroszer at gmail.com
Mon Jul 7 13:04:12 EDT 2008


Log message for revision 88084:
  merge of branch alg-compiler

Changed:
  U   Sandbox/adamg/ocql/trunk/src/ocql/aoptimizer/aoptimizer.py
  U   Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.py
  U   Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.txt
  U   Sandbox/adamg/ocql/trunk/src/ocql/interfaces.py
  U   Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py
  A   Sandbox/adamg/ocql/trunk/src/ocql/rewriter/interfaces.py
  U   Sandbox/adamg/ocql/trunk/src/ocql/rewriter/rewriter.py
  U   Sandbox/adamg/ocql/trunk/src/ocql/tests/run.txt
  U   Sandbox/adamg/ocql/trunk/src/ocql/tests/test_old.py
  U   Sandbox/adamg/ocql/trunk/src/ocql/tests/test_zope.py

-=-
Modified: Sandbox/adamg/ocql/trunk/src/ocql/aoptimizer/aoptimizer.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/aoptimizer/aoptimizer.py	2008-07-07 16:32:26 UTC (rev 88083)
+++ Sandbox/adamg/ocql/trunk/src/ocql/aoptimizer/aoptimizer.py	2008-07-07 17:04:12 UTC (rev 88084)
@@ -14,7 +14,7 @@
 
 from ocql.interfaces import IAlgebraOptimizer
 
-from ocql.interfaces import IAlgebraObject
+from ocql.interfaces import IAlgebraObjectHead
 from ocql.interfaces import IOptimizedAlgebraObject
 
 def addMarkerIF(obj, marker):
@@ -24,7 +24,7 @@
 
 class AlgebraOptimizer(object):
     implements(IAlgebraOptimizer)
-    adapts(IAlgebraObject)
+    adapts(IAlgebraObjectHead)
 
     def __init__(self, context):
         self.context = context

Modified: Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.py	2008-07-07 16:32:26 UTC (rev 88083)
+++ Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.py	2008-07-07 17:04:12 UTC (rev 88084)
@@ -10,10 +10,16 @@
 
 from zope.component import adapts
 from zope.interface import implements
+from zope.component import provideAdapter
 
 from ocql.interfaces import IAlgebraCompiler
+#from ocql.interfaces import IAlgebraCompiler
 from ocql.interfaces import IOptimizedAlgebraObject
+#from ocql.interfaces import ICompiledAlgebraObject
+from ocql.rewriter.algebra import Head
 
+from ocql.rewriter.interfaces import *
+
 from ocql.compiler.runnablequery import RunnableQuery
 
 class AlgebraCompiler(object):
@@ -25,7 +31,218 @@
         #self.db = db
 
     def __call__(self, metadata, algebra):
-        algebra = self.context
-        code = algebra.compile()
-        run = RunnableQuery(metadata, algebra, code)
-        return run
\ No newline at end of file
+        algebra = self.context.tree
+        #code = algebra.compile()
+        adapter = IAlgebraCompiler(algebra)
+        code = adapter()
+        run = RunnableQuery(metadata, self.context, code)
+        return run
+
+class BaseCompiler(object):
+    def __init__(self, context):
+        #context becomes the adapted object
+        self.context = context
+
+class EmptyCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IEmpty)
+
+    def __call__(self):
+        if self.context.klass == set:
+            return 'set()'
+        elif self.context.klass == list:
+            return '[]'
+
+class SingleCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(ISingle)
+
+    def __call__(self):
+        if self.context.klass == set:
+            return 'set(['+IAlgebraCompiler(self.context.expr)()+'])'
+        elif self.context.klass == list:
+            return '['+IAlgebraCompiler(self.context.expr)()+']'
+
+class UnionCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IUnion)
+
+    def __call__(self):
+        if self.context.klass == set:
+            return 'set.union(%s, %s)' % (
+                IAlgebraCompiler(self.context.coll1)(),
+                IAlgebraCompiler(self.context.coll2)())
+        elif self.context.klass == list:
+            return '(%s)+(%s)' % (
+                IAlgebraCompiler(self.context.coll1)(),
+                IAlgebraCompiler(self.context.coll2)())
+
+class IterCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IIter)
+
+    def __call__(self):
+        if self.context.func is LambdaCompiler and \
+        self.context.call is set and \
+        self.context.expr is IfCompiler:
+
+            if self.context.klass == set:
+                return 'reduce(set.union, map(%s,%s), set())' % (
+                    IAlgebraCompiler(self.context.func)(),
+                    IAlgebraCompiler(self.context.coll)())
+            if self.context.klass == list:
+                return 'reduce(operator.add, map(%s, %s), [])' % (
+                    IAlgebraCompiler(self.context.func)(),
+                    IAlgebraCompiler(self.context.coll)())
+        else:
+            if self.context.klass == set:
+                return 'reduce(set.union, map(%s,%s), set())' % (
+                    IAlgebraCompiler(self.context.func)(),
+                    IAlgebraCompiler(self.context.coll)())
+            if self.context.klass == list:
+                return 'reduce(operator.add, map(%s, %s), [])' % (
+                    IAlgebraCompiler(self.context.func)(),
+                    IAlgebraCompiler(self.context.coll)())
+
+
+class SelectCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(ISelect)
+
+    def __call__(self):
+        if self.context.klass == set:
+            return 'set(filter(%s, %s))' % (
+                IAlgebraCompiler(self.context.func)(),
+                IAlgebraCompiler(self.context.call)())
+        if self.context.klass == list:
+            return 'filter()%s, %s' % (
+                IAlgebraCompiler(self.context.func)(),
+                IAlgebraCompiler(self.context.call)())
+
+
+class ReduceCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IReduce)
+
+    def __call__(self):
+        if self.context.klass == set:
+            return 'reduce(%s, map(%s, %s), %s)' % (
+                IAlgebraCompiler(self.context.aggreg)(),
+                IAlgebraCompiler(self.context.func)(),
+                IAlgebraCompiler(self.context.coll)(),
+                IAlgebraCompiler(self.context.expr)())
+        elif self.context.klass == list:
+            return 'reduce(%s, map(%s, %s), %s)'% (
+                IAlgebraCompiler(self.context.aggreg)(),
+                IAlgebraCompiler(self.context.func)(),
+                IAlgebraCompiler(self.context.coll)(),
+                IAlgebraCompiler(self.context.expr)())
+
+
+class RangeCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IRange)
+
+    def __call__(self):
+        if self.context.klass == set:
+            return 'set(range(%s,%s))' % (
+                IAlgebraCompiler(self.context.start)(),
+                IAlgebraCompiler(self.context.end)())
+        elif self.context.klass == list:
+            return 'range(%s,%s)' % (
+                IAlgebraCompiler(self.context.start)(),
+                IAlgebraCompiler(self.context.end)())
+
+
+class MakeCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IMake)
+
+    def __call__(self):
+        return '%s(metadata.getAll("%s"))' % (
+            self.context.coll1.__name__,
+            IAlgebraCompiler(self.context.expr)())
+
+
+class IfCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IIf)
+
+    def __call__(self):
+        return '((%s) and (%s) or (%s))' % (
+            IAlgebraCompiler(self.context.cond)(),
+            IAlgebraCompiler(self.context.expr1)(),
+            IAlgebraCompiler(self.context.expr2)())
+
+
+class LambdaCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(ILambda)
+
+    def __call__(self):
+        return 'lambda %s: %s'%(
+            self.context.var,
+            IAlgebraCompiler(self.context.expr)())
+
+
+class ConstantCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IConstant)
+
+    def __call__(self):
+        return '%s'% (self.context.value)
+
+
+class IdentifierCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IIdentifier)
+
+    def __call__(self):
+        return self.context.name
+
+
+class BinaryCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IBinary)
+
+    def __call__(self):
+        return '%s%s%s' % (
+            IAlgebraCompiler(self.context.left)(),
+            self.context.op.op,
+            IAlgebraCompiler(self.context.right)())
+
+
+class OperatorCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IOperator)
+
+    ops = {
+        'or': 'operator.or_',
+        'and': 'operator.and_',
+        'not': 'operator.not_',
+        '+': 'operator.add', '-': 'operator.sub',
+        '*': 'operator.mul', '/': 'operator.div',
+        '<': 'operator.lt', '>': 'operator.gt',
+        '<=': 'operator.le', '>=': 'operator.ge',
+        '==': 'operator.eq', '~=': 'operator.ne',
+        }
+
+    def __call__(self):
+        return self.ops[self.context.op]
+
+
+def registerAdapters():
+    provideAdapter(EmptyCompiler)
+    provideAdapter(SingleCompiler)
+    provideAdapter(UnionCompiler)
+    provideAdapter(IterCompiler)
+    provideAdapter(SelectCompiler)
+    provideAdapter(ReduceCompiler)
+    provideAdapter(RangeCompiler)
+    provideAdapter(MakeCompiler)
+    provideAdapter(IfCompiler)
+    provideAdapter(LambdaCompiler)
+    provideAdapter(ConstantCompiler)
+    provideAdapter(IdentifierCompiler)
+    provideAdapter(BinaryCompiler)
+    provideAdapter(OperatorCompiler)

Modified: Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.txt
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.txt	2008-07-07 16:32:26 UTC (rev 88083)
+++ Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.txt	2008-07-07 17:04:12 UTC (rev 88084)
@@ -11,6 +11,9 @@
 
     >>> from ocql.testing.database import TestMetadata
 
+    >>> from ocql.compiler.compiler import registerAdapters
+    >>> registerAdapters()
+
     >>> metadata = TestMetadata()
     >>> qo = QueryParser("set [ | 1 ]")(metadata)
     >>> opt = QueryOptimizer(qo)()
@@ -28,7 +31,7 @@
     >>> aopt = AlgebraOptimizer(alg)()
     >>> run = AlgebraCompiler(aopt)(metadata, alg)
     >>> print str(run)
-    RunnableQuery: set.union(set([1]),set([2]))
+    RunnableQuery: set.union(set([1]), set([2]))
 
 
     ##Differ not implemented
@@ -47,7 +50,7 @@
     >>> aopt = AlgebraOptimizer(alg)()
     >>> run = AlgebraCompiler(aopt)(metadata, alg)
     >>> print str(run)
-    RunnableQuery: reduce(set.union, map(lambda i: set([i]),set(metadata.getAll("ICourse"))) , set())
+    RunnableQuery: reduce(set.union, map(lambda i: set([i]),set(metadata.getAll("ICourse"))), set())
 
 
     ##bag not implemented
@@ -64,4 +67,4 @@
     >>> aopt = AlgebraOptimizer(alg)()
     >>> run = AlgebraCompiler(aopt)(metadata, alg)
     >>> print str(run)
-    RunnableQuery: reduce(set.union, map(lambda c: set([c.code]),set(metadata.getAll("ICourse"))) , set())
\ No newline at end of file
+    RunnableQuery: reduce(set.union, map(lambda c: set([c.code]),set(metadata.getAll("ICourse"))), set())
\ No newline at end of file

Modified: Sandbox/adamg/ocql/trunk/src/ocql/interfaces.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/interfaces.py	2008-07-07 16:32:26 UTC (rev 88083)
+++ Sandbox/adamg/ocql/trunk/src/ocql/interfaces.py	2008-07-07 17:04:12 UTC (rev 88084)
@@ -69,10 +69,20 @@
     as python objects
     """
 
+class IAlgebraObjectHead(Interface):
+    """Represents head of the algebra object tree
+    """
+    tree = Attribute('holds the root of the algebra object tree')
+
+    def walk(self):
+        """Iterate the Algebra object tree"""
+
 class IAlgebraObject(Interface):
     """Objects providing this interface represent the
     rewritten ObjectQuery to Algebra objects
     """
+    def walk(self):
+        """Iterate the Algebra object tree"""
 
 class IOptimizedAlgebraObject(Interface):
     """Objects providing this interface represent the

Modified: Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py	2008-07-07 16:32:26 UTC (rev 88083)
+++ Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py	2008-07-07 17:04:12 UTC (rev 88084)
@@ -13,103 +13,70 @@
 from zope.interface import implements
 
 from ocql.interfaces import IAlgebraObject
+from ocql.interfaces import IAlgebraObjectHead
+from ocql.rewriter.interfaces import *
+from zope.location import Location, locate
 
-class Algebra:
-    """Signature definition of Algebra operation classes.
-    shall be moved to an IF later
-    """
-    #TODO: this is dirty here, at the end we'll need to have a tree of
-    #Algebra's whose topmost element will only get this IF
-    implements(IAlgebraObject)
+class Head(Location):
+    implements(IAlgebraObjectHead)
 
-    def compile(self):
-        """Return the compiled python code"""
+    def __init__(self, tree):
+        name = 'head'
+        self.tree = tree
 
-    def walk(self):
-        """Iterate the Algebra object tree"""
+    def __repr__(self):
+        return ('%s') % (self.tree)
 
-class BaseAlgebra(Algebra):
-    pass
 
+class BaseAlgebra(Location):
+    implements(IAlgebraObject)
+    children = []
+
+    def walk(self):
+        yield self
+        for child in self.children:
+            for t in child.walk():
+                yield t
+
 class Empty(BaseAlgebra):
-    """
-    >>> Empty(set,None).compile()
-    'set()'
-    >>> Empty(list,None).compile()
-    '[]'
-    """
+
+    implements(IEmpty)
+
     def __init__(self, klass, expr):
         self.klass = klass
 
-    def compile(self):
-        if self.klass==set:
-            return 'set()'
-        elif self.klass==list:
-            return '[]'
-
     def __repr__(self):
         return 'Empty(%s)'%(self.klass)
 
-    def walk(self):
-        yield self
 
 class Single(BaseAlgebra):
-    """
-    >>> Single(set,Constant('c')).compile()
-    'set([c])'
-    >>> Single(list,Constant('c')).compile()
-    '[c]'
-    """
 
+    implements(ISingle)
+
     def __init__(self, klass, expr):
         self.klass = klass
         self.expr = expr
+        locate(expr, self, 'expr')
+        self.children.extend([klass, expr])
 
-    def compile(self):
-        if self.klass==set:
-            return 'set(['+self.expr.compile()+'])'
-        elif self.klass==list:
-            return '['+self.expr.compile()+']'
-
     def __repr__(self):
         return 'Single(%s,%s)'%(self.klass, self.expr)
 
-    def walk(self):
-        yield self
-        for t in self.expr.walk():
-            yield t
-
 class Union(BaseAlgebra):
-    """
-    >>> Union(set,Empty(set,None),Single(set,Identifier('c'))).compile()
-    'set.union(set(),set([c]))'
-    >>> Union(list,Empty(list,None),Single(list,Identifier('c'))).compile()
-    '([])+([c])'
-    """
+
+    implements(IUnion)
+
     def __init__(self, klass, coll1, coll2):
         self.klass=klass
         self.coll1=coll1
         self.coll2=coll2
+        locate(coll1, self, 'coll1')
+        locate(coll2, self, 'coll2')
+        self.children.extend([coll1, coll2])
 
-    def compile(self):
-        if self.klass==set:
-            return 'set.union(%s,%s)' % (
-                self.coll1.compile(),
-                self.coll2.compile())
-        elif self.klass==list:
-            return '(%s)+(%s)'%(
-                self.coll1.compile(),
-                self.coll2.compile())
-
     def __repr__(self):
         return 'Union(%s,%s,%s)'%(self.klass, self.coll1, self.coll2)
 
-    def walk(self):
-        yield self
-        for t in self.coll1.walk():
-            yield t
-        for t in self.coll2.walk():
-            yield t
 
 #class Differ:
 #    def __init__(self, klass, start, end):
@@ -125,102 +92,56 @@
 
 
 class Iter(BaseAlgebra):
+
+    implements(IIter)
+
     def __init__(self, klass, func, coll):
         self.klass = klass
         self.func = func
         self.coll = coll
+        locate(func, self, 'func')
+        locate(coll, self, 'coll')
+        self.children.extend([func,coll])
 
-    def compile(self):
-        if self.func is Lambda and \
-            self.coll is Collection and \
-            self.func.expr is If:
-
-            # You can place here some specialized code...
-            if self.klass == set:
-                return 'reduce(set.union, map(%s,%s) , set())' % \
-                    (self.func.compile(), self.coll.compile())
-            if self.klass == list:
-                return 'reduce(operator.add, map(%s,%s) , [])' % \
-                    (self.func.compile(), self.coll.compile())
-        else:
-            if self.klass == set:
-                return 'reduce(set.union, map(%s,%s) , set())' % \
-                    (self.func.compile(), self.coll.compile())
-            if self.klass == list:
-                return 'reduce(operator.add, map(%s,%s) , [])' % \
-                    (self.func.compile(), self.coll.compile())
-
     def __repr__(self):
         return "Iter(%s,%s,%s)"%(self.klass, self.func, self.coll)
 
-    def walk(self):
-        yield self
-        for t in self.func.walk():
-            yield t
-        for t in self.coll.walk():
-            yield t
 
 class Select(BaseAlgebra):
+
+    implements(ISelect)
+
     def __init__(self, klass, func, coll):
         self.klass = klass
         self.func = func
         self.coll = coll
+        locate(func, self, 'func')
+        locate(coll, self, 'coll')
+        self.children.extend([func,coll])
 
-    def compile(self):
-        if self.klass == set:
-            return 'set(filter(%s,%s))' % (
-                self.func.compile(),
-                self.coll.compile())
-        if self.klass == list:
-            return 'filter(%s,%s)' % (
-                self.func.compile(),
-                self.coll.compile())
-
     def __repr__(self):
         return "Select(%s,%s,%s)"%(self.klass, self.func, self.coll)
 
-    def walk(self):
-        yield self
-        for t in self.func.walk():
-            yield t
-        for t in self.coll.walk():
-            yield t
 
 class Reduce(BaseAlgebra):
+
+    implements(IReduce)
+
     def __init__(self, klass, expr, func, aggreg, coll):
         self.klass = klass
         self.expr = expr
         self.func = func
         self.aggreg = aggreg
         self.coll = coll
+        locate(expr, self, 'expr')
+        locate(func, self, 'func')
+        locate(aggreg, self, 'aggreg')
+        locate(coll, self, 'coll')
+        self.children.extend([expr, func, aggreg, coll])
 
-    def compile(self):
-        if self.klass == set:
-            return 'reduce(%s,map(%s,%s),%s)' % (
-                self.aggreg.compile(),
-                self.func.compile(),
-                self.coll.compile(),
-                self.expr.compile())
-        if self.klass == list:
-            return 'reduce(%s,map(%s,%s),%s)' % (
-                self.aggreg.compile(),
-                self.func.compile(),
-                self.coll.compile(),
-                self.expr.compile())
-
     def __repr__(self):
         return "Reduce(%s,%s,%s,%s,%s)"%(self.klass, self.expr, self.func, self.aggreg, self.coll)
 
-    def walk(self):
-        yield self
-        for t in self.expr.walk():
-            yield t
-        for t in self.func.walk():
-            yield t
-        for t in self.aggreg.walk():
-            yield t
-        for t in self.coll.walk():
-            yield t
 
 #class Equal:
 #    def __init__(self, klass, coll1, coll2):
@@ -235,169 +156,121 @@
 #            return 'filter(%s,%s)' % (self.coll1.compile(),self.coll2.compile())
 #
 class Range(BaseAlgebra):
-    def __init__(self, klass, start, enf):
+
+    implements(IRange)
+
+    def __init__(self, klass, start, end):
         self.klass = klass
         self.start = start
         self.end = end
+        locate(start, self, 'start')
+        locate(end, self, 'end')
+        self.children.extend([start, end])
 
-    def compile(self):
-        if self.klass == set:
-            return 'set(range(%s,%s))' % (
-                self.start.compile(),
-                self.end.compile())
-        if self.klass == list:
-            return 'range(%s,%s)' % (
-                self.start.compile(),
-                self.end.compile())
 
-    def walk(self):
-        yield self
-        for t in self.start.walk():
-            yield t
-        for t in self.end.walk():
-            yield t
-
-
 #class Index
 
 class Make(BaseAlgebra):
+
+    implements(IMake)
+
     def __init__(self, coll1, coll2, expr):
         self.expr = expr
         self.coll1 = coll1
         self.coll2 = coll2
+        locate(expr, self, 'expr')
+#        locate(coll1, self, 'coll1')
+#        locate(coll2, self, 'coll2')
+        self.children.append(expr)
 
-    def compile(self):
-        #TODO: no conversion??? or you just didn't know the from-to?
-        return '%s(metadata.getAll("%s"))' % (
-            self.coll1.__name__,
-            self.expr.compile())
-
     def __repr__(self):
         return "Make(%s,%s,%s)" %(self.coll1, self.coll2, self.expr)
 
-    def walk(self):
-        yield self
-        for t in self.expr.walk():
-            yield t
 
 #class And:
 #class Being:
 
 class If(BaseAlgebra):
+
+    implements(IIf)
+
     def __init__(self, cond, expr1, expr2):
         self.cond = cond
         self.expr1 = expr1
         self.expr2 = expr2
+        locate(cond, self, 'cond')
+        locate(expr1, self, 'expr1')
+        locate(expr2, self, 'expr2')
+        self.children.extend([cond, expr1, expr2])
 
-    def compile(self):
-        #TODO: is this 100%?
-        return '((%s) and (%s) or (%s))' % (
-            self.cond.compile(),
-            self.expr1.compile(),
-            self.expr2.compile())
-
     def __repr__(self):
         return "If(%s,%s,%s)" % (self.cond, self.expr1, self.expr2)
 
-    def walk(self):
-        yield self
-        for t in self.cond.walk():
-            yield t
-        for t in self.expr1.walk():
-            yield t
-        for t in self.expr2.walk():
-            yield t
 
 #
 #
 #
 class Lambda(BaseAlgebra):
+
+    implements(ILambda)
+
     def __init__(self, var, expr):
         self.var = var
         self.expr = expr
+        locate(expr, self, 'expr')
+        self.children.append(expr)
 
-    def compile(self):
-        return 'lambda %s: %s'%(
-            self.var,
-            self.expr.compile())
-
     def __repr__(self):
         return "Lambda %s: %s" %(self.var, self.expr)
 
-    def walk(self):
-        yield self
-        for t in self.expr.walk():
-            yield t
 
 class Constant(BaseAlgebra):
+
+    implements(IConstant)
+
     def __init__(self, value):
         self.value = value
 
-    def compile(self):
-        return '%s'%(self.value)
-
     def __repr__(self):
         return "`%s`" %(self.value)
 
-    def walk(self):
-        yield self
 
 class Identifier(BaseAlgebra):
+
+    implements(IIdentifier)
+
     def __init__(self, name):
         self.name=name
 
-    def compile(self):
-        return self.name
-
     def __repr__(self):
         return "%s" % self.name
 
-    def walk(self):
-        yield self
-
 class Binary(BaseAlgebra):
+
+    implements(IBinary)
+
     def __init__(self, left, op, right):
         self.left = left
         self.op = op
         self.right = right
+        locate(left, self, 'left')
+        locate(right, self, 'right')
+        self.children.extend([left, right])
 
-    def compile(self):
-        return '%s%s%s' % (self.left.compile(),
-                           self.op.op,
-                           self.right.compile())
-
     def __repr__(self):
         return "%s%s%s" % (self.left, self.op.op, self.right)
 
-    def walk(self):
-        yield self
-        for t in self.left.walk():
-            yield t
-        for t in self.right.walk():
-            yield t
 
 class Operator(BaseAlgebra):
-    ops = {
-            'or': 'operator.or_',
-            'and': 'operator.and_',
-            'not': 'operator.not_',
-            '+': 'operator.add', '-': 'operator.sub',
-            '*': 'operator.mul', '/': 'operator.div',
-            '<': 'operator.lt', '>': 'operator.gt',
-            '<=': 'operator.le', '>=': 'operator.ge',
-            '==': 'operator.eq', '~=': 'operator.ne',
-            }
+
+    implements(IOperator)
+
     def __init__(self, op):
         self.op = op
 
-    def compile(self):
-        return self.ops[self.op]
-
     def __repr__(self):
         return self.op
 
-    def walk(self):
-        yield self
 #class Property:
 #   def __init__(self, left, right):
 #        self.left = left

Copied: Sandbox/adamg/ocql/trunk/src/ocql/rewriter/interfaces.py (from rev 88083, Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/rewriter/interfaces.py)
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/rewriter/interfaces.py	                        (rev 0)
+++ Sandbox/adamg/ocql/trunk/src/ocql/rewriter/interfaces.py	2008-07-07 17:04:12 UTC (rev 88084)
@@ -0,0 +1,128 @@
+# -*- coding: UTF-8 -*-
+
+from ocql.interfaces import IAlgebraObject
+from zope.schema import Dict, Text, Int, TextLine
+from zope.interface import Attribute, Interface
+
+################
+#Algebra operation interfaces
+################
+
+class IEmpty(IAlgebraObject):
+    """Objects providing this interface represent the
+    Empty Algebra object
+    """
+    klass = Attribute('collection type name')
+    expr = Attribute('expression')
+
+
+class ISingle(IAlgebraObject):
+    """Objects providing this interface represent the
+    Single Algebra object
+    """
+    klass = Attribute('collection type name')
+    expr = Attribute('expression')
+
+
+class IUnion(IAlgebraObject):
+    """Objects providing this interface represent the
+    Union Algebra object
+    """
+    klass = Attribute('collection type name')
+    coll1 = Attribute('first collection')
+    coll2 = Attribute('second collection')
+
+
+class IIter(IAlgebraObject):
+    """Objects providing this interface represent the
+    Iter Algebra object
+    """
+    klass = Attribute('collection type name')
+    fun = Attribute('function')
+    coll = Attribute('collection')
+
+
+class ISelect(IAlgebraObject):
+    """Objects providing this interface represent the
+    Select Algebra object
+    """
+    klass = Attribute('collection type name')
+    fun = Attribute('function')
+    coll = Attribute('collection')
+
+
+class IReduce(IAlgebraObject):
+    """Objects providing this interface represent the
+    Reduce Algebra object
+    """
+    klass = Attribute('collection type name')
+    expr = Attribute('expression')
+    fun = Attribute('function')
+    aggreg = Attribute('aggregation')
+    coll = Attribute('collection')
+
+
+class IRange(IAlgebraObject):
+    """Objects providing this interface represent the
+    Range Algebra object
+    """
+    klass = Attribute('collection type name')
+    start = Attribute('range start point')
+    end = Attribute('range end point')
+
+
+class IMake(IAlgebraObject):
+    """Objects providing this interface represent the
+    Make Algebra object
+    """
+    expr = Attribute('expression')
+    coll1 = Attribute('first collection')
+    coll2 = Attribute('second collection')
+
+
+class IIf(IAlgebraObject):
+    """Objects providing this interface represent the
+    If Algebra object
+    """
+    cond = Attribute('condition')
+    expr1 = Attribute('first expression')
+    expr2 =Attribute('second expression')
+
+
+class ILambda(IAlgebraObject):
+    """Objects providing this interface represent the
+    Lambda Algebra object
+    """
+    var = Attribute('variable')
+    expr = Attribute('expression')
+
+
+class IConstant(IAlgebraObject):
+    """Objects providing this interface represent the
+    Constant Algebra object
+    """
+    value = Attribute('constant value')
+
+
+class IIdentifier(IAlgebraObject):
+    """Objects providing this interface represent the
+    Identifier Algebra object
+    """
+    name = Text()
+
+
+class IBinary(IAlgebraObject):
+    """Objects providing this interface represent the
+    Binery Algebra object
+    """
+    left = Attribute('left expression')
+    op = Attribute('operator')
+    right = Attribute('right expression')
+
+
+class IOperator(IAlgebraObject):
+    """Objects providing this interface represent the
+    Operator Algebra object
+    """
+    ops = Dict(Text(), Text())
+    op = Text()

Modified: Sandbox/adamg/ocql/trunk/src/ocql/rewriter/rewriter.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/rewriter/rewriter.py	2008-07-07 16:32:26 UTC (rev 88083)
+++ Sandbox/adamg/ocql/trunk/src/ocql/rewriter/rewriter.py	2008-07-07 17:04:12 UTC (rev 88084)
@@ -11,9 +11,11 @@
 
 from zope.component import adapts
 from zope.interface import implements
+from zope.location import locate
 
 from ocql.interfaces import IRewriter
 from ocql.interfaces import IOptimizedObjectQuery
+from ocql.rewriter.algebra import Head
 
 from ocql.rewriter import algebra as target_algebra
 
@@ -26,4 +28,7 @@
 
     def __call__(self):
         query = self.context
-        return query.rewrite(target_algebra)
+        alg = query.rewrite(target_algebra)
+        head = Head(alg)
+        return head
+    
\ No newline at end of file

Modified: Sandbox/adamg/ocql/trunk/src/ocql/tests/run.txt
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/tests/run.txt	2008-07-07 16:32:26 UTC (rev 88083)
+++ Sandbox/adamg/ocql/trunk/src/ocql/tests/run.txt	2008-07-07 17:04:12 UTC (rev 88084)
@@ -19,7 +19,10 @@
 
     >>> from ocql.engine import OCQLEngine
 
+    >>> from ocql.compiler.compiler import registerAdapters
+    >>> registerAdapters()
 
+
     >>> engine = OCQLEngine()
     >>> run = engine.compile("set [ | 1 ]")
     >>> run
@@ -37,7 +40,7 @@
     >>> engine = OCQLEngine()
     >>> run = engine.compile("set [ | 1 ] union set [|2]")
     >>> run
-    RunnableQuery: set.union(set([1]),set([2]))
+    RunnableQuery: set.union(set([1]), set([2]))
 
     >>> result = run.execute()
     >>> result
@@ -51,7 +54,7 @@
     >>> engine = OCQLEngine()
     >>> run = engine.compile("set [ i in ICourse | i ]")
     >>> run
-    RunnableQuery: reduce(set.union, map(lambda i: set([i]),set(metadata.getAll("ICourse"))) , set())
+    RunnableQuery: reduce(set.union, map(lambda i: set([i]),set(metadata.getAll("ICourse"))), set())
 
     >>> result = run.execute()
     >>> result
@@ -65,7 +68,7 @@
     >>> engine = OCQLEngine()
     >>> run = engine.compile("set [ c in ICourse | c.code ]")
     >>> run
-    RunnableQuery: reduce(set.union, map(lambda c: set([c.code]),set(metadata.getAll("ICourse"))) , set())
+    RunnableQuery: reduce(set.union, map(lambda c: set([c.code]),set(metadata.getAll("ICourse"))), set())
 
     >>> result = run.execute()
     >>> result

Modified: Sandbox/adamg/ocql/trunk/src/ocql/tests/test_old.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/tests/test_old.py	2008-07-07 16:32:26 UTC (rev 88083)
+++ Sandbox/adamg/ocql/trunk/src/ocql/tests/test_old.py	2008-07-07 17:04:12 UTC (rev 88084)
@@ -26,6 +26,8 @@
 from ocql.compiler.compiler import AlgebraCompiler
 from ocql.testing.database import TestMetadata
 
+from ocql.compiler.compiler import registerAdapters
+
 from ocql.testing.database import C1, C2, C3
 from ocql.testing.database import D1, D2, D3
 
@@ -55,6 +57,7 @@
         provideAdapter(AlgebraOptimizer)
         provideAdapter(AlgebraCompiler)
         provideAdapter(TestMetadata)
+        registerAdapters()
 
         self.engine = OCQLEngine()
 

Modified: Sandbox/adamg/ocql/trunk/src/ocql/tests/test_zope.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/tests/test_zope.py	2008-07-07 16:32:26 UTC (rev 88083)
+++ Sandbox/adamg/ocql/trunk/src/ocql/tests/test_zope.py	2008-07-07 17:04:12 UTC (rev 88084)
@@ -7,6 +7,7 @@
 
 from ocql.aoptimizer.aoptimizer import AlgebraOptimizer
 from ocql.compiler.compiler import AlgebraCompiler
+from ocql.compiler.compiler import registerAdapters
 from ocql.database import metadata
 from ocql.database.metadata import Metadata
 from ocql.engine import OCQLEngine
@@ -33,7 +34,7 @@
         provideAdapter(AlgebraOptimizer)
         provideAdapter(AlgebraCompiler)
         provideAdapter(Metadata)
-
+        registerAdapters()
         setupInterfaces(self)
         setupCatalog(self)
 



More information about the Checkins mailing list