[Checkins] SVN: Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/
use zope adapter pattern to do the algebra object compile
Charith Paranaliyanage
paranaliyanage at gmail.com
Wed Jul 2 04:55:26 EDT 2008
Log message for revision 87917:
use zope adapter pattern to do the algebra object compile
Changed:
U Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/compiler/compiler.py
U Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/compiler/compiler.txt
U Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/rewriter/algebra.py
U Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/rewriter/interfaces.py
-=-
Modified: Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/compiler/compiler.py
===================================================================
--- Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/compiler/compiler.py 2008-07-02 01:22:17 UTC (rev 87916)
+++ Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/compiler/compiler.py 2008-07-02 08:55:21 UTC (rev 87917)
@@ -14,7 +14,7 @@
from ocql.interfaces import IAlgebraCompiler
from ocql.interfaces import IOptimizedAlgebraObject
-from ocql.interfaces import ICompiledAlgebraObject
+#from ocql.interfaces import ICompiledAlgebraObject
from ocql.rewriter.interfaces import *
@@ -60,6 +60,176 @@
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.call)(),
+ 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.call)(),
+ 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 BineryCompiler(BaseCompiler):
+ implements(IAlgebraCompiler)
+ adapts(IBinery)
+
+ def __call__(self):
+ return '%s%s%s' % (
+ IAlgebraCompiler(self.context.left)(),
+ IAlgebraCompiler(self.context.op.op)(),
+ IAlgebraCompiler(self.context.right)())
+
+
+class OperatorCompiler(BaseCompiler):
+ implements(IAlgebraCompiler)
+ adapts(IOperator)
+
+ def __call__(self):
+ return self.context.ops[self.context.op]
+
+
def registerAdapters():
provideAdapter(EmptyCompiler)
- provideAdapter(SingleCompiler)
\ No newline at end of file
+ provideAdapter(SingleCompiler)
+ provideAdapter(UnionCompiler)
+ provideAdapter(IterCompiler)
+ provideAdapter(SelectCompiler)
+ provideAdapter(ReduceCompiler)
+ provideAdapter(RangeCompiler)
+ provideAdapter(MakeCompiler)
+ provideAdapter(IfCompiler)
+ provideAdapter(LambdaCompiler)
+ provideAdapter(ConstantCompiler)
+ provideAdapter(IdentifierCompiler)
+ provideAdapter(BineryCompiler)
+ provideAdapter(OperatorCompiler)
+
\ No newline at end of file
Modified: Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/compiler/compiler.txt
===================================================================
--- Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/compiler/compiler.txt 2008-07-02 01:22:17 UTC (rev 87916)
+++ Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/compiler/compiler.txt 2008-07-02 08:55:21 UTC (rev 87917)
@@ -31,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
@@ -50,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
@@ -67,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/branches/alg-compiler/src/ocql/rewriter/algebra.py
===================================================================
--- Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/rewriter/algebra.py 2008-07-02 01:22:17 UTC (rev 87916)
+++ Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/rewriter/algebra.py 2008-07-02 08:55:21 UTC (rev 87917)
@@ -23,9 +23,6 @@
#Algebra's whose topmost element will only get this IF
implements(IAlgebraObject)
- def compile(self):
- """Return the compiled python code"""
-
def walk(self):
"""Iterate the Algebra object tree"""
@@ -33,24 +30,12 @@
pass
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)
@@ -58,12 +43,6 @@
yield self
class Single(BaseAlgebra):
- """
- >>> Single(set,Constant('c')).compile()
- 'set([c])'
- >>> Single(list,Constant('c')).compile()
- '[c]'
- """
implements(ISingle)
@@ -71,12 +50,6 @@
self.klass = klass
self.expr = 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)
@@ -86,12 +59,6 @@
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)
@@ -100,16 +67,6 @@
self.coll1=coll1
self.coll2=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)
@@ -142,26 +99,6 @@
self.func = func
self.coll = 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)
@@ -181,16 +118,6 @@
self.func = func
self.coll = 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)
@@ -212,20 +139,6 @@
self.aggreg = aggreg
self.coll = 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)
@@ -261,16 +174,6 @@
self.start = start
self.end = 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():
@@ -290,12 +193,6 @@
self.coll1 = coll1
self.coll2 = coll2
- 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)
@@ -316,13 +213,6 @@
self.expr1 = expr1
self.expr2 = 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)
@@ -346,11 +236,6 @@
self.var = var
self.expr = expr
- def compile(self):
- return 'lambda %s: %s'%(
- self.var,
- self.expr.compile())
-
def __repr__(self):
return "Lambda %s: %s" %(self.var, self.expr)
@@ -366,9 +251,6 @@
def __init__(self, value):
self.value = value
- def compile(self):
- return '%s'%(self.value)
-
def __repr__(self):
return "`%s`" %(self.value)
@@ -382,9 +264,6 @@
def __init__(self, name):
self.name=name
- def compile(self):
- return self.name
-
def __repr__(self):
return "%s" % self.name
@@ -400,11 +279,6 @@
self.op = op
self.right = 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)
@@ -432,14 +306,12 @@
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
Modified: Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/rewriter/interfaces.py
===================================================================
--- Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/rewriter/interfaces.py 2008-07-02 01:22:17 UTC (rev 87916)
+++ Sandbox/adamg/ocql/branches/alg-compiler/src/ocql/rewriter/interfaces.py 2008-07-02 08:55:21 UTC (rev 87917)
@@ -1,7 +1,7 @@
# -*- coding: UTF-8 -*-
from ocql.interfaces import IAlgebraObject
-from zope.schema import Dict, Text
+from zope.schema import Dict, Text, Int
from zope.interface import Attribute
################
@@ -61,47 +61,68 @@
aggreg = Attribute('aggregation')
coll = Attribute('collection')
-
-class IOperator(IAlgebraObject):
- """Objects providing this interface represent the
- Operator Algebra object
- """
- ops = Dict(Text(), Text())
- op = Text()
-
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 IBinery(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()
More information about the Checkins
mailing list