Source code: desmoj/CustomerProcess.java
1 package desmoj;
2
3 import desmoj.*; // import all the core desmoj stuff
4 import desmoj.dist.*;
5
6 /**
7 * CustomerProcess is a kind of process representing a customer. He is fetching
8 * a certain number of products from an <code>Entrepot</code> to use them up.
9 * He is not doing anything special with them and so the products will be
10 * collected by the garbage collector. To change this behavior overwrite the
11 * method <code>lifeCycle()</code> in a subclass. The CustomerProcess can be
12 * viewed as the border of the simulated system, where the products disappear.
13 * It is intended to be used in conjunction with the <code>DemandProcess</code>.
14 * CustomerProcess has two main parameters to be specified
15 * (see the Constructor): The <code>Entrepot</code> where the products
16 * are to be fetched from and the number of demanded products.
17 *
18 * @author: Soenke Claassen
19 *
20 * @see: DemandProcess
21 *
22 * @version DESMO-J, Ver. 1.5 copyright (c) 2001 licensed under GNU GPL
23 */
24 public class CustomerProcess extends SimProcess {
25
26 /**
27 * The number of demanded products.
28 */
29 private int demand;
30
31 /**
32 * The <code>Entrepot</code> supplying the products this CustomerProcess is
33 * demanding.
34 */
35 private Entrepot entrepot;
36 /**
37 * Constructor for a CustomerProcess. The CustomerProcess is fetching a given
38 * number of products from the given <code>Entrepot</code>.
39 * @param owner desmoj.Model : The model this CustomerProcess is associated to.
40 * @param name java.lang.String : The name of this CustomerProcess.
41 * @param supplier desmoj.Entrepot : The <code>Entrepot</code> supplying the
42 * products this CustomerProcess is buying.
43 * @param quantity int : The number of products this CustomerProcess is
44 * fetching from the <code>Entrepot</code>.
45 * @param showInTrace boolean : Flag, if this CustomerProcess should produce
46 * a trace output or not.
47 */
48 public CustomerProcess(Model owner, String name, Entrepot supplier,
49 int quantity, boolean showInTrace)
50 {
51 super(owner, name, showInTrace); // make a SimProcess
52
53 // save the parameters
54 this.entrepot = supplier;
55 this.demand = quantity;
56 }
57 /**
58 * The CustomerProcess is fetching the given number of products from the given
59 * <code>Entrepot</code>. As there are not enough products available at the
60 * moment this CustomerProcess has to wait in a queue in the
61 * <code>Entrepot</code>. The obtained products are consumed (used up),
62 * that means all references to them are released and the product SimProcess is
63 * activated (as the process is not terminated yet), to end its lifeCycle and
64 * terminate by itself. Then the garbage collector can delete it.
65 * Therefore the <code>ComplexSimProcess</code>es are disassembled automatically.
66 */
67 public void lifeCycle() {
68
69 // fetch the given number of products from the Entrepot
70 SimProcess[] fetchedProducts = entrepot.removeProducts(demand);
71
72 // disassemble the ComplexSimProcesses to all their elements
73 for (int i = 0; i < fetchedProducts.length; i++)
74 {
75 if ( fetchedProducts[i] instanceof desmoj.ComplexSimProcess )
76 {
77 // all elements (SimProcesses) will be removed
78 // (and activated if not terminated yet)
79 ((ComplexSimProcess)fetchedProducts[i]).removeAllComponents();
80 }
81 }
82
83 // just to make sure the reference is really destroyed
84 fetchedProducts = null;
85 }
86 }