2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.ws.jws2;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
27 import compbio.metadata.*;
29 public class ParameterUtils
31 public static List<String> writeParameterSet(List<Option> optSet,
34 List<String> pset = new ArrayList<String>();
35 for (Option o : optSet)
37 pset.add(o.toCommand(pseparator));
43 * Converts options supplied via parameters file into {@code Option} objects
44 * (Refactored from compbio.ws.client.Jws2Client)
50 * @return List of Options of type T
54 * @SuppressWarnings(value = { "true" }) public static <T> List<Option<T>>
55 * processParameters(List<String> params, RunnerConfig<T> options, String
58 public static List<Option> processParameters(List<String> params,
59 RunnerConfig options, String pseparator)
61 List<Option> chosenOptions = new ArrayList<Option>();
62 for (String param : params)
65 if (isParameter(param, pseparator))
67 oname = getParamName(param, pseparator);
73 Option o = options.getArgumentByOptionName(oname);
76 System.out.println("WARN ignoring unsuppoted parameter: " + oname);
79 if (o instanceof Parameter)
81 o = copyParameter((Parameter) o);
90 o.setDefaultValue(isParameter(param, pseparator) ? getParamValue(
91 param, pseparator) : param);
92 } catch (WrongParameterException e)
94 System.out.println("Problem setting value for the parameter: "
101 return chosenOptions;
104 static String getParamName(String fullName, String pseparator)
106 assert isParameter(fullName, pseparator);
107 return fullName.substring(0, fullName.indexOf(pseparator));
110 static String getParamValue(String fullName, String pseparator)
112 assert isParameter(fullName, pseparator);
113 return fullName.substring(fullName.indexOf(pseparator) + 1);
116 static boolean isParameter(String param, String pseparator)
118 return param.contains(pseparator);
121 public static Option copyOption(Option option)
123 Option copy = new Option(option.getName(), option.getDescription());
124 setOptionFrom(copy, option);
128 public static void setOptionFrom(Option copy, Option option)
130 copy.setName(option.getName());
131 copy.setDescription(option.getDescription());
132 copy.setFurtherDetails(option.getFurtherDetails());
133 copy.setRequired(option.isRequired());
134 List<String> names = option.getOptionNames();
137 if (names.size() == 1)
139 HashSet<String> st = new HashSet();
140 st.add(names.get(0));
141 copy.setOptionNames(st);
145 copy.addOptionNames(names.toArray(new String[]
151 if (option.getDefaultValue() != null)
153 copy.setDefaultValue(option.getDefaultValue());
155 } catch (Exception ex)
157 ex.printStackTrace();
161 public static ValueConstrain copyValueConstrain(ValueConstrain vc)
165 ValueConstrain copy = new ValueConstrain();
166 if (vc.getMax() != null)
168 copy.setMax(vc.getMax().toString());
170 if (vc.getMin() != null)
172 copy.setMin(vc.getMin().toString());
174 if (vc.getType() != null)
176 copy.setType(vc.getType());
179 } catch (Exception e)
183 "Implementation error: could not copy ValueConstrain!");
187 public static Parameter copyParameter(Parameter parameter)
189 Parameter copy = new Parameter(parameter.getName(),
190 parameter.getDescription());
191 if (parameter.getValidValue() != null)
193 copy.setValidValue(copyValueConstrain(parameter.getValidValue()));
195 List<String> pv = parameter.getPossibleValues();
198 copy.addPossibleValues(pv.toArray(new String[]
201 setOptionFrom(copy, parameter);