@Override public void afterTick(TimeLapse timeLapse) { manager.checkExceptions(); final boolean wasComputing = manager.isComputing(); synchronized (receivedEvents) { for (final Event e : receivedEvents) { manager.doHandleEvent(e); final boolean isComputing = manager.isComputing() || wasComputing; if (!isComputing && clock.isTicking() && clock.getClockMode() == ClockMode.REAL_TIME) {
RtSolverModel(RealtimeClockController c, PDPRoadModel rm, PDPModel pm, Mode m, int threads, boolean threadGrouping) { clock = c; roadModel = rm; pdpModel = pm; manager = new SimSolversManager(); executor = Optional.absent(); mode = m; threadGroupingEnabled = threadGrouping; threadPoolSize = threads; receivedEvents = Collections.synchronizedList(new ArrayList<Event>()); clock.getEventAPI().addListener(new Listener() { @Override public void handleEvent(Event event) { manager.checkExceptions(); shutdown(); } }, ClockEventType.STOPPED); pdpModel.getEventAPI().addListener(new Listener() { @Override public void handleEvent(Event e) { manager.checkExceptions(); LOGGER.debug("new parcel -> switch to real time"); clock.switchToRealTime(); } }, PDPModelEventType.NEW_PARCEL); initExecutor(); }
void handleFailure(Throwable t) { if (t instanceof CancellationException || t instanceof InterruptedException) { LOGGER.info("RealtimeSolver execution got cancelled/interrupted"); try { eventDispatcher.dispatchEvent( new Event(RtSimSolverSchedulerBridge.EventType.DONE_COMPUTING, reference)); } catch (final RuntimeException e) { simSolversManager.addException(e); } } else { simSolversManager.addException(t); } }
while (model.manager.isComputing()) { verify(clock, times(0)).switchToSimulatedTime(); model.manager.checkExceptions(); model.afterTick(TimeLapseFactory.ms(100, 200)); try {
RtSimSolverSchedulerBridge(RealtimeClockController c, RealtimeSolver s, PDPRoadModel rm, PDPModel pm, Set<Vehicle> vehicles, ListeningExecutorService ex, SimSolversManager manager) { solver = s; clock = c; converter = Solvers.converterBuilder() .with(clock) .with(rm) .with(pm) .with(vehicles) .build(); currentSchedule = Optional.absent(); isUpdated = false; reference = this; eventDispatcher = new EventDispatcher(EventType.values()); simSolverEventDispatcher = new EventDispatcher(RtSimSolver.EventType.values()); executor = ex; rtSimSolver = new InternalRtSimSolver(); scheduler = new InternalScheduler(); simSolversManager = manager; solver.init(scheduler); simSolversManager.register(this); solveCount = new AtomicInteger(); }
void doHandleEvent(Event e) { synchronized (computingSimSolvers) { final boolean isComputingBefore = isComputing(); LOGGER.trace("receive: {}, computing: {}, clock is ticking: {}, {}", e, isComputingBefore, clock.isTicking(), computingSimSolvers); if (e.getEventType() == EventType.START_COMPUTING) { if (!isComputingBefore) { LOGGER.debug("start computing -> switch to real time"); clock.switchToRealTime(); } computingSimSolvers.add((RtSimSolverSchedulerBridge) e.getIssuer()); } else if (e.getEventType() == EventType.DONE_COMPUTING) { // done computing checkState(computingSimSolvers.remove(e.getIssuer())); // if (!isComputing()) { // stop(); // } } else { throw new IllegalArgumentException("Unexpected event: " + e); } } }
void shutdown() { if (executor.isPresent()) { manager.stop(); LOGGER.info("Shutting down executor.."); executor.get().shutdownNow(); try { final boolean success = executor.get().awaitTermination(2, TimeUnit.SECONDS); if (success) { LOGGER.info("Executor shutdown."); } else { LOGGER.warn("Shutting down executor timed out."); } } catch (final InterruptedException e) { if (executor.get().isShutdown()) { LOGGER.info("Interrupt, but executor shutdown."); } else { LOGGER.warn("Executor shutdown interrupted.."); } } } }
public boolean isComputing() { return manager.isComputing(); }
@Override public void tick(TimeLapse timeLapse) { manager.checkExceptions(); }
@Override public void handleEvent(Event e) { manager.checkExceptions(); LOGGER.debug("new parcel -> switch to real time"); clock.switchToRealTime(); } }, PDPModelEventType.NEW_PARCEL);
@Override public void handleEvent(Event event) { manager.checkExceptions(); shutdown(); } }, ClockEventType.STOPPED);
@Override public void uncaughtException(@SuppressWarnings("null") Thread t, @SuppressWarnings("null") Throwable e) { addException(e); } }