Exemple #1
0
    public void testInsert() {
      people.insert(new String[] {"Holub", "Allen", "1"});
      people.insert(new String[] {"Flintstone", "Wilma", "2"});
      people.insert(
          new String[] {"addrId", "first", "last"}, new String[] {"2", "Fred", "Flintstone"});

      address.insert(new String[] {"1", "123 MyStreet", "Berkeley", "CA", "99999"});

      List l = new ArrayList();
      l.add("2");
      l.add("123 Quarry Ln.");
      l.add("Bedrock ");
      l.add("XX");
      l.add("12345");
      assert (address.insert(l) == 1);

      l.clear();
      l.add("3");
      l.add("Bogus");
      l.add("Bad");
      l.add("XX");
      l.add("12345");

      List c = new ArrayList();
      c.add("addrId");
      c.add("street");
      c.add("city");
      c.add("state");
      c.add("zip");
      assert (address.insert(c, l) == 1);

      System.out.println(people.toString());
      System.out.println(address.toString());

      try {
        people.insert(new String[] {"x"});
        throw new AssertionError("insert wrong number of fields test failed");
      } catch (Throwable t) {
        /* Failed correctly, do nothing */
      }

      try {
        people.insert(new String[] {"?"}, new String[] {"y"});
        throw new AssertionError("insert-nonexistent-field test failed");
      } catch (Exception t) {
        /* Failed correctly, do nothing */
      }
    }
Exemple #2
0
 @ManagedOperation(description = "Prints the contents of the send windows for all members")
 public String printSendWindowMessages() {
   StringBuilder ret = new StringBuilder(local_addr + ":\n");
   for (Map.Entry<Address, SenderEntry> entry : send_table.entrySet()) {
     Address addr = entry.getKey();
     Table<Message> buf = entry.getValue().sent_msgs;
     ret.append(addr).append(": ").append(buf.toString()).append('\n');
   }
   return ret.toString();
 }
  // Test if toString() returns a correct PrimaryKey field description from a Table
  public void testTableToStringWithPrimaryKey() {
    Table t = getTableWithStringPrimaryKey();
    t.addColumn(RealmFieldType.INTEGER, "intCol");
    t.addColumn(RealmFieldType.BOOLEAN, "boolCol");

    t.add("s1", 1, true);
    t.add("s2", 2, false);

    String expected =
        "The Table has 'colName' field as a PrimaryKey, and contains 3 columns: colName, intCol, boolCol. And 2 rows.";
    assertEquals(expected, t.toString());
  }
Exemple #4
0
  /** @see org.melati.poem.Database#getDisplayTables() */
  public void testGetDisplayTables() {
    final String expected =
        "user (from the data structure definition)"
            + "group (from the data structure definition)"
            + "capability (from the data structure definition)"
            + "groupMembership (from the data structure definition)"
            + "groupCapability (from the data structure definition)"
            + "tableInfo (from the data structure definition)"
            + "columnInfo (from the data structure definition)"
            + "tableCategory (from the data structure definition)"
            + "setting (from the data structure definition)";

    String outcome = "";
    for (Table<?> t : getDb().getDisplayTables()) {
      outcome += t.toString();
    }
    if (getDb().getDbms().canDropColumns()) {
      assertEquals(expected, outcome);
    }
  }
Exemple #5
0
  /**
   * Run the LAPIN algorithm
   *
   * @param input the input file path
   * @param minsupRel the minsup threshold as a percentage
   */
  private void lapin(String input, double minsupRel) throws IOException {

    if (DEBUG) {
      System.out.println(
          "=== First database scan to count number of sequences and support of single items ===");
    }

    // FIRST DATABASE SCAN: SCAN THE DATABASE TO COUNT
    //  - THE NUMBER OF SEQUENCES
    //  - THE SUPPORT OF EACH SINGLE ITEM
    // - THE LARGEST ITEM ID
    int sequenceCount = 0;
    int largestItemID = 0;
    // This map will store for each item (key) the first position where the item appears in each
    // sequence where it appears (value)
    Map<Integer, List<Position>> mapItemFirstOccurrences = new HashMap<Integer, List<Position>>();
    try {
      // Read the input file
      BufferedReader reader =
          new BufferedReader(new InputStreamReader(new FileInputStream(new File(input))));
      String thisLine;
      // for each sequence of the input fiel
      while ((thisLine = reader.readLine()) != null) {
        // we use a set to remember which item have been seen already
        Set<Integer> itemsAlreadySeen = new HashSet<Integer>();
        // to know the itemset number
        short itemsetID = 0;
        // for each token in this line
        for (String integer : thisLine.split(" ")) {
          // if it is the end of an itemset
          if ("-1".equals(integer)) {
            itemsetID++;
          } else if ("-2".equals(integer)) { // if it is the end of line
            // nothing to do here
          } else {
            // otherwise, it is an item
            Integer item = Integer.valueOf(integer);
            // if this item was not seen already in that sequence
            if (itemsAlreadySeen.contains(item) == false) {
              // Get the list of positions of that item
              List<Position> list = mapItemFirstOccurrences.get(item);
              // if that list is null, create a new list
              if (list == null) {
                list = new ArrayList<Position>();
                mapItemFirstOccurrences.put(item, list);
              }
              // Add the position of the item in that sequence to the list of first positions
              // of that item
              Position position = new Position(sequenceCount, itemsetID);
              list.add(position);
              // Remember that we have seen this item
              itemsAlreadySeen.add(item);
              // Check if the item is the largest item until now
              if (item > largestItemID) {
                largestItemID = item;
              }
            }
          }
        }
        // Increase the count of sequences from the input file
        sequenceCount++;
      }
      reader.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    ;

    // Initialize the list of tables
    tables = new Table[sequenceCount];

    // Calculate absolute minimum support  as a number of sequences
    minsup = (int) Math.ceil(minsupRel * sequenceCount);
    if (minsup == 0) {
      minsup = 1;
    }

    if (DEBUG) {
      System.out.println("Number of items: " + mapItemFirstOccurrences.size());
      System.out.println("Sequence count:  " + sequenceCount);
      System.out.println("Abs. minsup: " + minsup + " sequences");
      System.out.println("Rel. minsup: " + minsupRel + " %");

      System.out.println("=== Determining the frequent items ===");
    }

    //		// For each frequent item,  save it and add it to the list of frequent items
    List<Integer> frequentItems = new ArrayList<Integer>();
    for (Entry<Integer, List<Position>> entry : mapItemFirstOccurrences.entrySet()) {
      // Get the border created by this item
      List<Position> itemBorder = entry.getValue();
      // if the item is frequent
      if (itemBorder.size() >= minsup) {
        // Output the item and add it to the list of frequent items
        Integer item = entry.getKey();
        savePattern(item, itemBorder.size());
        frequentItems.add(item);
        if (DEBUG) {
          System.out.println(" Item " + item + " is frequent with support = " + itemBorder.size());
        }
      }
    }

    if (DEBUG) {
      System.out.println("=== Second database scan to construct item-is-exist tables ===");
    }
    // sort the frequent items (useful when generating 2-IE-sequences, later on).
    Collections.sort(frequentItems);

    // SECOND DATABASE SCAN:
    // Now we will read the database again to create the Item-is-exist-table
    // and SE-position-lists and count support of 2-IE-sequences
    matrixPairCount = new SparseTriangularMatrix(largestItemID + 1);

    // Initialise the IE position lists and SE position lists
    sePositionList = new SEPositionList[sequenceCount];
    iePositionList = new IEPositionList[sequenceCount];

    try {
      // Prepare to read the file
      BufferedReader reader =
          new BufferedReader(new InputStreamReader(new FileInputStream(new File(input))));
      String thisLine;
      // For each sequence in the file
      int currentSequenceID = 0;
      while ((thisLine = reader.readLine()) != null) {

        // (1) ------- PARSE THE SEQUENCE BACKWARD TO CREATE THE ITEM-IS-EXIST TABLE FOR THATS
        // SEQUENCE
        // AND COUNT THE SUPPORT OF 2-IE-Sequences

        // We will also use a structure to remember in which sequence we have seen each pair of
        // items
        // Note that in this structure, we will add +1 to the sid because by default the matrix is
        // filled with 0
        // and we don't want to think that the first sequence was already seen for all pairs.
        AbstractTriangularMatrix matrixPairLastSeenInSID =
            new SparseTriangularMatrix(largestItemID + 1);

        // We count the number of positions (number of itemsets).
        // To do that we count the number of "-" symbols in the file.
        // We need to subtract 1 because the end of line "-2" contains "-".
        int positionCount = -1;
        for (char caracter : thisLine.toCharArray()) {
          if (caracter == '-') {
            positionCount++;
          }
        }

        // Now we will scan the sequence again.
        // This time we will remember which item were seen already
        Set<Integer> itemsAlreadySeen = new HashSet<Integer>();

        // During this scan, we will create the table for this sequence
        Table table = new Table();

        // To do that, we first create an initial position vector for that table
        BitSet currentBitset = new BitSet(mapItemFirstOccurrences.size()); // OK ?

        // This variable will be used to remember if a new item appeared in the current itemset
        boolean seenNewItem = false;

        // We will scan the sequence backward, starting from the end because
        // we should not create a bit vector for all positions but for only
        // the positions that are different from the previous one.
        String[] tokens = thisLine.split(" ");
        // This is the number of itemsets
        int currentPosition = positionCount;
        // to keep the current itemset in memory
        List<Integer> currentItemset = new ArrayList<Integer>();
        // For each token in that sequence
        for (int i = tokens.length - 1; i >= 0; i--) {
          // get the token
          String token = tokens[i];

          // if we reached the end of an itemset
          if ("-1".equals(token)) {
            // update the triangular matrix for counting 2-IE-sequences
            // by comparing each pairs of items in the current itemset
            for (int k = 0; k < currentItemset.size(); k++) {
              Integer item1 = currentItemset.get(k);
              for (int m = k + 1; m < currentItemset.size(); m++) {
                Integer item2 = currentItemset.get(m);

                // if that pair is frequent
                int sid = matrixPairLastSeenInSID.getSupportForItems(item1, item2);
                // and if we have not seen this sequence yet
                if (sid != currentSequenceID + 1) {
                  // increment support count of this pair
                  matrixPairCount.incrementCount(item1, item2);
                  // remember that we have seen this pair so that we don't count it again
                  matrixPairLastSeenInSID.setSupport(item1, item2, currentSequenceID + 1);
                }
              }
            }
            currentItemset.clear();
            // Decrease the current index of the position (itemset) in the sequence
            currentPosition--;
            // if the bit vector has changed since previous position, then
            // we need to add a new bit vector to the table
            if (seenNewItem) {
              // create the position vector and add it to the item-is-exist table
              PositionVector vector =
                  new PositionVector(currentPosition, (BitSet) currentBitset.clone());
              table.add(vector);
            }

          } else if ("-2".equals(token)) { // if end of sequence, nothing to do

          } else {
            // otherwise, it is an item
            Integer item = Integer.valueOf(token);
            if (mapItemFirstOccurrences.get(item).size() >= minsup) { // only for frequent items
              // if first time that we see this item
              if (itemsAlreadySeen.contains(item) == false) {
                // remember that we have seen a new item
                seenNewItem = true;
                // remember that we have seen this item
                itemsAlreadySeen.add(item);
                // add this item to the current bit vector
                currentBitset.set(item);
              }
              // add this item to the current itemset
              currentItemset.add(item);
            }
          }
        }

        // Lastly,
        // update the triangular matrix for counting 2-IE-sequences one more time
        // for the case where the pair is in first position of the sequence
        // by considering each pair of items in the last itemset.
        // This is done like it was done above, so I will not comment this part of the code again.
        for (int k = 0; k < currentItemset.size(); k++) {
          Integer item1 = currentItemset.get(k);
          for (int m = k + 1; m < currentItemset.size(); m++) {
            Integer item2 = currentItemset.get(m);
            // if th
            int sid = matrixPairLastSeenInSID.getSupportForItems(item1, item2);
            if (sid != currentSequenceID + 1) {
              matrixPairCount.incrementCount(item1, item2);
              matrixPairLastSeenInSID.setSupport(item1, item2, currentSequenceID + 1);
            }
          }
        }

        // If a new item was seen
        // Add an extra row to the item-is-exist table that will be called -1  with all items in
        // this sequence
        if (seenNewItem) {
          PositionVector vector = new PositionVector(-1, (BitSet) currentBitset.clone());
          table.add(vector);
        }
        //
        //
        //				// Initialize the IE lists and SE lists for that sequence
        // which will be filled with the next database scan.
        sePositionList[currentSequenceID] = new SEPositionList(itemsAlreadySeen);
        iePositionList[currentSequenceID] = new IEPositionList();

        if (DEBUG) {
          System.out.println("Table for sequence " + currentSequenceID + " : " + thisLine);
          System.out.println(table.toString());
        }
        // put the current table in the array of item-is-exist-tables
        tables[currentSequenceID] = table;
        // we will process the next sequence id
        currentSequenceID++;
      }
      reader.close();
    } catch (Exception e) {
      e.printStackTrace();
    }

    // THIRD SCAN TO
    //  PARSE THE SEQUENCE FORWARD TO CREATE THE SE-POSITION LIST OF THAT SEQUENCE
    // AND IEPositionList for frequent 2-IE-SEQUENCES
    try {
      BufferedReader reader =
          new BufferedReader(new InputStreamReader(new FileInputStream(new File(input))));
      String thisLine;
      // For each sequence
      int currentSequenceID = 0;
      while ((thisLine = reader.readLine()) != null) {

        // We will scan the sequence backward, starting from the end.
        String[] tokens = thisLine.split(" ");
        // to keep the current itemset in memory
        List<Integer> currentItemset = new ArrayList<Integer>();

        // this variable will be used to remember which itemset we are visiting
        short itemsetID = 0;
        // empty the object to track the current itemset (if it was used for the previous sequence)
        currentItemset.clear();

        // for each token of the current sequence
        for (int i = 0; i < tokens.length; i++) {
          String token = tokens[i];

          // if we reached the end of an itemset
          if ("-1".equals(token)) {
            // if the current itemset contains more than one item
            if (currentItemset.size() > 1) {
              // update the position list for 2-IE-sequences
              for (int k = 0; k < currentItemset.size(); k++) {
                Integer item1 = currentItemset.get(k);
                for (int m = k + 1; m < currentItemset.size(); m++) {
                  Integer item2 = currentItemset.get(m);
                  // if the pair is frequent
                  int support = matrixPairCount.getSupportForItems(item1, item2);
                  if (support >= minsup) {
                    iePositionList[currentSequenceID].register(item1, item2, itemsetID);
                  }
                }
              }
            }
            // increase itemsetID
            itemsetID++;
            // clear itemset
            currentItemset.clear();
          } else if ("-2".equals(token)) {
            // if the end of a sequence, nothing special to do

          } else {
            // otherwise, the current token is an item
            Integer item = Integer.valueOf(token);
            // if the item is frequent
            if (mapItemFirstOccurrences.get(item).size() >= minsup) {
              // we add the current position to the item SE-position list
              sePositionList[currentSequenceID].register(item, itemsetID);
              // we add the item to the current itemset
              currentItemset.add(item);
            }
          }
        }

        if (DEBUG) {
          System.out.println("SE Position list for sequence " + currentSequenceID);
          System.out.println(sePositionList[currentSequenceID]);
          System.out.println("IE Position list for sequence " + currentSequenceID);
          System.out.println(iePositionList[currentSequenceID]);
        }

        iePositionList[currentSequenceID].sort(); // sort the IE-position list
        // update the sequence id for the next sequence
        currentSequenceID++;
      }
      reader.close();
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (DEBUG) {
      System.out.println("=== Starting sequential pattern generation ===");
    }

    // For each frequent item,  call the recursive method to explore larger patterns
    for (int i = 0; i < frequentItems.size(); i++) {
      // Get the item
      int item1 = frequentItems.get(i);
      // Get the border for that item
      List<Position> item1Border = mapItemFirstOccurrences.get(item1);
      if (DEBUG) {
        System.out.println("=== Considering item " + item1);
        System.out.println("  Border of " + item1);
        for (Position pos : item1Border) {
          System.out.println("    seq: " + pos.sid + "    itemset: " + pos.position);
        }
      }
      // if the border contains at least minsup sequence (if the item is frequent)
      if (item1Border.size() >= minsup) {
        // Create an object prefix to represent the sequential pattern containing the item
        Prefix prefix = new Prefix();
        List<Integer> itemset = new ArrayList<Integer>(1);
        itemset.add(item1);
        prefix.itemsets.add(itemset);
        // make a recursive call to find s-extensions of this prefix
        genPatterns(
            prefix,
            item1Border,
            frequentItems,
            frequentItems,
            item1,
            true); // true, to disallow I-extension because we explore 2-IE sequences separately
      }

      // For each frequent 2-IE sequences stating with item1, we will explore 2-IE sequences
      // by considering each frequent item larger than item1
      for (int k = i + 1; k < frequentItems.size(); k++) {
        // We consider item2
        int item2 = frequentItems.get(k);
        // Get the support of item1, item2
        int support = matrixPairCount.getSupportForItems(item1, item2);

        // if the pair {item1, item2} is frequent
        if (support >= minsup) {
          // get the list of position of item2
          List<Position> item2Border = mapItemFirstOccurrences.get(item2);
          // Create the border by using the 2-IE position list
          List<Position> ie12Border = new ArrayList<Position>();

          // We will loop over the border of item1 or item2 (the smallest one)
          List<Position> borderToUse;
          if (item2Border.size() < item1Border.size()) {
            borderToUse = item2Border;
          } else {
            borderToUse = item1Border;
          }
          // For each sequence of the border that we consider
          for (Position sequenceToUse : borderToUse) {
            // Get the sequence id
            int sid = sequenceToUse.sid;
            // For this sequence, we will get the position list of each item
            List<Short> listPosition1 = sePositionList[sid].getListForItem(item1);
            List<Short> listPosition2 = sePositionList[sid].getListForItem(item2);
            // if one of them is null, that means that both item1 and item2 do not appear in that
            // sequence
            // so we continue to the next sequence
            if (listPosition1 == null || listPosition2 == null) {
              continue;
            }
            // otherwise
            // find the first common position of item1 and item2 in the sequence
            int index1 = 0;
            int index2 = 0;

            // we do that by the following while loop
            while (index1 < listPosition1.size() && index2 < listPosition2.size()) {
              short position1 = listPosition1.get(index1);
              short position2 = listPosition2.get(index2);
              if (position1 < position2) {
                index1++;
              } else if (position1 > position2) {
                index2++;
              } else {
                // we have found the position, so we add it to the new border and
                // then stop because we do not want to add more than one position for
                // the same sequence in the new border
                ie12Border.add(new Position(sid, position1));
                break;
              }
            }
          }
          if (DEBUG) {
            System.out.println(
                "=== Considering the 2-IE sequence {"
                    + item1
                    + ","
                    + item2
                    + "}  with support "
                    + support);
            System.out.println("  Border of {" + item1 + "," + item2 + "}");
            for (Position pos : ie12Border) {
              System.out.println("    seq: " + pos.sid + "    itemset: " + pos.position);
            }
          }

          // finally, we create the prefix for the pattern  {item1, item2}
          Prefix prefix = new Prefix();
          List<Integer> itemset = new ArrayList<Integer>(2);
          itemset.add(item1);
          itemset.add(item2);
          prefix.itemsets.add(itemset);
          // save the pattern
          savePattern(prefix, support);
          // perform recursive call to extend that pattern
          genPatterns(
              prefix,
              ie12Border,
              frequentItems,
              frequentItems,
              item2,
              false); // false, to allow I-extension
        }
      }
    }

    // Record the maximum memory usage
    MemoryLogger.getInstance().checkMemory();
    writer.close();
  }
  public String getTable(Table tbl) {
    if (dbms == DBMS.H2) return tbl.getName();

    return plugin.getConfig().getString("mysql.tables." + tbl.toString().toLowerCase());
  }
Exemple #7
0
    public void testUndo() {
      // Verify that commit works properly
      people.begin();
      System.out.println("begin/insert into people (Solo, Han, 5)");

      people.insert(new String[] {"Solo", "Han", "5"});
      System.out.println(people.toString());

      people.begin();
      System.out.println("begin/insert into people (Lea, Princess, 6)");

      people.insert(new String[] {"Lea", "Princess", "6"});
      System.out.println(people.toString());

      System.out.println("commit(THIS_LEVEL)\n" + "rollback(Table.THIS_LEVEL)\n");
      people.commit(Table.THIS_LEVEL);
      people.rollback(Table.THIS_LEVEL);
      System.out.println(people.toString());

      // Now test that nested transactions work correctly.

      System.out.println(people.toString());

      System.out.println("begin/insert into people (Vader,Darth, 4)");
      people.begin();
      people.insert(new String[] {"Vader", "Darth", "4"});
      System.out.println(people.toString());

      System.out.println("begin/update people set last=Skywalker where last=Vader");

      people.begin();
      people.update(
          new Selector() {
            public boolean approve(Cursor[] tables) {
              return tables[0].column("last").equals("Vader");
            }

            public void modify(Cursor current) {
              current.update("last", "Skywalker");
            }
          });
      System.out.println(people.toString());

      System.out.println("delete from people where last=Skywalker");
      people.delete(
          new Selector.Adapter() {
            @Override
            public boolean approve(Cursor[] tables) {
              return tables[0].column("last").equals("Skywalker");
            }
          });
      System.out.println(people.toString());

      System.out.println("rollback(Table.THIS_LEVEL) the delete and update");
      people.rollback(Table.THIS_LEVEL);
      System.out.println(people.toString());

      System.out.println("rollback(Table.THIS_LEVEL) insert");
      people.rollback(Table.THIS_LEVEL);
      System.out.println(people.toString());
    }
Exemple #8
0
    public void testJoin() {
      // First test a two-way join

      System.out.println(
          "\nSELECT first,last,street,city,state,zip"
              + " FROM people, address"
              + " WHERE people.addrId = address.addrId");

      // Collection version chains to String[] version,
      // so this code tests both:
      List columns = new ArrayList();
      columns.add("first");
      columns.add("last");
      columns.add("street");
      columns.add("city");
      columns.add("state");
      columns.add("zip");

      List tables = new ArrayList();
      tables.add(address);
      // WHERE people.addrID = address.addrID
      Table result =
          people.select(
              new Selector.Adapter() {
                @Override
                public boolean approve(Cursor[] tables) {
                  String a = tables[0].column("addrId");
                  String b = tables[1].column("addrId");
                  return StringUtil.equals(a, b);
                  // return tables[0].column("addrId").equals(tables[1].column("addrId"));
                }
              },
              columns,
              tables);

      print(result);
      System.out.println("");

      // Now test a three-way join
      //
      System.out.println(
          "\nSELECT first,last,street,city,state,zip,text"
              + " FROM people, address, third"
              + " WHERE (people.addrId = address.addrId)"
              + " AND (people.addrId = third.addrId)");

      Table third = TableFactory.create("third", new String[] {"addrId", "text"});
      third.insert(new String[] {"1", "addrId=1"});
      third.insert(new String[] {"2", "addrId=2"});

      result =
          people.select(
              new Selector.Adapter() {
                @Override
                public boolean approve(Cursor[] tables) {
                  return (StringUtil.equals(tables[0].column("addrId"), tables[1].column("addrId"))
                      && StringUtil.equals(tables[0].column("addrId"), tables[2].column("addrId")));
                }
              },
              new String[] {"last", "first", "state", "text"},
              new Table[] {address, third});

      System.out.println(result.toString() + "\n");
    }