Commits

Pierre Carbonnelle committed 81f6df4

support for the len_ function (not the aggregate operator)

  • Participants
  • Parent commits b081338

Comments (0)

Files changed (3)

File pyDatalog/examples/test.py

         assert ask(a_sum3[a]==X) == set([(2,)])
 
     @pyDatalog.program()
-    def len(): 
+    def len_(): 
         assert(len((1,2))) == 2
         (a_len[X] == len(Z)) <= p(X, Z, Y)
         assert ask(a_len[X]==Y) == set([('a', 2), ('b', 1)])
         (a_len2[X,Y] == len(Z)) <= p(X, Y, Z)
         assert ask(a_len2[a,b]==X) == set([(1,)])
         assert ask(a_len2[a,X]==Y) == set([('b', 1), ('c', 1)])
-
-        + q(a, c, 1)
-        + q(a, b, 2)
-        + q(b, b, 4)
+        
+        (a_len3[X] == Y) <= (Y==1+len_(X))
+        assert ask((X==(1,2)) & (a_len3[X]==3)) == set([((1,2),)])
+        assert ask((X==(1,2)) & (a_len3[X]==Y)) == set([((1,2),3)])
+        assert ask(a_len3[((1,2),)]==3) == set([()])
+        assert (ask(a_len3[X]==Y)) == None
 
     @pyDatalog.program()
     def tuple_():
     
     @pyDatalog.program()
     def concat(): 
+        + q(a, c, 1)
+        + q(a, b, 2)
+        + q(b, b, 4)
+
         (a_concat[X] == concat(Y, key=Z, sep='+')) <= q(X, Y, Z)
         assert ask(a_concat[X]==Y) == set([('b', 'b'), ('a', 'c+b')])
         assert ask(a_concat[a]=='c+b') == set([()])

File pyDatalog/pyEngine.py

             if isinstance(rhs, VarTuple):
                 return Interned.of(lhs._id.__getitem__(slice(*rhs.id)))
             return Interned.of(lhs._id.__getitem__(rhs.id))
+        if self.operator == '#' and isinstance(rhs, VarTuple):
+            return Interned.of(len(rhs))
         if self.operator == '.' and rhs.is_constant:
             return Interned.of(range(rhs.id))
         if lhs.is_constant and rhs.is_constant:

File pyDatalog/pyParser.py

     def add(self, row):
         self._value += row[-self.arity].id
         
-class Len_aggregate(Aggregate):
-    """ represents len_(X)"""
+class Len_aggregate(Aggregate, Operation):
+    """ represents len_(X) : a simple or aggregate operation"""
     required_kw = ('Y')
 
+    def __init__(self, Y):
+        Aggregate.__init__(self, Y)
+        Operation.__init__(self, None, '#', Y)
+        
     def add(self, row):
         self._value += 1