Exemplo n.º 1
0
  void mediate(int subtasks) throws InterruptedException, ExecutionException {

    ExecutorService executorService = Executors.newFixedThreadPool(subtasks * 2);

    List<Callable<StringPair>> tasks = Lists.newArrayList();
    Broker<String> broker = new Broker<String>();

    for (int subtask = 0; subtask < subtasks; subtask++) {
      tasks.add(new IterationHead(broker, subtask, "value" + subtask));
      tasks.add(new IterationTail(broker, subtask));
    }

    Collections.shuffle(tasks);

    int numSuccessfulHandovers = 0;
    for (Future<StringPair> future : executorService.invokeAll(tasks)) {
      StringPair stringPair = future.get();
      if (stringPair != null) {
        assertEquals("value" + stringPair.getFirst(), stringPair.getSecond());
        numSuccessfulHandovers++;
      }
    }

    assertEquals(subtasks, numSuccessfulHandovers);
  }
Exemplo n.º 2
0
 PyObject extractStrings(List s) {
   boolean ustring = false;
   boolean bstring = false;
   Token last = null;
   StringBuffer sb = new StringBuffer();
   Iterator iter = s.iterator();
   while (iter.hasNext()) {
     last = (Token) iter.next();
     StringPair sp = extractString(last);
     if (sp.isUnicode()) {
       ustring = true;
     }
     if (sp.isBytes()) {
       bstring = true;
     }
     sb.append(sp.getString());
   }
   if (bstring) {
     return new PyBytes(sb.toString());
   }
   return new PyString(sb.toString());
 }
Exemplo n.º 3
0
 public void map(LongWritable key, Text value, Context context)
     throws IOException, InterruptedException {
   String filter = value.toString().replaceAll(" ", "");
   String[] line = filter.split("\t");
   if (line.length == 7) {
     try {
       delay.set(line[4]);
       tempDelay = Integer.parseInt(line[4]);
     } catch (Exception e) {
     }
     // if(tempDelay>0)// && !line[3].isEmpty() )
     for (int i = 0; i < preferences.length; i++) {
       if (line[3].toUpperCase().equals(preferences[i])) {
         gameName.set(line[3].toUpperCase());
         pair.set(gameName, delay);
         context.write(new IntWritable(1), pair);
         break;
       }
     }
     // }
   }
 }
Exemplo n.º 4
0
 @Override
 public int getPartition(StringPair pair, Text value, int numReduceTask) {
   return (pair.getCount().hashCode() % numReduceTask);
 }
Exemplo n.º 5
0
  private void writeToFile(String outName)
      throws FileNotFoundException, UnsupportedEncodingException {
    String minSupport = itemsetsSupport.get(findSmallerValue(itemsetsSupport));
    String minConfidence = findMinConfidence();
    PrintWriter writer = new PrintWriter(outName, "UTF-8");
    // Writting Info
    writer.println(
        "<PMML xmlns=\"http://www.dmg.org/PMML-4_1\" version=\"4.1\">\n"
            + "  <Header copyright=\"www.dmg.org\" description=\"Undefined\"/>\n"
            + "  <AssociationModel functionName=\"associationRules\" numberOfTransactions=\"Undefined\" numberOfItems=\""
            + items.size()
            + "\" minimumSupport=\""
            + minSupport
            + "\" minimumConfidence=\""
            + minConfidence
            + "\" numberOfItemsets=\""
            + itemsets.size()
            + "\" numberOfRules=\""
            + rules.size()
            + "\">\n"
            + "");

    writer.println("\n\n<!-- Items -->");
    // Write items
    for (Map.Entry<String, Integer> entry : items.entrySet()) {
      String key = entry.getKey();
      Integer id = entry.getValue();
      writer.println("<Item id=\"" + id + "\" value=\"" + key + "\"/>");
    }

    writer.println("\n\n<!-- Itemsets -->");
    // Write itemsets
    for (Map.Entry<String, ArrayList<Integer>> entry : itemsets.entrySet()) {
      String key = entry.getKey();
      ArrayList<Integer> ids = entry.getValue();
      String support = itemsetsSupport.get(key);
      int n = ids.size() - 1;
      // First element is the itemset id
      writer.println(
          "<Itemset id=\""
              + ids.get(0)
              + "\" support=\""
              + support
              + "\" numberOfItems=\""
              + n
              + "\">");
      for (int i = 1; i < ids.size(); i++) {
        Integer id = ids.get(i);
        writer.println("<ItemRef itemRef=\"" + id + "\"/>");
      }
      writer.println("</Itemset>");
    }

    writer.println("\n\n<!-- Assotiation Rules -->");
    // Write rules
    for (int i = 0; i < rules.size(); i++) {
      String measures = "";
      for (StringPair measure : rulesMeasures.get(i)) {
        measures += measure.getKey() + "=" + '"' + measure.getValue() + "\" ";
      }

      writer.println(
          "<AssociationRule id=\""
              + i
              + "\" "
              + measures
              + " antecedent=\""
              + rules.get(i).get(0)
              + "\" consequent=\""
              + rules.get(i).get(1)
              + "\"/>");
    }

    // Finish file
    writer.println("</AssociationModel>\n" + "</PMML>");

    writer.flush();
    writer.close();
  }
 @Override
 public int compare(StringPair first, StringPair second) {
   return first.getKey().compareTo(second.getKey());
 }
 @Override
 public boolean equalToReference(StringPair candidate) {
   return this.reference.equals(candidate.getKey());
 }
 @Override
 public void setReference(StringPair toCompare) {
   this.reference = toCompare.getKey();
 }
 @Override
 public int hash(StringPair record) {
   return record.getKey().hashCode();
 }