Beispiel #1
1
 @Override
 public String getHeaderField(final String field) {
   final List<String> values = headers.get(field);
   final StringBuilder sb = new StringBuilder();
   for (final String v : values) sb.append(v).append(';');
   return sb.substring(0, sb.length() - 1);
 }
  private void printStat(LiveStatistics ls) {
    long ms = ls.getTimeperiod() * 15000;
    DateTime time = new DateTime(ms);

    StringBuilder sb = new StringBuilder();
    sb.append(time.getDayOfMonth())
        .append(".")
        .append(time.getMonthOfYear())
        .append(".")
        .append(time.getYear());
    sb.append(" ")
        .append(time.getHourOfDay())
        .append(":")
        .append(time.getMinuteOfHour())
        .append(":")
        .append(time.getSecondOfMinute());

    System.out.println(
        ls.getTimeperiod()
            + ";"
            + ls.getAccountName()
            + "; date: "
            + sb.toString()
            + " value: "
            + ls.getValue()
            + " unitType: "
            + ls.getUnitType()
            + " valueType: "
            + ls.getValueType());
  }
  @Override
  protected void writeConnectors(StringBuilder output) throws Exception {
    String[] connectorClasses = getConnectorClasses();
    String[] connectorNames = getConnectorNames();
    for (int i = 0; i < connectorNames.length; i++) {
      output.append(
          "    <repositoryconnector name=\""
              + connectorNames[i]
              + "\" class=\""
              + connectorClasses[i]
              + "\"/>\n");
    }

    String[] outputClasses = getOutputClasses();
    String[] outputNames = getOutputNames();
    for (int i = 0; i < outputNames.length; i++) {
      output.append(
          "    <outputconnector name=\""
              + outputNames[i]
              + "\" class=\""
              + outputClasses[i]
              + "\"/>\n");
    }

    String[] authorityClasses = getAuthorityClasses();
    String[] authorityNames = getAuthorityNames();
    for (int i = 0; i < authorityNames.length; i++) {
      output.append(
          "    <authorityconnector name=\""
              + authorityNames[i]
              + "\" class=\""
              + authorityClasses[i]
              + "\"/>\n");
    }
  }
 @Test
 public void canExtractCorrectPath() {
   Handshake76 handshake = new Handshake76();
   RequestHeaderMap headerMapper = new RequestHeaderMap();
   Map<Keys, String> map = headerMapper.extractHeader(getClientHandshakeString());
   Assert.assertNotNull(handshake.createResponseHandshake(map));
   byte[] bytes = handshake.createResponseHandshake(map);
   StringBuilder builder = new StringBuilder();
   for (int i = 0; i < bytes.length; i++) {
     builder.append((int) bytes[i]);
   }
   Assert.assertEquals(correctResponse, builder.toString());
 }
  public static String randomString(String fldName, int length) {

    if (fldName.length() >= length) {
      return fldName.substring(0, length);
    }

    sb.setLength(0);
    sb.append(fldName);
    for (int i = fldName.length(); i < length; i++) {
      sb.append(chars.charAt(random.nextInt(chars.length())));
    }
    return sb.toString();
  }
Beispiel #6
0
  /** Tests the internal parser (Option {@link MainOptions#INTPARSE}). */
  @Test
  public void intParse() {
    set(MainOptions.CHOP, false);

    final StringBuilder sb = new StringBuilder();

    final String[] docs = {
      "<x/>",
      " <x/> ",
      "<x></x>",
      "<x>A</x>",
      "<x><x>",
      "<x/><x/>",
      "<x></x><x/>",
      "<x>",
      "</x>",
      "<x></x></x>",
      "x<x>",
      "<x>x",
      "<x><![CDATA[ ]]></x>",
    };
    for (final String doc : docs) {
      // parse document with default parser (expected to yield correct result)
      set(MainOptions.INTPARSE, false);
      boolean def = true;
      try {
        new CreateDB(NAME, doc).execute(context);
      } catch (final BaseXException ex) {
        def = false;
      }

      // parse document with internal parser
      set(MainOptions.INTPARSE, true);
      boolean cust = true;
      try {
        new CreateDB(NAME, doc).execute(context);
      } catch (final BaseXException ex) {
        cust = false;
      }

      // compare results
      if (def != cust) {
        sb.append('\n').append(def ? "- not accepted: " : "- not rejected: ").append(doc);
      }
    }

    // list all errors
    if (sb.length() != 0) fail(sb.toString());

    set(MainOptions.MAINMEM, false);
  }
 private ArrayList<String> randomStrings() {
   ArrayList<String> strings = new ArrayList<String>();
   Random gen = new Random();
   for (int i = 0; i < 1000000; i++) {
     StringBuilder s = new StringBuilder();
     int thisLength = gen.nextInt(15) + 4;
     for (int j = 0; j < thisLength; j++) {
       int random = gen.nextInt(256);
       char c = (char) random;
       s.append(c);
     }
     strings.add(s.toString());
   }
   return strings;
 }
  @Test
  public void testInput4() {
    String test = "ABCDE-+$*EF*-";
    // ((A (B (C (DE-) +) $) *) (EF*) -)

    addLine("LD", "D");
    addLine("SB", "E");
    addLine("ST", "TEMP1");
    addLine("LD", "C");
    addLine("AD", "TEMP1");
    addLine("ST", "TEMP2");
    addLine("LD", "B");
    addLine("XP", "TEMP2");
    addLine("ST", "TEMP3");
    addLine("LD", "A");
    addLine("ML", "TEMP3");
    addLine("ST", "TEMP4");
    addLine("LD", "E");
    addLine("ML", "F");
    addLine("ST", "TEMP5");
    addLine("LD", "TEMP4");
    addLine("SB", "TEMP5");
    addLine("ST", "TEMP6");

    PostFixToMachineInstructions pf = new PostFixToMachineInstructions(test);
    assertEquals("Input4: ", ans.toString(), pf.convert());
  }
  public static String readProgramFromFile(String path) {
    StringBuilder programString = new StringBuilder();
    String newLine = System.getProperty("line.separator");
    Scanner scanner = new Scanner("");
    try {
      scanner = new Scanner(new FileReader(path));
      while (scanner.hasNextLine())
        programString.append(scanner.nextLine().replaceAll("\\p{javaWhitespace}+$", "") + newLine);
    } catch (FileNotFoundException e) {
      System.err.println(
          "readProgramFromFile: " + "The file with the provided path was not found.");
    } finally {
      scanner.close();
    }

    return programString.toString().trim();
  }
  /*
     This is a copy of buildDeterministicValue() from core:com.yahoo.ycsb.workloads.CoreWorkload.java.
     That method is neither public nor static so we need a copy.
  */
  private String buildDeterministicValue(String key, String fieldkey) {
    int size = FIELD_LENGTH;
    StringBuilder sb = new StringBuilder(size);
    sb.append(key);
    sb.append(':');
    sb.append(fieldkey);
    while (sb.length() < size) {
      sb.append(':');
      sb.append(sb.toString().hashCode());
    }
    sb.setLength(size);

    return sb.toString();
  }
  @Test
  public void testInput2() {
    String test = "ABC+-";
    addLine("LD", "B");
    addLine("AD", "C");
    addLine("ST", "TEMP1");
    addLine("LD", "A");
    addLine("SB", "TEMP1");
    addLine("ST", "TEMP2");

    PostFixToMachineInstructions pf = new PostFixToMachineInstructions(test);
    assertEquals("Input2: ", ans.toString(), pf.convert());
  }
Beispiel #12
0
  /**
   * It transforms a ResultSet instance to rows represented as strings.
   *
   * @param resultSet ResultSet that contains a query result
   * @return String
   * @throws SQLException
   */
  public String resultSetToString(ResultSet resultSet) throws SQLException {
    StringBuilder sb = new StringBuilder();
    ResultSetMetaData rsmd = resultSet.getMetaData();
    int numOfColumns = rsmd.getColumnCount();

    for (int i = 1; i <= numOfColumns; i++) {
      if (i > 1) sb.append(",");
      String columnName = rsmd.getColumnName(i);
      sb.append(columnName);
    }
    sb.append("\n-------------------------------\n");

    while (resultSet.next()) {
      for (int i = 1; i <= numOfColumns; i++) {
        if (i > 1) sb.append(",");
        String columnValue = resultSet.getObject(i).toString();
        sb.append(columnValue);
      }
      sb.append("\n");
    }
    return sb.toString();
  }
  @Test
  public void testInput9() {
    String test = "ABC+$CBA-+*";
    // ((A (BC+) $) (C (BA-) +) *)
    addLine("LD", "B");
    addLine("AD", "C");
    addLine("ST", "TEMP1");
    addLine("LD", "A");
    addLine("XP", "TEMP1");
    addLine("ST", "TEMP2");
    addLine("LD", "B");
    addLine("SB", "A");
    addLine("ST", "TEMP3");
    addLine("LD", "C");
    addLine("AD", "TEMP3");
    addLine("ST", "TEMP4");
    addLine("LD", "TEMP2");
    addLine("ML", "TEMP4");
    addLine("ST", "TEMP5");

    PostFixToMachineInstructions pf = new PostFixToMachineInstructions(test);
    assertEquals("Input9: ", ans.toString(), pf.convert());
  }
  @Test
  public void testInput3() {
    String test = "AB-C+DEF-+$";
    // (((AB-) C+)  (D (EF-)+) $)

    addLine("LD", "A");
    addLine("SB", "B");
    addLine("ST", "TEMP1");
    addLine("LD", "TEMP1");
    addLine("AD", "C");
    addLine("ST", "TEMP2");
    addLine("LD", "E");
    addLine("SB", "F");
    addLine("ST", "TEMP3");
    addLine("LD", "D");
    addLine("AD", "TEMP3");
    addLine("ST", "TEMP4");
    addLine("LD", "TEMP2");
    addLine("XP", "TEMP4");
    addLine("ST", "TEMP5");

    PostFixToMachineInstructions pf = new PostFixToMachineInstructions(test);
    assertEquals("Input3: ", ans.toString(), pf.convert());
  }
  @Test
  /*public void getResponseKey() {
  	Handshake76 handshake = new Handshake76();
  	byte[] bytes = handshake.getResponseKey(
  			handshake.getKey("21 xq120c! o287Q 5@5a"),
  			handshake.getKey("2  s9 98  6 b 3Q6 Y  355 D"),
  			"8AP]ïv".getBytes());
  	StringBuilder builder = new StringBuilder();
  	for (int i = 0; i < bytes.length; i++) {
  		builder.append((int)bytes[i]);
  	}
  	Assert.assertEquals(
  			"127205494-111-20-11-39-9521134-11266221",
  			builder.toString());
  }*/

  private String getClientHandshakeString() {
    StringBuilder builder = new StringBuilder();
    builder.append("GET / HTTP/1.1\r\n");
    builder.append("Upgrade: WebSocket\r\n");
    builder.append("Connection: Upgrade\r\n");
    builder.append("Host: localhost\r\n");
    builder.append("Origin: null\r\n");
    builder.append("Sec-WebSocket-Key1: 21 xq120c! o287Q 5@5a\r\n");
    builder.append("Sec-WebSocket-Key2: 2  s9 98  6 b 3Q6 Y  355 D\r\n");
    builder.append("\r\n");
    builder.append("8AP]ïv");
    return builder.toString();
  }
 private void addLine(String str) {
   ans.append(str + System.getProperty("line.separator"));
 }
Beispiel #17
0
  // Test-on-Train.  Slow test, needed to build a good model.
  @Test
  public void testGBMTrainTest() {
    File file1 = TestUtil.find_test_file("..//classifcation1Train.txt");
    if (file1 == null) return; // Silently ignore if file not found
    Key fkey1 = NFSFileVec.make(file1);
    Key dest1 = Key.make("train.hex");
    File file2 = TestUtil.find_test_file("..//classification1Test.txt");
    Key fkey2 = NFSFileVec.make(file2);
    Key dest2 = Key.make("test.hex");
    GBM gbm = null;
    Frame fr = null, fpreds = null;
    try {
      gbm = new GBM();
      fr = ParseDataset2.parse(dest1, new Key[] {fkey1});
      UKV.remove(fkey1);
      UKV.remove(fr.remove("agentId")._key); // Remove unique ID; too predictive
      gbm.response = fr.remove("outcome"); // Train on the outcome
      gbm.source = fr;
      gbm.ntrees = 5;
      gbm.max_depth = 10;
      gbm.learn_rate = 0.2f;
      gbm.min_rows = 10;
      gbm.nbins = 100;
      gbm.invoke();

      // Test on the train data
      Frame ftest = ParseDataset2.parse(dest2, new Key[] {fkey2});
      UKV.remove(fkey2);
      fpreds = gbm.score(ftest);

      // Build a confusion matrix
      ConfusionMatrix CM = new ConfusionMatrix();
      CM.actual = ftest;
      CM.vactual = ftest.vecs()[ftest.find("outcome")];
      CM.predict = fpreds;
      CM.vpredict = fpreds.vecs()[fpreds.find("predict")];
      CM.serve(); // Start it, do it

      // Really crappy cut-n-paste of what should be in the ConfusionMatrix class itself
      long cm[][] = CM.cm;
      long acts[] = new long[cm.length];
      long preds[] = new long[cm[0].length];
      for (int a = 0; a < cm.length; a++) {
        long sum = 0;
        for (int p = 0; p < cm[a].length; p++) {
          sum += cm[a][p];
          preds[p] += cm[a][p];
        }
        acts[a] = sum;
      }
      String adomain[] = ConfusionMatrix.show(acts, CM.vactual.domain());
      String pdomain[] = ConfusionMatrix.show(preds, CM.vpredict.domain());

      StringBuilder sb = new StringBuilder();
      sb.append("Act/Prd\t");
      for (String s : pdomain) if (s != null) sb.append(s).append('\t');
      sb.append("Error\n");

      long terr = 0;
      for (int a = 0; a < cm.length; a++) {
        if (adomain[a] == null) continue;
        sb.append(adomain[a]).append('\t');
        long correct = 0;
        for (int p = 0; p < pdomain.length; p++) {
          if (pdomain[p] == null) continue;
          if (adomain[a].equals(pdomain[p])) correct = cm[a][p];
          sb.append(cm[a][p]).append('\t');
        }
        long err = acts[a] - correct;
        terr += err; // Bump totals
        sb.append(String.format("%5.3f = %d / %d\n", (double) err / acts[a], err, acts[a]));
      }
      sb.append("Totals\t");
      for (int p = 0; p < pdomain.length; p++)
        if (pdomain[p] != null) sb.append(preds[p]).append("\t");
      sb.append(
          String.format(
              "%5.3f = %d / %d\n", (double) terr / CM.vactual.length(), terr, CM.vactual.length()));

      System.out.println(sb);

    } finally {
      UKV.remove(dest1); // Remove original hex frame key
      UKV.remove(fkey2);
      UKV.remove(dest2);
      if (gbm != null) {
        UKV.remove(gbm.dest()); // Remove the model
        UKV.remove(gbm.response._key);
        gbm.remove(); // Remove GBM Job
      }
      if (fr != null) fr.remove();
      if (fpreds != null) fpreds.remove();
    }
  }