Clover icon

Coverage Report

  1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.ws.jws2

File JabaParamStore.java

 

Coverage histogram

../../../img/srcFileCovDistChart2.png
54% of files have more coverage

Code metrics

54
122
16
1
376
319
45
0.37
7.62
16
2.81

Classes

Class Line # Actions
JabaParamStore 47 122 45
0.1458333314.6%
 

Contributing tests

This file is covered by 195 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.ws.jws2;
22   
23    import jalview.util.MessageManager;
24    import jalview.ws.jws2.dm.JabaOption;
25    import jalview.ws.jws2.dm.JabaParameter;
26    import jalview.ws.jws2.dm.JabaWsParamSet;
27    import jalview.ws.jws2.jabaws2.Jws2Instance;
28    import jalview.ws.params.ArgumentI;
29    import jalview.ws.params.ParamDatastoreI;
30    import jalview.ws.params.ParamManager;
31    import jalview.ws.params.WsParamSetI;
32   
33    import java.io.IOException;
34    import java.util.ArrayList;
35    import java.util.Arrays;
36    import java.util.Hashtable;
37    import java.util.List;
38    import java.util.StringTokenizer;
39   
40    import compbio.metadata.Argument;
41    import compbio.metadata.Option;
42    import compbio.metadata.Parameter;
43    import compbio.metadata.Preset;
44    import compbio.metadata.PresetManager;
45    import compbio.metadata.RunnerConfig;
46   
 
47    public class JabaParamStore implements ParamDatastoreI
48    {
49   
50    Hashtable<String, JabaWsParamSet> editedParams = new Hashtable<String, JabaWsParamSet>();
51   
52    private Jws2Instance service;
53   
54    private RunnerConfig serviceOptions;
55   
56    private Hashtable<String, JabaPreset> servicePresets;
57   
 
58  0 toggle public JabaParamStore(Jws2Instance service)
59    {
60  0 this(service, null);
61    }
62   
63    ParamManager manager;
64   
 
65  380 toggle public JabaParamStore(Jws2Instance service, ParamManager manager)
66    {
67  380 this.service = service;
68  380 serviceOptions = service.getRunnerConfig();
69  380 this.manager = manager;
70    // discover all the user's locally stored presets for this service and
71    // populate the hash table
72  380 if (manager != null)
73    {
74  380 manager.registerParser(service.getUri(), this);
75  380 WsParamSetI[] prams = manager.getParameterSet(null, service.getUri(),
76    true, false);
77  380 if (prams != null)
78    {
79  0 for (WsParamSetI paramset : prams)
80    {
81  0 if (paramset instanceof JabaWsParamSet)
82    {
83  0 editedParams.put(paramset.getName(), (JabaWsParamSet) paramset);
84    }
85    else
86    {
87  0 jalview.bin.Console.errPrintln(
88    "Warning: Ignoring parameter set instance of type "
89    + paramset.getClass()
90    + " : Bound but not applicable for service at "
91    + service.getUri());
92    }
93    }
94    }
95    }
96    }
97   
 
98  6101 toggle @Override
99    public List<WsParamSetI> getPresets()
100    {
101  6101 List<WsParamSetI> prefs = new ArrayList<>();
102  6101 if (servicePresets == null)
103    {
104  380 servicePresets = new Hashtable<String, JabaPreset>();
105  380 PresetManager prman;
106  ? if ((prman = service.getPresets()) != null)
107    {
108  199 List pset = prman.getPresets();
109  199 if (pset != null)
110    {
111  199 for (Object pr : pset)
112    {
113  631 JabaPreset prset = new JabaPreset(service, (Preset) pr);
114  631 servicePresets.put(prset.getName(), prset);
115    }
116    }
117    }
118    }
119  6101 for (JabaPreset pr : servicePresets.values())
120    {
121  16328 prefs.add(pr);
122    }
123  6101 for (WsParamSetI wspset : editedParams.values())
124    {
125  0 prefs.add(wspset);
126    }
127  6101 return prefs;
128    }
129   
 
130  0 toggle @Override
131    public WsParamSetI getPreset(String name)
132    {
133  0 for (WsParamSetI pr : getPresets())
134    {
135  0 if (pr.getName().equals(name))
136    {
137  0 return pr;
138    }
139    }
140  0 return null;
141    }
142   
 
143  0 toggle public static List<ArgumentI> getJwsArgsfromJaba(List jabargs)
144    {
145  0 return getJwsArgsfromJaba(jabargs, true);
146    }
147   
 
148  0 toggle public static List<ArgumentI> getJwsArgsfromJaba(List jabargs,
149    boolean sortByOpt)
150    {
151  0 List<ArgumentI> rgs = new ArrayList<ArgumentI>();
152  0 List<String> rgnames = new ArrayList<String>();
153  0 for (Object rg : jabargs)
154    {
155  0 ArgumentI narg = null;
156  0 String nargstring = null;
157  0 if (rg instanceof Parameter)
158    {
159  0 narg = new JabaParameter((Parameter) rg);
160  0 nargstring = narg.getName(); // just sort by name for this
161    }
162  0 else if (rg instanceof Option)
163    {
164  0 narg = new JabaOption((Option) rg);
165  0 nargstring = (String) ((Option) rg).getOptionNames().get(0);
166    }
167  0 if (narg == null)
168    {
169  0 throw new Error(MessageManager.formatMessage(
170    "error.implementation_error_cannot_handle_jaba_param",
171    new String[]
172    { rg.getClass().toString() }));
173    }
174    else
175    {
176  0 rgs.add(narg);
177  0 rgnames.add(nargstring);
178    }
179    }
180  0 if (!sortByOpt)
181    {
182  0 return rgs;
183    }
184  0 ArgumentI[] rgssort = rgs.toArray(new ArgumentI[rgs.size()]);
185  0 String[] rgssorton = rgnames.toArray(new String[rgs.size()]);
186  0 jalview.util.QuickSort.sort(rgssorton, rgssort);
187  0 ArgumentI tmp1;
188  0 int i = 0;
189  0 while (rgssort.length - i > i)
190    {
191  0 tmp1 = rgssort[rgssort.length - i - 1];
192  0 rgssort[rgssort.length - i - 1] = rgssort[i];
193  0 rgssort[i++] = tmp1;
194    }
195  0 return Arrays.asList(rgssort);
196    }
197   
 
198  0 toggle public static List getJabafromJwsArgs(List<ArgumentI> jwsargs)
199    {
200  0 List rgs = new ArrayList();
201  0 for (ArgumentI rg : jwsargs)
202    {
203  0 Argument narg = (rg instanceof JabaOption)
204    ? ((JabaOption) rg).getOption()
205    : null;
206  0 if (narg == null)
207    {
208  0 throw new Error(MessageManager.formatMessage(
209    "error.implementation_error_cannot_handle_jaba_param",
210    new String[]
211    { rg.getClass().toString() }));
212    }
213    else
214    {
215  0 rgs.add(narg);
216    }
217    }
218  0 return rgs;
219    }
220   
 
221  0 toggle @Override
222    public List<ArgumentI> getServiceParameters()
223    {
224  0 return getJwsArgsfromJaba(serviceOptions.getArguments());
225    }
226   
 
227  0 toggle @Override
228    public boolean presetExists(String name)
229    {
230  0 return (editedParams.containsKey(name)
231    || servicePresets.containsKey(name));
232    }
233   
 
234  0 toggle @Override
235    public void deletePreset(String name)
236    {
237  0 if (editedParams.containsKey(name))
238    {
239  0 WsParamSetI parameterSet = editedParams.get(name);
240  0 editedParams.remove(name);
241  0 if (manager != null)
242    {
243  0 manager.deleteParameterSet(parameterSet);
244    }
245  0 return;
246    }
247  0 if (servicePresets.containsKey(name))
248    {
249  0 throw new Error(MessageManager.getString(
250    "error.implementation_error_attempt_to_delete_service_preset"));
251    }
252    }
253   
 
254  0 toggle @Override
255    public void storePreset(String presetName, String text,
256    List<ArgumentI> jobParams)
257    {
258  0 JabaWsParamSet jps = new JabaWsParamSet(presetName, text, jobParams);
259  0 jps.setApplicableUrls(new String[] { service.getUri() });
260  0 editedParams.put(jps.getName(), jps);
261  0 if (manager != null)
262    {
263  0 manager.storeParameterSet(jps);
264    }
265    }
266   
 
267  0 toggle @Override
268    public void updatePreset(String oldName, String presetName, String text,
269    List<ArgumentI> jobParams)
270    {
271  0 JabaWsParamSet jps = (JabaWsParamSet) ((oldName != null)
272    ? getPreset(oldName)
273    : getPreset(presetName));
274  0 if (jps == null)
275    {
276  0 throw new Error(MessageManager.formatMessage(
277    "error.implementation_error_cannot_locate_oldname_presetname",
278    new String[]
279    { oldName, presetName }));
280    }
281  0 jps.setName(presetName);
282  0 jps.setDescription(text);
283  0 jps.setArguments(jobParams);
284  0 jps.setApplicableUrls(new String[] { service.getUri() });
285  0 if (oldName != null && !oldName.equals(jps.getName()))
286    {
287  0 editedParams.remove(oldName);
288    }
289  0 editedParams.put(jps.getName(), jps);
290   
291  0 if (manager != null)
292    {
293  0 manager.storeParameterSet(jps);
294    }
295    }
296   
297    /**
298    * create a new, empty parameter set for this service
299    *
300    * @return
301    */
 
302  0 toggle WsParamSetI newWsParamSet()
303    {
304  0 return new JabaWsParamSet();
305    };
306   
 
307  0 toggle private boolean involves(String[] urls)
308    {
309  0 boolean found = false;
310  0 for (String url : urls)
311    {
312  0 if (service.getServiceTypeURI().equals(url)
313    || service.getUri().equalsIgnoreCase(url))
314    {
315  0 found = true;
316  0 break;
317    }
318    }
319  0 return found;
320    }
321   
 
322  0 toggle @Override
323    public WsParamSetI parseServiceParameterFile(String name, String descr,
324    String[] urls, String parameterfile) throws IOException
325    {
326  0 if (!involves(urls))
327    {
328  0 throw new IOException(MessageManager.getString(
329    "error.implementation_error_cannot_find_service_url_in_given_set"));
330   
331    }
332  0 JabaWsParamSet wsp = new JabaWsParamSet();
333  0 wsp.setName(name);
334  0 wsp.setDescription(descr);
335  0 wsp.setApplicableUrls(urls.clone());
336   
337  0 List<String> lines = new ArrayList<String>();
338  0 StringTokenizer st = new StringTokenizer(parameterfile, "\n");
339  0 while (st.hasMoreTokens())
340    {
341  0 lines.add(st.nextToken());
342    }
343  0 wsp.setjabaArguments(
344    ParameterUtils.processParameters(lines, serviceOptions, " "));
345  0 return wsp;
346    }
347   
 
348  0 toggle @Override
349    public String generateServiceParameterFile(WsParamSetI pset)
350    throws IOException
351    {
352  0 if (!involves(pset.getApplicableUrls()))
353    {
354  0 throw new IOException(MessageManager.formatMessage(
355    "error.implementation_error_cannot_find_service_url_in_given_set_param_store",
356    new String[]
357    { service.getUri() }));
358    }
359  0 if (!(pset instanceof JabaWsParamSet))
360    {
361  0 throw new Error(MessageManager.getString(
362    "error.implementation_error_jabaws_param_set_only_handled_by"));
363    }
364   
365  0 StringBuffer rslt = new StringBuffer();
366  0 for (String ln : ParameterUtils.writeParameterSet(
367    ((JabaWsParamSet) pset).getjabaArguments(), " "))
368    {
369  0 rslt.append(ln);
370  0 rslt.append("\n");
371    }
372  0 ;
373  0 return rslt.toString();
374    }
375   
376    }