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.datamodel.AlignmentAnnotation;
15 import jalview.gui.AlignFrame;
16 import jalview.gui.WsJobParameters;
17 import jalview.io.AnnotationFile;
18 import jalview.io.FeaturesFile;
19 import jalview.util.MathUtils;
20 import jalview.util.MessageManager;
21 import jalview.ws.params.ArgumentI;
22 import jalview.ws.params.WsParamSetI;
23 import jalview.ws2.MenuEntryProviderI;
24 import jalview.ws2.ResultSupplier;
25 import jalview.ws2.WSJob;
26 import jalview.ws2.WSJobStatus;
27 import jalview.ws2.WebServiceExecutor;
28 import jalview.ws2.WebServiceI;
29 import jalview.ws2.WebServiceWorkerI;
30 import jalview.ws2.utils.WSJobList;
32 import static java.lang.String.format;
39 public class AnnotationOperation implements Operation
41 final WebServiceI service;
43 final String typeName;
45 final ResultSupplier<List<AlignmentAnnotation>> annotationSupplier;
47 final ResultSupplier<FeaturesFile> featuresSupplier;
49 public AnnotationOperation(WebServiceI service,
50 ResultSupplier<List<AlignmentAnnotation>> annotSupplier,
51 ResultSupplier<FeaturesFile> featSupplier, String operationName)
53 this.service = service;
54 this.annotationSupplier = annotSupplier;
55 this.featuresSupplier = featSupplier;
56 this.typeName = operationName;
60 public String getName()
62 return service.getName();
66 public String getTypeName()
72 public String getHostName()
74 return service.getHostName();
78 public int getMinSequences()
84 public int getMaxSequences()
86 return Integer.MAX_VALUE;
90 public boolean canSubmitGaps()
96 public boolean isProteinOperation()
102 public boolean isNucleotideOperation()
108 public boolean isInteractive()
114 public MenuEntryProviderI getMenuBuilder()
116 return this::buildMenu;
119 protected void buildMenu(JMenu parent, AlignFrame frame)
121 final var calcName = service.getName();
122 WebServiceExecutor wsExecutor = frame.getViewport().getWSExecutor();
124 var item = new JMenuItem(MessageManager.formatMessage(
125 "label.calcname_with_default_settings", calcName));
126 item.addActionListener((event) -> {
127 WebServiceWorkerI worker = new AnnotationWorker();
128 wsExecutor.submit(worker);
132 if (service.hasParameters())
134 var item = new JMenuItem(
135 MessageManager.getString("label.edit_settings_and_run"));
136 item.setToolTipText(MessageManager.getString(
137 "label.view_and_change_parameters_before_running_calculation"));
138 item.addActionListener((event) -> {
139 openEditParamsDialog(service, null, null)
140 .thenAcceptAsync((arguments) -> {
141 if (arguments != null)
150 private CompletionStage<List<ArgumentI>> openEditParamsDialog(
151 WebServiceI service, WsParamSetI preset, List<ArgumentI> arguments)
153 WsJobParameters jobParams;
154 if (preset == null && arguments != null && arguments.size() > 0)
155 jobParams = new WsJobParameters(service.getParamStore(), preset,
158 jobParams = new WsJobParameters(service.getParamStore(), preset,
162 jobParams.setName(MessageManager.getString(
163 "label.adjusting_parameters_for_calculation"));
165 var stage = jobParams.showRunDialog();
166 return stage.thenApply((startJob) -> {
169 if (jobParams.getPreset() == null)
170 return jobParams.getJobParams();
172 return jobParams.getPreset().getArguments();
181 private class AnnotationWorker implements WebServiceWorkerI
183 private long uid = MathUtils.getUID();
185 private WSJobList jobs = new WSJobList();
187 private HashMap<Long, Integer> exceptionCount = new HashMap<>();
189 private static final int MAX_RETRY = 5;
198 public WebServiceI getWebService()
204 public List<WSJob> getJobs()
206 return Collections.unmodifiableList(jobs);
210 public void startJobs() throws IOException
216 public boolean pollJobs() throws IOException
219 for (WSJob job : getJobs())
221 if (!job.getStatus().isDone() && !job.getStatus().isFailed())
223 Cache.log.debug(format("Polling job %s", job));
226 service.updateProgress(job);
227 exceptionCount.remove(job.getUid());
228 } catch (IOException e)
230 Cache.log.error(format("Polling job %s failed.", job), e);
231 int count = exceptionCount.getOrDefault(job.getUid(),
235 job.setStatus(WSJobStatus.SERVER_ERROR);
236 Cache.log.warn(format(
237 "Attempts limit exceeded. Droping job %s.", job));
239 exceptionCount.put(job.getUid(), count);
240 } catch (OutOfMemoryError e)
242 job.setStatus(WSJobStatus.BROKEN);
244 format("Out of memory when retrieving job %s", job), e);
247 format("Job %s status is %s", job, job.getStatus()));
249 done &= job.getStatus().isDone() || job.getStatus().isFailed();
257 // TODO Auto-generated method stub