public static boolean setParamsInListFromXML(ParameterList paramList, Element paramListEl) { boolean failure = false; for (ParameterAPI<?> param : paramList) { Iterator<Element> it = paramListEl.elementIterator(); boolean matched = false; while (it.hasNext()) { Element el = it.next(); if (param.getName().equals(el.attribute("name").getValue())) { matched = true; // System.out.println("Found a match!"); if (param.setValueFromXMLMetadata(el)) { // System.out.println("Parameter set successfully!"); } else { System.err.println("Parameter could not be set from XML!"); System.err.println( "It is possible that the parameter type doesn't yet support loading from XML"); failure = true; } } } if (!matched) { System.err.println( "Parameter '" + param.getName() + "' from XML can not be set because it can't be" + " found in the given ParameterList!"); failure = true; } } return !failure; }
/** * Returns true if all the parameters have the same names and values. One use will be to determine * if two DisctetizedFunctions are the same, i.e. set up with the same independent parameters. */ public boolean equalsParameterList(ParameterList list) { // Not same size, can't be equal if (this.size() != list.size()) return false; // Check each individual Parameter ListIterator<ParameterAPI<?>> it = this.getParametersIterator(); while (it.hasNext()) { // This list's parameter ParameterAPI<?> param1 = it.next(); // List may not contain parameter with this list's parameter name if (!list.containsParameter(param1.getName())) return false; // Found two parameters with same name, check equals, actually // redundent, // because that is what equals does ParameterAPI<?> param2 = list.getParameter(param1.getName()); if (!param1.equals(param2)) return false; // Now try compare to to see if value the same, can fail if two // values // are different, or if the value object types are different try { if (param1.compareTo(param2) != 0) return false; } catch (ClassCastException ee) { return false; } } // Passed all tests - return true return true; }
/** Returns the index of the named Parameter in this list. Returns -1 if not found. */ private int getIndexOf(String key) { int size = params.size(); for (int i = 0; i < size; ++i) { ParameterAPI param = (ParameterAPI) params.get(i); if (key.equalsIgnoreCase(param.getName())) return i; } return -1; }
/** * Returns an iterator of all parameter names of the paramters in the list. Returns the list in * the order the elements were added. */ public ListIterator<String> getParameterNamesIterator() { ArrayList<String> v = new ArrayList<String>(); int size = this.params.size(); for (int i = 0; i < size; ++i) { ParameterAPI obj = (ParameterAPI) params.get(i); v.add(obj.getName()); } return v.listIterator(); }
/** Checks if the parameter exists in the list. Returns true if it does, else returns false. */ public boolean containsParameter(ParameterAPI param) { String name = param.getName(); int index = getIndexOf(name); if (index != -1) { return true; } else { return false; } }
/** Prints out all parameters in this list. For debugging purposes */ public String toString() { String S = C + ": toString():"; StringBuffer b = new StringBuffer(); boolean first = true; ArrayList<String> v = new ArrayList<String>(); int vectorSize = params.size(); for (int i = 0; i < vectorSize; ++i) { ParameterAPI param = (ParameterAPI) params.get(i); v.add(param.getName()); } Iterator<String> it = v.iterator(); while (it.hasNext()) { String key = (String) it.next(); if (D) System.out.println(S + "Next Parameter Key = " + key); int index = getIndexOf(key); ParameterAPI param = (ParameterAPI) params.get(index); ParameterConstraintAPI constraint = param.getConstraint(); boolean ok = true; if (constraint instanceof DiscreteParameterConstraintAPI) { int size = ((DiscreteParameterConstraintAPI) constraint).size(); if (size < 2) ok = false; } if (ok) { String val = "N/A"; Object obj = param.getValue(); if (obj != null) val = obj.toString(); if (D) System.out.println(S + val); if (first) { first = false; b.append(key + " = " + val); } else { b.append(", " + key + " = " + val); } } } return b.toString(); }
/** * Returns true if all the parameters have the same names. One use will be to determine if two * DisctetizedFunctions can be plotted on the same axis, i.e. set up with the same independent * parameters. */ public boolean equalNames(ParameterList list) { // Not same size, can't be equal if (this.size() != list.size()) return false; // Check each individual Parameter ListIterator<ParameterAPI<?>> it = this.getParametersIterator(); while (it.hasNext()) { // This list's parameter ParameterAPI<?> param1 = it.next(); // List may not contain parameter with this list's parameter name if (!list.containsParameter(param1.getName())) return false; } // Passed all tests - return true return true; }
/** * Adds the parameter to the internal sotrage of parameters if it doesn't exist, else throws * exception. If the constraint has a different name from the parameter, the constraint name is * mapped to the parameter name. */ public void addParameter(ParameterAPI param) throws ParameterException { String S = C + ": addParameter(): "; String name = param.getName(); String constraintName = param.getConstraintName(); if (getIndexOf(name) == -1) params.add(param); else throw new ParameterException(S + "A Parameter already exists named " + name); if (constraintName == null || constraintName.equals("") || constraintName.equals(name)) return; if (!constraintNameMap.containsKey(constraintName)) { constraintNameMap.put(constraintName, name); } else { params.remove(name); throw new ParameterException( S + "A Parameter already exists with this constraint named " + constraintName); } }
/** * compares 2 ParameterList to see if they are equal. It compares them by checking each parameter * with the passed argument obj is present in the parameterList, it is compared with. It also * checks if both the parameterLists have same parameters and have the same values. * * @param obj instance of ParameterList * @return int 0 if both object are same else return -1 */ public int compareTo(Object obj) { int result = 0; if (!(obj instanceof ParameterList)) { throw new ClassCastException(C + "Object not a ParameterList, unable to compare"); } ParameterList paramList = (ParameterList) obj; ListIterator<ParameterAPI<?>> it = paramList.getParametersIterator(); if (size() != paramList.size()) return -1; while (it.hasNext()) { ParameterAPI param1 = (ParameterAPI) it.next(); ParameterAPI param2 = (ParameterAPI) getParameter(param1.getName()); result = param2.compareTo(param1); if (result != 0) break; } return result; }
/** * Updates an existing parameter with the new value. Throws parameter exception if parameter * doesn't exist. */ public void updateParameter(ParameterAPI param) throws ParameterException { String name = param.getName(); removeParameter(name); addParameter(param); }