Commits

Pierre Carbonnelle committed dc6b154

use pyDatalogError instead of assert

Comments (0)

Files changed (4)

pyDatalog/examples/test.py

     assert_error("p(X) <= (manager[X]== max(X, order_by=X))", "Aggregation cannot appear in the body of a clause")
     assert_error("q(min(X, order_by=X)) <= p(X)", "Syntax error: Incorrect use of aggregation\.")
     assert_error("manager[X]== min(X, order_by=X) <= manager(X)", "Syntax error: please verify parenthesis around \(in\)equalities")
-    assert_error("(manager[X]== min(X, order_by=X+2)) <= manager(X)", "order_by argument of aggregate must be variable\(s\), not expression\(s\).")
+    assert_error("(manager[X]== min(X, order_by=X+2)) <= manager(X)", "order_by argument of aggregate must be variable\(s\).")
     assert_error("ask(X<1)", 'Error: left hand side of comparison must be bound: ')
     assert_error("ask(X<Y)", 'Error: left hand side of comparison must be bound: ')
     assert_error("ask(1<Y)", 'Error: left hand side of comparison must be bound: ')

pyDatalog/pyDatalog.py

         for arg in set(args + ['_sum','sum_','_min','min_','_max','max_',
         '_len','len_','concat','concat_','rank','rank_','running_sum','running_sum_']):
             if arg in locals_: 
-                assert isinstance(locals_[arg], (pyParser.Symbol, pyParser.Variable)), \
-                    "Name conflict.  Can't redefine %s as atom" % arg
+                if not isinstance(locals_[arg], (pyParser.Symbol, pyParser.Variable)):
+                    raise util.DatalogError("Name conflict.  Can't redefine %s as atom" % arg, None, None)
             else:
                 if arg[0] not in string.ascii_uppercase:
                     locals_[arg] = pyParser.Symbol(arg)

pyDatalog/pyEngine.py

                        _search(base_literal, subgoal, literal)))
         return
 
-    assert not class0 or not terms[0].is_constant or isinstance(terms[0].id, class0), \
-        "TypeError: First argument of %s must be a %s, not a %s " % (str(literal0), class0.__name__, type(terms[0].id).__name__)
+    if class0 and terms[0].is_constant and not isinstance(terms[0].id, class0):
+        raise util.DatalogError("TypeError: First argument of %s must be a %s, not a %s " 
+                    % (str(literal0), class0.__name__, type(terms[0].id).__name__), None, None)
     for _class in literal0.pred.parent_classes():
         literal = literal0.rebased(_class)
         
     y = literal.terms[1]
     if not x.is_constant:
         if literal.pred.name == '_pyD_in':
-            assert y.is_const, "Error: right hand side must be bound: %s" % literal
+            if not y.is_const:
+                raise util.DatalogError("Error: right hand side must be bound: %s" % literal, None, None)
             for v in y.id:
                 yield [v, y]
         else:
-            assert False, "Error: left hand side of comparison must be bound: %s" % literal.pred.id
+            raise util.DatalogError("Error: left hand side of comparison must be bound: %s" 
+                                    % literal.pred.id, None, None)
     elif y.is_constant:
         if compare(x.id, literal.pred.name, y.id):
             yield True
     else:
-        assert False, "Error: right hand side of comparison must be bound: %s" % literal.pred.id
+        raise util.DatalogError("Error: right hand side of comparison must be bound: %s" 
+                                % literal.pred.id, None, None)
 
 # generic comparison function
 def compare(l,op,r):

pyDatalog/pyParser.py

         if self.data is True: 
             return True
         elif self.data:
-            assert isinstance(variable, Variable)
+            if not isinstance(variable, Variable):
+                raise util.DatalogError("Syntax error: Variable expected after >=", None, None)
             for t in self.literal().terms:
                 if id(t) == id(variable):
                     return t.data[0]
     # handlers of inequality and operations
     def __eq__(self, other):
         if isinstance(self, Operation) and self.operator in '+-' and self.lhs._pyD_value == 0:
-            assert False, "Did you mean to assert or retract a fact ? Please add parenthesis."
+            raise util.DatalogError("Did you mean to assert or retract a fact ? Please add parenthesis.", None, None)
         return Literal.make_for_comparison(self, "==", other)
     def __ne__(self, other):
         return Literal.make_for_comparison(self, '!=', other)
     @classmethod
     def make_for_comparison(cls, self, operator, other):
         """ factory of Literal for a comparison """
-        assert operator=="==" or not isinstance(other, Aggregate), "Aggregate operators can only be used with =="
+        if operator != "==" and isinstance(other, Aggregate):
+            raise util.DatalogError("Aggregate operators can only be used with ==", None, None)
         other = Expression._for(other)
         if isinstance(self, Function):
             if isinstance(other, Aggregate): # p[X]==aggregate() # TODO create 2 literals here
 
     def __pos__(self):
         " unary + means insert into database as fact "
-        assert not self._variables(), "Cannot assert a fact containing Variables"
+        if self._variables():
+            raise util.DatalogError("Cannot assert a fact containing Variables", None, None)
         clause = pyEngine.Clause(self.lua, [])
         pyEngine.assert_(clause)
 
     def __neg__(self):
         " unary - means retract fact from database "
-        assert not self._variables(), "Cannot retract a fact containing Variables"
+        if self._variables():
+            raise util.DatalogError("Cannot assert a fact containing Variables", None, None)
         clause = pyEngine.Clause(self.lua, [])
         pyEngine.retract(clause)
         
         self.for_each = tuple([e.__dict__.get('variable', e) for e in self.for_each]) 
         self.order_by = tuple([e.__dict__.get('variable', e) for e in self.order_by])
         
-        assert all([isinstance(e, VarSymbol) for e in self.for_each]), "for_each argument of aggregate must be variable(s), not expression(s)."
-        assert all([isinstance(e, VarSymbol) for e in self.order_by]), "order_by argument of aggregate must be variable(s), not expression(s)."
+        if not all([isinstance(e, VarSymbol) for e in self.for_each]):
+            raise util.DatalogError("for_each argument of aggregate must be variable(s).", None, None)
+        if not all([isinstance(e, VarSymbol) for e in self.order_by]):
+            raise util.DatalogError("order_by argument of aggregate must be variable(s).", None, None)
         
         if sep and not isinstance(sep, six.string_types):
             raise util.DatalogError("Separator in aggregation must be a string", None, None)