/** {@inheritDoc} */
  @Override
  public void txEnd(GridCacheTx tx, boolean commit) throws GridException {
    init();

    Session ses = tx.removeMeta(ATTR_SES);

    if (ses != null) {
      Transaction hTx = ses.getTransaction();

      if (hTx != null) {
        try {
          if (commit) {
            ses.flush();

            hTx.commit();
          } else hTx.rollback();

          if (log.isDebugEnabled())
            log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']');
        } catch (HibernateException e) {
          throw new GridException(
              "Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e);
        } finally {
          ses.close();
        }
      }
    }
  }
  // Determine F(k+1) by support counting on (C(K+1), T) and retaining itemsets from C(k+1) with
  // support at least minsup
  private static List<Itemset> determineFrequentItemsets(
      List<Itemset> candicates, List<Transaction> transactions, double minsup) {
    if (candicates.isEmpty()) {
      return null;
    }

    HashTree hashTree = new HashTree(candicates, candicates.get(0).getNumOfItems());
    HashMap<Itemset, Integer> frequentCount = new HashMap<>();
    for (Itemset itemset : candicates) {
      frequentCount.put(itemset, 0);
    }

    for (Transaction transaction : transactions) {
      Set<Itemset> candidatesInTranscation = hashTree.candidatesInTransaction(transaction);
      if (candidatesInTranscation == null) {
        continue;
      }
      for (Itemset itemset : candidatesInTranscation) {
        if (transaction.containItemset(itemset)) {
          frequentCount.put(itemset, frequentCount.get(itemset) + 1);
        }
      }
    }

    List<Itemset> result = new ArrayList<>();
    for (Itemset itemset : candicates) {
      if ((double) (frequentCount.get(itemset)) / transactions.size() >= minsup) {
        result.add(itemset);
      }
    }
    return result;
  }
Esempio n. 3
0
 /** Returns a {@link Wallet.SendRequest} suitable for broadcasting to the network. */
 public Wallet.SendRequest getSendRequest() {
   Transaction tx = new Transaction(params);
   for (Protos.Output output : paymentDetails.getOutputsList())
     tx.addOutput(
         new TransactionOutput(
             params, tx, Coin.valueOf(output.getAmount()), output.getScript().toByteArray()));
   return Wallet.SendRequest.forTx(tx).fromPaymentDetails(paymentDetails);
 }
Esempio n. 4
0
 private void writeFitID(final Transaction transaction) {
   // write out the banks transaction id if previously imported
   if (transaction.getFitid() != null && !transaction.getFitid().isEmpty()) {
     indentedWriter.println(wrap(FITID, transaction.getFitid()), indentLevel);
   } else {
     indentedWriter.println(wrap(FITID, transaction.getUuid()), indentLevel);
   }
 }
  /**
   * Rolls back hibernate session.
   *
   * @param ses Hibernate session.
   * @param tx Cache ongoing transaction.
   */
  private void rollback(SharedSessionContract ses, GridCacheTx tx) {
    // Rollback only if there is no cache transaction,
    // otherwise txEnd() will do all required work.
    if (tx == null) {
      Transaction hTx = ses.getTransaction();

      if (hTx != null && hTx.isActive()) hTx.rollback();
    }
  }
    @Test
    public void nemesisTransactionsAreVerifiable() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Assert:
      for (final Transaction transaction : block.getTransactions()) {
        Assert.assertThat(transaction.verify(), IsEqual.equalTo(true));
      }
    }
Esempio n. 7
0
  // Test the change plan when trying to change a student plan
  @Test
  public void transferpairtest() {
    Backend b = new Backend();
    String[] args = new String[1];
    args[0] = "MasterBankAccounts.txt";

    tlist.clear();

    Transaction t = new Transaction();
    t.setCode("10");
    t.setMisc("A");
    t.setName("");

    Transaction t2 = new Transaction();
    t2.setCode("02");
    t2.setNum("00005");

    Transaction t3 = new Transaction();
    t3.setCode("00");

    tlist.add(t);
    tlist.add(t2);
    tlist.add(t3);

    b.load(args);
    b.setTransactions(tlist);
    b.handletransactions();
    assertEquals("Transfer transactions must come in pairs\n", outContent.toString());
  }
  /**
   * Ends hibernate session.
   *
   * @param ses Hibernate session.
   * @param tx Cache ongoing transaction.
   */
  private void end(Session ses, GridCacheTx tx) {
    // Commit only if there is no cache transaction,
    // otherwise txEnd() will do all required work.
    if (tx == null) {
      Transaction hTx = ses.getTransaction();

      if (hTx != null && hTx.isActive()) hTx.commit();

      ses.close();
    }
  }
  // The Apriori Algorithm
  private static List<Itemset> apriori(List<Transaction> transactions, double minsup) {

    if (transactions.isEmpty()) {
      return null;
    }

    // Generate frequent 1-itemset
    List<Integer> occurancesOfItems = new ArrayList<>();
    for (int i = 0; i != transactions.get(0).getNumOfTotalItems(); ++i) {
      occurancesOfItems.add(0);
    }

    for (Transaction transaction : transactions) {
      for (int index : transaction.items()) {
        occurancesOfItems.set(index, occurancesOfItems.get(index) + 1);
      }
    }

    int numOfTransactions = transactions.size();
    List<Itemset> frequentOneItemsets = new ArrayList<>();
    for (int i = 0; i != occurancesOfItems.size(); ++i) {
      if ((double) occurancesOfItems.get(i) / numOfTransactions >= minsup) {
        Itemset itemset = new Itemset(i);
        frequentOneItemsets.add(itemset);
      }
    }

    // A list of itemsets to store the frequent k-itemsets, k = 1, 2, ...
    List<List<Itemset>> listOfFrequentItemsets = new ArrayList<List<Itemset>>();
    listOfFrequentItemsets.add(
        new ArrayList<Itemset>()); // case: k = 0, we fill it with an empty itemsets
    listOfFrequentItemsets.add(frequentOneItemsets); // case: k = 1

    List<Itemset> prunedCandidates;
    List<Itemset> frequentItemsets;
    int k = 1;
    while ((frequentItemsets = listOfFrequentItemsets.get(k)) != null) {
      List<Itemset> newCandidates = generateCandidates(frequentItemsets);
      prunedCandidates = prune(newCandidates, frequentItemsets);
      List<Itemset> candidates = determineFrequentItemsets(prunedCandidates, transactions, minsup);
      listOfFrequentItemsets.add(candidates);
      k++;
    }

    // Union the frequent itemsets as result
    List<Itemset> result = new ArrayList<>();
    for (List<Itemset> list : listOfFrequentItemsets) {
      if (list != null) {
        result.addAll(list);
      }
    }
    return result;
  }
Esempio n. 10
0
 private static float generateAveragePrice(int itemID) {
   float price = 0;
   List<Transaction> prices =
       ChestShop.getDB()
           .find(Transaction.class)
           .where()
           .eq("itemID", itemID)
           .eq("buy", true)
           .findList();
   for (Transaction t : prices) price += t.getAveragePricePerItem();
   float toReturn = price / prices.size();
   return (!Float.isNaN(toReturn) ? toReturn : 0);
 }
    @Test
    public void nemesisTransactionsHaveCorrectFees() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Assert:
      for (final Transaction transaction : block.getTransactions()) {
        final Amount expectedFee =
            TransactionTypes.TRANSFER == transaction.getType()
                ? Amount.ZERO
                : NemGlobals.getTransactionFeeCalculator().calculateMinimumFee(transaction);
        Assert.assertThat(transaction.getFee(), IsEqual.equalTo(expectedFee));
      }
    }
Esempio n. 12
0
 /**
  * Generates a Payment message based on the information in the PaymentRequest. Provide
  * transactions built by the wallet. If the PaymentRequest did not specify a payment_url, returns
  * null.
  *
  * @param txns list of transactions to be included with the Payment message.
  * @param refundAddr will be used by the merchant to send money back if there was a problem.
  * @param memo is a message to include in the payment message sent to the merchant.
  */
 @Nullable
 public Protos.Payment getPayment(
     List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
     throws IOException, PaymentProtocolException.InvalidNetwork {
   if (paymentDetails.hasPaymentUrl()) {
     for (Transaction tx : txns)
       if (!tx.getParams().equals(params))
         throw new PaymentProtocolException.InvalidNetwork(params.getPaymentProtocolId());
     return PaymentProtocol.createPaymentMessage(
         txns, totalValue, refundAddr, memo, getMerchantData());
   } else {
     return null;
   }
 }
Esempio n. 13
0
  public final void handle(Transaction transaction) throws Exception {
    SessionEnvironment sessionEnvironment = transaction.getSessionEnvironment();

    if (sessionEnvironment.getUser() != null) {

      Logs.log(
          Logs.SECURITY_WARNING_CAT,
          "User tries to login while logged.",
          Logs.USER_ID_TAG,
          Integer.toString(sessionEnvironment.getUser().userId));

      sessionEnvironment.setUser(null);
    }

    transaction.sendHttpRedirection(SSLLogin);
  }
Esempio n. 14
0
  public static void main(String[] args) {

    // Minimum support value
    double minsup = 0.144;
    //        double minsup = 0.04;
    // Load data, you should set your own data file location here
    File data =
        new File("/Users/walker/Desktop/Courses/DataMining/Assignments/2/assignment2-data.txt");
    ArrayList<Transaction> transactions = new ArrayList<>();
    ArrayList<String> items = new ArrayList<>();
    try (BufferedReader br =
        new BufferedReader(new InputStreamReader(new FileInputStream(data), "utf-8"))) {
      String line;
      // Load item's name
      if ((line = br.readLine()) != null) {
        Collections.addAll(items, line.split("\\s+"));
      }

      // Load transactions
      Transaction transaction;
      while ((line = br.readLine()) != null) {
        transaction = new Transaction(line);
        transactions.add(transaction);
      }

    } catch (IOException e) {
      e.printStackTrace();
    }

    List<Itemset> frequentItemset = apriori(transactions, minsup);
    HashMap<Itemset, Integer> frequentCount = new HashMap<>();
    for (Itemset itemset : frequentItemset) {
      frequentCount.put(itemset, 0);
    }
    for (Transaction transaction : transactions) {
      for (Itemset itemset : frequentItemset) {
        if (transaction.containItemset(itemset)) {
          frequentCount.put(itemset, frequentCount.get(itemset) + 1);
        }
      }
    }

    for (Itemset itemset : frequentItemset) {
      System.out.print(itemset);
      System.out.println((double) frequentCount.get(itemset) / transactions.size());
    }
  }
Esempio n. 15
0
  /*
   * create a container that all threads can use
   */
  private long commonContainer() throws T_Fail {

    ContextManager cm1 = contextService.newContextManager();
    contextService.setCurrentContextManager(cm1);
    long cid;

    try {
      Transaction t = t_util.t_startTransaction();
      cid = t_util.t_addContainer(t, 0);
      t_util.t_commit(t);
      t.close();
    } catch (StandardException se) {

      cm1.cleanupOnError(se);
      throw T_Fail.exceptionFail(se);
    } finally {
      contextService.resetCurrentContextManager(cm1);
    }
    return cid;
  }
  /** {@inheritDoc} */
  @Override
  public void onSessionEnd(CacheStoreSession ses, boolean commit) {
    Session hibSes = ses.attach(null);

    if (hibSes != null) {
      try {
        Transaction tx = hibSes.getTransaction();

        if (commit) {
          hibSes.flush();

          if (tx.isActive()) tx.commit();
        } else if (tx.isActive()) tx.rollback();
      } catch (HibernateException e) {
        throw new CacheWriterException(
            "Failed to end store session [tx=" + ses.transaction() + ']', e);
      } finally {
        hibSes.close();
      }
    }
  }
Esempio n. 17
0
  /**
   * Retrieves and returns that ProtocolProviderService that this transaction belongs to, or
   * <tt>null</tt> if we couldn't associate it with a provider based on neither the request nor the
   * transaction itself.
   *
   * @param transaction the transaction that we'd like to determine a provider for.
   * @return a reference to the <tt>ProtocolProviderServiceSipImpl</tt> that <tt>transaction</tt>
   *     was associated with or <tt>null</tt> if we couldn't determine which one it is.
   */
  private ProtocolProviderServiceSipImpl getServiceData(Transaction transaction) {
    ProtocolProviderServiceSipImpl service =
        (ProtocolProviderServiceSipImpl)
            SipApplicationData.getApplicationData(
                transaction.getRequest(), SipApplicationData.KEY_SERVICE);

    if (service != null) {
      if (logger.isTraceEnabled()) logger.trace("service was found in request data");
      return service;
    }

    service =
        (ProtocolProviderServiceSipImpl)
            SipApplicationData.getApplicationData(
                transaction.getDialog(), SipApplicationData.KEY_SERVICE);
    if (service != null) {
      if (logger.isTraceEnabled()) logger.trace("service was found in dialog data");
    }

    return service;
  }
Esempio n. 18
0
 /**
  * Generates a Payment message based on the information in the PaymentRequest. Provide
  * transactions built by the wallet. If the PaymentRequest did not specify a payment_url, returns
  * null.
  *
  * @param txns list of transactions to be included with the Payment message.
  * @param refundAddr will be used by the merchant to send money back if there was a problem.
  * @param memo is a message to include in the payment message sent to the merchant.
  */
 public @Nullable Protos.Payment getPayment(
     List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
     throws IOException {
   if (!paymentDetails.hasPaymentUrl()) return null;
   Protos.Payment.Builder payment = Protos.Payment.newBuilder();
   if (paymentDetails.hasMerchantData()) payment.setMerchantData(paymentDetails.getMerchantData());
   if (refundAddr != null) {
     Protos.Output.Builder refundOutput = Protos.Output.newBuilder();
     refundOutput.setAmount(totalValue.longValue());
     refundOutput.setScript(
         ByteString.copyFrom(ScriptBuilder.createOutputScript(refundAddr).getProgram()));
     payment.addRefundTo(refundOutput);
   }
   if (memo != null) {
     payment.setMemo(memo);
   }
   for (Transaction txn : txns) {
     txn.verify();
     ByteArrayOutputStream o = new ByteArrayOutputStream();
     txn.bitcoinSerialize(o);
     payment.addTransactions(ByteString.copyFrom(o.toByteArray()));
   }
   return payment.build();
 }
Esempio n. 19
0
  private static double generateItemTotal(int itemID, boolean bought, boolean sold) {
    double amount = 0;
    List<Transaction> list;
    if (bought)
      list =
          ChestShop.getDB()
              .find(Transaction.class)
              .where()
              .eq("buy", 1)
              .eq("itemID", itemID)
              .findList();
    else if (sold)
      list =
          ChestShop.getDB()
              .find(Transaction.class)
              .where()
              .eq("buy", 0)
              .eq("itemID", itemID)
              .findList();
    else list = ChestShop.getDB().find(Transaction.class).where().eq("itemID", itemID).findList();

    for (Transaction t : list) amount += t.getAmount();
    return amount;
  }
Esempio n. 20
0
  /**
   * Writes one bank transaction
   *
   * @param transaction <code>Transaction</code> to write
   */
  private void writeBankTransaction(final Transaction transaction) {
    indentedWriter.println(wrapOpen(STMTTRN), indentLevel++);
    indentedWriter.println(
        wrapOpen(TRNTYPE)
            + (transaction.getAmount(account).compareTo(BigDecimal.ZERO) == 1 ? CREDIT : DEBIT),
        indentLevel);

    indentedWriter.println(wrapOpen(DTPOSTED) + encodeDate(transaction.getDate()), indentLevel);
    indentedWriter.println(
        wrapOpen(TRNAMT) + transaction.getAmount(account).toPlainString(), indentLevel);
    indentedWriter.println(wrapOpen(REFNUM) + transaction.getUuid(), indentLevel);
    indentedWriter.println(wrapOpen(NAME) + transaction.getPayee(), indentLevel);
    indentedWriter.println(wrapOpen(MEMO) + transaction.getMemo(), indentLevel);

    // write the check number if applicable
    if (account.getAccountType() == AccountType.CHECKING && !transaction.getNumber().isEmpty()) {
      indentedWriter.println(wrapOpen(CHECKNUM) + transaction.getNumber(), indentLevel);
    }

    // write out the banks transaction id if previously imported
    writeFitID(transaction);

    indentedWriter.println(wrapClose(STMTTRN), --indentLevel);
  }
Esempio n. 21
0
  /*
   * create a stream container load with rowCount number of rows.
   * fetch it all back, and check to make sure all rows are correct.
   */
  protected void SF001(int rowCount, int columnCount, int columnType, boolean forceAbort)
      throws StandardException, T_Fail {

    Transaction t = t_util.t_startTransaction();

    int segmentId = StreamContainerHandle.TEMPORARY_SEGMENT;
    Properties properties = new Properties();
    properties.put(RawStoreFactory.STREAM_FILE_BUFFER_SIZE_PARAMETER, "16384");

    // create row source
    T_RowSource testRowSource = null;
    if (forceAbort)
      testRowSource = new T_RowSource(rowCount, columnCount, columnType, forceAbort, t);
    else testRowSource = new T_RowSource(rowCount, columnCount, columnType, forceAbort, null);

    long startms = System.currentTimeMillis();

    long containerId = t.addAndLoadStreamContainer(segmentId, properties, testRowSource, null);

    long endms = System.currentTimeMillis();
    long time = endms - startms;
    REPORT("SF001 - write: " + time + "ms");

    // open the container, and start fetching...
    StreamContainerHandle scHandle = t.openStreamContainer(segmentId, containerId, false);

    // set up the template row
    DataValueDescriptor template[] = null;
    template = testRowSource.getTemplate();

    DataValueDescriptor readRow[] = null;
    readRow = testRowSource.getTemplate();
    segmentId = StreamContainerHandle.TEMPORARY_SEGMENT;
    int fetchCount = 0;
    startms = System.currentTimeMillis();
    ValueRow vReadRow = new ValueRow(readRow.length);
    while (scHandle.fetchNext(vReadRow)) {
      fetchCount++;
      // check to make sure the row is what we inserted.
      // this depends on T_RowSource generate identical rows.
      if (!vReadRow.toString().equals(template.toString()))
        throw T_Fail.testFailMsg(
            "Record's value incorrect, expected :"
                + template.toString()
                + ": - got :"
                + vReadRow.toString());
    }
    endms = System.currentTimeMillis();
    time = endms - startms;
    // check to make sure we got the right number of rows.
    if (fetchCount != rowCount)
      throw T_Fail.testFailMsg(
          "incorrect number of row fetched.  Expecting "
              + rowCount
              + " rows, got "
              + fetchCount
              + ", rows instead.");
    REPORT("SF001 - fetch: " + time + "ms");

    scHandle.close();

    t_util.t_commit(t);
    t.close();

    PASS(
        "SF001, rowCount = "
            + rowCount
            + ", columnCount = "
            + columnCount
            + ", clumn type: "
            + columnType);
  }