예제 #1
0
 /** Compute the parameters need to create each cells */
 private void initParameters() {
   this.minX = envelope.getMinX();
   this.minY = envelope.getMinY();
   double cellWidth = envelope.getWidth();
   double cellHeight = envelope.getHeight();
   this.maxI = (int) Math.ceil(cellWidth / deltaX);
   this.maxJ = (int) Math.ceil(cellHeight / deltaY);
 }
예제 #2
0
  public int score_Des(int nbDes) {

    int score = 0;
    for (int i = 0; i < nbDes; i++) {
      score = score + (int) Math.floor(Math.random() * (3)) + 1;
    }
    return (score);
  }
예제 #3
0
 public boolean loi_reussite() {
   int proba = (int) Math.floor(Math.random() * (100)) + 1;
   if (proba <= 80) {
     System.out.println("Action REUSSIE ! (" + proba + " sur 80%)");
     return (true);
   } else {
     System.out.println("Action RATEE ! (" + proba + " sur 80%)");
     return (false);
   }
 }
예제 #4
0
 private String getTweetPadding(int tweetIdx) { // copiado do Utils.java
   int MaxNTweets = 10000;
   StringBuilder strBuild = new StringBuilder();
   int current = (int) Math.floor(Math.log10(tweetIdx)) + 1;
   int expected = (int) Math.floor(Math.log10(MaxNTweets));
   if (tweetIdx == 0) current = 1;
   for (int i = 0; i < (expected - current); i++) strBuild.append(0);
   strBuild.append(tweetIdx);
   return strBuild.toString();
 }
예제 #5
0
 public troll(int id, String nom, int att, int deg, int esq, int tailleEchiquier) {
   idTroll = id;
   nomTroll = nom;
   this.att = att;
   this.deg = deg;
   this.esq = esq;
   this.vie = 40 - (att + deg + esq);
   paRestants = 6;
   positionX = (int) Math.floor(Math.random() * (tailleEchiquier)) + 1;
   positionY = (int) Math.floor(Math.random() * (tailleEchiquier)) + 1;
   valeur = "" + nomTroll.charAt(0);
 }
예제 #6
0
  /**
   * Compare approximate and exact diameter computation. Approximate is very fast but provides only
   * a rough upper bound. For a discretized pairwise distance distribution, this is absolutely
   * sufficient.
   */
  public void testMaxPhaseSpaceDiameter() {

    int tsLen = 25000;
    TimeSeriesGenerator tsg = new TimeSeriesGenerator(1, tsLen, "lorenz");
    double[][][][] trajectories = tsg.getAllTrajectories();
    double[][] ts = trajectories[4][0];
    System.out.println("Computing max phase space diameter.");
    long tic = System.currentTimeMillis();
    double diam = PhaseSpaceDistribution.maxPhaseSpaceDiameter(ts, ts);
    System.out.println("diam = " + diam);
    long toc = System.currentTimeMillis();
    System.out.println(toc - tic);

    System.out.println("Computing max phase space diameter, approximate.");
    tic = System.currentTimeMillis();
    double diamApprox = PhaseSpaceDistribution.maxPhaseSpaceDiameterApproximate(ts);
    System.out.println("diamApprox = " + diamApprox);
    toc = System.currentTimeMillis();
    System.out.println(toc - tic);
    System.out.println("Relative error: " + (Math.abs(diamApprox - diam) / diam));

    //        GeometryFactory factory = new GeometryFactory(new
    // PrecisionModel(PrecisionModel.FIXED));
    //        Point[] points = new Point[tsLen];
    //        for (int i = 0; i < tsLen; i++) {
    //            Coordinate[] coordinates = new Coordinate[ts.length];
    //            for (int j = 0; j < ts.length; j++) {
    //                coordinates[j] = new Co
    //            }
    //            points[i] = new Point(new CoordinateArraySequence(coordinates), factory);
    //        }
    //        MultiPoint data = new MultiPoint(points, factory);
    //        MinimumDiameter minimumDiameter = new MinimumDiameter(geom);

  }
예제 #7
0
 /**
  * Correct all pointers that point behind a new entry.
  *
  * @param startOffset the startoffset of the current node
  * @param oldOffset the offset of the new entry, only pointer that point behind it need to
  *     correct.
  * @param diff the differenz that need added to the pointers
  * @param level the stack level. There are only 3 levels with pointers.
  */
 private final void correctPointers(int startOffset, int oldOffset, int diff, int level) {
   offset = startOffset;
   boolean firstNode = true;
   while (offset < size) {
     if (offset == oldOffset) {
       int absDiff = Math.abs(diff);
       if (absDiff == 2) return;
       offset += absDiff;
       firstNode = false;
       continue;
     }
     int value = getUnsignedShort();
     if (value != 0 || firstNode) {
       int pointer = getPointer();
       if (pointer > oldOffset) {
         offset -= pointerSize;
         writePointer(pointer + diff);
         if (diff > 0) pointer += diff;
       }
       if (level < 2) {
         startOffset = offset;
         correctPointers(pointer, oldOffset, diff, level + 1);
         offset = startOffset;
       }
       firstNode = false;
     } else {
       return;
     }
   }
 }
예제 #8
0
 @Override
 public int executeUpdate(String sql) throws SQLException {
   checkClosed();
   if (execute(sql) && sqlResponse.rowCount() > 0) {
     resultSet = null;
     throw new SQLException("Execution of statement returned a ResultSet");
   } else {
     // return 0 if no affected Rows are given
     return (int) Math.max(0L, sqlResponse.rowCount());
   }
 }
예제 #9
0
  public boolean equalRecords(Object a, Object b) {
    if (a instanceof java.sql.Timestamp && b instanceof java.sql.Timestamp) {
      // hack here, since different DB realization;
      long diff = ((Timestamp) a).getTime() - ((Timestamp) b).getTime();
      // if more than N milliseconds;
      if (Math.abs(diff) < stampPrecision) {
        return true;
      }
    }

    return a.equals(b);
  }
예제 #10
0
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      Connection con =
          DriverManager.getConnection(Utility.connection, Utility.username, Utility.password);

      String email = request.getParameter("email_id");

      String number = "";
      boolean exists = false;
      String user_name = "";
      int user_id = -1;
      String str1 = "SELECT USER_ID,NAME,PHONE_NUMBER FROM USERS WHERE EMAIL_ID=?";
      PreparedStatement prep1 = con.prepareStatement(str1);
      prep1.setString(1, email);
      ResultSet rs1 = prep1.executeQuery();
      if (rs1.next()) {
        exists = true;
        user_id = rs1.getInt("USER_ID");
        user_name = rs1.getString("NAME");
        number = rs1.getString("PHONE_NUMBER");
      }
      int verification = 0;
      JSONObject data = new JSONObject();
      if (exists) {
        verification = (int) (Math.random() * 9535641 % 999999);
        System.out.println("Number " + number + "\nVerification: " + verification);
        SMSProvider.sendSMS(
            number, "Your One Time Verification Code for PeopleConnect Is " + verification);
      }

      data.put("user_name", user_name);
      data.put("user_id", user_id);
      data.put("verification_code", "" + verification);
      data.put("phone_number", number);

      String toSend = data.toJSONString();
      out.print(toSend);
      System.out.println(toSend);

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      out.close();
    }
  }
예제 #11
0
 /* cols are experiment (already set), probeid, block, row, col, ip, wce, mor */
 public void processBodyLine(String fields[]) {
   try {
     double ip = 0.001 + Double.parseDouble(fields[ipcol]);
     double wce = 0.001 + Double.parseDouble(fields[wcecol]);
     insert.setString(2, fields[idcol]);
     insert.setInt(3, blockcol == -1 ? 1 : Integer.parseInt(fields[blockcol]));
     insert.setInt(4, Integer.parseInt(fields[rowcol]));
     insert.setInt(5, Integer.parseInt(fields[colcol]));
     insert.setDouble(6, ip);
     insert.setDouble(7, wce);
     insert.setDouble(8, Math.pow(10, Double.parseDouble(fields[lrcol])));
     insert.execute();
   } catch (SQLException e) {
     throw new DatabaseException(e.toString(), e);
   } catch (ArrayIndexOutOfBoundsException e) {
     System.err.println("Skipping a line of input because of ");
     e.printStackTrace();
   }
 }
예제 #12
0
    protected List<DatabaseObject> mergeDatabaseObjects(List<DatabaseObject> objects) {
      int maxIdx = -1;

      for (DatabaseObject object : objects) {
        maxIdx = Math.max(maxIdx, object.getOrder());
      }

      final List<DatabaseObject> orderedObjects = createListAndFillNull(maxIdx);
      final List<DatabaseObject> unorderedObjects = new ArrayList<>();
      final List<DatabaseObject> mergedObjects = new ArrayList<>();

      for (DatabaseObject object : objects) {
        if (object.getOrder() > -1) {
          int objIdx = object.getOrder();

          if (objIdx < orderedObjects.size() && orderedObjects.get(objIdx) != null) {
            throw new TajoInternalError(
                "This catalog configuration contains duplicated order of DatabaseObject");
          }

          orderedObjects.add(objIdx, object);
        } else {
          unorderedObjects.add(object);
        }
      }

      for (DatabaseObject object : orderedObjects) {
        if (object != null) {
          mergedObjects.add(object);
        }
      }

      for (DatabaseObject object : unorderedObjects) {
        if (object != null) {
          mergedObjects.add(object);
        }
      }

      return mergedObjects;
    }
 public WeightMatrix getRepresentative(Cluster<WeightMatrix> cluster) {
   Set<WeightMatrix> matrices = cluster.getElements();
   WeightMatrix bestwm = null;
   double bestdist = Double.MAX_VALUE;
   for (WeightMatrix i : matrices) {
     double sum = 0;
     for (WeightMatrix j : matrices) {
       sum += Math.pow(comp.compare(i, j), 2);
     }
     //            System.err.println("  " + i + " : " + sum + " <? " + bestdist);
     sum = sum / matrices.size();
     if (sum < bestdist) {
       bestwm = i;
       bestdist = sum;
     }
   }
   if (bestwm == null) {
     System.err.println("OOPS!" + bestdist);
     System.err.println(matrices.toString());
   }
   return bestwm;
 }
예제 #14
0
  // dalam menit
  public int waktuTunggu() {
    int out = 13; // nilai aman

    // tentukan dulu diantara 3
    // pendek 5 menit
    // menengah 15 menit
    // panjang 30 menit

    int[] arrBatasWaktu = new int[4];
    arrBatasWaktu[0] = 3;
    arrBatasWaktu[1] = 5;
    arrBatasWaktu[2] = 8;
    arrBatasWaktu[3] = 10;

    // setelah itu baru tentukan random dalam rentang +- 30%
    int pil = randomWithRange(0, 3);
    int batas = (int) Math.round(arrBatasWaktu[pil] * 0.3);
    int batasAtas = arrBatasWaktu[pil] + batas;
    int batasBawah = arrBatasWaktu[pil] - batas;
    // System.out.println("batas atas  ="+batasAtas);
    // System.out.println("batas bawah ="+batasBawah);
    out = randomWithRange(batasBawah, batasAtas);
    return out;
  }
예제 #15
0
  public void start2() {

    wm = new double[20][Data.windows_size];
    String QueryKinasesName = "%" + Data.kinease + "%";
    // data.code = data.codenames[3];
    String QueryCodeName = Data.code;
    int windows_size = Data.windows_size;

    // int windows_size = 9;

    int shift = windows_size / 2;

    try {

      {
        shift = windows_size / 2;
        // establish connection to database

        String ACC = null;
        String SEQUENCE = null;
        String KINASES = null;
        String LIKE = "LIKE";
        // int POSITION = 0;
        // int index = 0;
        String temp = null;
        // int numtemp = 0;
        // int LENGTH = (int) 0;
        // int count = 0;
        double[] totalAAcount = new double[windows_size];

        double weightmatrix[][] = new double[windows_size][128]; // windowssize;aa;

        /// fout.println("#"+statementquery1);

        int seqsize = 0;

        for (int i = 0; i < pwmseq.size(); i++) {
          String posseq = pwmseq.elementAt(i).toString();
          seqsize = posseq.length();
          {
            for (int j = 0; j < seqsize; j++) {

              weightmatrix[j][posseq.charAt(j)]++;
            }
            // possequence.addElement(posseq);
          }
        } // end while
        char[] aaMap = {
          'A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W',
          'Y', 'V', 'X'
        };
        double[] expmatrix = {
          0.0701313443873091,
          0.0582393695201718,
          0.0359362961736045,
          0.0520743144385134,
          0.0172010453343506,
          0.0498574962335004,
          0.0796465136978452,
          0.0624720283551962,
          0.0241405228512130,
          0.0416989778376737,
          0.0934441156861220,
          0.0632334844952389,
          0.0213293464067050,
          0.0324554733241482,
          0.0651181982370858,
          0.0881672518230193,
          0.0524630941595624,
          0.0101093184162382,
          0.0244701177088640,
          0.0578116909136386
        };
        // double[] aaMapfreq = new double[windows_size];
        double freq = 0;
        for (int j = 0; j < weightmatrix.length; j++) {
          for (int i = 0; i < aaMap.length - 1; i++) {
            totalAAcount[j] += weightmatrix[j][aaMap[i]];
          }
        }

        for (int i = 0; i < aaMap.length - 1; i++) {
          // profilefout.print(aaMap[i]);
          for (int j = 0; j < windows_size; j++) {

            freq = ((weightmatrix[j][aaMap[i]] + 0.05) / (totalAAcount[j] + 1));

            wm[i][j] = Math.log10((freq / expmatrix[i])) / Math.log10(2.0);

            //  profilefout.print(","+aaMapfreq[i]);

          }
          // profilefout.println();
        }

        // fout.close();
        // profilefout.close();

      }

    } // end try
    catch (NullPointerException nullpointerException) {
      nullpointerException.printStackTrace();
      System.exit(1);

    } catch (NoClassDefFoundError ex) {

    } finally { // ensure statement and connection are closed properly
      try {

      } catch (Exception exception) { // end try
        exception.printStackTrace();
        System.exit(1);
      } // end catch
    } // end finally
  } // end main
예제 #16
0
  private void jButton3ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton3ActionPerformed
    jTextArea1.setText("" + "Termset\n\t\t");
    int c = 0;
    String[] tn = new String[50];
    int i1 = 0;
    float v1 = 0;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection con =
          DriverManager.getConnection("jdbc:mysql://localhost:3306/txt mine", "root", "root");
      Statement st = con.createStatement();
      Statement st1 = con.createStatement();
      // ResultSet rs=st.executeQuery("");
      ResultSet rs = st.executeQuery("SELECT DISTINCT `sumwt`,`tname` FROM `totwet`");
      while (rs.next()) {
        c = c + rs.getInt(1);
        tn[i1] = rs.getString(2);
        i1++;
      }
      for (int i = 0; i < i1; i++) {
        st1.executeUpdate(
            "UPDATE `totwet` SET `nd`='" + c + "'" + " WHERE `tname`='" + tn[i] + "'");
      }

      System.out.println("SUM OF THE VALUE IS:" + c);
      for (int i = 0; i < i1; i++) {
        jTextArea1.append("(" + tna[i] + "," + sumt[i] + "/" + c + ")\n\t\t");
      }
      jTextArea1.append("\n Offender\n\t\t");
      for (int i = 0; i < k; i++) {
        jTextArea1.append(rwt[i]);
        st.executeUpdate("INSERT INTO `txt mine`.`offender` (`nd`) VALUES ('" + rwt[i] + "')");
      }
      jTextArea1.append("\n\n Experimental Coefficient:" + 2);
      ResultSet rs1 = st.executeQuery("SELECT DISTINCT `nd`FROM `offender`");
      while (rs1.next()) {
        ResultSet rs2 =
            st1.executeQuery(
                "SELECT DISTINCT `sumwt`,`nd` FROM `totwet` WHERE `tname`='"
                    + rs1.getString(1)
                    + "'");
        while (rs2.next()) {
          v1 += (float) rs2.getInt(1) / (float) rs2.getInt(2);
        }
      }

      float res = val * v1;
      System.out.println("offering:" + res);
      double n = res;

      denominator =
          (int)
              (1
                  / (Math.abs(
                      n - (int) n
                          - 0.0001))); // - 0.0001 so the division doesn't get affected by the float
      // point aproximated representation
      int units = (int) n;

      numerator = units * denominator + 1;

      System.out.println("" + numerator + "/" + denominator);
      JOptionPane.showMessageDialog(this, "Suffle Process is done");
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
    // TODO add your handling code here:
  } // GEN-LAST:event_jButton3ActionPerformed
예제 #17
0
  private String processRequest(
      HttpServletRequest request, HttpServletResponse response, ConnectionPool conPool)
      throws Exception {
    // getting id parameters
    ParameterParser parameter = new ParameterParser(request);
    String bookID = parameter.getString(bookInterface.FIELD_ID, null);
    String sellerID = parameter.getString(bookInterface.FIELD_SELLERID, null);
    String message = parameter.getString(FIELD_MESSAGE, null);
    int codeID = parameter.getInt(bookInterface.FIELD_HIDDENID, 0);
    // get buyer's user object
    User user = (User) request.getSession().getAttribute(StringInterface.USERATTR);
    // security feauture:
    // if one of ids is missing or incorrect return false
    if (bookID == null
        || sellerID == null
        || codeID == 0
        || bookID.length() != booksTable.ID_LENGTH
        || sellerID.length() != usersTable.ID_LENGTH
        || codeID != Math.abs(bookID.hashCode())) {
      return "We were unable to find the book you specified! Please make sure that the book id is correct.";
    }
    if (user.getID().equals(sellerID)) {
      return "You may not purchase an item from yourself!";
    }
    // get connection
    Connection con = conPool.getConnection();
    try {
      booksTable book = generalUtils.getBook(bookID, con);
      /*security feauture:
       *check seller id == passed hidden id
       *book != null
       */
      if (book == null || !book.getSellerID().equals(sellerID)) {
        return "We were unable to find the book you specified! Please make sure that the book id is correct.";
      }
      usersTable sellerInfo = userUtils.getUserInfo(sellerID, con);
      usersTable buyerInfo = userUtils.getUserInfo(user.getID(), con);
      collegeTable college = getCollege(book.getCollegeID() + "", con);
      // if still here continue
      if (message == null) {
        request.setAttribute(ATTR_BOOK, book);
        request.setAttribute(ATTR_SELLER, sellerInfo);
        request.setAttribute(ATTR_BUYER, buyerInfo);
        request.setAttribute(ATTR_COLLEGE, college.getFull());
        RequestDispatcher rd = getServletContext().getRequestDispatcher(PATH_BUY_CONFIRM);
        rd.include(request, response);
        return null;
      } else if (buy(book, user, con)) {
        // sending email to buyer
        request.setAttribute(mailInterface.USERATTR, buyerInfo.getUsername());
        request.setAttribute(mailInterface.EMAILATTR, buyerInfo.getEmail());
        request.setAttribute(mailInterface.BOOKATTR, book);
        request.setAttribute("book_id", bookID);
        request.setAttribute("seller_id", sellerID);

        RequestDispatcher rd = getServletContext().getRequestDispatcher(PATHBIDCONFIRMATION);
        rd.include(request, response);
        // sending email to seller
        request.setAttribute(ATTR_COLLEGE, college.getFull());
        request.setAttribute(mailInterface.USERATTR, sellerInfo.getUsername());
        request.setAttribute(mailInterface.EMAILATTR, sellerInfo.getEmail());
        request.setAttribute(mailInterface.MESSAGEATTR, message);
        request.setAttribute(mailInterface.BOOKATTR, book);
        request.setAttribute(mailInterface.MOREATTR, buyerInfo);

        request.setAttribute("book_id", bookID);
        request.setAttribute("buyer_id", user.getID());
        rd = getServletContext().getRequestDispatcher(PATHBOOKUPDATE);
        rd.include(request, response);
        // showing success message
        rd = getServletContext().getRequestDispatcher(PATH_BUY_SUCCESS);
        rd.include(request, response);
        return null;
      } else {
        throw new Exception("failed to process with buy");
      }
    } catch (Exception e) {
      throw e;
    } finally {
      // recycle
      conPool.free(con);
      con = null;
    }
  }
예제 #18
0
 private int randomWithRange(int min, int max) {
   int range = (max - min) + 1;
   return (int) (Math.random() * range) + min;
 }
예제 #19
0
  // constructeur
  public troll(int numtroll, int tailleEchiquier, Menu menu) {

    try {
      this.menu = menu;
      paRestants = 6;
      System.out.println();
      idTroll = numtroll;
      System.out.print("Nom du troll " + numtroll + ": ");
      nomTroll = IO.lireChaine();
      int points = 40;
      // System.out.println("Vous allez maitenant definir les carac de votre troll (attaque, degats,
      // esquive, vie)");
      boolean ok = false;
      while (ok == false) {
        System.out.println();
        System.out.print(
            "Nombre de D3 d'attaque (Il reste " + points + " a repartir dans 4 carac) : ");
        att = IO.lireEntier();
        if (att > points - 3) {
          System.out.println("Impossible de mettre autant de points dans 1 seule carac !!");
        } else {
          points = points - att;
          ok = true;
        }
      }
      ok = false;
      while (ok == false) {
        System.out.println();
        System.out.print(
            "Nombre de D3 de degats (Il reste " + points + " a repartir dans 3 carac) : ");
        deg = IO.lireEntier();
        if (deg > points - 2) {
          System.out.println("Impossible de mettre autant de points dans 1 seule carac !!");
        } else {
          points = points - deg;
          ok = true;
        }
      }
      ok = false;
      while (ok == false) {
        System.out.println();
        System.out.print(
            "Nombre de D3 d'esquive (Il reste " + points + " a repartir dans 2 carac) : ");
        esq = IO.lireEntier();
        if (esq > points - 1) {
          System.out.println("Impossible de mettre autant de points dans 1 seule carac !!");
        } else {
          points = points - esq;
          ok = true;
        }
      }
      System.out.println();
      System.out.println("Points de vie : " + points + "*" + "10 = " + points * 10);
      vie = points * 10;
      positionX = (int) Math.floor(Math.random() * (tailleEchiquier)) + 1;
      positionY = (int) Math.floor(Math.random() * (tailleEchiquier)) + 1;
      System.out.println("Il apparaitra en X=" + positionX + " Y=" + positionY);
      stmt.executeUpdate("delete from troll where idTroll=" + idTroll);
      proc = conn.prepareCall("{call init_troll(?,?,?,?,?,?,?,?)}");
      proc.setInt(1, numtroll);
      proc.setString(2, nomTroll);
      proc.setInt(3, att);
      proc.setInt(4, deg);
      proc.setInt(5, esq);
      proc.setInt(6, vie);
      proc.setInt(7, positionX);
      proc.setInt(8, positionY);
      proc.executeUpdate();
      proc.close();
    } catch (SQLException E) {
      System.err.println("SQLException: " + E.getMessage());
      System.err.println("SQLState:     " + E.getSQLState());
    }
    valeur = "" + nomTroll.charAt(0);
  }
예제 #20
0
  private void kjorSql() throws SQLException, IOException {
    // hent konfigurasjonsparametre
    hentKonfig();

    BufferedReader sqlfil = Text.open(sqlfilNavn);
    //        BufferedReader connectfil = Text.open(connectfilNavn);
    BufferedReader inn = Text.open(System.in);
    PrintWriter utfil = Text.create(utfilNavn);

    String feltNavn = "", tekst = "";
    int index = 0, diff = 0, lengde = 0, maksAntFeltLengder = 0;
    char[] feltJusteringTegn = new char[100];
    int[] feltLengde = new int[100];

    String[] feltTab = new String[100];

    // Initier feltjustering til venstrejustert for alle felt som default
    for (int i = 0; i < 100; i++) {
      feltJusteringTegn[i] = TEXTFORMAT;
      feltLengde[i] = 0;
    }

    sqlStatement = "";

    mld("J", "\n SQL statement som kjøres:");

    mld("J", "_________________________________________________");

    try {
      linje = sqlfil.readLine();

      tekst = "";

      if (linje.length() > 16) tekst = linje.substring(0, 16);

      // sett feltformat ut fra definisjoner fra inputfil
      if (tekst.equals("-- fieldlengths=")) {
        startPos = 16;
        index = 0;
        tegn = linje.charAt(startPos);
        sqlStmLength = linje.length() + 1;

        while (startPos < sqlStmLength && (linje.charAt(startPos) != ' ')) {
          sluttPos = startPos + 3;

          //                   mld("J","lengde feltnr. " + index + ": " + linje.substring(startPos,
          // sluttPos));

          feltJusteringTegn[index] = linje.charAt(startPos);

          if (feltJusteringTegn[index] == VENSTREJUSTERT
              || feltJusteringTegn[index] == TEXTFORMAT) {
            feltJusteringTegn[index] = TEXTFORMAT;
          } else {
            feltJusteringTegn[index] = NUMERICFORMAT;
          }

          feltLengde[index] = Integer.parseInt(linje.substring(startPos + 1, sluttPos));

          //                   mld("J","lengde feltnr. " + index + ": " + feltJusteringTegn[index] +
          // "/" + feltLengde[index]);

          startPos = startPos + 4;

          index++;
        } // end while

        //          maksAntFeltLengder = index - 1;

        linje = sqlfil.readLine();
      } // end if

      // les inn SQL linje for linje og bygg opp felttabell
      while (linje != null) {
        mld("J", linje);

        startPos = linje.indexOf("--");

        if (startPos > 0) {
          linje = linje.substring(0, startPos);
        }

        if (!(startPos == 0)) {
          sqlStatement = sqlStatement + linje.trim() + " ";
        }

        linje = sqlfil.readLine();
      }

      mld("J", "_________________________________________________");

      //          Text.prompt("Trykk enter for å fortsette!");
      //          inn.readLine();

      if (mldInd.equals("J")) System.out.println("Startpos for select er : " + startPos);

      startPos = 6;
      sluttPos = 0;

      sqlStmLength = sqlStatement.length() + 1;

      fromPos = sqlStatement.indexOf(" from ");

      fromPos2 = sqlStatement.indexOf(" FROM ");

      if (fromPos < 0) fromPos = sqlStmLength;
      if (fromPos2 < 0) fromPos2 = sqlStmLength;
      if (fromPos2 < fromPos) fromPos = fromPos2;

      while ((startPos < fromPos) & antFelt < 99) {
        boolean isAsFelt = false;
        // finn først kommaposisjonen som skiller mot neste felt

        tegn = sqlStatement.charAt(startPos);
        int pos = startPos;
        int antParenteser = 0;
        while ((!(tegn == ',') || antParenteser > 0) && pos < fromPos) {
          if (tegn == '(') {
            antParenteser++;
            isAsFelt = true;
          } else if (tegn == ')') {
            antParenteser--;
          }
          pos++;
          tegn = sqlStatement.charAt(pos);
        }

        kommaPos = Math.min(pos, fromPos);

        asPos = sqlStatement.substring(startPos, fromPos).indexOf(" as ");
        if (asPos < 0) asPos = fromPos;
        else asPos = asPos + startPos;

        asPos2 = sqlStatement.substring(startPos, fromPos).indexOf(" AS ");
        if (asPos2 < 0) asPos2 = fromPos;
        else asPos2 = asPos2 + startPos;

        if (asPos2 < asPos) asPos = asPos2;

        if (isAsFelt) {
          kommaPos = sqlStatement.substring(asPos, fromPos).indexOf(",");
          if (kommaPos < 0) kommaPos = fromPos;
          else kommaPos = kommaPos + asPos;
        }

        if ((asPos < kommaPos)) startPos = asPos + 3;

        while (tegn == ' ' & startPos < sqlStmLength) {
          startPos++;

          if (mldInd.equals("J")) System.out.println(tegn);

          tegn = sqlStatement.charAt(startPos);
        }

        if (kommaPos < fromPos) sluttPos = kommaPos;
        else sluttPos = fromPos;

        feltNavn = sqlStatement.substring((startPos), sluttPos).trim();

        feltTab[antFelt] = feltNavn;

        antFelt++;

        startPos = sluttPos + 1;
      } //  end while

      for (int i = 0; i < antFelt; i++) {
        if (i != 0) utfil.print(";");
        utfil.print(feltTab[i]);

        if (mldInd.equals("J")) {
          if (i != 0) System.out.print(";");
          System.out.print(feltTab[i]);
        }
      }
      utfil.println("");

      if (mldInd.equals("J")) System.out.println("");
    } // try
    catch (NullPointerException e) {
      mld(mldInd, "  NullPointerException!");
    }

    //      eksekver SQL
    PreparedStatement ps = null;
    ResultSet rs = null;
    Connection con = null;

    con = getConnection();
    ps = con.prepareStatement(sqlStatement);
    // Setter parameter for alle det som er markert som ? i sqlStatement string
    //         ps.setLong(1, 33);
    rs = ps.executeQuery();

    try {
      // skriv resultatsett til utfil
      int antUtFilLinjer = 0;

      while (rs.next()) {
        antUtFilLinjer++;

        for (int i = 0; i < antFelt; i++) {
          if (i != 0) utfil.print(";");

          feltVerdi = rs.getString(i + 1);

          if (feltVerdi == null) feltVerdi = "null";

          lengde = feltVerdi.length();

          //              mld("J", "feltnr. " + i + ": " + feltVerdi + "/" + lengde);

          diff = feltLengde[i] - lengde;

          if (feltJusteringTegn[i] == NUMERICFORMAT) {
            feltVerdi = feltVerdi.replace('.', ',');
          } else {
            if (feltJusteringTegn[i] == TEXTFORMAT
                && feltVerdi.length() > 10
                && !(feltVerdi.indexOf('.') > 0)
                && !(feltVerdi.indexOf('-') > 0)) {
              feltVerdi = "\'" + feltVerdi;
            }
          }

          if (diff > 0) {
            if (feltJusteringTegn[i] == NUMERICFORMAT) {
              utfil.print(blankeTegn.substring(0, diff));
              utfil.print(feltVerdi);
            } else {
              utfil.print(feltVerdi);
              utfil.print(blankeTegn.substring(0, diff));
            }
          } else utfil.print(feltVerdi);

          if (mldInd.equals("J")) {
            if (i != 0) System.out.print(";");
            System.out.print(feltVerdi);
          }
        }
        utfil.println("");

        if (mldInd.equals("J")) System.out.println("");
      }
      System.out.println("");
      System.out.println("Antall rader i svarsettet: " + antUtFilLinjer);

    } catch (SQLException e) {
      e.printStackTrace(); // To change body of catch statement use Options | File Templates.
    } finally {
      rs.close();
      ps.close();
      con.close();

      sqlfil.close();
      utfil.close();
    }
  }
예제 #21
0
  public void load(String queryFile, String modules, String tables)
      throws SQLException, IOException, InterruptedException, ExecutionException {
    Properties properties = new Properties();
    properties.load(new FileInputStream(queryFile));

    Collection<String> keys = properties.stringPropertyNames();

    // Filtering by validating if property starts with any of the module names
    if (!Config.ALL.equalsIgnoreCase(modules)) {
      keys =
          Util.filter(
              keys, "^(" + modules.replaceAll(Config.COMMA_SEPARATOR, Config.MODULE_SUFFIX) + ")");
    }

    // Filtering by table names
    if (!Config.ALL.equalsIgnoreCase(tables)) {
      keys =
          Util.filter(
              keys, "(" + tables.replaceAll(Config.COMMA_SEPARATOR, Config.TABLE_SUFFIX) + ")$");
    }

    logger.info("The final modules and tables that are being considered" + keys.toString());

    ExecutorService executor = Executors.newFixedThreadPool(keys.size() * 3);
    CompletionService completion = new ExecutorCompletionService(executor);

    for (String key : keys) {
      String query = properties.getProperty(key);
      key =
          (key.contains(Config.DOT_SEPARATOR)
              ? key.substring(key.indexOf(Config.DOT_SEPARATOR) + 1)
              : key);

      while (query.contains("[:")) {
        String param = query.substring(query.indexOf("[:") + 2, query.indexOf("]"));

        query = query.replaceFirst("\\[\\:" + param + "\\]", properties.getProperty(param));
      }
      int pages = 1;
      String base = "";
      if (config.srisvoltdb) {
        if (config.isPaginated) {
          try {
            // find count
            String countquery = query;
            if (countquery.contains("<") || countquery.contains(">")) {
              int bracketOpen = countquery.indexOf("<");
              int bracketClose = countquery.indexOf(">");
              String orderCol = countquery.substring(bracketOpen + 1, bracketClose);
              countquery = countquery.replace("<" + orderCol + ">", "");
            }
            VoltTable vcount = client.callProcedure("@AdHoc", countquery).getResults()[0];
            int count = vcount.getRowCount();
            pages = (int) Math.ceil((double) count / config.pageSize);
          } catch (Exception e) {
            System.out.println("Count formation failure!");
          }
        }
        // set up data in order
      } else {
        // find count
        String countquery = query.replace("*", "COUNT(*)");
        Connection conn =
            DriverManager.getConnection(config.jdbcurl, config.jdbcuser, config.jdbcpassword);
        base = conn.getMetaData().getDatabaseProductName().toLowerCase();
        System.out.println("BASE: " + base);
        Statement jdbcStmt =
            conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        if (countquery.contains("<") || countquery.contains(">")) {
          int bracketOpen = countquery.indexOf("<");
          int bracketClose = countquery.indexOf(">");
          String orderCol = countquery.substring(bracketOpen + 1, bracketClose);
          countquery = countquery.replace("<" + orderCol + ">", "");
        }
        ResultSet rcount = jdbcStmt.executeQuery(countquery);
        rcount.next();
        int count = Integer.parseInt(rcount.getArray(1).toString());

        // THIS IF NEEDS A WAY TO DETERMINE IF POSTGRES
        if (base.contains("postgres") && config.isPaginated) {
          pages = (int) Math.ceil((double) count / config.pageSize);
        }
        // set up data in order
      }
      // establish new SourceReaders and DestinationWriters for pages
      SourceReader[] sr = new SourceReader[pages];
      DestinationWriter[] cr = new DestinationWriter[pages];
      for (int i = 0; i < pages; i++) {
        sr[i] = new SourceReader();
        cr[i] = new DestinationWriter();
      }
      Controller processor =
          new Controller<ArrayList<Object[]>>(
              client, sr, cr, query, key.toUpperCase() + ".insert", config, pages, base);
      completion.submit(processor);
    }

    // wait for all tasks to complete.
    for (int i = 0; i < keys.size(); ++i) {
      logger.info(
          "****************"
              + completion.take().get()
              + " completed *****************"); // will block until the next sub task has
      // completed.
    }

    executor.shutdown();
  }
  public MutationPlan compile(UpsertStatement upsert, List<Object> binds) throws SQLException {
    final PhoenixConnection connection = statement.getConnection();
    ConnectionQueryServices services = connection.getQueryServices();
    final int maxSize =
        services
            .getProps()
            .getInt(
                QueryServices.MAX_MUTATION_SIZE_ATTRIB,
                QueryServicesOptions.DEFAULT_MAX_MUTATION_SIZE);
    final ColumnResolver resolver = FromCompiler.getResolver(upsert, connection);
    final TableRef tableRef = resolver.getTables().get(0);
    PTable table = tableRef.getTable();
    if (table.getType() == PTableType.VIEW) {
      throw new ReadOnlyTableException("Mutations not allowed for a view (" + tableRef + ")");
    }
    Scan scan = new Scan();
    final StatementContext context =
        new StatementContext(connection, resolver, binds, upsert.getBindCount(), scan);
    // Setup array of column indexes parallel to values that are going to be set
    List<ColumnName> columnNodes = upsert.getColumns();
    List<PColumn> allColumns = table.getColumns();

    int[] columnIndexesToBe;
    int[] pkSlotIndexesToBe;
    PColumn[] targetColumns;
    // Allow full row upsert if no columns or only dynamic one are specified and values count match
    if (columnNodes.isEmpty()
        || columnNodes.size() == upsert.getTable().getDynamicColumns().size()) {
      columnIndexesToBe = new int[allColumns.size()];
      pkSlotIndexesToBe = new int[columnIndexesToBe.length];
      targetColumns = new PColumn[columnIndexesToBe.length];
      int j = table.getBucketNum() == null ? 0 : 1; // Skip over the salting byte.
      for (int i = 0; i < allColumns.size(); i++) {
        columnIndexesToBe[i] = i;
        targetColumns[i] = allColumns.get(i);
        if (SchemaUtil.isPKColumn(allColumns.get(i))) {
          pkSlotIndexesToBe[i] = j++;
        }
      }
    } else {
      columnIndexesToBe = new int[columnNodes.size()];
      pkSlotIndexesToBe = new int[columnIndexesToBe.length];
      targetColumns = new PColumn[columnIndexesToBe.length];
      Arrays.fill(
          columnIndexesToBe,
          -1); // TODO: necessary? So we'll get an AIOB exception if it's not replaced
      Arrays.fill(
          pkSlotIndexesToBe,
          -1); // TODO: necessary? So we'll get an AIOB exception if it's not replaced
      BitSet pkColumnsSet = new BitSet(table.getPKColumns().size());
      for (int i = 0; i < columnNodes.size(); i++) {
        ColumnName colName = columnNodes.get(i);
        ColumnRef ref =
            resolver.resolveColumn(null, colName.getFamilyName(), colName.getColumnName());
        columnIndexesToBe[i] = ref.getColumnPosition();
        targetColumns[i] = ref.getColumn();
        if (SchemaUtil.isPKColumn(ref.getColumn())) {
          pkColumnsSet.set(pkSlotIndexesToBe[i] = ref.getPKSlotPosition());
        }
      }
      int i = table.getBucketNum() == null ? 0 : 1;
      for (; i < table.getPKColumns().size(); i++) {
        PColumn pkCol = table.getPKColumns().get(i);
        if (!pkColumnsSet.get(i)) {
          if (!pkCol.isNullable()) {
            throw new ConstraintViolationException(
                table.getName().getString()
                    + "."
                    + pkCol.getName().getString()
                    + " may not be null");
          }
        }
      }
    }

    List<ParseNode> valueNodes = upsert.getValues();
    QueryPlan plan = null;
    RowProjector projector = null;
    int nValuesToSet;
    boolean runOnServer = false;
    if (valueNodes == null) {
      SelectStatement select = upsert.getSelect();
      assert (select != null);
      TableRef selectTableRef = FromCompiler.getResolver(select, connection).getTables().get(0);
      boolean sameTable = tableRef.equals(selectTableRef);
      // Pass scan through if same table in upsert and select so that projection is computed
      // correctly
      QueryCompiler compiler =
          new QueryCompiler(connection, 0, sameTable ? scan : new Scan(), targetColumns);
      plan = compiler.compile(select, binds);
      projector = plan.getProjector();
      nValuesToSet = projector.getColumnCount();
      // Cannot auto commit if doing aggregation or topN or salted
      // Salted causes problems because the row may end up living on a different region
      runOnServer =
          plan.getGroupBy() == null
              && sameTable
              && select.getOrderBy().isEmpty()
              && table.getBucketNum() == null;
    } else {
      nValuesToSet = valueNodes.size();
    }
    final QueryPlan queryPlan = plan;
    // Resize down to allow a subset of columns to be specifiable
    if (columnNodes.isEmpty()) {
      columnIndexesToBe = Arrays.copyOf(columnIndexesToBe, nValuesToSet);
      pkSlotIndexesToBe = Arrays.copyOf(pkSlotIndexesToBe, nValuesToSet);
    }

    if (nValuesToSet != columnIndexesToBe.length) {
      throw new SQLExceptionInfo.Builder(SQLExceptionCode.UPSERT_COLUMN_NUMBERS_MISMATCH)
          .setMessage(
              "Numbers of columns: "
                  + columnIndexesToBe.length
                  + ". Number of values: "
                  + nValuesToSet)
          .build()
          .buildException();
    }

    final int[] columnIndexes = columnIndexesToBe;
    final int[] pkSlotIndexes = pkSlotIndexesToBe;

    // TODO: break this up into multiple functions
    ////////////////////////////////////////////////////////////////////
    // UPSERT SELECT
    /////////////////////////////////////////////////////////////////////
    if (valueNodes == null) {
      /* We can run the upsert in a coprocessor if:
       * 1) the into table matches from table
       * 2) the select query isn't doing aggregation
       * 3) autoCommit is on
       * 4) not topN
       * Otherwise, run the query to pull the data from the server
       * and populate the MutationState (upto a limit).
       */
      final boolean isAutoCommit = connection.getAutoCommit();
      runOnServer &= isAutoCommit;

      ////////////////////////////////////////////////////////////////////
      // UPSERT SELECT run server-side (maybe)
      /////////////////////////////////////////////////////////////////////
      if (runOnServer) {
        // At most this array will grow bigger by the number of PK columns
        int[] allColumnsIndexes = Arrays.copyOf(columnIndexes, columnIndexes.length + nValuesToSet);
        int[] reverseColumnIndexes = new int[table.getColumns().size()];
        List<Expression> projectedExpressions =
            Lists.newArrayListWithExpectedSize(reverseColumnIndexes.length);
        Arrays.fill(reverseColumnIndexes, -1);
        for (int i = 0; i < nValuesToSet; i++) {
          projectedExpressions.add(projector.getColumnProjector(i).getExpression());
          reverseColumnIndexes[columnIndexes[i]] = i;
        }
        /*
         * Order projected columns and projected expressions with PK columns
         * leading order by slot position
         */
        int offset = table.getBucketNum() == null ? 0 : 1;
        for (int i = 0; i < table.getPKColumns().size() - offset; i++) {
          PColumn column = table.getPKColumns().get(i + offset);
          int pos = reverseColumnIndexes[column.getPosition()];
          if (pos == -1) {
            // Last PK column may be fixed width and nullable
            // We don't want to insert a null expression b/c
            // it's not valid to set a fixed width type to null.
            if (column.getDataType().isFixedWidth()) {
              continue;
            }
            // Add literal null for missing PK columns
            pos = projectedExpressions.size();
            Expression literalNull = LiteralExpression.newConstant(null, column.getDataType());
            projectedExpressions.add(literalNull);
            allColumnsIndexes[pos] = column.getPosition();
          }
          // Swap select expression at pos with i
          Collections.swap(projectedExpressions, i, pos);
          // Swap column indexes and reverse column indexes too
          int tempPos = allColumnsIndexes[i];
          allColumnsIndexes[i] = allColumnsIndexes[pos];
          allColumnsIndexes[pos] = tempPos;
          reverseColumnIndexes[tempPos] = reverseColumnIndexes[i];
          reverseColumnIndexes[i] = i;
        }
        // If any pk slots are changing, be conservative and don't run this server side.
        // If the row ends up living in a different region, we'll get an error otherwise.
        for (int i = 0; i < table.getPKColumns().size(); i++) {
          PColumn column = table.getPKColumns().get(i);
          Expression source = projectedExpressions.get(i);
          if (source == null
              || !source.equals(
                  new ColumnRef(tableRef, column.getPosition()).newColumnExpression())) {
            // TODO: we could check the region boundaries to see if the pk will still be in it.
            runOnServer = false; // bail on running server side, since PK may be changing
            break;
          }
        }

        ////////////////////////////////////////////////////////////////////
        // UPSERT SELECT run server-side
        /////////////////////////////////////////////////////////////////////
        if (runOnServer) {
          // Iterate through columns being projected
          List<PColumn> projectedColumns =
              Lists.newArrayListWithExpectedSize(projectedExpressions.size());
          for (int i = 0; i < projectedExpressions.size(); i++) {
            // Must make new column if position has changed
            PColumn column = allColumns.get(allColumnsIndexes[i]);
            projectedColumns.add(column.getPosition() == i ? column : new PColumnImpl(column, i));
          }
          // Build table from projectedColumns
          PTable projectedTable =
              PTableImpl.makePTable(
                  table.getName(),
                  table.getType(),
                  table.getTimeStamp(),
                  table.getSequenceNumber(),
                  table.getPKName(),
                  table.getBucketNum(),
                  projectedColumns);

          // Remove projection of empty column, since it can lead to problems when building another
          // projection
          // using this same scan. TODO: move projection code to a later stage, like
          // QueryPlan.newScanner to
          // prevent having to do this.
          ScanUtil.removeEmptyColumnFamily(context.getScan(), table);
          List<AliasedNode> select =
              Collections.<AliasedNode>singletonList(
                  NODE_FACTORY.aliasedNode(
                      null,
                      NODE_FACTORY.function(
                          CountAggregateFunction.NORMALIZED_NAME, LiteralParseNode.STAR)));
          // Ignore order by - it has no impact
          final RowProjector aggProjector =
              ProjectionCompiler.getRowProjector(
                  context, select, false, GroupBy.EMPTY_GROUP_BY, OrderBy.EMPTY_ORDER_BY, null);
          /*
           * Transfer over PTable representing subset of columns selected, but all PK columns.
           * Move columns setting PK first in pkSlot order, adding LiteralExpression of null for any missing ones.
           * Transfer over List<Expression> for projection.
           * In region scan, evaluate expressions in order, collecting first n columns for PK and collection non PK in mutation Map
           * Create the PRow and get the mutations, adding them to the batch
           */
          scan.setAttribute(
              UngroupedAggregateRegionObserver.UPSERT_SELECT_TABLE,
              UngroupedAggregateRegionObserver.serialize(projectedTable));
          scan.setAttribute(
              UngroupedAggregateRegionObserver.UPSERT_SELECT_EXPRS,
              UngroupedAggregateRegionObserver.serialize(projectedExpressions));
          final QueryPlan aggPlan =
              new AggregatePlan(
                  context,
                  tableRef,
                  projector,
                  null,
                  GroupBy.EMPTY_GROUP_BY,
                  false,
                  null,
                  OrderBy.EMPTY_ORDER_BY);
          return new MutationPlan() {

            @Override
            public PhoenixConnection getConnection() {
              return connection;
            }

            @Override
            public ParameterMetaData getParameterMetaData() {
              return context.getBindManager().getParameterMetaData();
            }

            @Override
            public MutationState execute() throws SQLException {
              Scanner scanner = aggPlan.getScanner();
              ResultIterator iterator = scanner.iterator();
              try {
                Tuple row = iterator.next();
                ImmutableBytesWritable ptr = context.getTempPtr();
                final long mutationCount =
                    (Long) aggProjector.getColumnProjector(0).getValue(row, PDataType.LONG, ptr);
                return new MutationState(maxSize, connection) {
                  @Override
                  public long getUpdateCount() {
                    return mutationCount;
                  }
                };
              } finally {
                iterator.close();
              }
            }

            @Override
            public ExplainPlan getExplainPlan() throws SQLException {
              List<String> queryPlanSteps = aggPlan.getExplainPlan().getPlanSteps();
              List<String> planSteps =
                  Lists.newArrayListWithExpectedSize(queryPlanSteps.size() + 1);
              planSteps.add("UPSERT ROWS");
              planSteps.addAll(queryPlanSteps);
              return new ExplainPlan(planSteps);
            }
          };
        }
      }

      ////////////////////////////////////////////////////////////////////
      // UPSERT SELECT run client-side
      /////////////////////////////////////////////////////////////////////
      final int batchSize = Math.min(connection.getMutateBatchSize(), maxSize);
      return new MutationPlan() {

        @Override
        public PhoenixConnection getConnection() {
          return connection;
        }

        @Override
        public ParameterMetaData getParameterMetaData() {
          return context.getBindManager().getParameterMetaData();
        }

        @Override
        public MutationState execute() throws SQLException {
          byte[][] values = new byte[columnIndexes.length][];
          Scanner scanner = queryPlan.getScanner();
          int estSize = scanner.getEstimatedSize();
          int rowCount = 0;
          Map<ImmutableBytesPtr, Map<PColumn, byte[]>> mutation =
              Maps.newHashMapWithExpectedSize(estSize);
          ResultSet rs = new PhoenixResultSet(scanner, statement);
          PTable table = tableRef.getTable();
          PColumn column;
          while (rs.next()) {
            for (int i = 0; i < values.length; i++) {
              column = table.getColumns().get(columnIndexes[i]);
              byte[] byteValue = rs.getBytes(i + 1);
              Object value = rs.getObject(i + 1);
              int rsPrecision = rs.getMetaData().getPrecision(i + 1);
              Integer precision = rsPrecision == 0 ? null : rsPrecision;
              int rsScale = rs.getMetaData().getScale(i + 1);
              Integer scale = rsScale == 0 ? null : rsScale;
              // If ColumnModifier from expression in SELECT doesn't match the
              // column being projected into then invert the bits.
              if (column.getColumnModifier() == ColumnModifier.SORT_DESC) {
                byte[] tempByteValue = Arrays.copyOf(byteValue, byteValue.length);
                byteValue =
                    ColumnModifier.SORT_DESC.apply(byteValue, tempByteValue, 0, byteValue.length);
              }
              // We are guaranteed that the two column will have compatible types,
              // as we checked that before.
              if (!column
                  .getDataType()
                  .isSizeCompatible(
                      column.getDataType(),
                      value,
                      byteValue,
                      precision,
                      column.getMaxLength(),
                      scale,
                      column.getScale())) {
                throw new SQLExceptionInfo.Builder(SQLExceptionCode.DATA_INCOMPATIBLE_WITH_TYPE)
                    .setColumnName(column.getName().getString())
                    .build()
                    .buildException();
              }
              values[i] =
                  column
                      .getDataType()
                      .coerceBytes(
                          byteValue,
                          value,
                          column.getDataType(),
                          precision,
                          scale,
                          column.getMaxLength(),
                          column.getScale());
            }
            setValues(values, pkSlotIndexes, columnIndexes, table, mutation);
            rowCount++;
            // Commit a batch if auto commit is true and we're at our batch size
            if (isAutoCommit && rowCount % batchSize == 0) {
              MutationState state = new MutationState(tableRef, mutation, 0, maxSize, connection);
              connection.getMutationState().join(state);
              connection.commit();
              mutation.clear();
            }
          }
          // If auto commit is true, this last batch will be committed upon return
          return new MutationState(
              tableRef, mutation, rowCount / batchSize * batchSize, maxSize, connection);
        }

        @Override
        public ExplainPlan getExplainPlan() throws SQLException {
          List<String> queryPlanSteps = queryPlan.getExplainPlan().getPlanSteps();
          List<String> planSteps = Lists.newArrayListWithExpectedSize(queryPlanSteps.size() + 1);
          planSteps.add("UPSERT SELECT");
          planSteps.addAll(queryPlanSteps);
          return new ExplainPlan(planSteps);
        }
      };
    }

    ////////////////////////////////////////////////////////////////////
    // UPSERT VALUES
    /////////////////////////////////////////////////////////////////////
    int nodeIndex = 0;
    // Allocate array based on size of all columns in table,
    // since some values may not be set (if they're nullable).
    UpsertValuesCompiler expressionBuilder = new UpsertValuesCompiler(context);
    final byte[][] values = new byte[nValuesToSet][];
    for (ParseNode valueNode : valueNodes) {
      if (!valueNode.isConstant()) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.VALUE_IN_UPSERT_NOT_CONSTANT)
            .build()
            .buildException();
      }
      PColumn column = allColumns.get(columnIndexes[nodeIndex]);
      expressionBuilder.setColumn(column);
      LiteralExpression literalExpression = (LiteralExpression) valueNode.accept(expressionBuilder);
      byte[] byteValue = literalExpression.getBytes();
      if (literalExpression.getDataType() != null) {
        // If ColumnModifier from expression in SELECT doesn't match the
        // column being projected into then invert the bits.
        if (literalExpression.getColumnModifier() != column.getColumnModifier()) {
          byte[] tempByteValue = Arrays.copyOf(byteValue, byteValue.length);
          byteValue = ColumnModifier.SORT_DESC.apply(byteValue, tempByteValue, 0, byteValue.length);
        }
        if (!literalExpression
            .getDataType()
            .isCoercibleTo(column.getDataType(), literalExpression.getValue())) {
          throw new TypeMismatchException(
              literalExpression.getDataType(),
              column.getDataType(),
              "expression: " + literalExpression.toString() + " in column " + column);
        }
        if (!column
            .getDataType()
            .isSizeCompatible(
                literalExpression.getDataType(),
                literalExpression.getValue(),
                byteValue,
                literalExpression.getMaxLength(),
                column.getMaxLength(),
                literalExpression.getScale(),
                column.getScale())) {
          throw new SQLExceptionInfo.Builder(SQLExceptionCode.DATA_INCOMPATIBLE_WITH_TYPE)
              .setColumnName(column.getName().getString())
              .setMessage("value=" + literalExpression.toString())
              .build()
              .buildException();
        }
      }
      byteValue =
          column
              .getDataType()
              .coerceBytes(
                  byteValue,
                  literalExpression.getValue(),
                  literalExpression.getDataType(),
                  literalExpression.getMaxLength(),
                  literalExpression.getScale(),
                  column.getMaxLength(),
                  column.getScale());
      values[nodeIndex] = byteValue;
      nodeIndex++;
    }
    return new MutationPlan() {

      @Override
      public PhoenixConnection getConnection() {
        return connection;
      }

      @Override
      public ParameterMetaData getParameterMetaData() {
        return context.getBindManager().getParameterMetaData();
      }

      @Override
      public MutationState execute() {
        Map<ImmutableBytesPtr, Map<PColumn, byte[]>> mutation = Maps.newHashMapWithExpectedSize(1);
        setValues(values, pkSlotIndexes, columnIndexes, tableRef.getTable(), mutation);
        return new MutationState(tableRef, mutation, 0, maxSize, connection);
      }

      @Override
      public ExplainPlan getExplainPlan() throws SQLException {
        return new ExplainPlan(Collections.singletonList("PUT SINGLE ROW"));
      }
    };
  }
예제 #23
0
 private Integer getHours(Integer time) {
   return (int) Math.floor(time / 60);
 }
예제 #24
0
  public void loadTables(String tableNames, String procNames)
      throws SQLException, IOException, InterruptedException, ExecutionException {
    String[] tableNameArray =
        tableNames != null && !"".equals(tableNames) ? tableNames.split(",") : null;
    String[] procNameArray =
        procNames != null && !"".equals(procNames) ? procNames.split(",") : null;

    ExecutorService executor = Executors.newFixedThreadPool(tableNameArray.length * 3);
    CompletionService completion = new ExecutorCompletionService(executor);

    for (int j = 0; j < tableNameArray.length && tableNameArray != null; j++) {
      String tableName = tableNameArray[j];
      String procName = procNameArray != null ? procNameArray[j] : "";

      // if procName not provided, use the default VoltDB TABLENAME.insert procedure
      if (procName.length() == 0) {
        if (tableName.contains("..")) {
          procName = tableName.split("\\.\\.")[1].toUpperCase() + ".insert";
        } else {
          procName = tableName.toUpperCase() + ".insert";
        }
      }

      // query the table
      String jdbcSelect = "SELECT * FROM " + tableName + ";";

      // create query to find count
      String countquery = jdbcSelect.replace("*", "COUNT(*)");
      int pages = 1;
      String base = "";
      if (config.srisvoltdb) {
        if (config.isPaginated) {
          try {
            // find count
            if (countquery.contains("<") || countquery.contains(">")) {
              int bracketOpen = countquery.indexOf("<");
              int bracketClose = countquery.indexOf(">");
              String orderCol = countquery.substring(bracketOpen + 1, bracketClose);
              countquery = countquery.replace("<" + orderCol + ">", "");
            }
            VoltTable vcount = client.callProcedure("@AdHoc", countquery).getResults()[0];
            int count = Integer.parseInt(vcount.toString());
            // determine number of pages from total data and page size
            pages = (int) Math.ceil((double) count / config.pageSize);
            System.out.println(pages);
          } catch (Exception e) {
            System.out.println("Count formation failure!");
          }
        }
      } else {
        // find count
        Connection conn =
            DriverManager.getConnection(config.jdbcurl, config.jdbcuser, config.jdbcpassword);
        base = conn.getMetaData().getDatabaseProductName().toLowerCase();
        Statement jdbcStmt =
            conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        if (countquery.contains("<") || countquery.contains(">")) {
          int bracketOpen = countquery.indexOf("<");
          int bracketClose = countquery.indexOf(">");
          String orderCol = countquery.substring(bracketOpen + 1, bracketClose);
          countquery = countquery.replace("<" + orderCol + ">", "");
        }
        ResultSet rcount =
            jdbcStmt.executeQuery(
                countquery); // determine number of pages from total data and page size
        if (base.contains("postgres") && config.isPaginated) {
          int count = Integer.parseInt(rcount.toString());
          pages = (int) Math.ceil((double) count / config.pageSize);
        }
      }

      // establish new SourceReaders and DestinationWriters for pages
      SourceReader[] sr = new SourceReader[pages];
      DestinationWriter[] cr = new DestinationWriter[pages];
      for (int i = 0; i < pages; i++) {
        sr[i] = new SourceReader();
        cr[i] = new DestinationWriter();
      }
      Controller processor =
          new Controller<ArrayList<Object[]>>(
              client, sr, cr, jdbcSelect, procName, config, pages, base);
      completion.submit(processor);
    }

    // wait for all tasks to complete.
    for (int i = 0; i < tableNameArray.length; ++i) {
      logger.info(
          "****************"
              + completion.take().get()
              + " completed *****************"); // will block until the next sub task has
      // completed.
    }
    executor.shutdown();
  }
예제 #25
0
  /** Create data. */
  private void createData() {
    try {
      String[] categories = new String[] {"Hardware", "Software"};
      String[][] subCategories =
          new String[][] {
            {"PC", "Monitor", "Printer"},
            {"O.S.", "IDEs", "Office Appl.", "Database", "Games", "Other SW"}
          };
      String[] agents = new String[] {"Agent 1", "Agent 2", "Agent 3"};
      String[] countries = new String[] {"Italy", "France", "Germany", "Other"};
      String[][] zones =
          new String[][] {
            {"North Italy", "Center Italy", "South Italy"},
            {"Paris", "Outside Paris"},
            {"East", "West"},
            {"Iberica Peninsula", "U.K. & Ireland", "Be.Ne.Lux.", "East Europe"}
          };
      HashMap items = new HashMap();
      items.put("PC", new String[] {"Dell", "HP"});
      items.put("Monitor", new String[] {"LG", "Sony", "Philips"});
      items.put("Printer", new String[] {"HP", "Epson"});
      items.put("O.S.", new String[] {"Windows", "Linux", "Mac"});
      items.put("IDEs", new String[] {"JBuilder", "IBM RAD", "MS Visual Studio .NET"});
      items.put("Office Appl.", new String[] {"MS Office 2007", "Open Office"});
      items.put("Database", new String[] {"Oracle", "MS SQLServer", "Sybase", "IBM DB2"});
      items.put("Games", new String[] {"Need4Speed", "Fifa"});
      items.put("Other SW", new String[] {"Norton AV", "Photoshop"});

      FileOutputStream out = new FileOutputStream("orders.txt");
      String line = null;
      int i = 0;
      Calendar cal = Calendar.getInstance();
      cal.set(cal.YEAR, 2007);
      cal.set(cal.MONTH, 0);
      cal.set(cal.DAY_OF_MONTH, 1);
      cal.set(cal.HOUR_OF_DAY, 0);
      cal.set(cal.MINUTE, 0);
      cal.set(cal.SECOND, 0);
      cal.set(cal.MILLISECOND, 0);
      long t = cal.getTimeInMillis();
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      String date = null;
      int maxRows = 2000000;
      String[] ii = null;
      String[] sub = null;
      String[] z = null;
      do {
        date = sdf.format(new java.util.Date(t));
        for (int k1 = 0; k1 < categories.length; k1++) {
          sub = subCategories[k1];
          for (int k2 = 0; k2 < sub.length; k2++) {
            if (i >= maxRows) break;
            for (int k3 = 0; k3 < countries.length; k3++) {
              z = zones[k3];
              for (int k4 = 0; k4 < z.length; k4++)
                for (int k5 = 0; k5 < agents.length; k5++) {
                  if (i >= maxRows) break;
                  ii = (String[]) items.get(sub[k2]);
                  for (int k6 = 0; k6 < ii.length; k6++) {
                    if (i >= maxRows) break;

                    line =
                        date
                            + ";"
                            + categories[k1]
                            + ";"
                            + sub[k2]
                            + ";"
                            + countries[k3]
                            + ";"
                            + z[k4]
                            + ";"
                            + agents[k5]
                            + ";"
                            + ii[k6]
                            + ";"
                            + new BigDecimal(Math.random() * 10)
                                .setScale(0, BigDecimal.ROUND_HALF_UP)
                            + ";"
                            + new BigDecimal(Math.random() * 1000)
                                .setScale(2, BigDecimal.ROUND_HALF_UP)
                            + ";"
                            + "\n";
                    out.write(line.getBytes());
                    out.flush();
                    i++;
                  }
                }
            }
          }
        }
        t += 86400000;
      } while (i < maxRows);
      out.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
예제 #26
0
  @Override
  public String execute() throws Exception {
    Connection conn = null;
    PreparedStatement cStmt = null;
    String query;
    try {
      conn = Constants.DATASOURCE.getConnection();
      Long step = (super.cF.getTimeInMillis() - super.cI.getTimeInMillis()) / 1000;
      step = step / super.spansNumber + (step % super.spansNumber == 0L ? 0 : 1); // round up

      cI.add(Calendar.SECOND, (int) (start * step));
      Calendar c = null;
      if (limit != null && limit <= super.spansNumber)
        (c = (Calendar) cI.clone()).add(Calendar.SECOND, (int) (limit * step));
      else c = (Calendar) cF.clone();
      if (c.before(cF)) cF = c;

      // CallableStatement cStmt = conn.prepareCall(SqlQuery.SP_RTT_CHART.getSql());
      // cStmt.setInt(1, super.idServiceInstance);
      // cStmt.setTimestamp(2, new Timestamp(super.cI.getTimeInMillis()));
      // cStmt.setTimestamp(3, new Timestamp(super.cF.getTimeInMillis()));
      // cStmt.setLong(4, step);

      table = new ActivityTable();
      List<ActivityRecord> records = new LinkedList<ActivityRecord>();
      DateFormat formatter = new DateFormat(getLocale().getLanguage());
      c = (Calendar) cI.clone();
      // boolean hadResults = cStmt.execute();
      boolean fullTime = step < 60;

      if (step > 0) {
        while (c.getTimeInMillis() < cF.getTimeInMillis()) {

          query =
              "SELECT AVG(elapsedTime) as avgRtt FROM  esgf_dashboard.service_status WHERE idServiceInstance=";
          query = query + super.idServiceInstance;
          query = query + " AND timestamp between '";
          query = query + new Timestamp(c.getTimeInMillis()) + "' AND '";

          // while(hadResults) {
          // ResultSet rs = cStmt.getResultSet();
          ActivityRecord record = new ActivityRecord();
          record.setStartDate(formatter.formatDate(c));
          record.setStartTime(formatter.formatTime(c, fullTime));
          c.add(Calendar.SECOND, step.intValue());

          query = query + new Timestamp(c.getTimeInMillis()) + "'";
          // System.out.println("|||->>> RTTTableAction Query = "+query);

          cStmt = conn.prepareStatement(query);
          ResultSet rs = cStmt.executeQuery();

          record.setEndDate(formatter.formatDate(c));
          record.setEndTime(formatter.formatTime(c, fullTime));
          record.setValues(new LinkedList<Number>());
          if (rs.next() && rs.getBigDecimal("avgRtt") != null)
            record
                .getValues()
                .add(Math.round(rs.getBigDecimal("avgRtt").doubleValue() / 10.) / 100.);
          else record.getValues().add(null);
          records.add(record);
          // hadResults = cStmt.getMoreResults();
        } // endwhile
      } // endif
      cStmt.close();
      table.setActivityRecords(records);
      table.setTotalCount(super.spansNumber);
    } catch (SQLException e) {
      return ERROR;
    } finally {
      if (conn != null) conn.close();
    }
    return SUCCESS;
  }
예제 #27
0
  public void start() {

    Connection connection = null; // manages connection
    Connection connection2 = null;
    Statement statement = null; // query statement
    Statement statement2 = null;
    wm = new double[20][Data.windows_size];
    String QueryKinasesName = "%" + Data.kinease + "%";
    // data.code = data.codenames[3];
    String QueryCodeName = Data.code;
    int windows_size = Data.windows_size;

    // int windows_size = 9;

    int shift = windows_size / 2;

    try {
      Class.forName(JDBC_DRIVER); // load database driver class
      for (windows_size = Data.windows_size; windows_size <= Data.windows_size; windows_size += 2) {
        shift = windows_size / 2;
        // establish connection to database
        connection = DriverManager.getConnection(DATABASE_URL, "", "");
        connection2 = DriverManager.getConnection(DATABASE_URL, "", "");
        // create Statement for querying database
        statement = connection.createStatement();
        statement2 = connection2.createStatement();
        String ACC = null;
        String SEQUENCE = null;
        String KINASES = null;
        String LIKE = "LIKE";
        // int POSITION = 0;
        // int index = 0;
        String temp = null;
        // int numtemp = 0;
        // int LENGTH = (int) 0;
        // int count = 0;
        double[] totalAAcount = new double[windows_size];

        double weightmatrix[][] = new double[windows_size][128]; // windowssize;aa;

        String statementquery1 =
            "SELECT Mid(sequence,(position-"
                + shift
                + "),"
                + windows_size
                + ") AS TARGET, index,code,length,position,sequence FROM Dataset_041106 WHERE ((position-"
                + shift
                + ")>1) AND ((position +"
                + shift
                + ")<length) AND kinases "
                + LIKE
                + " '"
                + QueryKinasesName
                + "' AND (code LIKE '"
                + QueryCodeName
                + "')";
        System.out.println("#" + statementquery1);
        /// fout.println("#"+statementquery1);
        ResultSet resultSet1 = statement.executeQuery(statementquery1);
        int seqsize = 0;
        while ((resultSet1.next())) {
          String posseq = resultSet1.getString("TARGET");
          seqsize = posseq.length();
          if (posseq.charAt(0) != 'X' && posseq.charAt(seqsize - 1) != 'X') { // �h����t

            for (int i = 0; i < seqsize; i++) {

              weightmatrix[i][posseq.charAt(i)]++;
            }
            // possequence.addElement(posseq);
          }
        } // end while
        char[] aaMap = {
          'A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W',
          'Y', 'V', 'X'
        };
        double[] expmatrix = {
          0.0701313443873091,
          0.0582393695201718,
          0.0359362961736045,
          0.0520743144385134,
          0.0172010453343506,
          0.0498574962335004,
          0.0796465136978452,
          0.0624720283551962,
          0.0241405228512130,
          0.0416989778376737,
          0.0934441156861220,
          0.0632334844952389,
          0.0213293464067050,
          0.0324554733241482,
          0.0651181982370858,
          0.0881672518230193,
          0.0524630941595624,
          0.0101093184162382,
          0.0244701177088640,
          0.0578116909136386
        };
        // double[] aaMapfreq = new double[windows_size];
        double freq = 0;
        for (int j = 0; j < weightmatrix.length; j++) {
          for (int i = 0; i < aaMap.length - 1; i++) {
            totalAAcount[j] += weightmatrix[j][aaMap[i]];
          }
        }

        for (int i = 0; i < aaMap.length - 1; i++) {
          // profilefout.print(aaMap[i]);
          for (int j = 0; j < windows_size; j++) {

            freq = ((weightmatrix[j][aaMap[i]]) / (totalAAcount[j])) + 1;

            wm[i][j] = Math.log10((freq / expmatrix[i])) / Math.log10(2.0);

            //  profilefout.print(","+aaMapfreq[i]);

          }
          // profilefout.println();
        }

        // fout.close();
        // profilefout.close();

        resultSet1.close();
        connection.close();
      }

    } // end try
    catch (ClassNotFoundException classNotFound) {
      classNotFound.printStackTrace();
      System.exit(1);
    } catch (NullPointerException nullpointerException) {
      nullpointerException.printStackTrace();
      System.exit(1);

    } catch (SQLException ex) {
      /** @todo Handle this exception */
      /*        } catch (IOException ex) {
       */
      /** @todo Handle this exception */
    } catch (NoClassDefFoundError ex) {

    } finally { // ensure statement and connection are closed properly
      try {
        statement.close();
        statement2.close();

        connection.close();

        connection2.close();
      } catch (Exception exception) { // end try
        exception.printStackTrace();
        System.exit(1);
      } // end catch
    } // end finally
  } // end main
예제 #28
0
  public static void main(String[] args) {
    //////////////////////////
    // The values in following 4 lines should be user input
    int startPos = 200;
    int endPos = 1000;
    int totalNumPixel = 1044;
    double threshhold = 0.0001;
    /////////////////////////
    int numPixel = endPos - startPos;
    double wavelength[] = new double[totalNumPixel];
    double photonCount[] = new double[totalNumPixel];
    double SpecNoBk[] = new double[numPixel];
    double ThisSpectrum[] = new double[numPixel];
    double ThisXaxis[] = new double[numPixel];
    double Po[] = new double[numPixel];
    double Re[] = new double[numPixel];
    double P[] = new double[6];
    double Re2[] = new double[numPixel - 1];
    double mySUM[] = new double[numPixel];
    int ind[];
    double DEV;
    double prevDEV;
    Connection connection = null;
    Statement stmt = null;
    String pattern = "##.##";

    try {
      Scanner in = new Scanner(new FileReader(args[0]));
      int i = 0;
      while (in.hasNextDouble()) {
        wavelength[i] = in.nextDouble();
        photonCount[i] = in.nextDouble();
        ++i;
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    ThisSpectrum = Arrays.copyOfRange(photonCount, startPos, endPos);
    ThisXaxis = Arrays.copyOfRange(wavelength, startPos, endPos);
    final WeightedObservedPoints obs = new WeightedObservedPoints();

    for (int i = 0; i < numPixel; i++) {
      obs.add(ThisXaxis[i], ThisSpectrum[i]);
    }

    final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(5);
    P = fitter.fit(obs.toList());
    Polyval pVal = new Polyval(P, ThisXaxis, numPixel);
    Po = pVal.evl();

    for (int i = 0; i < numPixel; i++) {
      Re[i] = ThisSpectrum[i] - Po[i];
    }

    for (int i = 0; i < numPixel - 1; i++) {
      Re2[i] = Re[i + 1] - Re[i];
    }

    DEV = Math.sqrt(StatUtils.populationVariance(Re2, 0, Re2.length));
    for (int i = 0; i < numPixel; i++) {
      mySUM[i] = Po[i] + DEV;
    }

    int jj = 0; // jj is the length of points to be removed
    for (int i = 0; i < numPixel; i++) {
      if (ThisSpectrum[i] > mySUM[i]) {
        jj++;
        ;
      }
    }
    ind = new int[jj];

    int jjj = 0;
    for (int i = 0; i < numPixel; i++) {
      if (ThisSpectrum[i] > mySUM[i]) {
        ind[jjj] = i;
        jjj++;
      }
    }

    int indKeepLength = numPixel - ind.length;
    int indKeep[] = new int[indKeepLength];
    int k = 0;
    for (int i = 0; i < numPixel; i++) {
      if (!ArrayUtils.contains(ind, i)) {
        indKeep[k] = i;
        k++;
      }
    }
    double ThisSpectrumKeep[] = new double[indKeepLength];
    double ThisXaxisKeep[] = new double[indKeepLength];
    double PoKeep[] = new double[indKeepLength];
    double ReKeep[] = new double[indKeepLength];
    double Re2Keep[] = new double[indKeepLength - 1];
    double mySUMKeep[] = new double[indKeepLength];

    for (int i = 0; i < indKeepLength; i++) {
      ThisSpectrumKeep[i] = ThisSpectrum[indKeep[i]];
      ThisXaxisKeep[i] = ThisXaxis[indKeep[i]];
    }

    prevDEV = DEV;

    // at the point, ThisSpectrum and ThisXaxis should have reduced size
    final WeightedObservedPoints obs1 = new WeightedObservedPoints();

    for (int i = 0; i < indKeepLength; i++) {
      obs1.add(ThisXaxisKeep[i], ThisSpectrumKeep[i]);
    }

    while (true) {
      final PolynomialCurveFitter fitter1 = PolynomialCurveFitter.create(5);
      P = fitter1.fit(obs1.toList());
      Polyval pVal1 = new Polyval(P, ThisXaxisKeep, indKeepLength);
      PoKeep = pVal1.evl();

      for (int i = 0; i < indKeepLength; i++) {
        ReKeep[i] = ThisSpectrumKeep[i] - PoKeep[i];
      }

      for (int i = 0; i < indKeepLength - 1; i++) {
        Re2Keep[i] = ReKeep[i + 1] - ReKeep[i];
      }

      DEV = Math.sqrt(StatUtils.populationVariance(Re2Keep, 0, Re2Keep.length));

      for (int i = 0; i < indKeepLength; i++) {
        mySUMKeep[i] = PoKeep[i] + DEV;
      }

      for (int i = 0; i < indKeepLength; i++) {
        if (ThisSpectrumKeep[i] > mySUMKeep[i]) ThisSpectrumKeep[i] = mySUMKeep[i];
      }
      if ((Math.abs(DEV - prevDEV) / DEV) < threshhold) break;
      prevDEV = DEV;

      obs1.clear();
      for (int i = 0; i < indKeepLength; i++) {
        obs1.add(ThisXaxisKeep[i], ThisSpectrumKeep[i]);
      }
    }
    Polyval pVal2 = new Polyval(P, ThisXaxis, numPixel);
    double FLbk[] = pVal2.evl();
    for (int i = 0; i < ThisXaxis.length; i++) {
      SpecNoBk[i] = ThisSpectrum[i] - FLbk[i];
    }

    // the write-to-file part is only for testing purpose, ThisXaxis and SpecNoBk are two outputs
    try {
      FileWriter fr = new FileWriter(args[1]);
      BufferedWriter br = new BufferedWriter(fr);
      PrintWriter out = new PrintWriter(br);
      DecimalFormat df = new DecimalFormat(pattern);
      for (int j = 0; j < ThisXaxis.length; j++) {
        if (Double.toString(wavelength[j]) != null) out.write(ThisXaxis[j] + "\t" + SpecNoBk[j]);
        out.write("\r\n");
      }
      out.close();
    } catch (IOException e) {
      System.out.println(e);
    }
  }
예제 #29
0
  /** Tests the computation of line length histogram descriptors. */
  public void testHistogramDescriptors() {

    DRQA drqa = new DRQA(trajectory, eps);
    drqa.computeRQA(10, 10, 10); // should not change the result.
    drqa.computeRQA(2, 2, 2);

    Map<DRQA.HistogramStatistic, Double>
        expected_diagonal_rqa = new HashMap<DRQA.HistogramStatistic, Double>(),
        expected_vertical_rqa = new HashMap<DRQA.HistogramStatistic, Double>(),
        expected_white_vertical_rqa = new HashMap<DRQA.HistogramStatistic, Double>(),
        expected_orthogonal_rqa = new HashMap<DRQA.HistogramStatistic, Double>(),
        expected_diagonal_rqa_definite = new HashMap<DRQA.HistogramStatistic, Double>(),
        expected_vertical_rqa_definite = new HashMap<DRQA.HistogramStatistic, Double>(),
        expected_white_vertical_rqa_definite = new HashMap<DRQA.HistogramStatistic, Double>(),
        expected_orthogonal_rqa_definite = new HashMap<DRQA.HistogramStatistic, Double>();

    expected_diagonal_rqa.put(DRQA.HistogramStatistic.DIFFERENCE_ENTROPY, 0.6365141682948128);
    expected_diagonal_rqa.put(DRQA.HistogramStatistic.SORTEDNESS, 0.6666666666666666);
    expected_diagonal_rqa.put(DRQA.HistogramStatistic.MAX, 14.0);
    expected_diagonal_rqa.put(DRQA.HistogramStatistic.DEFINITENESS, 0.2727272727272727);
    expected_diagonal_rqa.put(DRQA.HistogramStatistic.ENTROPY, 1.0986122886681096);
    expected_diagonal_rqa.put(DRQA.HistogramStatistic.FILTER_RATIO, 0.6060606060606061);
    expected_diagonal_rqa.put(DRQA.HistogramStatistic.SPARSENESS, 0.8333333333333334);
    expected_diagonal_rqa.put(DRQA.HistogramStatistic.INVERSE_MAX, 0.07142857142857142);
    expected_diagonal_rqa.put(DRQA.HistogramStatistic.LOCAL_MAXIMA, 3.0 / 7.0);
    expected_diagonal_rqa.put(DRQA.HistogramStatistic.AVERAGE, 6.666666666666666);
    expected_diagonal_rqa.put(DRQA.HistogramStatistic.MEDIAN, 4.);

    expected_vertical_rqa.put(DRQA.HistogramStatistic.DIFFERENCE_ENTROPY, 0.0);
    expected_vertical_rqa.put(DRQA.HistogramStatistic.SORTEDNESS, 1.0);
    expected_vertical_rqa.put(DRQA.HistogramStatistic.MAX, 3.0);
    expected_vertical_rqa.put(DRQA.HistogramStatistic.DEFINITENESS, 0.7115384615384616);
    expected_vertical_rqa.put(DRQA.HistogramStatistic.ENTROPY, 0.5402041423888608);
    expected_vertical_rqa.put(DRQA.HistogramStatistic.FILTER_RATIO, 0.5576923076923077);
    expected_vertical_rqa.put(DRQA.HistogramStatistic.SPARSENESS, 0.0);
    expected_vertical_rqa.put(DRQA.HistogramStatistic.INVERSE_MAX, 0.3333333333333333);
    expected_vertical_rqa.put(DRQA.HistogramStatistic.LOCAL_MAXIMA, 1.0);
    expected_vertical_rqa.put(DRQA.HistogramStatistic.AVERAGE, 2.230769230769231);
    expected_vertical_rqa.put(DRQA.HistogramStatistic.MEDIAN, 2.);

    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.DIFFERENCE_ENTROPY, 1.3296613488547582);
    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.SORTEDNESS, 0.65);
    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.MAX, 7.0);
    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.DEFINITENESS, 0.6041666666666666);
    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.ENTROPY, 1.6470013963439956);
    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.FILTER_RATIO, 1.0);
    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.SPARSENESS, 0.16666666666666666);
    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.INVERSE_MAX, 0.14285714285714285);
    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.LOCAL_MAXIMA, 2.0 / 4.0);
    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.AVERAGE, 3.5999999999999996);
    expected_white_vertical_rqa.put(DRQA.HistogramStatistic.MEDIAN, 3.);

    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.DIFFERENCE_ENTROPY, 0.0);
    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.SORTEDNESS, 0.5);
    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.MAX, 5.0);
    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.DEFINITENESS, 0.42105263157894735);
    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.ENTROPY, 0.6931471805599453);
    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.FILTER_RATIO, 0.42105263157894735);
    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.SPARSENESS, 0.6666666666666666);
    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.INVERSE_MAX, 0.2);
    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.LOCAL_MAXIMA, 1.0);
    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.AVERAGE, 4.0);
    expected_orthogonal_rqa.put(DRQA.HistogramStatistic.MEDIAN, 3.);

    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.DIFFERENCE_ENTROPY, 0.0);
    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.SORTEDNESS, 1.0);
    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.MAX, 2.0);
    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.DEFINITENESS, 0.2727272727272727);
    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.ENTROPY, 0.0);
    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.FILTER_RATIO, 0.6060606060606061);
    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.SPARSENESS, 0.0);
    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.INVERSE_MAX, 0.5);
    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.LOCAL_MAXIMA, 1.0);
    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.AVERAGE, 2.0);
    expected_diagonal_rqa_definite.put(DRQA.HistogramStatistic.MEDIAN, 2.);

    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.DIFFERENCE_ENTROPY, 0.0);
    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.SORTEDNESS, 1.0);
    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.MAX, 3.0);
    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.DEFINITENESS, 0.7115384615384616);
    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.ENTROPY, 0.6615632381579821);
    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.FILTER_RATIO, 0.5576923076923077);
    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.SPARSENESS, 0.0);
    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.INVERSE_MAX, 0.3333333333333333);
    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.LOCAL_MAXIMA, 1.0);
    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.AVERAGE, 2.375);
    expected_vertical_rqa_definite.put(DRQA.HistogramStatistic.MEDIAN, 2.);

    expected_white_vertical_rqa_definite.put(
        DRQA.HistogramStatistic.DIFFERENCE_ENTROPY, 1.3296613488547582);
    expected_white_vertical_rqa_definite.put(
        DRQA.HistogramStatistic.SORTEDNESS, 0.6363636363636364);
    expected_white_vertical_rqa_definite.put(DRQA.HistogramStatistic.MAX, 7.0);
    expected_white_vertical_rqa_definite.put(
        DRQA.HistogramStatistic.DEFINITENESS, 0.6041666666666666);
    expected_white_vertical_rqa_definite.put(DRQA.HistogramStatistic.ENTROPY, 1.5196682491027627);
    expected_white_vertical_rqa_definite.put(DRQA.HistogramStatistic.FILTER_RATIO, 1.0);
    expected_white_vertical_rqa_definite.put(
        DRQA.HistogramStatistic.SPARSENESS, 0.3333333333333333);
    expected_white_vertical_rqa_definite.put(
        DRQA.HistogramStatistic.INVERSE_MAX, 0.14285714285714285);
    expected_white_vertical_rqa_definite.put(DRQA.HistogramStatistic.LOCAL_MAXIMA, 0.5);
    expected_white_vertical_rqa_definite.put(DRQA.HistogramStatistic.AVERAGE, 3.954545454545454);
    expected_white_vertical_rqa_definite.put(DRQA.HistogramStatistic.MEDIAN, 3.);

    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.DIFFERENCE_ENTROPY, 0.0);
    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.SORTEDNESS, 0.0);
    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.MAX, 3.0);
    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.DEFINITENESS, 0.42105263157894735);
    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.ENTROPY, 0.0);
    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.FILTER_RATIO, 0.42105263157894735);
    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.SPARSENESS, 1.0);
    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.INVERSE_MAX, 0.3333333333333333);
    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.LOCAL_MAXIMA, 1.0);
    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.AVERAGE, 3.0);
    expected_orthogonal_rqa_definite.put(DRQA.HistogramStatistic.MEDIAN, 3.);

    assertEquals(expected_diagonal_rqa, drqa.diagonal_rqa);
    assertEquals(expected_vertical_rqa, drqa.vertical_rqa);
    assertEquals(expected_white_vertical_rqa, drqa.white_vertical_rqa);
    assertEquals(expected_orthogonal_rqa, drqa.orthogonal_rqa);
    assertEquals(expected_diagonal_rqa_definite, drqa.diagonal_rqa_definite);
    assertEquals(expected_vertical_rqa_definite, drqa.vertical_rqa_definite);
    assertEquals(expected_white_vertical_rqa_definite, drqa.white_vertical_rqa_definite);
    assertEquals(expected_orthogonal_rqa_definite, drqa.orthogonal_rqa_definite);

    assertEquals(Math.max(2, Math.round(37.0 / 26.)), drqa.estimate_l_min(), 1e-10);
  }