View Javadoc

1   /*
2    * @(#) Customer.java Dec 8, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.tutorial.section42;
11  
12  import java.rmi.RemoteException;
13  
14  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
15  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
16  import nl.tudelft.simulation.jstats.distributions.DistContinuous;
17  import nl.tudelft.simulation.jstats.distributions.DistCustom;
18  import nl.tudelft.simulation.jstats.distributions.DistDiscrete;
19  import nl.tudelft.simulation.jstats.distributions.DistExponential;
20  import nl.tudelft.simulation.jstats.streams.StreamInterface;
21  import nl.tudelft.simulation.logger.Logger;
22  
23  /***
24   * A Customer <br>
25   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
26   * University of Technology </a>, the Netherlands. <br>
27   * See for project information <a
28   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
29   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
30   * License (GPL) </a>, no warranty <br>
31   * 
32   * @version 1.0 Dec 8, 2003 <br>
33   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
34   *         Jacobs </a>
35   */
36  public class Customer implements BuyerInterface
37  {
38  	/*** the simulator to schedule on */
39  	private DEVSSimulatorInterface simulator = null;
40  
41  	/*** the retailer by whom we order our product */
42  	private SellerInterface retailer = null;
43  
44  	/*** the intervalTime between consequtive orders */
45  	private DistContinuous intervalTime = null;
46  
47  	/*** the orderBatchSize of an order */
48  	private DistDiscrete orderBatchSize = null;
49  
50  	/***
51  	 * constructs a new Customer
52  	 * 
53  	 * @param simulator the simulator to schedule on
54  	 * @param retailer the retailer to buy at. In more advanced examples, we
55  	 *        would look up this retailer at a yellow page.
56  	 * @throws RemoteException on network failure
57  	 *  
58  	 */
59  	public Customer(final DEVSSimulatorInterface simulator,
60  			final SellerInterface retailer) throws RemoteException
61  	{
62  		super();
63  		this.simulator = simulator;
64  		this.retailer = retailer;
65  
66  		StreamInterface stream = this.simulator.getReplication().getStream(
67  				"default");
68  		this.intervalTime = new DistExponential(stream, 0.1);
69  		this.orderBatchSize = new DistCustom(stream, new DistCustom.Entry[]{
70  				new DistCustom.Entry(1, 1.0 / 6.0),
71  				new DistCustom.Entry(2, 1.0 / 3.0),
72  				new DistCustom.Entry(3, 1.0 / 3.0),
73  				new DistCustom.Entry(4, 1.0 / 6.0)});
74  		this.createOrder();
75  	}
76  
77  	/***
78  	 * @see nl.tudelft.simulation.dsol.tutorial.section42.BuyerInterface
79  	 *      #receiveProduct(long)
80  	 */
81  	public void receiveProduct(final long amount)
82  	{
83  		Logger.finest(this, "receiveProduct", "received " + amount);
84  	}
85  
86  	/***
87  	 * creates an order
88  	 */
89  	private void createOrder()
90  	{
91  		this.retailer.order(this, this.orderBatchSize.draw());
92  		try
93  		{
94  			this.simulator
95  					.scheduleEvent(new SimEvent(this.simulator
96  							.getSimulatorTime()
97  							+ this.intervalTime.draw(), this, this,
98  							"createOrder", null));
99  		} catch (Exception exception)
100 		{
101 			Logger.warning(this, "createOrder", exception);
102 		}
103 	}
104 }