Commits

Pierre Carbonnelle  committed 07ee20c

simplify Literal.__init__()

  • Participants
  • Parent commits 9894c72

Comments (0)

Files changed (3)

File pyDatalog/examples/test.py

     assert_error('(two(X)==Z) <= (Z==X+(lambda X: X))', 'Syntax error near equality: consider using brackets. two\(X\)')
     assert_error('p(X) <= sum(X, key=X)', 'Invalid body for clause')
     assert_error('ask(- manager[X]==1)', "Left-hand side of equality must be a symbol or function, not an expression.")
-    assert_error("p(X) <= (X=={})", "Syntax error: Symbol or Expression expected")
+    assert_error("p(X) <= (X=={})", "unhashable type: 'dict'")
 
     """ SQL Alchemy                    """
 
     assert(eq(3, 3)) == [()]
 
     # list unification
-    pyDatalog.create_atoms('X,Y, X1, X2, p, a, eq')
+    pyDatalog.create_atoms('X,Y, X1, X2, X3, p, a, eq')
     assert ( X==(1,2) ) == [((1,2),)]
     assert ( X==(1,(2,)) ) == [((1,(2,)),)] # nested
     assert ( X==(1,) + (2,) ) == [((1,2),)] # expression
     assert ( (X==(1,2)) & (X==Y)) == [((1, 2), (1, 2))]
     assert ( (X==(1,2)) & (Y==(1,2)) & (X==Y)) == [((1, 2), (1, 2))]
     assert ( (X==(1,2)) & (Y==(1,3)) & (X==Y)) == []
+    #print ( (X==(1,X1)) & (Y==(1,X2)) & (X==Y) & (X1==1) & (X3==X2)) # TODO drop last literal
     
     eq(X,Y) <= (X==Y)
     assert ( eq(X,(1,2))) == [((1,2),)]

File pyDatalog/pyEngine.py

         return all(element.is_safe(clause) for element in self._id)
 
     def __str__(self): 
-        return "'%s'" % str(str(e) for e in self.id)
+        return "'%s'" % str([str(e) for e in self.id])
     def equals_primitive(self, term, subgoal):
         if self == term:          # Both terms are constant and equal.
             literal = Literal(binary_equals_pred, (self, self))

File pyDatalog/pyParser.py

 class Expression(object):
     @classmethod
     def _for(cls, operand):
-        if operand is None or isinstance(operand, (six.string_types, int, list, tuple, xrange)):
-            return Symbol(operand, forced_type="constant")
-        elif isinstance(operand, type(lambda: None)):
+        if isinstance(operand, (Expression, Aggregate)):
+            return operand
+        if isinstance(operand, type(lambda: None)):
             return Lambda(operand)
-        return operand
+        return Symbol(operand, forced_type="constant")
     
     def _precalculation(self):
         return Body() # by default, there is no precalculation needed to evaluate an expression
             elif isinstance(arg, Aggregate):
                 raise pyDatalog.DatalogError("Syntax error: Incorrect use of aggregation.", None, None)
             else:
-                terms.append(arg)
+                terms.append(Expression._for(arg))
         self.terms = terms
                             
-        tbl = []
-        for a in terms:
-            if isinstance(a, Symbol):
-                tbl.append(a._pyD_lua)
-            else:
-                tbl.append(pyEngine.Const(a))
+        tbl = [a._pyD_lua for a in terms]
         # now create the literal for the head of a clause
         self.lua = pyEngine.Literal(predicate_name, tbl, prearity, aggregate)
         # TODO check that l.pred.aggregate is empty