コード例 #1
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
  /**
   * 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;
  }
コード例 #2
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
  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;
  }
コード例 #3
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
 /** 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;
 }
コード例 #4
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
 /**
  * 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();
 }
コード例 #5
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
 /** Returns parameter type of named parameter in list, if not exist throws exception. */
 public String getType(String name) throws ParameterException {
   name = getParameterName(name);
   int index = getIndexOf(name);
   if (index != -1) {
     ParameterAPI param = (ParameterAPI) params.get(index);
     String str = param.getType();
     return str;
   } else {
     String S = C + ": getType(): ";
     throw new ParameterException(S + "No parameter exists named " + name);
   }
 }
コード例 #6
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
 public String getParameterListMetadataString(String delimiter) {
   int size = params.size();
   StringBuffer metaData = new StringBuffer();
   boolean first = true;
   for (int i = 0; i < size; ++i) {
     ParameterAPI tempParam = (ParameterAPI) params.get(i);
     if (first) {
       metaData.append(tempParam.getMetadataString());
       first = false;
     } else metaData.append(delimiter + tempParam.getMetadataString());
   }
   return metaData.toString();
 }
コード例 #7
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
  /**
   * Returns a copy of this list, therefore any changes to the copy cannot affect this original
   * list.
   */
  public Object clone() {

    String S = C + ": clone(): ";
    ParameterList list = new ParameterList();
    if (this.size() < 1) return list;
    int size = this.params.size();
    for (int i = 0; i < size; ++i) {
      ParameterAPI param = (ParameterAPI) params.get(i);
      list.addParameter((ParameterAPI) param.clone());
    }

    return list;
  }
コード例 #8
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
  /** Returns parameter contained value object if the parameter exist, else throws exception. */
  public Object getValue(String name) throws ParameterException {

    name = getParameterName(name);
    int index = getIndexOf(name);
    if ((index != -1)) {
      ParameterAPI param = (ParameterAPI) params.get(index);
      Object obj = param.getValue();
      return obj;
    } else {
      String S = C + ": getValue(): ";
      throw new ParameterException(S + "No parameter exists named " + name);
    }
  }
コード例 #9
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
  /** 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();
  }
コード例 #10
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
  /** Set's a new value to a Parameter in the list if it exists, else throws exception. */
  public void setValue(String name, Object value) throws ParameterException, ConstraintException {

    String S = C + ": setValue(): ";
    if (D) System.out.println(S + "Starting");

    name = getParameterName(name);
    int index = getIndexOf(name);
    if (index != -1) {
      ParameterAPI param = (ParameterAPI) params.get(index);
      param.setValue(value);
    } else {

      throw new ParameterException(S + "No parameter exists named " + name);
    }

    if (D) System.out.println(S + "Ending");
  }
コード例 #11
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
  /**
   * 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;
  }
コード例 #12
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
  /** 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;
    }
  }
コード例 #13
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
  /**
   * 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);
    }
  }
コード例 #14
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
  /**
   * 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;
  }
コード例 #15
0
ファイル: ParameterList.java プロジェクト: angri/OpenSHA
 /**
  * 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);
 }