Ejemplo n.º 1
0
  /**
   * 将对象转换为XML报文字符串
   *
   * <p>
   * <li>通过java反射对象,将对象的每一个成员变量都转换成xml的标签
   * <li>然后将获取每个变量的值,放入标签之间
   *
   * @param obj 待转换对象
   * @return xml字符串
   */
  public static String obj2Xml(Object obj) {
    StringBuffer sb = new StringBuffer();
    try {
      Class clz = obj.getClass();
      Field[] fields = clz.getDeclaredFields();
      for (Field field : fields) {
        String fieldName = field.getName();
        sb.append("<" + fieldName + ">");
        String methodName = "get" + DataTools.stringUpdateFirst(field.getName());
        // 反射得到方法
        Method m = clz.getDeclaredMethod(methodName);
        // 通过反射调用set方法
        sb.append(m.invoke(obj));
        sb.append("</" + fieldName + ">");
      }
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }

    return sb.toString();
  }
Ejemplo n.º 2
0
  /** Returns true if the given filename is a gzip file. */
  public static boolean isGZipFile(String file) throws IOException {
    if (!file.toLowerCase().endsWith(".gz")) return false;

    FileInputStream s = new FileInputStream(file);
    byte[] b = new byte[2];
    s.read(b);
    s.close();
    return DataTools.bytesToInt(b, true) == GZIPInputStream.GZIP_MAGIC;
  }
Ejemplo n.º 3
0
  /**
   * <br>
   * Created on: Dec 1, 2012 2:46:13 PM
   *
   * @param fileName
   * @return
   */
  public static String replaceProHome(String fileName) {

    String basedir = System.getProperty("basedir");

    if (DataTools.isEmpty(basedir)) {
      String proHome = "\\$PRO_HOME/";
      return fileName.replaceAll(proHome, "");
    }

    if (Env.getPlatform() == Env.getOsWindows()) basedir = replaceWinSepaToUnix(basedir);

    return fileName.replaceAll("\\$PRO_HOME", basedir);
  }
Ejemplo n.º 4
0
 /**
  * <br>
  * Created on: 2013-8-20 下午09:09:33
  *
  * @param filePath
  * @return
  */
 public static File getFilePath(String filePath) {
   File path = new File(filePath);
   if (!DataTools.isEmpty(path)) {
     if (!path.exists())
       if (!path.mkdirs()) {
         return null;
       }
   } else {
     if (!path.mkdirs()) {
       return null;
     }
   }
   return path;
 }
Ejemplo n.º 5
0
  /**
   * 将XML格式报文转换成对象
   *
   * <p>
   * <li>通过java反射对象,根据对象中的每个成员变量,获取对应报文的值
   * <li>然后调用set方法,放入对象
   * <li>如果里面有LIST,则获取LIST其中的内容,递归调用本方法获取对象
   *
   * @param dgXml 待转换xml
   * @param clz 对象Class
   * @return obj对象
   */
  public static Object xml2Obj(String dgXml, Class clz) {
    Object obj = null;
    try {
      // 创建传出的对象
      obj = clz.newInstance();

      // 获取对象的成员变量
      Field[] fields = clz.getDeclaredFields();
      for (Field field : fields) {
        String fieldName = field.getName();
        if (dgXml.indexOf("</" + fieldName + ">") != -1) {

          String value = getDatagramXmlValue(dgXml, fieldName);

          if (CommonValidate.equals(fieldName, "ROWS")) {
            List<Object> list = new ArrayList<Object>();
            if (!CommonValidate.isEmpty(value)) {
              String className = clz.getName();
              className = className + "ROWS";
              do {
                Object valueObj =
                    xml2Obj(
                        value.substring(
                            value.indexOf("<LIST>") + ("<LIST>").length(),
                            value.indexOf("</LIST>")),
                        Class.forName(className));
                list.add(valueObj);
                value = value.substring(value.indexOf("</LIST>") + ("</LIST>").length());
              } while (!CommonValidate.isEmpty(value));
            }
            String methodName = "setROWS";
            // 反射得到方法
            Method m = clz.getDeclaredMethod(methodName, List.class);
            // 通过反射调用set方法
            m.invoke(obj, list);

          } else {
            String methodName = "set" + DataTools.stringUpdateFirst(field.getName());
            // 反射得到方法
            Method m = clz.getDeclaredMethod(methodName, String.class);
            // 通过反射调用set方法
            m.invoke(obj, value);
          }
        }
      }
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    return obj;
  }
  /**
   * Run an experiment, iterating through all possible combinations of parameters.
   *
   * @param sizes
   * @param dimensions
   * @param repeats
   * @param rbfBasisFunction
   * @param activationFunction
   * @param learningRate
   * @param clusters {number of hidden nodes in each layer, number of clusters}
   * @param momentum
   * @param hiddenNum number of hidden layers
   */
  public static void runExperiment(
      int[] sizes,
      int[] dimensions,
      int[][] repeats,
      int[] rbfBasisFunction,
      ActivationFunction[] activationFunction,
      double[] learningRate,
      int[][] clusters,
      double[] momentum,
      int[] hiddenNum) {

    // Check for empty and null-valued arguments and initialize to default values.
    if (sizes == null || sizes.length == 0) {
      sizes = new int[] {100000};
    }
    if (dimensions == null || dimensions.length == 0) {
      dimensions = new int[] {4};
    }
    if (repeats == null || repeats.length == 0) {
      repeats = new int[][] {{1, 1}};
    }
    if (rbfBasisFunction == null || rbfBasisFunction.length == 0) {
      rbfBasisFunction = new int[] {0};
    }
    if (activationFunction == null || activationFunction.length == 0) {
      activationFunction = new ActivationFunction[] {ActivationFunction.LOGISTIC};
    }
    if (learningRate == null || learningRate.length == 0) {
      learningRate = new double[] {0.01};
    }
    if (clusters == null || clusters.length == 0) {
      clusters = new int[][] {{100, 10000}};
    }
    if (momentum == null || momentum.length == 0) {
      momentum = new double[] {0.01};
    }
    if (hiddenNum == null || hiddenNum.length == 0) {
      hiddenNum = new int[] {1};
    }

    // calculate the number of experiments
    int experimentCount =
        sizes.length
            * repeats.length
            * dimensions.length
            * rbfBasisFunction.length
            * activationFunction.length
            * learningRate.length
            * clusters.length
            * momentum.length
            * hiddenNum.length;

    double[][][][] results = new double[2][experimentCount][][];
    int experimentIndex = 0;

    // Loop through all possible combinations of variables (most arrays should be of size 1)
    for (int a = 0; a < sizes.length; a++) {
      for (int b = 0; b < dimensions.length; b++) {
        // Retrieve a dataset of size a with b inputs, or generate one if it does not exist
        double[][] datasets = DataTools.getDataFromFile(dimensions[b], sizes[a]);

        for (int c = 0; c < repeats.length; c++) {
          for (int d = 0; d < rbfBasisFunction.length; d++) {
            for (int e = 0; e < activationFunction.length; e++) {
              for (int f = 0; f < learningRate.length; f++) {
                for (int g = 0; g < clusters.length; g++) {
                  for (int h = 0; h < momentum.length; h++) {
                    for (int i = 0; i < hiddenNum.length; i++) {

                      // Print information about the next experiment
                      System.out.println("Experiment " + experimentIndex);
                      System.out.println("Number of training examples: " + sizes[a]);
                      System.out.println("Number of inputs: " + dimensions[b]);
                      System.out.println("Repeats: {" + repeats[c][0] + "," + repeats[c][1] + "}");
                      System.out.println("RBF basis function: " + rbfBasisFunction[d]);
                      System.out.println("FF activation function: " + activationFunction[e]);
                      System.out.println("Learning rate: " + learningRate[f]);
                      System.out.println("Clusters: " + clusters[g][1]);
                      System.out.println("Momentum: " + momentum[h]);
                      System.out.println("Hidden layers: " + hiddenNum[i]);
                      System.out.println();

                      // Train and test an RBF neural net
                      results[0][experimentIndex] =
                          trainRBF(
                              sizes[a],
                              dimensions[b],
                              repeats[c],
                              rbfBasisFunction[d],
                              activationFunction[e],
                              learningRate[f],
                              clusters[g],
                              momentum[h],
                              hiddenNum[i],
                              datasets);

                      double aboveRight = results[0][experimentIndex][0][0];
                      double aboveWrong = results[0][experimentIndex][0][1];
                      double belowRight = results[0][experimentIndex][0][2];
                      double belowWrong = results[0][experimentIndex][0][3];
                      double varianceSum = results[0][experimentIndex][0][4];
                      double realSum = results[0][experimentIndex][0][5];
                      double predictionSum = results[0][experimentIndex][0][6];
                      double minError = results[0][experimentIndex][0][7];
                      double maxError = results[0][experimentIndex][0][8];
                      double percentError = results[0][experimentIndex][0][9] / (sizes[a] * 10);

                      double averageError = varianceSum / (sizes[a] * 10);

                      double standardDeviation = 0.0;
                      double averageReal = realSum / (sizes[a] * 10);
                      double averagePrediction = predictionSum / (sizes[a] * 10);

                      double[] errors = results[0][experimentIndex][1];
                      for (int j = 0; j < errors.length; j++) {
                        double error = results[0][experimentIndex][1][j];
                        standardDeviation += Math.pow(error - averageError, 2.0);
                      }

                      standardDeviation /= 10 * sizes[a] - 1;
                      standardDeviation = Math.sqrt(standardDeviation);

                      System.out.println("------------RBF Neural Network------------");
                      System.out.println("    Correct above guesses: " + aboveRight);
                      System.out.println("  Incorrect above guesses: " + aboveWrong);
                      System.out.println("    Correct below guesses: " + belowRight);
                      System.out.println("  Incorrect below guesses: " + belowWrong);
                      System.out.println("       Average real value: " + averageReal);
                      System.out.println("  Average predicted value: " + averagePrediction);
                      System.out.println("            Average error: " + averageError);
                      System.out.println("    Average percent error: " + percentError);
                      System.out.println("  Standard dev. of errors: " + standardDeviation);
                      System.out.println("            Minimum error: " + minError);
                      System.out.println("            Maximum error: " + maxError);
                      System.out.println(
                          "Percent correctly guessed: "
                              + ((aboveRight + belowRight)
                                  / (aboveRight + belowRight + aboveWrong + belowWrong)
                                  * 100));
                      System.out.println();

                      // Train and test a Feedforward neural net
                      results[1][experimentIndex] =
                          trainFF(
                              sizes[a],
                              dimensions[b],
                              repeats[c],
                              rbfBasisFunction[d],
                              activationFunction[e],
                              learningRate[f],
                              clusters[g],
                              momentum[h],
                              hiddenNum[i],
                              datasets);

                      double[] statistics = new double[5];
                      for (int j = 0; j < 5; j++) {
                        for (int k = 0; k < 10; k++) {
                          statistics[k % 5] += results[1][experimentIndex][j][k];
                        }
                      }

                      statistics[4] /= 10;

                      double[] confidences = results[1][experimentIndex][6];

                      confidences[0] /= statistics[0];
                      confidences[1] /= statistics[1];
                      confidences[2] /= statistics[2];
                      confidences[3] /= statistics[3];

                      System.out.println("--------Feed Forward Neural Network--------");
                      System.out.println("  Correct above guesses: " + statistics[0]);
                      System.out.println("Incorrect above guesses: " + statistics[1]);
                      System.out.println("  Correct below guesses: " + statistics[2]);
                      System.out.println("Incorrect below guesses: " + statistics[3]);
                      System.out.println("     Average confidence: " + statistics[4]);
                      System.out.println("Average confidence when...");
                      System.out.println("      Correct and above: " + confidences[0]);
                      System.out.println("    Incorrect and above: " + confidences[1]);
                      System.out.println("      Correct and below: " + confidences[2]);
                      System.out.println("    Incorrect and below: " + confidences[3]);
                      System.out.println();

                      experimentIndex++;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
Ejemplo n.º 7
0
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void addObject(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    Connection c;
    PreparedStatement st;
    ResultSet set;

    String objName;
    int objTileSrc;
    int objXOff;
    int objYOff;
    int objWidth;
    int objHeight;
    String objDesc;
    int objId;
    int objAuth;
    Gson Gson;
    FormResponse resp;

    resp = null;

    c = null;
    st = null;
    set = null;

    Gson = new Gson();

    try {

      //	Verify we have authorization to do this! TODO: Set a special response code on authorization
      // failure.

      //	Perform a quick, cursory validation

      resp = validateAddForm(request);

      if (!resp.isAccepted()) {
        resp.setResult("FAIL");

      } else {

        //	Apply parameters

        objName = request.getParameter("title");
        objDesc = request.getParameter("desc");
        objTileSrc = Integer.parseInt(request.getParameter("tileset"));
        objXOff = Integer.parseInt(request.getParameter("objXOff"));
        objYOff = Integer.parseInt(request.getParameter("objYOff"));
        objWidth = Integer.parseInt(request.getParameter("objWidth"));
        objHeight = Integer.parseInt(request.getParameter("objHeight"));
        // objAuth 	= (Integer) request.getSession().getAttribute("userid");
        // TODO: Get the public flag

        System.out.println("Object name: '" + objName + "'");

        //	Generate the query

        c = DBResourceManager.getConnection();

        st = c.prepareStatement(OBJTYPE_INSERT, PreparedStatement.RETURN_GENERATED_KEYS);

        st.setString(1, objName);
        st.setInt(2, objTileSrc);
        st.setInt(3, objXOff);
        st.setInt(4, objYOff);
        st.setInt(5, objWidth);
        st.setInt(6, objHeight);
        st.setString(7, objDesc);

        st.execute();

        set = st.getGeneratedKeys();

        //	Grab the generated key

        if (set.next()) {
          objId = set.getInt(1);

          resp.setResult("OK");
          resp.addParamResult("objId", "VALUEUP:" + objId);

          //	Send the OK. Note that Dojo requires us to wrap the response
          //	in an html doc's text area for max. compatibility.

        } else {

          System.out.println("Failure to create object.");

          resp.setResult("FAIL");
        }
      }

      //	Send the resposne object no matter what

      response
          .getWriter()
          .println("<html><body><textarea>" + Gson.toJson(resp) + "</textarea></html></body>");

    } catch (Exception ex) {
      ex.printStackTrace();

      if (resp == null) {
        resp = new FormResponse();
        resp.setAcceptance(false);
      }

      resp.setResult("FAIL");
      resp.addMessage(ex.toString());

      response
          .getWriter()
          .println("<html><body><textarea>" + ex.toString() + "</textarea></html></body>");

    } finally {

      DataTools.safeCleanUp(c, st, set);
    }
  }
Ejemplo n.º 8
0
  /**
   * When browsing objects, we'd like to make it possible to interface the object type list with a
   * Dojo Data Source. To do so, we stream the objects out as a JSON object.
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  private void doObjTypeRequestJSON(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int fromIndex, maxResult;
    Connection c;
    PreparedStatement st;
    ResultSet set;
    JsonWriter jOut;
    int recCount;

    //	Set the content up

    response.setContentType("application/json");

    //	Setup our database information

    c = null;
    st = null;
    set = null;

    //	Setup the rest of our environment

    recCount = 0;

    jOut = new JsonWriter(response.getWriter());

    try {

      //	Parse request parameters

      fromIndex = Integer.parseInt(request.getParameter("start"));
      maxResult = Integer.parseInt(request.getParameter("count"));

      //	Create connection to the database and prep the query

      c = DBResourceManager.getConnection();

      st = c.prepareStatement(OBJTYPE_LIST_JSON);
      st.setInt(1, fromIndex);
      st.setInt(2, maxResult);

      //	Execute the query

      set = st.executeQuery();

      //	Now, stream the results out as if they were an object

      jOut.beginObject();
      jOut.name("items").beginArray();

      while (set.next()) {
        //	Write each element out as an object

        jOut.beginObject();

        jOut.name("objid").value(set.getInt("id"));
        jOut.name("objname").value(set.getString("name"));
        jOut.name("objtilesrc").value(set.getString("tilesrc"));
        jOut.name("description").value(set.getString("description"));

        jOut.endObject();

        //	Increment the record count

        recCount++;
      }

      //	Terminate the array

      jOut.endArray();

      //	Note the number of rows

      jOut.name("numRows").value(recCount);

      //	End the overall object

      jOut.endObject();

      //	Flush the output

      jOut.flush();

    } catch (Exception ex) {
      ex.printStackTrace();

    } finally {

      DataTools.safeCleanUp(c, st, set);
    }
  }