Ejemplo n.º 1
0
	def __init__(self, head, body):
		self.__head = head
		self.__body = body
		
		# relations between variables like < <= = !=
		# also for relations between variables and constants
		# self.__var_rels = {}
	
		# a list  (body_index1,pos1,body_index2,pos2,comparison operator)
		self.__var_constraints = []

		# explicitly add all equality constraints between variables
		for bi1, b1 in enumerate(self.__body):
			positions1 = PositionIndex.get_all_positions(b1)
			for p1 in positions1:
				v1 = p1.fetch(b1)
				if isinstance(v1, Variable):
					# can't use enumerate here, have to preserve correct indexing
					# for bi2
					for bi2, b2 in zip(range(bi1, len(self.__body)), self.__body[bi1:]):
						positions2 = PositionIndex.get_all_positions(b2)
						for p2 in positions2:
							v2 = p2.fetch(b2)
							if isinstance(v2, Variable):
								if (p1 != p2 or bi1 != bi2) and v1 == v2:
									self.__var_constraints.append((bi1, p1, bi2, p2, Comparison('==')))

		# a mapping head_pos -> [(body_index, body_pos)]
		self.__headvar_bindings = {}

		hpositions = PositionIndex.get_all_positions(head)
		for hp in hpositions:
			hv = hp.fetch(head)
			if not isinstance(hv, Variable):
				continue
			bound = False
			for bi, b in enumerate(self.__body):
				bpositions = PositionIndex.get_all_positions(b)
				for bp in bpositions:
					bv = bp.fetch(b)
					if isinstance(bv, Variable):
						if (hv == bv):
							self.__headvar_bindings.setdefault(hp, []).append((bi, bp))
							bound = True

			#assert bound, "Head variable %s not bound to any body variables" % hv
			if not bound:
				print >> sys.stderr, "Head variable %s not bound to any body variables" % hv

		# equality constraints on head
		self.__headvar_constraints = []
		for i, p1 in enumerate(hpositions):
			t1 = p1.fetch(head)
			if isinstance(t1, Variable):
				for p2 in hpositions[i+1:]:
					t2 = p2.fetch(head)
					if t1 == t2:
						self.__headvar_constraints.append((p1, p2))

		CacheHash.__init__(self)
Ejemplo n.º 2
0
	def __init__(self, terms):
		self.__terms = terms
		CacheHash.__init__(self)
Ejemplo n.º 3
0
    def __init__(self, head, body):
        self.__head = head
        self.__body = body

        # relations between variables like < <= = !=
        # also for relations between variables and constants
        # self.__var_rels = {}

        # a list  (body_index1,pos1,body_index2,pos2,comparison operator)
        self.__var_constraints = []

        # explicitly add all equality constraints between variables
        for bi1, b1 in enumerate(self.__body):
            positions1 = PositionIndex.get_all_positions(b1)
            for p1 in positions1:
                v1 = p1.fetch(b1)
                if isinstance(v1, Variable):
                    # can't use enumerate here, have to preserve correct indexing
                    # for bi2
                    for bi2, b2 in zip(range(bi1, len(self.__body)),
                                       self.__body[bi1:]):
                        positions2 = PositionIndex.get_all_positions(b2)
                        for p2 in positions2:
                            v2 = p2.fetch(b2)
                            if isinstance(v2, Variable):
                                if (p1 != p2 or bi1 != bi2) and v1 == v2:
                                    self.__var_constraints.append(
                                        (bi1, p1, bi2, p2, Comparison('==')))

        # a mapping head_pos -> [(body_index, body_pos)]
        self.__headvar_bindings = {}

        hpositions = PositionIndex.get_all_positions(head)
        for hp in hpositions:
            hv = hp.fetch(head)
            if not isinstance(hv, Variable):
                continue
            bound = False
            for bi, b in enumerate(self.__body):
                bpositions = PositionIndex.get_all_positions(b)
                for bp in bpositions:
                    bv = bp.fetch(b)
                    if isinstance(bv, Variable):
                        if (hv == bv):
                            self.__headvar_bindings.setdefault(hp, []).append(
                                (bi, bp))
                            bound = True

            #assert bound, "Head variable %s not bound to any body variables" % hv
            if not bound:
                print >> sys.stderr, "Head variable %s not bound to any body variables" % hv

        # equality constraints on head
        self.__headvar_constraints = []
        for i, p1 in enumerate(hpositions):
            t1 = p1.fetch(head)
            if isinstance(t1, Variable):
                for p2 in hpositions[i + 1:]:
                    t2 = p2.fetch(head)
                    if t1 == t2:
                        self.__headvar_constraints.append((p1, p2))

        CacheHash.__init__(self)
Ejemplo n.º 4
0
 def __init__(self, terms):
     self.__terms = terms
     CacheHash.__init__(self)
Ejemplo n.º 5
0
	def __init__(self, index):
		self.__index = tuple(index)
		CacheHash.__init__(self)
Ejemplo n.º 6
0
 def __init__(self, index):
     self.__index = tuple(index)
     CacheHash.__init__(self)