Commits

Pierre Carbonnelle committed 9ab9592

Interned is for Pred only, now.

  • Participants
  • Parent commits 429a4ca

Comments (0)

Files changed (2)

File pyDatalog/pyEngine.py

 * variable bindings in a closure.  See http://tiny.cc/7837cw, http://tiny.cc/rq47cw
 """
 from collections import OrderedDict
-from decimal import Decimal
 import gc
 import logging
 import re
 # For example, after internalization, there is one constant for each
 # string used to name a constant.
 
-class Interned(object):
-    """ Abstract class for objects having only one instance in memory """
+class Term(object):
     @classmethod
     def of(cls, atom):
-        """ factory function for interned objects """
+        """ factory function for Term """
         if isinstance(atom, (Term, Operation)):
             return atom
         elif isinstance(atom, (list, tuple, util.xrange)):
-            return VarTuple(tuple(Interned.of(element) for element in atom))
+            return VarTuple(tuple(Term.of(element) for element in atom))
         else:
             return Const(atom)
-    notFound = object()
-    def __eq__(self, other):
-        return self is other
-    def __hash__(self): 
-        return id(self)
-    def __ne__(self, other):
-        return not self is other
-
-class Term(object):
     def __eq__(self, other):
         return self.key == other.key
     def __ne__(self, other):
         if self.operator == '[' and isinstance(lhs, (VarTuple, Const)) and rhs.is_constant:
             v = lhs._id if isinstance(lhs, VarTuple) else lhs.id
             if isinstance(rhs, VarTuple): # a slice
-                return Interned.of(v.__getitem__(slice(*rhs.id)))
-            return Interned.of(v.__getitem__(rhs.id))
+                return Term.of(v.__getitem__(slice(*rhs.id)))
+            return Term.of(v.__getitem__(rhs.id))
         if self.operator == '#' and isinstance(rhs, VarTuple):
-            return Interned.of(len(rhs))
+            return Term.of(len(rhs))
         if self.operator == '..' and rhs.is_constant:
-            return Interned.of(range(rhs.id))
+            return Term.of(range(rhs.id))
         if lhs.is_constant and rhs.is_constant:
             # calculate expression of constants
             if self.operator == '+':
-                return Interned.of(lhs.id + rhs.id)
+                return Term.of(lhs.id + rhs.id)
             elif self.operator == '-':
-                return Interned.of(lhs.id - rhs.id)
+                return Term.of(lhs.id - rhs.id)
             elif self.operator == '*':
-                return Interned.of(lhs.id * rhs.id)
+                return Term.of(lhs.id * rhs.id)
             elif self.operator == '/':
-                return Interned.of(lhs.id / rhs.id)
+                return Term.of(lhs.id / rhs.id)
             elif self.operator == '//':
-                return Interned.of(lhs.id // rhs.id)
+                return Term.of(lhs.id // rhs.id)
             elif self.operator == '**':
-                return Interned.of(lhs.id ** rhs.id)
+                return Term.of(lhs.id ** rhs.id)
             elif self.operator == '%':
-                return Interned.of(lhs.id.format(*(rhs.id)))
+                return Term.of(lhs.id.format(*(rhs.id)))
             elif isinstance(self.operator, type(lambda: None)):
-                return Interned.of(self.operator(*(rhs.id)))
+                return Term.of(self.operator(*(rhs.id)))
             elif self.operator == '.':
                 v = lhs.id
                 for attribute in rhs.id.split(".") :
                     v = getattr(v, attribute)
-                return Interned.of(v)
+                return Term.of(v)
             elif self.operator == '(':
-                return Interned.of(lhs.id.__call__(*(rhs.id)))
+                return Term.of(lhs.id.__call__(*(rhs.id)))
             assert False # dead code
         return Operation(lhs, self.operator, rhs)
             
     def __str__(self): 
         return "(%s%s%s)" % (str(self.lhs), str(self.operator), str(self.rhs))
 
+class Interned(object):
+    """ Abstract class for objects having only one instance in memory """
+    notFound = object()
+    def __eq__(self, other):
+        return self is other
+    def __hash__(self): 
+        return id(self)
+    def __ne__(self, other):
+        return not self is other
+
 class Pred(Interned):
     """ A predicate symbol has a name, an arity, and a database table.  
         It can also have a function used to implement a primitive."""
     """ add result as a candidate fact of class0 for subgoal"""
     if result is True:
         return fact(subgoal, True)
-    result = [Interned.of(r) for r in result]
+    result = [Term.of(r) for r in result]
     if class0 and result[1].id and not isinstance(result[1].id, class0): #prefixed
         return
     result = Literal(subgoal.literal.pred.name, result)
             if terms is True:
                 fact(subgoal, True)
             elif len(terms) == len(literal.terms):
-                new = Literal(pred, [Interned.of(term) for term in terms])
+                new = Literal(pred, [Term.of(term) for term in terms])
                 if match(literal, new) != None:
                     fact(subgoal, new)
     pred.prim = prim

File pyDatalog/pyParser.py

             self._pyD_value = list(map(Expression._pyD_for, name))
             self._pyD_name = util.unicode_type([element._pyD_name for element in self._pyD_value])
             self._pyD_type = 'tuple'
-            self._pyD_lua = pyEngine.Interned.of([e._pyD_lua for e in self._pyD_value])
+            self._pyD_lua = pyEngine.Term.of([e._pyD_lua for e in self._pyD_value])
             self._pyD_precalculations = pre_calculations(self._pyD_value)
         elif forced_type=="constant" or isinstance(name, (int, float, bool)) \
         or name is None \
             self._pyD_value = name
             self._pyD_name = util.unicode_type(name)
             self._pyD_type = 'constant'
-            self._pyD_lua = pyEngine.Interned.of(name)
+            self._pyD_lua = pyEngine.Term.of(name)
         else:
             self._pyD_value = name
             self._pyD_name = name