1 package jalview.ws2.operations;
3 import java.io.IOException;
4 import java.util.Collections;
5 import java.util.HashMap;
7 import java.util.concurrent.CancellationException;
8 import java.util.concurrent.CompletionStage;
10 import javax.swing.JMenu;
11 import javax.swing.JMenuItem;
13 import jalview.bin.Cache;
14 import jalview.gui.AlignFrame;
15 import jalview.gui.WsJobParameters;
16 import jalview.io.AnnotationFile;
17 import jalview.io.FeaturesFile;
18 import jalview.util.MathUtils;
19 import jalview.util.MessageManager;
20 import jalview.ws.params.ArgumentI;
21 import jalview.ws.params.WsParamSetI;
22 import jalview.ws2.MenuEntryProviderI;
23 import jalview.ws2.ResultSupplier;
24 import jalview.ws2.WSJob;
25 import jalview.ws2.WSJobStatus;
26 import jalview.ws2.WebServiceExecutor;
27 import jalview.ws2.WebServiceI;
28 import jalview.ws2.WebServiceWorkerI;
29 import jalview.ws2.utils.WSJobList;
31 import static java.lang.String.format;
38 public class AnnotationOperation implements Operation
40 final WebServiceI service;
42 final String typeName;
44 final ResultSupplier<AnnotationFile> annotationSupplier;
46 final ResultSupplier<FeaturesFile> featuresSupplier;
48 public AnnotationOperation(WebServiceI service,
49 ResultSupplier<AnnotationFile> annotSupplier,
50 ResultSupplier<FeaturesFile> featSupplier, String operationName)
52 this.service = service;
53 this.annotationSupplier = annotSupplier;
54 this.featuresSupplier = featSupplier;
55 this.typeName = operationName;
59 public String getName()
61 return service.getName();
65 public String getTypeName()
71 public String getHostName()
73 return service.getHostName();
77 public int getMinSequences()
83 public int getMaxSequences()
85 return Integer.MAX_VALUE;
89 public boolean canSubmitGaps()
95 public boolean isProteinOperation()
101 public boolean isNucleotideOperation()
107 public boolean isInteractive()
113 public MenuEntryProviderI getMenuBuilder()
115 return this::buildMenu;
118 protected void buildMenu(JMenu parent, AlignFrame frame)
120 final var calcName = service.getName();
121 WebServiceExecutor wsExecutor = frame.getViewport().getWSExecutor();
123 var item = new JMenuItem(MessageManager.formatMessage(
124 "label.calcname_with_default_settings", calcName));
125 item.addActionListener((event) -> {
126 WebServiceWorkerI worker = new AnnotationWorker();
127 wsExecutor.submit(worker);
131 if (service.hasParameters())
133 var item = new JMenuItem(
134 MessageManager.getString("label.edit_settings_and_run"));
135 item.setToolTipText(MessageManager.getString(
136 "label.view_and_change_parameters_before_running_calculation"));
137 item.addActionListener((event) -> {
138 openEditParamsDialog(service, null, null)
139 .thenAcceptAsync((arguments) -> {
140 if (arguments != null)
149 private CompletionStage<List<ArgumentI>> openEditParamsDialog(
150 WebServiceI service, WsParamSetI preset, List<ArgumentI> arguments)
152 WsJobParameters jobParams;
153 if (preset == null && arguments != null && arguments.size() > 0)
154 jobParams = new WsJobParameters(service.getParamStore(), preset,
157 jobParams = new WsJobParameters(service.getParamStore(), preset,
161 jobParams.setName(MessageManager.getString(
162 "label.adjusting_parameters_for_calculation"));
164 var stage = jobParams.showRunDialog();
165 return stage.thenApply((startJob) -> {
168 if (jobParams.getPreset() == null)
169 return jobParams.getJobParams();
171 return jobParams.getPreset().getArguments();
180 private class AnnotationWorker implements WebServiceWorkerI
182 private long uid = MathUtils.getUID();
184 private WSJobList jobs = new WSJobList();
186 private HashMap<Long, Integer> exceptionCount = new HashMap<>();
188 private static final int MAX_RETRY = 5;
197 public WebServiceI getWebService()
203 public List<WSJob> getJobs()
205 return Collections.unmodifiableList(jobs);
209 public void startJobs() throws IOException
215 public boolean pollJobs() throws IOException
218 for (WSJob job : getJobs())
220 if (!job.getStatus().isDone() && !job.getStatus().isFailed())
222 Cache.log.debug(format("Polling job %s", job));
225 service.updateProgress(job);
226 exceptionCount.remove(job.getUid());
227 } catch (IOException e)
229 Cache.log.error(format("Polling job %s failed.", job), e);
230 int count = exceptionCount.getOrDefault(job.getUid(),
234 job.setStatus(WSJobStatus.SERVER_ERROR);
235 Cache.log.warn(format(
236 "Attempts limit exceeded. Droping job %s.", job));
238 exceptionCount.put(job.getUid(), count);
239 } catch (OutOfMemoryError e)
241 job.setStatus(WSJobStatus.BROKEN);
243 format("Out of memory when retrieving job %s", job), e);
246 format("Job %s status is %s", job, job.getStatus()));
248 done &= job.getStatus().isDone() || job.getStatus().isFailed();
256 // TODO Auto-generated method stub