public void Rebuild(int simulationId, ProductionDomainContext evaluationContext) { //delete all demands but the head-demands _context.Demands.RemoveRange(_context.Demands.Where(a => (a.DemandRequesterId != null || a.GetType() == typeof(DemandProductionOrderBom)) && a.State != State.Finished).ToList()); _context.ProductionOrderBoms.RemoveRange(_context.ProductionOrderBoms.Where(a => a.State != State.Finished)); _context.SaveChanges(); var requester = _context.Demands.Where(a => null == a.DemandRequesterId && a.State != State.Finished).ToList(); requester = (from req in requester where req.GetType() == typeof(DemandStock) || _context.GetDueTimeByOrder(req) < _context.SimulationConfigurations.Single(a => a.Id == simulationId).Time + _context.SimulationConfigurations.Single(a => a.Id == simulationId).MaxCalculationTime select req).ToList(); //rebuild by using activity-slack to order demands while (requester.Any()) { var nextRequester = GetNextByActivitySlack(requester, simulationId); SatisfyRequest(nextRequester, simulationId, evaluationContext); requester.Remove(requester.Find(a => a.Id == nextRequester.Id)); } }