@Override public void onSuccess( @Nullable ImmutableList<ImmutableList<Parcel>> result) { LOGGER.trace("onSuccess: " + result); if (result == null) { scheduler.get().reportException( new IllegalArgumentException("Solver.solve(..) must return a " + "non-null result. Solver: " + solver)); } else { scheduler.get().updateSchedule(snapshot, result); scheduler.get().doneForNow(); } }
@Override public ListeningExecutorService getSharedExecutor() { return delegate.getSharedExecutor(); }
@Override public ImmutableList<ImmutableList<Parcel>> getCurrentSchedule() { getCurrentScheduleCalls++; return delegate.getCurrentSchedule(); }
@Override public void problemChanged(GlobalStateObject snapshot) { assertThat(scheduler.isPresent()).isTrue(); try { Thread.sleep(sleep); } catch (final InterruptedException e) { throw new IllegalStateException(e); } final ImmutableList<ImmutableList<Parcel>> schedule = ImmutableList .of(ImmutableList.copyOf(snapshot.getAvailableParcels()), ImmutableList.<Parcel>of()); scheduler.get().updateSchedule(snapshot, schedule); assertThat(scheduler.get().getCurrentSchedule()).isEqualTo(schedule); scheduler.get().doneForNow(); }
@Override public void doneForNow() { doneForNowCalls++; delegate.doneForNow(); }
synchronized void handleSolverFailure(Throwable t) { if (t instanceof CancellationException) { LOGGER.trace("{} Solver got cancelled.", this); return; } scheduler.get().reportException(t); }
@Override public void notify(ImmutableList<ImmutableList<Parcel>> schedule, double objectiveValue) { scheduler.get().updateSchedule(verifyNotNull(lastSnapshot), schedule); } }
assertThat(s2).isNotEqualTo(s3); assertThat(s1.getSharedExecutor()).isSameAs(s2.getSharedExecutor()); assertThat(s1.getSharedExecutor()).isSameAs(s3.getSharedExecutor()); s1.getCurrentSchedule(); } catch (final IllegalStateException e) { assertThat(e.getMessage()).contains("No schedule has been set");
@Override public void cancel() { if (isComputing()) { LOGGER.trace("attempt to cancel running Solver.."); currentFuture.get().cancel(true); scheduler.get().doneForNow(); } }
@Override public void onFailure(Throwable t) { if (t instanceof CancellationException) { LOGGER.trace("Solver execution got cancelled"); return; } scheduler.get().reportException(t); } });
@Override public void updateSchedule(GlobalStateObject state, ImmutableList<ImmutableList<Parcel>> routes) { updateScheduleCalls++; delegate.updateSchedule(state, routes); }
synchronized void handleSolverSuccess( @Nullable ImmutableList<ImmutableList<Parcel>> result) { if (result == null) { if (solver.isTerminateEarly() || currentFuture == null) { LOGGER.info("{} Solver was terminated early.", this); } else { scheduler.get().reportException(new IllegalArgumentException( "Solver.solve(..) must return a non-null result. Solver: " + solver)); } } else { LOGGER.info("{} Computations finished, update schedule.", this); scheduler.get().updateSchedule(verifyNotNull(lastSnapshot), result); scheduler.get().doneForNow(); } currentFuture = null; currentScheduleCallback = null; }
synchronized void start(final GlobalStateObject snapshot, boolean notifyCancel) { checkState(scheduler.isPresent()); doCancel(notifyCancel); checkState(currentFuture == null); checkState(currentScheduleCallback == null); lastSnapshot = snapshot; LOGGER.info("{} Start RT Optaplanner Solver.", this); currentFuture = scheduler.get().getSharedExecutor() .submit(new OptaplannerCallable(solver, snapshot)); currentScheduleCallback = new ScheduleCallback(this); Futures.addCallback(currentFuture, currentScheduleCallback); }
synchronized void doCancel(boolean notify) { LOGGER.trace("{} cancel", this); if (isComputing()) { LOGGER.trace("{} is computing, cancel future"); currentScheduleCallback.cancel(); currentFuture.cancel(true); currentScheduleCallback = null; currentFuture = null; if (notify) { scheduler.get().doneForNow(); } } if (solver.isSolving()) { LOGGER.trace("{} > terminate solver.", this); solver.terminateEarly(); try { while (solver.isSolving()) { Thread.sleep(WAIT_FOR_SOLVER_TERMINATION_PERIOD_MS); } LOGGER.info("{} Solver terminated early.", this); } catch (final InterruptedException e) { // stop waiting upon interrupt LOGGER.warn("Interrupt while waiting for solver termination."); } } }
@Override public void reportException(Throwable t) { delegate.reportException(t); } }
@Override public void bestSolutionChanged( @SuppressWarnings("null") BestSolutionChangedEvent<PDPSolution> event) { if (event.isNewBestSolutionInitialized() && event.getNewBestSolution().getScore().getHardScore() == 0) { final ImmutableList<ImmutableList<Parcel>> schedule = toSchedule(event.getNewBestSolution()); LOGGER.info("{} Found new best solution, update schedule. {}", ref, solver.isSolving()); sched.updateSchedule(verifyNotNull(lastSnapshot), schedule); } } });
@Override public void init(Scheduler s) { boolean fail = false; try { s.getCurrentSchedule(); } catch (final IllegalStateException e) { fail = true; assertThat(e.getMessage()).contains("No schedule has been set"); } assertThat(fail).isTrue(); scheduler = Optional.of(s); }
lastSnapshot = snapshot; currentFuture = Optional.of( scheduler.get().getSharedExecutor().submit( Solvers.createSolverCallable(solver, snapshot)));
cancel(); currentFuture = Optional.of( scheduler.get().getSharedExecutor().submit( Solvers.createSolverCallable(solver, snapshot)));