View Javadoc

1   /*
2    * @(#) PCShopOrderHandlerMake.java Jun 20, 2005
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software are proprietary information
11   * of Delft University of Technology.
12   */
13  package org.gscg.singleuser.handlers;
14  
15  import java.io.Serializable;
16  
17  import nl.tudelft.simulation.dsol.experiment.TimeUnit;
18  import nl.tudelft.simulation.dsol.experiment.TimeUnitInterface;
19  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
20  import nl.tudelft.simulation.dsol.formalisms.devs.SimEventInterface;
21  import nl.tudelft.simulation.logger.Logger;
22  import nl.tudelft.simulation.supplychain.actor.Trader;
23  import nl.tudelft.simulation.supplychain.content.Bill;
24  import nl.tudelft.simulation.supplychain.content.Order;
25  import nl.tudelft.simulation.supplychain.content.OrderBasedOnQuote;
26  import nl.tudelft.simulation.supplychain.content.OrderConfirmation;
27  import nl.tudelft.simulation.supplychain.content.ProductionOrder;
28  import nl.tudelft.simulation.supplychain.content.Shipment;
29  import nl.tudelft.simulation.supplychain.handlers.OrderHandler;
30  import nl.tudelft.simulation.supplychain.handlers.ProductionOrderHandler;
31  import nl.tudelft.simulation.supplychain.product.Cargo;
32  import nl.tudelft.simulation.supplychain.product.Product;
33  import nl.tudelft.simulation.supplychain.transport.TransportMode;
34  
35  /***
36   * This handler receives an order and tries to collect the products for it. If
37   * there is not enough on stock, it creates a production order and waits till
38   * the right amount of products is produced.
39   * <p>
40   * (c) copyright 2005 <a href="http://www.simulation.tudelft.nl">Delft
41   * University of Technology </a>, the Netherlands. <br>
42   * See for project information <a
43   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
44   * 
45   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
46   * Delft, the Netherlands. All rights reserved.
47   * 
48   * See for project information <a href="http://www.simulation.tudelft.nl/">
49   * www.simulation.tudelft.nl </a>.
50   * 
51   * The source code and binary code of this software are proprietary information
52   * of Delft University of Technology.
53   * 
54   * @author <a
55   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
56   *         van Houten </a>
57   * @version $Revision: 1.1 $ $Date: 2005/08/09 15:43:41 $
58   * @since 1.1.10
59   */
60  public class ManufacturerOrderHandlerProduce extends OrderHandler
61  {
62  	/*** the serial version uid */
63  	private static final long serialVersionUID = 10L;
64  
65  	/***
66  	 * constructs a new ManufacturerOrderHandlerProduce
67  	 * 
68  	 * @param trader the trader
69  	 * @param handler the handler for production
70  	 */
71  	public ManufacturerOrderHandlerProduce(final Trader trader,
72  			final ProductionOrderHandler handler)
73  	{
74  		super(trader, trader.getStock());
75  		super.owner.addContentHandler(ProductionOrder.class, handler);
76  	}
77  
78  	/***
79  	 * @see nl.tudelft.simulation.content.HandlerInterface#handleContent(java.io.Serializable)
80  	 */
81  	public boolean handleContent(final Serializable content)
82  	{
83  		// get the order
84  		Order order = (Order) content;
85  
86  		// send out the confirmation
87  		OrderConfirmation orderConfirmation = new OrderConfirmation(
88  				super.owner, order.getSender(), order.getInternalDemandID(),
89  				order, OrderConfirmation.CONFIRMED);
90  		super.owner.sendContent(orderConfirmation, 0.00001);
91  
92  		// tell the stock that we claimed some amount
93  		this.stock.changeClaimedAmount(order.getProduct(), order.getAmount());
94  
95  		// perform a check to see whether we have enough of the product
96  
97  		boolean enoughOnStock = this.checkStock(order);
98  
99  		if (!enoughOnStock)
100 		{
101 			// start production; create production order
102 			// TODO make the amount more intelligent, take into account
103 			// previously order products, the different between actual stock,
104 			// and claimed amount and expected amount, etc.
105 			ProductionOrder productionOrder = new ProductionOrder(
106 					(Trader) this.owner, order.getInternalDemandID(), Math.max(
107 							super.owner.getSimulatorTime(),
108 							((OrderBasedOnQuote) order).getQuote()
109 									.getProposedShippingDate()), order
110 							.getProduct(), order.getAmount());
111 			super.owner.handleContent(productionOrder);
112 		}
113 
114 		// schedule the shipment
115 		double shippingTime = Math.max(super.owner.getSimulatorTime(),
116 				((OrderBasedOnQuote) order).getQuote()
117 						.getProposedShippingDate());
118 		Object[] args = new Object[]{order};
119 		SimEventInterface simEvent = new SimEvent(shippingTime, this, this,
120 				"ship", args);
121 
122 		try
123 		{
124 			super.owner.getSimulator().scheduleEvent(simEvent);
125 		} catch (Exception exception)
126 		{
127 			Logger.severe(this, "handleContent", exception);
128 		}
129 
130 		return true;
131 	}
132 
133 	/***
134 	 * Method checkStock checks the stock to see whether there is enough of the
135 	 * specified product on stock. If yes, true is returned, false otherwise.
136 	 * True means that a Shipment can be scheduled, false means that production
137 	 * needs to be started.
138 	 * 
139 	 * @param order the order
140 	 * @return returns true if enough on stock, false otherwise
141 	 */
142 	private boolean checkStock(final Order order)
143 	{
144 		Product product = order.getProduct();
145 		double amount = order.getAmount();
146 
147 		if (this.stock.getActualAmount(product) < amount)
148 		{
149 			return false;
150 		}
151 		return true;
152 	}
153 
154 	/***
155 	 * Pick and ship the goods.
156 	 * 
157 	 * @param order the order that should be handled
158 	 */
159 	protected void ship(final Order order)
160 	{
161 		Product product = order.getProduct();
162 		double amount = order.getAmount();
163 		try
164 		{
165 			double day = TimeUnit.convert(1.0, TimeUnitInterface.DAY,
166 					super.owner.getSimulator());
167 
168 			if (this.stock.getActualAmount(product) < amount)
169 			{
170 				// try again in one day
171 				Object[] args = new Object[]{order};
172 				SimEventInterface simEvent = new SimEvent(super.owner
173 						.getSimulatorTime()
174 						+ day, this, this, "ship", args);
175 				super.owner.getSimulator().scheduleEvent(simEvent);
176 			} else
177 			{
178 
179 				// tell the stock that we got the claimed amount
180 				this.stock.changeClaimedAmount(order.getProduct(), -order
181 						.getAmount());
182 
183 				// available: make shipment and ship to customer
184 				Cargo cargo = this.stock.getStock(product, amount);
185 				Shipment shipment = new Shipment(super.owner,
186 						order.getSender(), order.getInternalDemandID(), order,
187 						cargo);
188 				shipment.setInTransit(true);
189 
190 				// TODO get the transportation mode from the shipment?
191 				super.owner.sendContent(shipment, TransportMode.PLANE
192 						.transportTime(shipment.getSender(), shipment
193 								.getReceiver()));
194 
195 				//
196 				// send a bill when the shipment leaves...
197 				Bill bill = new Bill(super.owner, order.getSender(), order
198 						.getInternalDemandID(), order, super.owner
199 						.getSimulatorTime()
200 						+ (14.0 * day), cargo.getValue(), "SALE");
201 				super.owner.sendContent(bill, 0.002);
202 			}
203 		} catch (Exception e)
204 		{
205 			Logger.severe(this, "ship", e);
206 			return;
207 		}
208 	}
209 }