/**
   * Executes example.
   *
   * @param args Command line arguments, none required.
   */
  public static void main(String[] args) {
    // Typedefs:
    // ---------
    // G -> GridFactory
    // CI1 -> GridInClosure
    // CO -> GridOutClosure
    // CA -> GridAbsClosure
    // F -> GridFunc

    // Data initialisation.
    Random rand = new Random();

    final int size = 20;

    Collection<Integer> nums = new ArrayList<Integer>(size);

    // Generate list of random integers.
    for (int i = 0; i < size; i++) {
      nums.add(rand.nextInt(size));
    }

    // Print generated list.
    X.println("Generated list:");

    F.forEach(nums, F.<Integer>print("", " "));

    // Retain all elements which value low than half generated list size.
    Collection<Integer> res =
        F.retain(
            nums,
            true,
            new P1<Integer>() {
              @Override
              public boolean apply(Integer i) {
                return i < size / 2;
              }
            });

    // Print result.
    X.println("\nResult list:");

    F.forEach(res, F.<Integer>print("", " "));

    // Retain first half of result list.
    F.retain(res, false, res.size() / 2);

    // Print result.
    X.println("\nResult list:");

    F.forEach(res, F.<Integer>print("", " "));
  }
Esempio n. 2
0
  /**
   * Executes example.
   *
   * @param args Command line arguments, none required.
   */
  public static void main(String[] args) {
    // Typedefs:
    // ---------
    // G -> GridFactory
    // CI1 -> GridInClosure
    // CO -> GridOutClosure
    // CA -> GridAbsClosure
    // F -> GridFunc

    // Data initialisation.
    Random rand = new Random();

    final int size = 20;

    Collection<Integer> nums = new ArrayList<Integer>(size);

    // Generate list of random integers.
    for (int i = 0; i < size; i++) {
      nums.add(rand.nextInt(size));
    }

    // Print generated list.
    X.println("Generated list:");

    F.forEach(nums, F.<Integer>print("", " "));

    // Get new unmodifiable collection with elements which value low than half generated list size.
    Collection<Integer> view =
        F.view(
            nums,
            new P1<Integer>() {
              @Override
              public boolean apply(Integer i) {
                return i < size / 2;
              }
            });

    // Print result.
    X.println("\nResult list:");

    F.forEach(view, F.<Integer>print("", " "));

    // Check for read only.
    try {
      view.add(12);
    } catch (Exception ignored) {
      X.println("\nView is read only.");
    }
  }
  /**
   * @param args Command arguments.
   * @throws GridException If failed.
   */
  public static void main(String[] args) throws GridException {
    // Starts grid.
    Grid grid = args.length == 0 ? G.start() : G.start(args[0]);

    try {
      // Create portfolio.
      GridCredit[] portfolio = new GridCredit[5000];

      Random rnd = new Random();

      // Generate some test portfolio items.
      for (int i = 0; i < portfolio.length; i++) {
        portfolio[i] =
            new GridCredit(
                50000 * rnd.nextDouble(), // Credit amount.
                rnd.nextInt(1000), // Credit term in days.
                rnd.nextDouble() / 10, // APR.
                rnd.nextDouble() / 20 + 0.02 // EDF.
                );
      }

      // Forecast horizon in days.
      int horizon = 365;

      // Number of Monte-Carlo iterations.
      int iter = 10000;

      // Percentile.
      double percentile = 0.95;

      // Mark the stopwatch.
      long start = System.currentTimeMillis();

      // Calculate credit risk and print it out.
      // As you can see the grid enabling is completely hidden from the caller
      // and it is fully transparent to him. In fact, the caller is never directly
      // aware if method was executed just locally or on the 100s of grid nodes.
      // Credit risk crdRisk is the minimal amount that creditor has to have
      // available to cover possible defaults.
      double crdRisk =
          grid.reduce(
              SPREAD,
              closures(grid.size(), portfolio, horizon, iter, percentile),
              new R1<Double, Double>() {
                /** Collected values sum. */
                private double sum;

                /** Collected values count. */
                private int count;

                /** {@inheritDoc} */
                @Override
                public boolean collect(Double e) {
                  sum += e;
                  count++;

                  return true;
                }

                /** {@inheritDoc} */
                @Override
                public Double apply() {
                  return sum / count;
                }
              });

      X.println(
          "Credit risk [crdRisk="
              + crdRisk
              + ", duration="
              + (System.currentTimeMillis() - start)
              + "ms]");
    }
    // We specifically don't do any error handling here to
    // simplify the example. Real application may want to
    // add error handling and application specific recovery.
    finally {
      // Stops grid.
      G.stop(true);
    }
  }