Source code for job_shop_lib.dispatching._unscheduled_operations_observer
"""Home of the `UnscheduledOperationsObserver` class."""importcollectionsfromcollections.abcimportIterableimportitertoolsfromjob_shop_libimportOperation,ScheduledOperationfromjob_shop_lib.dispatchingimportDispatcherObserver,Dispatcher
[docs]classUnscheduledOperationsObserver(DispatcherObserver):"""Stores the operations that have not been dispatched yet. This observer maintains a list of deques, each containing unscheduled operations for a specific job. It provides methods to access and manipulate unscheduled operations efficiently. """def__init__(self,dispatcher:Dispatcher,*,subscribe:bool=True):super().__init__(dispatcher,subscribe=subscribe)self.unscheduled_operations_per_job:list[collections.deque[Operation]]=[]self.reset()# In case the dispatcher has already scheduled some operations,# we need to remove them.# Note that we don't need to remove the operations in order.forscheduled_operationinitertools.chain(*self.dispatcher.schedule.schedule):self.update(scheduled_operation)@propertydefunscheduled_operations(self)->Iterable[Operation]:"""An iterable of all unscheduled operations across all jobs."""returnitertools.chain(*self.unscheduled_operations_per_job)@propertydefnum_unscheduled_operations(self)->int:"""The total number of unscheduled operations."""total_operations=self.dispatcher.instance.num_operationsnum_scheduled_operations=(self.dispatcher.schedule.num_scheduled_operations)returntotal_operations-num_scheduled_operations
[docs]defupdate(self,scheduled_operation:ScheduledOperation)->None:"""Removes a scheduled operation from the unscheduled operations. This method is called by the dispatcher when an operation is scheduled. It removes the operation from its job's deque of unscheduled operations. Args: scheduled_operation: The operation that has been scheduled. """job_id=scheduled_operation.operation.job_idjob_deque=self.unscheduled_operations_per_job[job_id]ifjob_deque:job_deque.popleft()
[docs]defreset(self)->None:"""Resets unscheduled operations to include all operations. This method reinitializes the list of deques with all operations from all jobs in the instance. """self.unscheduled_operations_per_job=[collections.deque(job)forjobinself.dispatcher.instance.jobs]