parameter set serialization/deserialization and preset/userdefined parameter set...
authorjprocter <Jim Procter>
Fri, 3 Sep 2010 08:19:23 +0000 (08:19 +0000)
committerjprocter <Jim Procter>
Fri, 3 Sep 2010 08:19:23 +0000 (08:19 +0000)
src/jalview/gui/WsJobParameters.java
src/jalview/ws/jws2/ParameterUtils.java

index cbfa273..3facfa1 100644 (file)
@@ -290,8 +290,8 @@ public class WsJobParameters extends JPanel implements ItemListener,
     setName.setEditable(true);
     setName.addItemListener(this);
     setName.getEditor().addActionListener(this);
-    JPanel setNameInfo=new JPanel(new FlowLayout(FlowLayout.LEFT));
-    SetNamePanel.setLayout(new GridLayout(2,1));
+    JPanel setNameInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
+    SetNamePanel.setLayout(new GridLayout(2, 1));
     SetNamePanel.setMinimumSize(new Dimension(300, 40));
     JLabel setNameLabel = new JLabel("Parameter set: ");
     setNameLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
@@ -305,14 +305,14 @@ public class WsJobParameters extends JPanel implements ItemListener,
     createpref.setVisible(false);
     JPanel setsavebuts = new JPanel();
     setsavebuts.setLayout(new FlowLayout(FlowLayout.LEFT)); // GridLayout(1,2));
-    ((FlowLayout)setsavebuts.getLayout()).setHgap(10);
-    ((FlowLayout)setsavebuts.getLayout()).setVgap(0);
+    ((FlowLayout) setsavebuts.getLayout()).setHgap(10);
+    ((FlowLayout) setsavebuts.getLayout()).setVgap(0);
     setsavebuts.add(deletepref);
     setsavebuts.add(revertpref);
     setsavebuts.add(createpref);
     setsavebuts.add(updatepref);
     setsavebuts.setSize(new Dimension(150, 20));
-    JPanel buttonArea=new JPanel(new GridLayout(1,1));
+    JPanel buttonArea = new JPanel(new GridLayout(1, 1));
     buttonArea.add(setsavebuts);
     SetNamePanel.add(setNameInfo);
     SetNamePanel.add(buttonArea);
@@ -689,7 +689,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
       while (_getUserPreset(tnm) != null)
       {
         tnm = nm + " (" + (++i) + ")";
-        renamed=true;
+        renamed = true;
       }
       if (i > 0)
       {
@@ -715,7 +715,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
     }
     if (renamed)
     {
-      settingDialog=false; // we need this name change to be registered.
+      settingDialog = false; // we need this name change to be registered.
       argSetModified(setName, renamed);
     }
     settingDialog = stn;
@@ -756,7 +756,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
     {
       cb = addOption(opt);
     }
-    cb.enabled.setSelected(string!=null); // initial state for an option.
+    cb.enabled.setSelected(string != null); // initial state for an option.
     if (string != null)
     {
       if (opt.getPossibleValues().contains(string))
@@ -1224,7 +1224,7 @@ public class WsJobParameters extends JPanel implements ItemListener,
       {
         return null;
       }
-      Option opt=null;
+      Option opt = null;
       try
       {
         opt = jalview.ws.jws2.ParameterUtils.copyOption(option);
@@ -1258,9 +1258,12 @@ public class WsJobParameters extends JPanel implements ItemListener,
         if (initVal != null)
         {
           notmod &= initVal.equals(val.getSelectedItem());
-        } else {
+        }
+        else
+        {
           // compare against default service setting
-          notmod &= option.getDefaultValue()==null || option.getDefaultValue().equals(val.getSelectedItem()); 
+          notmod &= option.getDefaultValue() == null
+                  || option.getDefaultValue().equals(val.getSelectedItem());
         }
       }
       else
@@ -1340,10 +1343,10 @@ public class WsJobParameters extends JPanel implements ItemListener,
   {
     FlowLayout fl = new FlowLayout();
     paramList.setLayout(fl);
-    int s = 2*fl.getVgap();
+    int s = 2 * fl.getVgap();
     for (ParamBox pbox : paramSet.values())
     {
-      s += fl.getVgap()+pbox.getBoxHeight(); // getBoxHeight();
+      s += fl.getVgap() + pbox.getBoxHeight(); // getBoxHeight();
     }
     paramList.setPreferredSize(new Dimension(PARAM_WIDTH, s));
     validate();
@@ -1458,6 +1461,46 @@ public class WsJobParameters extends JPanel implements ItemListener,
                 }
               }
             }
+            {
+              System.out.println("Testing param write:");
+              List<String> writeparam = null, readparam = null;
+              try
+              {
+                writeparam = jalview.ws.jws2.ParameterUtils
+                        .writeParameterSet(
+                                pr.getArguments(lastserv.getRunnerConfig()),
+                                " ");
+                System.out.println("Testing param read :");
+                List<Option> pset = jalview.ws.jws2.ParameterUtils
+                        .processParameters(writeparam,
+                                lastserv.getRunnerConfig(), " ");
+                readparam = jalview.ws.jws2.ParameterUtils
+                        .writeParameterSet(pset, " ");
+                Iterator<String> o=pr.getOptions().iterator(),s = writeparam.iterator(), t = readparam
+                .iterator();
+                boolean failed=false;
+                while (s.hasNext() && t.hasNext())
+                {
+                  String on=o.next(),sn = s.next(),st=t.next();
+                  if (!sn.equals(st))
+                  {
+                    System.out.println("Original was "+on+" Phase 1 wrote " + sn
+                          + "\tPhase 2 wrote " + st);
+                    failed=true;
+                  }
+                }
+                if (failed)
+                {
+                  System.out.println("Original parameters:\n"+pr.getOptions());
+                  System.out.println("Wrote parameters in first set:\n"+writeparam);
+                  System.out.println("Wrote parameters in second set:\n"+readparam);
+
+                }
+              } catch (Exception e)
+              {
+                e.printStackTrace();
+              }
+            }
             WsJobParameters pgui = new WsJobParameters(lastserv, pr);
             JFrame jf = new JFrame("Parameters for "
                     + lastserv.getActionText());
@@ -1715,15 +1758,16 @@ public class WsJobParameters extends JPanel implements ItemListener,
       if (isModified())
       {
         String lsetname = (curSetName != null) ? curSetName : lastParmSet;
-        if (lsetname.equals(setname)) {
+        if (lsetname.equals(setname))
+        {
           // setname was just edited - so ignore this event.
           return;
         }
-        settingDialog=true;
+        settingDialog = true;
         System.out.println("Prompting to save " + lsetname);
         if (javax.swing.JOptionPane.showConfirmDialog(this,
-                "Parameter set '"+lsetname+"' is modifed\nsave ?", "Save changes ?",
-                javax.swing.JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)
+                "Parameter set '" + lsetname + "' is modifed\nsave ?",
+                "Save changes ?", javax.swing.JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)
         {
           System.out.println("Saving for " + lsetname);
           _storeCurrentPreset(lsetname);
@@ -1820,4 +1864,25 @@ public class WsJobParameters extends JPanel implements ItemListener,
   {
     checkDescrModified();
   }
+
+  public Preset getPreset()
+  {
+    if (isUserPreset || isModified()
+            || (lastParmSet != null && lastParmSet.equals(SVC_DEF)))
+    {
+      return null;
+    }
+    else
+    {
+      Preset p = null;
+      try
+      {
+        return (Preset) service.getPresets().getPresetByName(lastParmSet);
+      } catch (Exception e)
+      {
+        e.printStackTrace();
+        return null;
+      }
+    }
+  }
 }
\ No newline at end of file
index 00878fb..503285a 100644 (file)
 package jalview.ws.jws2;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 
 import compbio.metadata.*;
+
 public class ParameterUtils
 {
+  public static List<String> writeParameterSet(List<Option> optSet, String pseparator)
+  {
+    List<String> pset = new ArrayList<String>();
+    for (Option o:optSet)
+    {
+      pset.add(o.toCommand(pseparator));
+    }
+    return pset;
+  }
+  /**
+   * Converts options supplied via parameters file into {@code Option} objects
+   * (Refactored from compbio.ws.client.Jws2Client)
+   * 
+   * @param <T>
+   *          web service type
+   * @param params
+   * @param options
+   * @return List of Options of type T
+   * 
+   */
+/*  @SuppressWarnings(value =
+  { "true" })
+  public static <T> List<Option<T>> processParameters(List<String> params,
+          RunnerConfig<T> options, String pseparator)
+  */
+  public static List<Option> processParameters(List<String> params,
+          RunnerConfig options, String pseparator)
+  {
+    List<Option> chosenOptions = new ArrayList<Option>();
+    for (String param : params)
+    {
+      String oname = null;
+      if (isParameter(param, pseparator))
+      {
+        oname = getParamName(param, pseparator);
+      }
+      else
+      {
+        oname = param;
+      }
+      Option o = options.getArgumentByOptionName(oname);
+      if (o == null)
+      {
+        System.out.println("WARN ignoring unsuppoted parameter: " + oname);
+        continue;
+      }
+      if (o instanceof Parameter)
+      {
+        o = copyParameter((Parameter) o);
+      }
+      else
+      {
+        o = copyOption(o);
+      }
+      {
+        try
+        {
+          o.setDefaultValue(isParameter(param, pseparator) ? getParamValue(param, pseparator) : param);
+        } catch (WrongParameterException e)
+        {
+          System.out.println("Problem setting value for the parameter: "
+                  + param);
+          e.printStackTrace();
+        }
+      }
+      chosenOptions.add(o);
+    }
+    return chosenOptions;
+  }
+  
+
+  static String getParamName(String fullName, String pseparator)
+  {
+    assert isParameter(fullName, pseparator);
+    return fullName.substring(0, fullName.indexOf(pseparator));
+  }
+
+  static String getParamValue(String fullName, String pseparator)
+  {
+    assert isParameter(fullName, pseparator);
+    return fullName.substring(fullName.indexOf(pseparator) + 1);
+  }
+
+  static boolean isParameter(String param, String pseparator)
+  {
+    return param.contains(pseparator);
+  }
 
   public static Option copyOption(Option option)
   {
@@ -13,6 +105,7 @@ public class ParameterUtils
     setOptionFrom(copy, option);
     return copy;
   }
+
   public static void setOptionFrom(Option copy, Option option)
   {
     copy.setName(option.getName());
@@ -20,18 +113,23 @@ public class ParameterUtils
     copy.setFurtherDetails(option.getFurtherDetails());
     copy.setRequired(option.isRequired());
     List<String> names = option.getOptionNames();
-    if (names!=null){
-      if (names.size()==1)
+    if (names != null)
+    {
+      if (names.size() == 1)
       {
         HashSet<String> st = new HashSet();
         st.add(names.get(0));
         copy.setOptionNames(st);
-      } else {
-      copy.addOptionNames(names.toArray(new String[]{}));
+      }
+      else
+      {
+        copy.addOptionNames(names.toArray(new String[]
+        {}));
       }
     }
-    try {
-      if (option.getDefaultValue()!=null)
+    try
+    {
+      if (option.getDefaultValue() != null)
       {
         copy.setDefaultValue(option.getDefaultValue());
       }
@@ -43,34 +141,43 @@ public class ParameterUtils
 
   public static ValueConstrain copyValueConstrain(ValueConstrain vc)
   {
-    try {ValueConstrain copy = new ValueConstrain();
-    if (vc.getMax()!=null) {
-      copy.setMax(vc.getMax().toString());
-    }
-    if (vc.getMin()!=null) {
-      copy.setMin(vc.getMin().toString());
-    }
-    if (vc.getType()!=null) {
-      copy.setType(vc.getType());
-    }
-    return copy;
-    }
-    catch (Exception e)
+    try
+    {
+      ValueConstrain copy = new ValueConstrain();
+      if (vc.getMax() != null)
+      {
+        copy.setMax(vc.getMax().toString());
+      }
+      if (vc.getMin() != null)
+      {
+        copy.setMin(vc.getMin().toString());
+      }
+      if (vc.getType() != null)
+      {
+        copy.setType(vc.getType());
+      }
+      return copy;
+    } catch (Exception e)
     {
       e.printStackTrace();
-      throw new Error("Implementation error: could not copy ValueConstrain!");
+      throw new Error(
+              "Implementation error: could not copy ValueConstrain!");
     }
   }
+
   public static Parameter copyParameter(Parameter parameter)
   {
-    Parameter copy = new Parameter(parameter.getName(), parameter.getDescription());
-    if (parameter.getValidValue()!=null) {
+    Parameter copy = new Parameter(parameter.getName(),
+            parameter.getDescription());
+    if (parameter.getValidValue() != null)
+    {
       copy.setValidValue(copyValueConstrain(parameter.getValidValue()));
     }
     List<String> pv = parameter.getPossibleValues();
-    if (pv!=null)
+    if (pv != null)
     {
-      copy.addPossibleValues(pv.toArray(new String[]{}));
+      copy.addPossibleValues(pv.toArray(new String[]
+      {}));
     }
     setOptionFrom(copy, parameter);
     return copy;