def unify_merge(d1, d2, U): d = d1.__class__() for k1, v1 in iteritems(d1): if k1 in d2: d[k1] = unify_merge(v1, d2[k1], U) else: d[k1] = unify_merge(v1, v1, U) for k2, v2 in iteritems(d2): if k2 not in d1: d[k2] = unify_merge(v2, v2, U) return d
def merge(self, new_best, *vars): """ Links all the specified vars to a Variable that represents their unification. """ if self.inplace: U = self else: # Copy all the unification data. U = Unification(self.inplace) for var, (best, pool) in iteritems(self.unif): # The pool of a variable is the set of all the variables that # are unified to it (all the variables that must have the same # value). The best is the Variable that represents a set of # values common to all the variables in the pool. U.unif[var] = (best, pool) # We create a new pool for our new set of unified variables, initially # containing vars and new_best new_pool = set(vars) new_pool.add(new_best) for var in copy(new_pool): best, pool = U.unif.get(var, (var, set())) # We now extend the new pool to contain the pools of all the variables. new_pool.update(pool) # All variables get the new pool. for var in new_pool: U.unif[var] = (new_best, new_pool) return U
def __str__(self): return ( self.__class__.__name__ + "(" + ", ".join("%s=%s" % (key, value) for key, value in iteritems(self.__dict__)) + ")" )
def __str__(self): return ( self.__class__.__name__ + "(" + ", ".join( "%s=%s" % (key, value) for key, value in iteritems(self.__dict__) ) + ")" )
def unify_walk(d1, d2, U): """ Tries to unify values of corresponding keys. """ for (k1, v1) in iteritems(d1): if k1 in d2: U = unify_walk(v1, d2[k1], U) if U is False: return False return U