8916be961caf344844a48c44b47e8333615f60e1
[jalview.git] / src / jalview / ws / jws2 / ParameterUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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
25 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
29
30 import compbio.metadata.*;
31
32 public class ParameterUtils
33 {
34   public static List<String> writeParameterSet(List<Option> optSet,
35           String pseparator)
36   {
37     List<String> pset = new ArrayList<String>();
38     for (Option o : optSet)
39     {
40       pset.add(o.toCommand(pseparator));
41     }
42     return pset;
43   }
44
45   /**
46    * Converts options supplied via parameters file into {@code Option} objects
47    * (Refactored from compbio.ws.client.Jws2Client)
48    * 
49    * @param <T>
50    *          web service type
51    * @param params
52    * @param options
53    * @return List of Options of type T
54    * 
55    */
56   /*
57    * @SuppressWarnings(value = { "true" }) public static <T> List<Option<T>>
58    * processParameters(List<String> params, RunnerConfig<T> options, String
59    * pseparator)
60    */
61   public static List<Option> processParameters(List<String> params,
62           RunnerConfig options, String pseparator)
63   {
64     List<Option> chosenOptions = new ArrayList<Option>();
65     for (String param : params)
66     {
67       String oname = null;
68       if (isParameter(param, pseparator))
69       {
70         oname = getParamName(param, pseparator);
71       }
72       else
73       {
74         oname = param;
75       }
76       Option o = options.getArgumentByOptionName(oname);
77       if (o == null)
78       {
79         System.out.println("WARN ignoring unsuppoted parameter: " + oname);
80         continue;
81       }
82       if (o instanceof Parameter)
83       {
84         o = copyParameter((Parameter) o);
85       }
86       else
87       {
88         o = copyOption(o);
89       }
90       {
91         try
92         {
93           o.setDefaultValue(isParameter(param, pseparator) ? getParamValue(
94                   param, pseparator) : param);
95         } catch (WrongParameterException e)
96         {
97           System.out.println("Problem setting value for the parameter: "
98                   + param);
99           e.printStackTrace();
100         }
101       }
102       chosenOptions.add(o);
103     }
104     return chosenOptions;
105   }
106
107   static String getParamName(String fullName, String pseparator)
108   {
109     assert isParameter(fullName, pseparator);
110     return fullName.substring(0, fullName.indexOf(pseparator));
111   }
112
113   static String getParamValue(String fullName, String pseparator)
114   {
115     assert isParameter(fullName, pseparator);
116     return fullName.substring(fullName.indexOf(pseparator) + 1);
117   }
118
119   static boolean isParameter(String param, String pseparator)
120   {
121     return param.contains(pseparator);
122   }
123
124   public static Option copyOption(Option option)
125   {
126     Option copy = new Option(option.getName(), option.getDescription());
127     setOptionFrom(copy, option);
128     return copy;
129   }
130
131   public static void setOptionFrom(Option copy, Option option)
132   {
133     copy.setName(option.getName());
134     copy.setDescription(option.getDescription());
135     copy.setBasicURL(option.getBasicURL());
136     copy.setFurtherDetails(option.getFurtherDetails());
137     copy.setRequired(option.isRequired());
138     List<String> names = option.getOptionNames();
139     if (names != null)
140     {
141       if (names.size() == 1)
142       {
143         HashSet<String> st = new HashSet();
144         st.add(names.get(0));
145         copy.setOptionNames(st);
146       }
147       else
148       {
149         copy.addOptionNames(names.toArray(new String[]
150         {}));
151       }
152     }
153     try
154     {
155       if (option.getDefaultValue() != null)
156       {
157         copy.setDefaultValue(option.getDefaultValue());
158       }
159     } catch (Exception ex)
160     {
161       ex.printStackTrace();
162     }
163   }
164
165   public static ValueConstrain copyValueConstrain(ValueConstrain vc)
166   {
167     try
168     {
169       ValueConstrain copy = new ValueConstrain();
170       if (vc.getMax() != null)
171       {
172         copy.setMax(vc.getMax().toString());
173       }
174       if (vc.getMin() != null)
175       {
176         copy.setMin(vc.getMin().toString());
177       }
178       if (vc.getType() != null)
179       {
180         copy.setType(vc.getType());
181       }
182       return copy;
183     } catch (Exception e)
184     {
185       e.printStackTrace();
186       throw new Error(MessageManager.getString("error.implementation_error_couldnt_copy_value_constraint"));
187     }
188   }
189
190   public static Parameter copyParameter(Parameter parameter)
191   {
192     Parameter copy = new Parameter(parameter.getName(),
193             parameter.getDescription());
194     if (parameter.getValidValue() != null)
195     {
196       copy.setValidValue(copyValueConstrain(parameter.getValidValue()));
197     }
198     List<String> pv = parameter.getPossibleValues();
199     if (pv != null)
200     {
201       copy.addPossibleValues(pv.toArray(new String[]
202       {}));
203     }
204     setOptionFrom(copy, parameter);
205     return copy;
206   }
207
208 }