/** Method declaration Adjust this method for large strings...ie multi megabtypes. */
  void execute() {

    String sCmd = null;

    if (4096 <= ifHuge.length()) {
      sCmd = ifHuge;
    } else {
      sCmd = txtCommand.getText();
    }

    if (sCmd.startsWith("-->>>TEST<<<--")) {
      testPerformance();

      return;
    }

    String g[] = new String[1];

    lTime = System.currentTimeMillis();

    try {
      sStatement.execute(sCmd);

      lTime = System.currentTimeMillis() - lTime;

      int r = sStatement.getUpdateCount();

      if (r == -1) {
        formatResultSet(sStatement.getResultSet());
      } else {
        g[0] = "update count";

        gResult.setHead(g);

        g[0] = String.valueOf(r);

        gResult.addRow(g);
      }

      addToRecent(txtCommand.getText());
    } catch (SQLException e) {
      lTime = System.currentTimeMillis() - lTime;
      g[0] = "SQL Error";

      gResult.setHead(g);

      String s = e.getMessage();

      s += " / Error Code: " + e.getErrorCode();
      s += " / State: " + e.getSQLState();
      g[0] = s;

      gResult.addRow(g);
    }

    updateResult();
    System.gc();
  }
Ejemplo n.º 2
0
  /* play the game */
  private void play() {
    t0 = System.currentTimeMillis(); /*Times the beginning of the game */
    currentTime = System.currentTimeMillis() - t0;
    generateRandomOrderTimes();
    System.out.println("next t1 is in " + orderTimes.get(0));
    System.out.println("next t2 is in " + orderTimes.get(1));
    System.out.println("next t3 is in " + orderTimes.get(2));

    while (System.currentTimeMillis() - t0 < ENDOFGAMETIME) {
      pause(100);
      /*Update the days label*/
      daysElapsed = (int) ((System.currentTimeMillis() - t0) / (LENGTHOFADAY * 1000));
      daysElapsedLabel.setLabel("DAYS " + daysElapsed);
      scanEquipments();
    }
  }
Ejemplo n.º 3
0
  public void init() {
    is_master = false;
    dyn_usr = 0;
    dyn_view = 0;
    id = (int) System.currentTimeMillis();

    ClientApplet.client_applet = this;

    appletContext = getAppletContext();

    hostsrv = getParameter("HOSTSRV");

    String http_portString = getParameter("HTTP_PORT");
    if (http_portString != null) {
      try {
        Integer port = Integer.valueOf(http_portString);
        http_port = port.intValue();
      } catch (NumberFormatException e) {
        http_port = 50000;
      }
    }

    URL applet_url = this.getCodeBase();

    String host = applet_url.getHost();
    if (host != null) hostsrv = host;

    int port = applet_url.getPort();
    if (port > 0) http_port = port;

    user_interface = new UserInterface();
    user_interface.init();
  }
Ejemplo n.º 4
0
 /*setup the game */
 private void setup() {
   t0 = System.currentTimeMillis(); /*Times the beginning of the game */
   // int size;
   Scanner in = new Scanner(System.in);
   // System.out.println("Enter the size of available");
   // size = in.nextInt();
   initiateScores();
   placeWalls();
   placeStates();
   placeLabels();
   randomizeOrderRent(); /*randomizes the order of the equipment for the On Rent state */
   System.out.println(
       "How many High Runners at price $" + EQUIPMENTCOSTS[0] + " do you want to buy?");
   int type1Equip = in.nextInt();
   System.out.println(
       "How many Medium Runners at price $" + EQUIPMENTCOSTS[1] + " do you want to buy?");
   int type2Equip = in.nextInt();
   System.out.println(
       "How many Low Runners at price $" + EQUIPMENTCOSTS[2] + " do you want to buy?");
   int type3Equip = in.nextInt();
   capitalInvested =
       EQUIPMENTCOSTS[0] * type1Equip
           + EQUIPMENTCOSTS[1] * type2Equip
           + EQUIPMENTCOSTS[2] * type3Equip;
   capitalLabel.setLabel("Capital Invested: $" + capitalInvested);
   fillAvailable(type1Equip, type2Equip, type3Equip); // fills with the proper number of equipment
   // fillStates(size,INITRENT,INITSHOP);
   placeEquipments();
 }
Ejemplo n.º 5
0
  /*compute the initial Time outs for all equipments */
  private void computeInitialTimes() {
    System.out.println("t0 is" + t0);
    double timeDiff = 0;

    /*Times for available equipment will depend on the type and position in queue as it's FIFO */
    generateRandomOrderTimes();

    /*for equipment on rent, it's simply at the end of the rental period */
    for (Equipment e : rentEquipment) {
      e.timeIn = System.currentTimeMillis() - t0;
      timeDiff = nextRandomTime(RENTALFREQUENCY[e.type - 1]);
      e.timeOut = System.currentTimeMillis() + timeDiff - t0;
      System.out.println("I am in state " + e.state + " and order " + rentEquipment.indexOf(e));
      System.out.println(
          "My time in is "
              + e.timeIn
              + " And my time out is "
              + e.timeOut
              + " and the difference between the 2 is "
              + (e.timeOut - e.timeIn));
      /*System.out.println("and nextRandomTime returns" + nextRandomTime(e.timeIn,RENTALFREQUENCY[e.type-1]) +" for type "+e.type+" for RentalFrequency "+RENTALFREQUENCY[e.type-1]);
      System.out.println("For equipment in state "+e.state+" and order "+rentEquipment.indexOf(e)+" : inter-arrival is "+(e.timeOut-e.timeIn)); */
    }

    /*for equipment in the shop, it's simply at the end of the rental period */
    for (Equipment e : shopEquipment) {
      e.timeIn = System.currentTimeMillis() - t0;
      timeDiff = nextRandomTime(SHOPFREQUENCY[e.type - 1]);
      e.timeOut = System.currentTimeMillis() + timeDiff - t0;
      System.out.println("I am in state " + e.state + " and order " + shopEquipment.indexOf(e));
      System.out.println(
          "My time in is "
              + e.timeIn
              + " And my time out is "
              + e.timeOut
              + " and the difference between the 2 is "
              + (e.timeOut - e.timeIn));
      /*System.out.println("For equipment in state "+e.state+" and order "+shopEquipment.indexOf(e)+" : inter-arrival is "+(e.timeOut-e.timeIn)); */
    }
  }
Ejemplo n.º 6
0
  /*Scans the particular equipment and decides whether or not it shoud be moved */
  private int scan(Equipment equip) {
    double currentTime = System.currentTimeMillis() - t0;
    int orderMoved = -1;
    int or = -1;
    switch (equip.state) {
      case 1:
        or = availEquipment.indexOf(equip);
        break;
      case 2:
        or = rentEquipment.indexOf(equip);
        break;
      case 3:
        or = shopEquipment.indexOf(equip);
        break;
    }
    // System.out.println("Now looking at state "+equip.state+" and type "+equip.type+" and order"
    // +or);
    // System.out.println("Current time is "+currentTime+" while equip.timeOut time is
    // "+equip.timeOut+" and the difference is "+(equip.timeOut-currentTime));

    switch (equip.state) {
      case 1:
        break;
      case 2:
        if (equip.timeOut < System.currentTimeMillis() - t0) {
          orderMoved = rentEquipment.indexOf(equip);
          return orderMoved;
        }
        break;
      case 3:
        if (equip.timeOut < System.currentTimeMillis() - t0) {
          orderMoved = shopEquipment.indexOf(equip);
          // System.out.println("I have just moved equipment from state 3 "+equip.state+" of type
          // "+equip.type+" and of order "+shopEquipment.indexOf(equip));
          return orderMoved;
        }
        break;
    }
    return orderMoved;
  }
Ejemplo n.º 7
0
  /* Generates random order times for the orders of each type in the Available state */
  private void generateRandomOrderTimes() {
    orderTime1 = System.currentTimeMillis() + nextRandomTime(ARRIVAL1) - t0;
    orderTime2 = System.currentTimeMillis() + nextRandomTime(ARRIVAL2) - t0;
    orderTime3 = System.currentTimeMillis() + nextRandomTime(ARRIVAL2) - t0;

    double[] arrivalFrequencies = {ARRIVAL1, ARRIVAL2, ARRIVAL3};
    orderTimes.add(System.currentTimeMillis() + nextRandomTime(ARRIVAL1) - t0);
    orderTimes.add(System.currentTimeMillis() + nextRandomTime(ARRIVAL2) - t0);
    orderTimes.add(System.currentTimeMillis() + nextRandomTime(ARRIVAL3) - t0);

    /*for (double counter : arrivalFrequencies) {
    	System.out.println("t0 is = "+t0+" and current time is "+currentTime);
    	currentTime=System.currentTimeMillis()-t0;
    	orderTimes.add(System.currentTimeMillis()+nextRandomTime(counter)-t0);
    }*/
  }
  /** Method declaration */
  void testPerformance() {

    String all = txtCommand.getText();
    StringBuffer b = new StringBuffer();
    long total = 0;

    for (int i = 0; i < all.length(); i++) {
      char c = all.charAt(i);

      if (c != '\n') {
        b.append(c);
      }
    }

    all = b.toString();

    String g[] = new String[4];

    g[0] = "ms";
    g[1] = "count";
    g[2] = "sql";
    g[3] = "error";

    gResult.setHead(g);

    int max = 1;

    lTime = System.currentTimeMillis() - lTime;

    while (!all.equals("")) {
      int i = all.indexOf(';');
      String sql;

      if (i != -1) {
        sql = all.substring(0, i);
        all = all.substring(i + 1);
      } else {
        sql = all;
        all = "";
      }

      if (sql.startsWith("--#")) {
        max = Integer.parseInt(sql.substring(3));

        continue;
      } else if (sql.startsWith("--")) {
        continue;
      }

      g[2] = sql;

      long l = 0;

      try {
        l = DatabaseManagerCommon.testStatement(sStatement, sql, max);
        total += l;
        g[0] = String.valueOf(l);
        g[1] = String.valueOf(max);
        g[3] = "";
      } catch (SQLException e) {
        g[0] = g[1] = "n/a";
        g[3] = e.toString();
      }

      gResult.addRow(g);
      System.out.println(l + " ms : " + sql);
    }

    g[0] = "" + total;
    g[1] = "total";
    g[2] = "";

    gResult.addRow(g);

    lTime = System.currentTimeMillis() - lTime;

    updateResult();
  }
Ejemplo n.º 9
0
 private double system_time() {
   return System.currentTimeMillis() / 1000.;
 }
Ejemplo n.º 10
0
 double clockTime() {
   return System.currentTimeMillis() / 1000.;
 }
Ejemplo n.º 11
0
Archivo: set.java Proyecto: wcyuan/Set
  ///////////////////////////////////////////////////////////////
  // inner classes: Cards
  ///////////////////////////////////////////////////////////////
  public static class Cards {
    private int[] deck;
    private int num_cards;
    private int current;
    private java.util.Random rand = new java.util.Random(System.currentTimeMillis());

    public Cards(int n) {
      if (n <= 0) n = 1;
      num_cards = n;
      deck = new int[n];
      for (int i = 0; i < num_cards; i++) deck[i] = i;
      shuffle();
    }

    public void shuffle() {
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < num_cards; j++) {
          int k = Math.abs(rand.nextInt() % num_cards);
          /* swap */
          int temp = deck[j];
          deck[j] = deck[k];
          deck[k] = temp;
        }
      }
      current = 0;
    }

    public boolean EOD() {
      return (current >= num_cards);
    }

    public int NextCard() {
      if (EOD()) shuffle();
      return deck[current++];
    }

    public static class Test {
      public static void main(String[] args) {

        if (args.length < 3) return;

        int a = java.lang.Integer.parseInt(args[0]);
        int b = java.lang.Integer.parseInt(args[1]);
        int c = java.lang.Integer.parseInt(args[2]);
        int mask = 1, div = 3;
        int x, y, z;
        // a--; b--; c--;
        for (int i = 0; i < 4; i++) {
          x = (a % div);
          y = (b % div);
          z = (c % div);
          System.out.println(
              "mask: " + mask + " div: " + div + " x: " + x + " y: " + y + " z: " + z);
          x /= mask;
          y /= mask;
          z /= mask;
          if (!(x == y && x == z) && !(x != y && x != z && y != z)) {
            System.out.println(
                "mask: " + mask + " div: " + div + " x: " + x + " y: " + y + " z: " + z);
            return;
          }
          mask *= 3;
          div *= 3;
        }
        return;

        /*
          if (IsSet(java.lang.Integer.parseInt(args[0]),
          java.lang.Integer.parseInt(args[1]),
          java.lang.Integer.parseInt(args[2])))
          {
          System.out.println("YES");
          } else
          {
          System.out.println("NO");
          }

          Cards c = new Cards(21);
          for (int j = 0; j < 2; j++)
          for (int i = 0; i < 21; i++)
          {
          System.out.println(i + " " + c.NextCard());
          }
        */
      }
    }
  }
Ejemplo n.º 12
0
  /*Scans all equipments and identifies the ones that need to be moved */
  private void scanEquipments() {
    double timeDiff = 0;
    int orderToMove = -1;
    double[] arrivalFrequencies = {ARRIVAL1, ARRIVAL2, ARRIVAL3};

    /*In the available state, the scan should not happen on the equipment as it's external customer demand and we should record a "lost sale" when there is no equipment*/
    for (int counter = 0; counter < TYPES; counter++) {
      // System.out.println("I am computing for arrival frequency "+arrivalFrequencies[counter]+ "
      // and the corresponding time is "+orderTimes.get(counter));
      if (orderTimes.get(counter) < System.currentTimeMillis() - t0) {
        System.out.println("Time is " + System.currentTimeMillis());
        orderTimes.set(
            counter, System.currentTimeMillis() + nextRandomTime(arrivalFrequencies[counter]) - t0);
        System.out.println(
            "I have just computed a new exit time for state 1 and type "
                + counter
                + " = "
                + orderTimes.get(counter));
        orderToMove =
            checkOrder(
                1,
                counter + 1); // checks the order of the type being moved in the available arraylist
        if (orderToMove >= 0) {
          Equipment e = availEquipment.get(orderToMove);
          timeDiff =
              nextRandomTime(
                  RENTALFREQUENCY[
                      e.type
                          - 1]); // Equipment is moving to rent - so its next timeOut should be that
          // of rent
          e.timeOut = System.currentTimeMillis() + timeDiff - t0;
          moveEquip(1, orderToMove);
          sales = sales + RATES[counter];
          salesLabel.setLabel("SALES : $" + sales);
        } else {
          System.out.println("You just lost a sale");
          int ls = lostSales.get(counter);
          lostSales.set(counter, ls + 1);
          switch (counter) {
            case 0:
              lostSalesLabel1.setLabel("Lost Sales HR = " + ls + 1);
              break;
            case 1:
              lostSalesLabel2.setLabel("Lost Sales MR = " + ls + 1);
              break;
            case 2:
              lostSalesLabel3.setLabel("Lost Sales LR = " + ls + 1);
              break;
          }
        }
      }
    }

    for (Equipment e : rentEquipment) {
      orderToMove = scan(e);
      if (orderToMove >= 0) {
        /*Entering this loop means there is an equipment to move */
        timeDiff =
            nextRandomTime(
                SHOPFREQUENCY[
                    e.type - 1]); // Equipment is moving to shop so its next timeOut is that of shop
        // System.out.println("Rental frequency is "+RENTALFREQUENCY[e.type-1]+" and timeDiff is
        // "+timeDiff);
        e.timeOut = System.currentTimeMillis() + timeDiff - t0;
        System.out.println("the new timeOut is " + e.timeOut);
        break;
      }
    }
    if (orderToMove >= 0) {
      moveEquip(2, orderToMove);
      // System.out.println("I have just moved equipment from state 2 and of order "+orderToMove);
      // placeEquipments();
    }

    for (Equipment e : shopEquipment) {
      orderToMove = scan(e);
      if (orderToMove >= 0) {
        /*Entering this loop means there is an equipment to move */
        e.timeOut = 0; // Doesn't matter what the time-out is as it's not governed by the equipment
        break;
      }
    }
    if (orderToMove >= 0) {
      moveEquip(3, orderToMove);
      // placeEquipments();
    }
  }