package jalview.ws2.client.slivka; import static java.util.Objects.requireNonNullElse; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import com.stevesoft.pat.NotImplementedError; import jalview.bin.Cache; import jalview.ws.params.ArgumentI; import jalview.ws.params.ParamDatastoreI; import jalview.ws.params.ParamManager; import jalview.ws.params.WsParamSetI; import jalview.ws.params.simple.BooleanOption; import jalview.ws.params.simple.DoubleParameter; import jalview.ws.params.simple.IntegerParameter; import jalview.ws.params.simple.Option; import jalview.ws.params.simple.StringParameter; import jalview.ws2.params.SimpleParamDatastore; import jalview.ws2.params.SimpleParamSet; import uk.ac.dundee.compbio.slivkaclient.Parameter; import uk.ac.dundee.compbio.slivkaclient.SlivkaService; class SlivkaParamStoreFactory { private final SlivkaService service; private final ParamManager manager; SlivkaParamStoreFactory(SlivkaService service, ParamManager manager) { this.service = service; this.manager = manager; } ParamDatastoreI createParamDatastore() { URL url = null; try { url = service.getUrl().toURL(); } catch (MalformedURLException e) { Cache.log.warn("Invalid service url " + service.getUrl(), e); } List presets = new ArrayList<>(service.getPresets().size()); for (var preset : service.getPresets()) { presets.add(createPreset(preset)); } List arguments = createPresetArguments(Collections.emptyMap()); return new SimpleParamDatastore(url, arguments, presets, manager); } WsParamSetI createPreset(SlivkaService.Preset preset) { var builder = SimpleParamSet.newBuilder(); builder.name(preset.name); builder.description(preset.description); builder.url(service.getUrl().toString()); builder.modifiable(false); builder.arguments(createPresetArguments(preset.values)); return builder.build(); } List createPresetArguments(Map values) { var args = new ArrayList(); for (Parameter param : service.getParameters()) { if (param instanceof Parameter.IntegerParameter) { args.add(createOption((Parameter.IntegerParameter) param, (Integer) values.get(param.getId()))); } else if (param instanceof Parameter.DecimalParameter) { args.add(createOption((Parameter.DecimalParameter) param, (Double) values.get(param.getId()))); } else if (param instanceof Parameter.TextParameter) { args.add(createOption((Parameter.TextParameter) param, (String) values.get(param.getId()))); } else if (param instanceof Parameter.FlagParameter) { args.add(createOption((Parameter.FlagParameter) param, (Boolean) values.get(param.getId()))); } else if (param instanceof Parameter.ChoiceParameter) { Object ovalue = values.get(param.getId()); List lvalue = null; if (param.isArray()) lvalue = (List) ovalue; else if (ovalue != null) lvalue = List.of((String) ovalue); args.addAll(createChoiceOptions((Parameter.ChoiceParameter) param, lvalue)); } else if (param instanceof Parameter.FileParameter) { // args.add(createOption((Parameter.FileParameter) param, null)); } else { args.add(createOption(param, values.get(param.getId()))); } } return args; } private Option createOption(Parameter.IntegerParameter param, Integer value) { var builder = IntegerParameter.newBuilder(); setCommonProperties(param, builder); builder.setDefaultValue((Integer) param.getDefault()); builder.setValue(value); builder.setBounds(param.getMin(), param.getMax()); return builder.build(); } private Option createOption(Parameter.DecimalParameter param, Double value) { var builder = DoubleParameter.newBuilder(); setCommonProperties(param, builder); builder.setDefaultValue((Double) param.getDefault()); builder.setValue(value); builder.setBounds(param.getMin(), param.getMax()); return builder.build(); } private Option createOption(Parameter.TextParameter param, String value) { var builder = StringParameter.newBuilder(); setCommonProperties(param, builder); builder.setDefaultValue((String) param.getDefault()); builder.setValue(value); return builder.build(); } private Option createOption(Parameter.FlagParameter param, Boolean value) { var builder = BooleanOption.newBuilder(); setCommonProperties(param, builder); builder.setDefaultValue((Boolean) param.getDefault()); builder.setValue(value); return builder.build(); } private List