@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); for (ResultListener listener : activityResultListeners.get(requestCode)) { listener.onActivityResult(requestCode, resultCode, data); } }
@Override public void resultsReceived(@Nullable TaskResultEvent event) { assert event != null; for (final Task<?> t : event.getTasks()) { final SimulationTask simTask = (SimulationTask) t; try { final SimulationResult res = processResult(simTask, scenariosMap, taskJobMap); results.add(res); for (final ResultListener l : listeners) { l.receive(res); } } catch (final IllegalArgumentException iae) { exception = Optional.of(iae); } } receivedNumResults += event.getTasks().size(); }
/** Needed by Quartz Scheduler. */ public final void run() throws Exception { logger.debug("Executing commands"); List<Command> finished_commands = new LinkedList<Command>(); try { // Cluster synchronization virtualCluster.start(this); // Execute the commands for (Command cmd : commands) { logger.debug("Executing " + cmd.getClass().getSimpleName()); cmd.execute(); finished_commands.add(cmd); } if (listener != null) listener.report(true, null); logger.debug("All commands executed successfully."); } catch (Exception e) { // Inform listener if (listener != null) listener.report(false, e.getMessage()); // One of the commands failed. Use the finished list to undo the action. if (finished_commands.size() > 0) { logger.error("Job " + jobName + " failed. Trying undo. " + e.getMessage(), e); try { Collections.reverse(finished_commands); for (Command cmd : finished_commands) { logger.info("Undoing " + cmd.getClass().getSimpleName()); cmd.undo(); } logger.info("" + finished_commands.size() + " undo steps completed"); } catch (Exception ee) { logger.warn("Job " + jobName + " undo failed too! " + ee.getMessage(), ee); } } else { logger.error("Job " + jobName + " failed. " + e.getMessage(), e); } // It would be pointless to throw anything at this point } finally { // Cluster synchronization virtualCluster.finish(this); } }
@Override public ExperimentResults compute(Builder builder, Set<SimArgs> inputs) { final IdMap<MASConfiguration> configMap = new IdMap<>("c", MASConfiguration.class); final IdMap<ScenarioProvider> scenarioMap = new IdMap<>("s", ScenarioProvider.class); final IdMap<ObjectiveFunction> objFuncMap = new IdMap<>("o", ObjectiveFunction.class); final List<ResultListener> listeners = newArrayList(builder.resultListeners); @SuppressWarnings({"rawtypes", "unchecked"}) final IdMap<PostProcessor<?>> ppMap = new IdMap("p", PostProcessor.class); final Map<String, Scenario> scenariosMap = newLinkedHashMap(); // create tasks final List<SimulationTask> tasks = newArrayList(); constructTasks(inputs, tasks, configMap, scenarioMap, objFuncMap, ppMap, scenariosMap); // this sorts tasks using this chain: scenario, configuration, objective // function, postprocessor, seed Collections.sort(tasks); // determine size of batches final int numBatches = Math.min(tasks.size(), builder.numBatches); final int batchSize = DoubleMath.roundToInt(tasks.size() / (double) numBatches, RoundingMode.CEILING); final Map<Task<?>, JPPFJob> taskJobMap = newLinkedHashMap(); final ResultsCollector res = new ResultsCollector(tasks.size(), scenariosMap, taskJobMap, listeners); final List<JPPFJob> jobs = newArrayList(); for (int i = 0; i < numBatches; i++) { final JPPFJob job = new JPPFJob(new MemoryMapDataProvider(), res); job.setName(Joiner.on("").join(JOB_NAME, " ", i + 1, "/", numBatches)); jobs.add(job); for (final SimulationTask t : tasks.subList(i * batchSize, (i + 1) * batchSize)) { try { final MASConfiguration config = configMap.getValue(t.getConfigurationId()); final ScenarioProvider scenario = scenarioMap.getValue(t.getScenarioId()); final ObjectiveFunction objFunc = objFuncMap.getValue(t.getObjectiveFunctionId()); job.getDataProvider() .setParameter(t.getPostProcessorId(), ppMap.getValue(t.getPostProcessorId())); job.getDataProvider().setParameter(t.getConfigurationId(), config); job.getDataProvider().setParameter(t.getScenarioId(), scenario); job.getDataProvider().setParameter(t.getObjectiveFunctionId(), objFunc); job.add(t); } catch (final JPPFException e) { throw new IllegalStateException(e); } taskJobMap.put(t, job); } } for (final ResultListener l : listeners) { l.startComputing(tasks.size()); } checkState(!getJPPFClient().isClosed()); try { for (final JPPFJob job : jobs) { getJPPFClient().submitJob(job); } } catch (final Exception e) { throw new IllegalStateException(e); } res.awaitResults(); for (final ResultListener l : listeners) { l.doneComputing(); } return ExperimentResults.create(builder, res.buildResults()); }