def __enter__(self): """Record all future changes to the model, undoing them when a call to __exit__ is received""" # Create a new context and add it to the stack try: self._contexts.append(HistoryManager()) except AttributeError: self._contexts = [HistoryManager()] return self
def __init__(self, model): self.history_manager = None self._model = model self.variables = {} self.constraints = {} self.objective = None self.original_objective = model.solver.objective self._contexts = [HistoryManager()] self.transaction_id = None
def reset(self): """ Removes all constraints and variables from the cache. """ variables = list(self.variables.keys()) constraints = list(self.constraints.keys()) while len(self._contexts) > 0: manager = self._contexts.pop() manager.reset() self._contexts.append(HistoryManager()) assert all(var_id not in self._model.solver.variables for var_id in variables) assert all(const_id not in self._model.solver.constraints for const_id in constraints) self.variables = {} self.constraints = {} self._model.objective = self.original_objective self.objective = None
def begin_transaction(self): """ Creates a time point. If rollback is called, the variables and constrains will be reverted to this point. """ self._contexts.append(HistoryManager())