[Checkins] SVN: Sandbox/ocql-foliage/ initial commit, still not working

Adam Groszer agroszer at gmail.com
Mon Sep 24 11:56:41 EDT 2007


Log message for revision 79885:
  initial commit, still not working

Changed:
  A   Sandbox/ocql-foliage/doc/
  A   Sandbox/ocql-foliage/doc/schema.py
  A   Sandbox/ocql-foliage/src/
  A   Sandbox/ocql-foliage/src/ocql/
  A   Sandbox/ocql-foliage/src/ocql/__init__.py
  A   Sandbox/ocql-foliage/src/ocql/engine/
  A   Sandbox/ocql-foliage/src/ocql/engine/__init__.py
  A   Sandbox/ocql-foliage/src/ocql/engine/algebra.py
  A   Sandbox/ocql-foliage/src/ocql/engine/algebracompiler.py
  A   Sandbox/ocql-foliage/src/ocql/engine/algebraoptimizer.py
  A   Sandbox/ocql-foliage/src/ocql/engine/metadata.py
  A   Sandbox/ocql-foliage/src/ocql/engine/queryobject.py
  A   Sandbox/ocql-foliage/src/ocql/engine/queryoptimizer.py
  A   Sandbox/ocql-foliage/src/ocql/engine/queryparser.py
  A   Sandbox/ocql-foliage/src/ocql/engine/rewriter.py
  A   Sandbox/ocql-foliage/src/ocql/engine/runnablequery.py
  A   Sandbox/ocql-foliage/src/ocql/ocqlengine.py
  A   Sandbox/ocql-foliage/src/temp.py
  A   Sandbox/ocql-foliage/src/test.py
  A   Sandbox/ocql-foliage/src/testalgebra.py
  A   Sandbox/ocql-foliage/src/testdb.py

-=-
Added: Sandbox/ocql-foliage/doc/schema.py
===================================================================
--- Sandbox/ocql-foliage/doc/schema.py	                        (rev 0)
+++ Sandbox/ocql-foliage/doc/schema.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,107 @@
+# -*- coding: UTF-8 -*-
+
+from zope.interface import Interface
+from zope.schema import TextLine, Int, Choice, Set, List
+
+class IAddress(Interface):
+    street = TextLine(
+        title=u"Street address",
+        required=True
+        )
+
+class IDepartment(Interface):
+    address = Choice(
+        title=u"Street address",
+        vocabulary="vocab_of_IAddress",
+        required=True
+        )
+
+class ICourse(Interface):
+    code = TextLine(
+        title=u"Course code",
+        required=True
+        )
+    runBy = Set(
+        title=u"Run by",
+        value_type = Choice(
+            title=u"Department",
+            vocabulary="vocab_of_IDepartment",
+            )
+        )
+    prerequisites = Set(
+        title=u"Prerequisite courses",
+        value_type = Choice(
+            title=u"Course",
+            vocabulary="vocab_of_ICourse",
+            )
+        )
+    salary = Int(
+        title=u"Credits",
+        )
+    assessment = List(
+        title=u"Assessment",
+        value_type = Int(
+            title=u"Assessment",
+            )
+        )
+
+class IPerson(Interface):
+    name = TextLine(
+        title=u"Name",
+        required=True
+        )
+
+class IStaff(IPerson):
+    department = Choice(
+        title=u"Department",
+        vocabulary="vocab_of_IDepartment",
+        )
+    teaches = Set(
+        title=u"Teaches",
+        value_type = Choice(
+            title=u"Course",
+            vocabulary="vocab_of_ICourse",
+            )
+        )
+    salary = Int(
+        title=u"Name",
+        )
+
+class IStudent(IPerson):
+    major = Choice(
+        title=u"Department",
+        vocabulary="vocab_of_IDepartment",
+        )
+    supervisedBy = List(
+        title=u"Supervised by",
+        value_type = Choice(
+            title=u"Member of staff",
+            vocabulary="vocab_of_IStaff",
+            )
+        )
+    takes = Set(
+        title=u"Takes",
+        value_type = Choice(
+            title=u"Course",
+            vocabulary="vocab_of_ICourse",
+            )
+        )
+
+class ITutor(IStaff, IStudent):
+    #salary = Int(
+    #    title=u"Name",
+    #    required=True
+    #    )
+    pass
+
+class IVisitingStaff(IStaff):
+    pass
+
+catalog.addValueIndex(IDepartment['address'], multiple=False)
+catalog.addValueIndex(ICourse['runBy'], multiple=True)
+catalog.addValueIndex(ICourse['prerequisites'], multiple=True)
+catalog.addValueIndex(IStudent['major'], multiple=False)
+catalog.addValueIndex(IStudent['supervisedBy'], multiple=True)
+catalog.addValueIndex(IStudent['takes'], multiple=True)
+catalog.addValueIndex(IStaff['teaches'], multiple=True)
+catalog.addValueIndex(IStaff['department'], multiple=False)
\ No newline at end of file


Property changes on: Sandbox/ocql-foliage/doc/schema.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/__init__.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/__init__.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/__init__.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1 @@
+from ocqlengine import OCQLEngine
\ No newline at end of file


Property changes on: Sandbox/ocql-foliage/src/ocql/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/engine/__init__.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/engine/__init__.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/engine/__init__.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1 @@
+#
\ No newline at end of file


Property changes on: Sandbox/ocql-foliage/src/ocql/engine/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/engine/algebra.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/engine/algebra.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/engine/algebra.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,6 @@
+class Algebra:
+    def compile(self):
+        """Returns the compiled python code"""
+         
+    def walk(self):
+        """Iterator on the tree"""


Property changes on: Sandbox/ocql-foliage/src/ocql/engine/algebra.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/engine/algebracompiler.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/engine/algebracompiler.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/engine/algebracompiler.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,17 @@
+class AlgebraCompiler:
+    """
+    Common compiler methods
+    """
+    def __init__(self, engine):
+        self.engine = engine
+    
+    def get_algebra(self):
+        pass
+    
+    def compile(self, alg):
+        print alg
+        #from pub.dbgpclient import brk; brk()
+
+        code = alg.compile()
+        print code
+        return compile(code,'','eval')
\ No newline at end of file


Property changes on: Sandbox/ocql-foliage/src/ocql/engine/algebracompiler.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/engine/algebraoptimizer.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/engine/algebraoptimizer.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/engine/algebraoptimizer.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,31 @@
+class EliminateMake:
+    def __init__(self, algebra):
+        self.algebra = algebra
+    
+    def applicable(self, alg):
+        return isinstance(alg,self.algebra.Make) and \
+            alg.coll1==alg.coll2 
+    
+    def __call__(self, alg):
+        if self.applicable(alg):
+            return alg.expr
+
+class AlgebraOptimizer:
+    def __init__(self, engine):
+        self.engine = engine
+    
+    def optimize(self, alg):
+        return alg
+
+#        algebra = self.engine.algebra
+#        return algebra.Iter(set,
+#            algebra.If(algebra.Eq(algebra.Identifier('d.name'),algebra.Identifier('"Computing Science"')),
+#                algebra.Select(set, algebra.Lambda('c',
+#                    algebra.And(algebra.Reduce(set, algebra.Identifier('False'), 
+#                               algebra.Lambda('d',algebra.Eq(algebra.Identifier('d'),algebra.Identifier('i'))), 
+#                                      algebra.Identifier('or'), algebra.Identifier('c.runBy')),
+#                        algebra.Le(algebra.Identifier('1'),algebra.Identifier('c.credits')),
+#                        algebra.Le(algebra.Identifier('c.credits'),algebra.Identifier('3')))),
+#                    algebra.Collection('ICurses')),
+#                algebra.Empty(set,None)), 
+#            algebra.Collection('IDepartments'))


Property changes on: Sandbox/ocql-foliage/src/ocql/engine/algebraoptimizer.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/engine/metadata.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/engine/metadata.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/engine/metadata.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,28 @@
+class MetaType:
+    def get_property(self, name):
+        """
+            Get type information and metadata for property.
+            Returns a MetaType
+        """
+    
+    def is_collection(self):
+        """Returns True if the represented type is a collection."""
+    
+    def get_type(self):
+        """Returns the represented type"""
+    
+    def get_contained(self):
+        """
+            Throws an exception if represented type is not a collection, or
+            the contained type if a collection
+        """
+    
+    def get_size(self):
+        """Returns the size of the collection or class if known"""
+
+class Metadata:
+    def __init__(self, engine):
+        self.engine = engine
+    
+    def get_class(self, classname):
+        """Returns a MetaType instance for the class."""
\ No newline at end of file


Property changes on: Sandbox/ocql-foliage/src/ocql/engine/metadata.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/engine/queryobject.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/engine/queryobject.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/engine/queryobject.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,247 @@
+class NotImplemented(Exception):
+    pass
+
+class QueryObject:
+    def rewrite(self, algebra):
+        raise NotImplemented(self) 
+
+class Term:
+    pass
+        
+class Expression(Term, QueryObject):
+    pass
+
+#
+# General
+# 
+class hasClassWith(Expression):
+    def __init__(self, expr, klass, conditional):
+        self.expression = expression
+        self.klass = klass
+        self.conditional = conditional
+
+class Identifier(Expression):
+    def __init__(self, name):
+        self.name = name
+
+    def rewrite(self, algebra):
+        return algebra.Identifier(self.name)
+
+class Constant(Expression):
+    def __init__(self, value):
+        self.value=value
+    
+    def rewrite(self, algebra):
+        return algebra.Constant(self.value)
+
+class StringConstant(Constant):
+    pass
+
+class NumericConstant(Constant):
+    pass
+
+class BooleanConstant(Constant):
+    pass
+   
+class Query(Expression):
+    def __init__(self, collection, terms, target):
+        self.collection = collection
+        self.terms = terms
+        self.target = target
+    
+    def rewrite(self, algebra):
+        if len(self.terms):
+            ft = self.terms[0]
+            if isinstance(ft,In):
+                return algebra.Iter(self.collection,
+                    algebra.Lambda(ft.identifier.name,
+                        Query(self.collection,self.terms[1:],self.target).rewrite(algebra)
+                    ), algebra.Make(self.collection,set,ft.expression.rewrite(algebra)) # FIXME: ?set? 
+ 
+                )
+            elif isinstance(ft,Alias):
+                return Query(self.collection, [In(ft.identifier,ft.expression)]+self.terms[1:], 
+                             self.target).rewrite(algebra)
+            else:
+                return algebra.If(
+                    ft.rewrite(algebra),
+                    Query(self.collection,self.terms[1:],self.target).rewrite(algebra),
+                    algebra.Empty(self.collection,None)
+                )
+        else:
+            return algebra.Single(self.collection,self.target.rewrite(algebra))
+            
+
+class In(Term):
+    def __init__(self, identifier, expression):
+        self.identifier = identifier
+        self.expression = expression
+
+class Alias(Term):
+    def __init__(self, identifier, expression):
+        self.identifier = identifier
+        self.expression = expression
+
+#
+# Binary operators
+#
+class Binary(Expression):
+    def __init__(self, left, right):
+        self.left = left
+        self.right = right
+
+    def rewrite(self, algebra):
+        return algebra.Binary(self.left.rewrite(algebra),self.get_operator(algebra),self.right.rewrite(algebra))
+
+# Sets and properties
+class Union(Binary):
+    def rewrite(self, algebra):
+        algebra.Union(left.rewrite(algebra),right.rewrite(algebra))
+
+class Differ(Binary):
+    def rewrite(self, algebra):
+        algebra.Differ(left.rewrite(algebra),right.rewrite(algebra))
+
+class And(Binary):
+    def get_operator(self, algebra):
+        return algebra.Operator('and')
+
+class Or(Binary):
+    def get_operator(self, algebra):
+        return algebra.Operator('or')
+
+class Property(Binary):
+    def rewrite(self, algebra): # FIXME: Ezt gondold at...
+        return algebra.Identifier('.'.join([self.left.name,self.right.name]))
+
+class Index(Binary):
+    pass
+
+
+# Arithmetic operators
+class Arithmetic(Binary):
+    pass
+
+class Add(Arithmetic):
+    def get_operator(self,algebra):
+        return algebra.Operator('+')
+
+class Mul(Arithmetic):
+    def get_operator(self,algebra):
+        return algebra.Operator('*')
+
+class Sub(Arithmetic):
+    def get_operator(self,algebra):
+        return algebra.Operator('-')
+
+class Div(Arithmetic):
+    def get_operator(self,algebra):
+        return algebra.Operator('/')
+
+#
+# Unary operators
+#
+class Unary(Expression):
+    def __init__(self, expression):
+        self.expression = expression
+
+class Not(Unary):
+    def rewrite(self, algebra):
+        return algebra.Not(self.expression.rewrite(algebra))
+
+class Aggregate(Unary):
+    pass
+
+class Count(Aggregate):
+    def rewrite(self, algebra):
+        return algebra.reduce(
+            bag, # FIXME milyen bag
+            0,
+            algebra.Lambda('i',algebra.Constant(1)),
+            algebra.Operator('+'), 
+            make(bag,set,self.expression.rewrite(algebra)) # FIXME ?set?
+        )
+
+class Sum(Aggregate):
+    pass
+
+#
+# Conditional
+#
+class Quantor:
+    def rewrite(self, algebra, expression):
+        raise NotImplemented()
+
+class Quanted(QueryObject):
+    def __init__(self, quantor, expression):
+        self.quantor = quantor
+        self.expression = expression
+    
+    def rewrite(self, algebra):
+        return self.quantor.rewrite(algebra,self.expression)
+
+class Condition(Expression):
+    def __init__(self, left, right):
+        self.left = left
+        self.right = right
+
+    def rewrite(self, algebra):
+        return algebra.Binary(self.left.rewrite(algebra),self.get_operator(algebra),self.right.rewrite(algebra))
+
+# Quantors
+class Every(Quantor):
+    pass
+
+class Some(Quantor):
+    def rewrite(self, algebra, expression):
+        return algebra.Reduce(
+            set, # FIXME ?set?
+            algebra.Identifier('False'),
+            algebra.Lambda('i',
+                expression.__class__(
+                    Identifier('i'),
+                    expression.right
+                ).rewrite(algebra)
+            ),
+            algebra.Operator('or'),
+            expression.left.rewrite(algebra)
+        )
+
+class Atmost(Quantor):
+    def __init__(self, expr):
+        self.expr = expr
+
+class Atleast(Quantor):
+    def __init__(self, expr):
+        self.expr = expr
+
+class Just(Quantor):
+    def __init__(self, expr):
+        self.expr = expr
+
+# Logical operators
+class Eq(Condition):
+    def get_operator(self,algebra):
+        return algebra.Operator('==')
+
+class Ne(Condition):
+    def get_operator(self,algebra):
+        return algebra.Operator('!=')
+
+class Lt(Condition):
+    def get_operator(self,algebra):
+        return algebra.Operator('<')
+
+class Gt(Condition):
+    def get_operator(self,algebra):
+        return algebra.Operator('>')
+
+class Le(Condition):
+    def get_operator(self,algebra):
+        return algebra.Operator('<=')
+
+class Ge(Condition):
+    def get_operator(self,algebra):
+        return algebra.Operator('>=')
+
+# TODO: missing xi{Es} xi{E1..E2} I(Es) K ==, ~==
\ No newline at end of file


Property changes on: Sandbox/ocql-foliage/src/ocql/engine/queryobject.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/engine/queryoptimizer.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/engine/queryoptimizer.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/engine/queryoptimizer.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,6 @@
+class QueryOptimizer:
+    def __init__(self, engine):
+        self.engine = engine
+        
+    def optimize(self, query):
+        return query
\ No newline at end of file


Property changes on: Sandbox/ocql-foliage/src/ocql/engine/queryoptimizer.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/engine/queryparser.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/engine/queryparser.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/engine/queryparser.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,29 @@
+class QueryParser:
+    def __init__(self, engine):
+        self.metadata = engine.metadata
+        
+    def parse(self, query):
+        queryobject = self.compile(query)
+        self.check(queryobject)
+        return queryobject
+    
+    def check(self, queryobject):
+        return True
+    
+    def compile(self, query):
+        from ocql.engine.queryobject import *
+        
+        return \
+            Query(
+                set,
+                [
+                    In(Identifier('c'),Identifier('ICurses')),
+                    In(Identifier('d'),Identifier('IDepartments')),
+                    Eq(Property(Identifier('d'),Identifier('name')),Constant('"Computing Science"')),
+                    Eq(Identifier('d'),Quanted(Some(),Property(Identifier('c'),Identifier('runBy')))),
+                    Le(Property(Identifier('c'),Identifier('credits')),Constant('3')),
+                    Le(Constant('1'),Property(Identifier('c'),Identifier('credits'))),
+                ],
+                Identifier('c')
+            )
+        
\ No newline at end of file


Property changes on: Sandbox/ocql-foliage/src/ocql/engine/queryparser.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/engine/rewriter.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/engine/rewriter.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/engine/rewriter.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,6 @@
+class Rewriter:
+    def __init__(self, engine):
+        self.engine = engine
+    
+    def rewrite(self, query):
+        return query.rewrite(self.engine.algebra)
\ No newline at end of file


Property changes on: Sandbox/ocql-foliage/src/ocql/engine/rewriter.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/engine/runnablequery.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/engine/runnablequery.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/engine/runnablequery.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,22 @@
+class RunnableQuery:
+    """
+        metadata: ocql.metadata instance
+        alg: algebra object
+    """
+    def __init__(self, engine, alg):
+        self.engine = engine
+        self.alg = alg
+        self.reanalyze()
+    
+    def reanalyze(self):
+        self.code = self.engine.compile_algebra(self.alg)
+    
+    def execute(self):
+        metadata = self.engine.metadata
+        
+        return reduce(set.union, map(lambda c: reduce(set.union, map(lambda d: ((d.name=="Computing Science") and (((d==set(filter(lambda i: i.runBy,c))) and (((c.credits<=3) and (((1<=c.credits) and (set([c])) or (set()))) or (set()))) or (set()))) or (set())),set(metadata.getAll("IDepartments"))) , set()),set(metadata.getAll("ICurses"))) , set())
+        
+        #return eval(self.code,
+        #            {},
+        #            {'metadata': self.engine.metadata})
+        return eval(self.code)


Property changes on: Sandbox/ocql-foliage/src/ocql/engine/runnablequery.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/ocql/ocqlengine.py
===================================================================
--- Sandbox/ocql-foliage/src/ocql/ocqlengine.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/ocql/ocqlengine.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,61 @@
+from ocql.engine.runnablequery import RunnableQuery
+
+class EngineFactory:
+    def __init__(self, engine):
+        self.engine = engine
+        
+    def get_query_optimizer(self):
+        from ocql.engine.queryoptimizer import QueryOptimizer
+        return QueryOptimizer(self.engine)
+    
+    def get_rewriter(self):
+        from ocql.engine.rewriter import Rewriter
+        return Rewriter(self.engine)
+    
+    def get_query_parser(self):
+        from ocql.engine.queryparser import QueryParser
+        return QueryParser(self.engine)
+
+    def get_algebra_optimizer(self):
+        from ocql.engine.algebraoptimizer import AlgebraOptimizer
+        return AlgebraOptimizer(self.engine)
+    
+    def get_metadata(self):
+        from testdb import TestMetadata
+        return TestMetadata(self.engine)
+        
+    def get_algebra_compiler(self):
+        from ocql.engine.algebracompiler import AlgebraCompiler
+        return AlgebraCompiler(self.engine)
+    
+    def get_algebra(self):
+        import testalgebra
+        return testalgebra
+    
+class OCQLEngine:
+    def __init__(self):
+        factory = EngineFactory(self)
+        
+        self.metadata = factory.get_metadata()
+        self.algebra_compiler = factory.get_algebra_compiler() 
+        self.algebra_optimizer = factory.get_algebra_optimizer()
+        self.query_parser = factory.get_query_parser() 
+        self.query_optimizer = factory.get_query_optimizer()
+        self.rewriter = factory.get_rewriter()
+        
+        self.algebra = factory.get_algebra()
+    
+    def compile(self, query):
+        return RunnableQuery(self, self.compile_query(query))
+
+    def compile_query(self, query):
+        return self.rewriter.rewrite(
+                   self.query_optimizer.optimize(
+                       self.query_parser.parse(query)
+                   )
+               )
+
+    def compile_algebra(self, alg):
+        return self.algebra_compiler.compile(
+                   self.algebra_optimizer.optimize(alg)
+               )


Property changes on: Sandbox/ocql-foliage/src/ocql/ocqlengine.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/temp.py
===================================================================
--- Sandbox/ocql-foliage/src/temp.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/temp.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,83 @@
+# -*- coding: UTF-8 -*-
+
+from testdb import TestMetadata
+
+metadata = TestMetadata(None)
+
+#reduce(set.union,
+#    map(lambda c: reduce(set.union,
+#        map(lambda d: (
+#            (d.name=="Computing Science") and
+#            (((d==set(filter(lambda i: i.runBy,c))) and
+#                (((c.credits<=3) and (((1<=c.credits) and
+#                    (set([c])) or (set()))) or (set()))) or
+#                (set()))) or (set())),
+#            set(metadata.getAll("IDepartments"))) ,
+#        set()),set(metadata.getAll("ICurses"))) ,
+#    set())
+
+
+#map(lambda c: reduce(set.union,
+#    map(lambda d: ((d.name=="Computing Science")),
+#        set(metadata.getAll("IDepartments"))), set()),set(metadata.getAll("ICurses")))
+
+#x = reduce(set.union,
+#    map(lambda c: reduce(set.union,
+#        map(lambda d: (
+#            (d.name=="Computing Science")
+#            and (d==set(filter(lambda i: i.runBy,c)))
+#            and (set([c]))
+#            or (set())) ,
+#            set(metadata.getAll("IDepartments"))) ,
+#        set()),set(metadata.getAll("ICurses"))) ,
+#    set())
+
+#x = reduce(set.union,
+#    map(lambda c: reduce(set.union,
+#        map(lambda d: (
+#            (d.name=="Computing Science")
+#            and (set([c]))
+#            or (set())) ,
+#            set(metadata.getAll("IDepartments"))) ,
+#        set()),set(metadata.getAll("ICurses"))) ,
+#    set())
+
+x=reduce(set.union,
+    map(lambda c: reduce(set.union,
+        map(lambda d: (
+            (d.name=="Computing Science") and
+            (((d==set(filter(lambda i: i.runBy,[c]))) and
+                (
+                    (
+                        (c.credits<=3)
+                        and (
+                                (
+                                    (1<=c.credits)
+                                    and
+                                    (set([c]))
+                                    or (set())
+                                )
+                            ) or (set())
+                    )
+                ) or
+                (set()))) or (set())),
+            set(metadata.getAll("IDepartments"))) ,
+        set()),set(metadata.getAll("ICurses"))) ,
+    set())
+
+
+x = reduce(set.union,
+    map(lambda c: reduce(set.union,
+        map(lambda d: (
+            (d.name=="Computing Science") and
+            (((d==set(filter(lambda i: i.runBy,[c]))) and
+                ((((
+                    (set([c])) or (set()))) or (set()))) or
+                (set()))) or (set())),
+            set(metadata.getAll("IDepartments"))) ,
+        set()),set(metadata.getAll("ICurses"))) ,
+    set())
+
+
+for i in x:
+    print i.name
\ No newline at end of file


Property changes on: Sandbox/ocql-foliage/src/temp.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/test.py
===================================================================
--- Sandbox/ocql-foliage/src/test.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/test.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,26 @@
+from ocql import OCQLEngine
+engine = OCQLEngine()
+query = engine.compile("""set [ c in ICurses; d in IDepartments; d.name="Computing Science"; d = some c.runBy; 1<=c.credits; c.credits <= 3 | c ]""") 
+print query.execute() 
+
+#q=Iter(set, Lambda('d', 
+#        If(Eq(Identifier('d.name'),Identifier('"Computing Science"')),
+#            Single(set,Identifier('x')),
+#            Empty(set,None)
+#        )
+#    ), Collection('IDepartments'))
+#
+#q=Iter(set,
+#    If('d.name="Computing Science"',
+#        Select(set, Lambda('c',
+#            And(Reduce(set, 'False', Lambda('d','d=i'), Or, 'c.runBy'),
+#                '1<=c.credits',
+#                'c.credits<=3')),
+#            ICurses),
+#        Empty(set,None)), 
+#    Collection('IDepartments'))
+#
+#m = q.compile()
+#print m
+#c = compile(m,'','eval')
+#print eval(c)


Property changes on: Sandbox/ocql-foliage/src/test.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/testalgebra.py
===================================================================
--- Sandbox/ocql-foliage/src/testalgebra.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/testalgebra.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,352 @@
+#
+# Algebra operators
+#
+from ocql.engine.algebra import Algebra
+
+class BaseAlgebra(Algebra):
+    pass
+
+class Empty(BaseAlgebra):
+    """
+    >>> Empty(set,None).compile()
+    'set()'
+    >>> Empty(list,None).compile()
+    '[]'
+    """
+    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]'
+    """
+
+    def __init__(self, klass, expr):
+        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)
+
+    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])'
+    """
+    def __init__(self, klass, coll1, coll2):
+        self.klass=klass
+        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)
+
+    def walk(self):
+        yield self
+        for t in self.coll1.walk():
+            yield t
+        for t in self.coll2.walk():
+            yield t
+
+class Iter(BaseAlgebra):
+    def __init__(self, klass, func, coll):
+        self.klass = klass
+        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)
+
+    def walk(self):
+        yield self
+        for t in self.func.walk():
+            yield t
+        for t in self.coll.walk():
+            yield t
+    
+class Select(BaseAlgebra):
+    def __init__(self, klass, func, coll):
+        self.klass = klass
+        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)
+
+    def walk(self):
+        yield self
+        for t in self.func.walk():
+            yield t
+        for t in self.coll.walk():
+            yield t
+        
+class Reduce(BaseAlgebra):
+    def __init__(self, klass, expr, func, aggreg, coll):
+        self.klass = klass
+        self.expr = expr
+        self.func = func
+        self.aggreg = aggreg
+        self.coll = coll
+
+    def compile(self):
+        if self.klass == set:
+            #adi: [] needed, otherwise iteration over non-seq
+            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 "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):
+#        self.klass = klass
+#        self.coll1 = coll1
+#        self.coll2 = coll2
+#    
+#    def compile(self):
+#        if self.klass == set:
+#            return 'set(filter(%s,%s))' % (self.coll1.compile(),self.coll1.compile())
+#        if self.klass == list:
+#            return 'filter(%s,%s)' % (self.coll1.compile(),self.coll2.compile())
+#
+#class Differ:
+#    def __init__(self, klass, start, enf):
+#        self.klass = klass
+#        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())
+        
+class Range(BaseAlgebra):
+    def __init__(self, klass, start, enf):
+        self.klass = klass
+        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():
+            yield t
+        for t in self.end.walk():
+            yield t
+    
+
+#class Index
+
+class Make(BaseAlgebra):
+    def __init__(self, coll1, coll2, expr):
+        self.expr = expr
+        self.coll1 = coll1
+        self.coll2 = coll2
+
+    def compile(self):
+        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):
+    def __init__(self, cond, expr1, expr2):
+        self.cond = cond
+        self.expr1 = expr1
+        self.expr2 = expr2
+    
+    def compile(self):
+        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):
+    def __init__(self, var, expr):
+        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)
+
+    def walk(self):
+        yield self
+        for t in self.expr.walk():
+            yield t
+        
+class Constant(BaseAlgebra):
+    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):
+    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):
+    def __init__(self, left, op, right):
+        self.left = left
+        self.op = op
+        self.right = right
+
+    def compile(self):
+        return '%s%s%s' % (self.left.compile(),self.op.compile(),self.right.compile())
+
+    def __repr__(self):
+        return "%s%s%s" % (self.left,self.op.compile(),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):
+    def __init__(self,op):
+        self.op = op
+        
+    def compile(self):
+        return self.op
+    
+    def __repr__(self):
+        return self.op
+
+    def walk(self):
+        yield self
+#class Property:
+#   def __init__(self, left, right):
+#        self.left = left
+#        self.right = right
+#    
+#    def compile(self):
+#        return '%s.%s' (self.left.compile(),self.right.compile())
+#    
+#    def __repr__(self):
+#        return '%s.%s' (self.left,self.right)
+
+def _test():
+    import doctest
+    doctest.testmod()
+
+if __name__ == "__main__":
+    _test()
+


Property changes on: Sandbox/ocql-foliage/src/testalgebra.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: Sandbox/ocql-foliage/src/testdb.py
===================================================================
--- Sandbox/ocql-foliage/src/testdb.py	                        (rev 0)
+++ Sandbox/ocql-foliage/src/testdb.py	2007-09-24 15:56:40 UTC (rev 79885)
@@ -0,0 +1,78 @@
+from ocql.engine.algebraoptimizer import AlgebraOptimizer
+from ocql.engine.algebracompiler import AlgebraCompiler
+from ocql.engine import metadata 
+
+# schema
+class ICurses:
+    pass
+
+class IDepartments:
+    pass
+
+class MClass(metadata.MetaType):
+    def __init__(self, klass):
+        self.klass = klass
+
+    def is_collection(self):
+        return True
+
+    def get_type(self):
+        return set
+    
+    def get_contained(self):
+        return self.klass
+    
+class MType(metadata.MetaType):
+    def __init__(self, klass, container=None):
+        self.klass = klass
+        self.container = container
+
+    def is_collection(self):
+        return (self.container is not None)
+
+    def get_type(self):
+        return self.container
+    
+    def get_contained(self):
+        return self.klass
+
+class Department(object):
+    def __init__(self, name):
+        self.name = name
+
+class Curses(object):
+    def __init__(self, name, runBy, credits):
+        self.name = name
+        self.runBy = runBy
+        self.credits = credits
+
+D1 = Department("Computing Science")
+D2 = Department("something")
+
+C1 = Curses("C1", runBy = set([D1, D2]), credits=2)
+C2 = Curses("C2", runBy = set(), credits=3)
+
+# metadata
+class TestMetadata(metadata.Metadata):
+    db = {
+            'IDepartments': [D1, D2],
+            'ICurses': [C1, C2]
+        }
+    classes = {
+            'IDepartments': MClass(ICurses),
+            'ICurses': MClass(IDepartments),
+            }
+    properties = {
+                'IDepartments': { 'name': str },
+                'ICurses': {
+                        'runBy': MType(IDepartments,set),
+                        'credits': MType(int)
+                },
+            }
+    
+    def getAll(self, klass):
+        x=self.db[klass]
+        return x
+    
+    def get_class(self, name):
+        return classes[name]


Property changes on: Sandbox/ocql-foliage/src/testdb.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native



More information about the Checkins mailing list