Commits

Pierre Carbonnelle  committed d3c861d

completely remove temporary query predicates after evaluation.
Fix variable extraction in in-line queries

  • Participants
  • Parent commits 71288f1

Comments (0)

Files changed (3)

File pyDatalog/examples/test.py

         assert ask(X==Y+1) == None
         assert ask((X==1) & (Y==1) & (X==Y)) == set([(1,1)])
         assert ask((X==1) & (Y==2) & (X==Y-1)) == set([(1,2)])
-        #assert ask((X==1) & (Y==2) & (X+2==Y+1)) == set([(1,2)])
+        assert ask((X==1) & (Y==2) & (X+2==Y+1)) == set([(1,2)])
         assert ask((X==2) & (Y==X/2)) == set([(2,1)])
         assert ask((X==2) & (Y==X//2)) == set([(2,1)])
         
     assert unsafe(3, 1) == [()] #this query is safe
     assert unsafe(3, Y) == [(1,)] #this query is safe
     assert unsafe(X, 1) == True # X is undefined
-    assert (unsafe(X,1) >= X) == True
+    assert (unsafe(X,1) >= X) == None
     assert (unsafe(2,Y) >= Y) == 1
-    assert (unsafe(X,Y) >= Y) == True
+    assert (unsafe(X,Y) >= Y) == None
     
     unsafe2(X) <= lower(3,2)
     assert unsafe2(3) == []
     assert ((X=='OK') & (Y==str.lower(X)) >= Y) == 'ok'
     assert ((X==('1','2')) & (Y==str.join('.', X)) >= Y) == '1.2'
     assert ((X=='ok') & (Y==str.format('Hello {0}', X)) >= Y) == 'Hello ok'
+    assert (((X=='o.k') & ((Y,Z)==str.split(X, '.'))) >= Y) == 'o'
     #not supported : print((X==('1','2')) & (Y=='.'.join(X)))
     
     # time

File pyDatalog/pyEngine.py

     return pred
 
 def remove(pred):
-    if pred.id in Logic.tl.logic.Db : del Logic.tl.logic.Db[pred.id]
+    if pred.id in Logic.tl.logic.Db : 
+        del Logic.tl.logic.Db[pred.id]
     return pred
     
 
         else:
             del pred.clauses[id_]
         del pred.db[id_]  # remove clause from pred.db
-    """ TODO retract last fact removes pred ??  problem with assert function
-    if len(pred.db) == 0 and pred.prim == None: # if no definition left
+    # delete it completely if it's a temporary query predicate
+    if pred.name.startswith('_pyD_query') and len(pred.db) == 0 and pred.prim == None:
         remove(pred)
-    """
     return clause
-
+    
 def relevant_clauses(literal):
     """ returns matching clauses for a literal query, using index """
     result = None

File pyDatalog/pyParser.py

     
     def v(self):
         """ returns the first value in the list, or None """
-        return True if self.data is True else self._data[0] if self.data else None
+        return True if self.data is True else self._data[0] if 0<len(self._data) else None
 
 class LazyListOfList(LazyList):
     """ represents the result of an inline query (a Literal or Body)"""
         """ returns the first value of the variable in the result of a query, or None """
         if not isinstance(variable, Variable):
             return variable.__le__(self)
-        
-        if self.data is True: 
-            return True
-        elif self.data:
-            for t in self.literal().terms:
-                if id(t) == id(variable):
-                    return t.data[0]
+        return variable.v()
     
     def __str__(self):
         """ pretty print the result """