1 package jalview.ws2.client.slivka;
3 import static java.util.Objects.requireNonNullElse;
6 import java.net.MalformedURLException;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.List;
13 import com.stevesoft.pat.NotImplementedError;
15 import jalview.bin.Cache;
16 import jalview.ws.params.ArgumentI;
17 import jalview.ws.params.ParamDatastoreI;
18 import jalview.ws.params.ParamManager;
19 import jalview.ws.params.WsParamSetI;
20 import jalview.ws.params.simple.BooleanOption;
21 import jalview.ws.params.simple.DoubleParameter;
22 import jalview.ws.params.simple.IntegerParameter;
23 import jalview.ws.params.simple.Option;
24 import jalview.ws.params.simple.StringParameter;
25 import jalview.ws2.params.SimpleParamDatastore;
26 import jalview.ws2.params.SimpleParamSet;
27 import uk.ac.dundee.compbio.slivkaclient.Parameter;
28 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
30 class SlivkaParamStoreFactory
32 private final SlivkaService service;
33 private final ParamManager manager;
35 SlivkaParamStoreFactory(SlivkaService service, ParamManager manager)
37 this.service = service;
38 this.manager = manager;
41 ParamDatastoreI createParamDatastore()
46 url = service.getUrl().toURL();
47 } catch (MalformedURLException e)
49 Cache.log.warn("Invalid service url " + service.getUrl(), e);
51 List<WsParamSetI> presets = new ArrayList<>(service.getPresets().size());
52 for (var preset : service.getPresets())
54 presets.add(createPreset(preset));
56 List<ArgumentI> arguments = createPresetArguments(Collections.emptyMap());
57 return new SimpleParamDatastore(url, arguments, presets, manager);
60 WsParamSetI createPreset(SlivkaService.Preset preset)
62 var builder = SimpleParamSet.newBuilder();
63 builder.name(preset.name);
64 builder.description(preset.description);
65 builder.url(service.getUrl().toString());
66 builder.modifiable(false);
67 builder.arguments(createPresetArguments(preset.values));
68 return builder.build();
71 List<ArgumentI> createPresetArguments(Map<String, Object> values)
73 var args = new ArrayList<ArgumentI>();
74 for (Parameter param : service.getParameters())
76 if (param instanceof Parameter.IntegerParameter)
78 args.add(createOption((Parameter.IntegerParameter) param,
79 (Integer) values.get(param.getId())));
81 else if (param instanceof Parameter.DecimalParameter)
83 args.add(createOption((Parameter.DecimalParameter) param,
84 (Double) values.get(param.getId())));
86 else if (param instanceof Parameter.TextParameter)
88 args.add(createOption((Parameter.TextParameter) param,
89 (String) values.get(param.getId())));
91 else if (param instanceof Parameter.FlagParameter)
93 args.add(createOption((Parameter.FlagParameter) param,
94 (Boolean) values.get(param.getId())));
96 else if (param instanceof Parameter.ChoiceParameter)
98 Object ovalue = values.get(param.getId());
99 List<String> lvalue = null;
101 lvalue = (List<String>) ovalue;
102 else if (ovalue != null)
103 lvalue = List.of((String) ovalue);
104 args.addAll(createChoiceOptions((Parameter.ChoiceParameter) param, lvalue));
106 else if (param instanceof Parameter.FileParameter)
108 // args.add(createOption((Parameter.FileParameter) param, null));
112 args.add(createOption(param, values.get(param.getId())));
118 private Option createOption(Parameter.IntegerParameter param, Integer value)
120 var builder = IntegerParameter.newBuilder();
121 setCommonProperties(param, builder);
122 builder.setDefaultValue((Integer) param.getDefault());
123 builder.setValue(value);
124 builder.setBounds(param.getMin(), param.getMax());
125 return builder.build();
128 private Option createOption(Parameter.DecimalParameter param, Double value)
130 var builder = DoubleParameter.newBuilder();
131 setCommonProperties(param, builder);
132 builder.setDefaultValue((Double) param.getDefault());
133 builder.setValue(value);
134 builder.setBounds(param.getMin(), param.getMax());
135 return builder.build();
138 private Option createOption(Parameter.TextParameter param, String value)
140 var builder = StringParameter.newBuilder();
141 setCommonProperties(param, builder);
142 builder.setDefaultValue((String) param.getDefault());
143 builder.setValue(value);
144 return builder.build();
147 private Option createOption(Parameter.FlagParameter param, Boolean value)
149 var builder = BooleanOption.newBuilder();
150 setCommonProperties(param, builder);
151 builder.setDefaultValue((Boolean) param.getDefault());
152 builder.setValue(value);
153 return builder.build();
156 private List<Option> createChoiceOptions(Parameter.ChoiceParameter param, List<String> value)
158 value = requireNonNullElse(value, Collections.emptyList());
162 * Array parameter means that multiple values can be provided.
163 * Use multiple boolean checkboxes to represent the value.
165 List<Option> options = new ArrayList<>();
166 List<?> selected = requireNonNullElse(
167 (List<?>) param.getDefault(), Collections.emptyList());
169 var builder = BooleanOption.newBuilder();
170 setCommonProperties(param, builder);
171 for (String choice : param.getChoices())
173 builder.setName(String.format("%s$%d", param.getId(), i++));
174 builder.setLabel(choice);
175 builder.setDefaultValue(selected.contains(choice));
176 builder.setValue(value.contains(choice));
177 builder.setReprValue(choice);
178 options.add(builder.build());
185 * Single value parameter means a single string with limited possible
186 * values can be used.
188 var builder = StringParameter.newBuilder();
189 setCommonProperties(param, builder);
190 builder.setDefaultValue((String) param.getDefault());
191 if (value.size() > 0)
192 builder.setValue(value.get(0));
193 builder.setPossibleValues(param.getChoices());
194 return List.of(builder.build());
198 private Option createOption(Parameter.FileParameter param, File value)
200 throw new NotImplementedError("file paramters are not implemented for slivka");
203 private Option createOption(Parameter param, Object value)
205 var builder = StringParameter.newBuilder();
206 setCommonProperties(param, builder);
207 if (param.getDefault() != null)
208 builder.setDefaultValue(param.getDefault().toString());
210 builder.setValue(value.toString());
211 return builder.build();
214 private void setCommonProperties(Parameter param, Option.Builder builder)
216 builder.setName(param.getId());
217 builder.setLabel(param.getName());
218 builder.setDescription(param.getDescription());
219 builder.setRequired(param.isRequired());
222 builder.setDetailsUrl(service.getUrl().toURL());
223 } catch (MalformedURLException e)
225 Cache.log.warn("invalid service url " + service.getUrl(), e);