View Javadoc
1   package nl.tudelft.simulation.dsol.tutorial.section42;
2   
3   import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEvent;
4   import nl.tudelft.simulation.dsol.logger.Cat;
5   import nl.tudelft.simulation.dsol.simulators.DevsSimulatorInterface;
6   import nl.tudelft.simulation.jstats.distributions.DistContinuous;
7   import nl.tudelft.simulation.jstats.distributions.DistDiscrete;
8   import nl.tudelft.simulation.jstats.distributions.DistEmpiricalDiscreteLong;
9   import nl.tudelft.simulation.jstats.distributions.DistExponential;
10  import nl.tudelft.simulation.jstats.distributions.empirical.DiscreteEmpiricalDistribution;
11  import nl.tudelft.simulation.jstats.distributions.empirical.ProbabilityDensities;
12  import nl.tudelft.simulation.jstats.streams.StreamInterface;
13  
14  /**
15   * A Customer.
16   * <p>
17   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
19   * project is distributed under a three-clause BSD-style license, which can be found at
20   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
21   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
22   * </p>
23   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
24   */
25  public class Customer implements BuyerInterface
26  {
27      /** the simulator to schedule on. */
28      private DevsSimulatorInterface<Double> simulator = null;
29  
30      /** the retailer by whom we order our product. */
31      private SellerInterface retailer = null;
32  
33      /** the intervalTime between consequtive orders. */
34      private DistContinuous intervalTime = null;
35  
36      /** the orderBatchSize of an order. */
37      private DistDiscrete orderBatchSize = null;
38  
39      /**
40       * constructs a new Customer.
41       * @param simulator DevsSimulatorInterface&lt;Double&gt;; the simulator to schedule on
42       * @param retailer SellerInterface; the retailer to buy at. In more advanced examples, we would look up this retailer at a
43       *            yellow page.
44       */
45      public Customer(final DevsSimulatorInterface<Double> simulator, final SellerInterface retailer)
46      {
47          super();
48          this.simulator = simulator;
49          this.retailer = retailer;
50          StreamInterface stream = this.simulator.getModel().getStream("default");
51          this.intervalTime = new DistExponential(stream, 0.1);
52          DiscreteEmpiricalDistribution empDist = ProbabilityDensities.createDiscreteDistribution(new long[] {1, 2, 3, 4},
53                  new double[] {1.0 / 6.0, 1.0 / 3.0, 1.0 / 3.0, 1.0 / 6.0});
54          this.orderBatchSize = new DistEmpiricalDiscreteLong(stream, empDist);
55          this.createOrder();
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public void receiveProduct(final long amount)
61      {
62          this.simulator.getLogger().filter(Cat.DSOL).trace("receiveProduct: received " + amount);
63      }
64  
65      /**
66       * creates an order.
67       */
68      private void createOrder()
69      {
70          this.retailer.order(this, this.orderBatchSize.draw());
71          try
72          {
73              this.simulator.scheduleEvent(new SimEvent<Double>(this.simulator.getSimulatorTime() + this.intervalTime.draw(),
74                      this,"createOrder", null));
75          }
76          catch (Exception exception)
77          {
78              this.simulator.getLogger().always().error(exception, "createOrder");
79          }
80      }
81  }