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