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 org.gscg.singleuser.production.ProductionSchedule;
18  
19  import nl.tudelft.simulation.dsol.experiment.TimeUnit;
20  import nl.tudelft.simulation.dsol.experiment.TimeUnitInterface;
21  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
22  import nl.tudelft.simulation.dsol.formalisms.devs.SimEventInterface;
23  import nl.tudelft.simulation.logger.Logger;
24  import nl.tudelft.simulation.supplychain.actor.Trader;
25  import nl.tudelft.simulation.supplychain.content.Bill;
26  import nl.tudelft.simulation.supplychain.content.Order;
27  import nl.tudelft.simulation.supplychain.content.OrderBasedOnQuote;
28  import nl.tudelft.simulation.supplychain.content.OrderConfirmation;
29  import nl.tudelft.simulation.supplychain.content.Shipment;
30  import nl.tudelft.simulation.supplychain.handlers.OrderHandler;
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   * A production handler is able to generate products for stock with a certain
37   * interval. Ideally this one is used by a supplier and simulates the products
38   * process. Furthermore this handler is able to chance its production pattern
39   * (time, amount).
40   * <p>
41   * (c) copyright 2005 <a href="http://www.simulation.tudelft.nl">Delft
42   * University of Technology </a>, the Netherlands. <br>
43   * See for project information <a
44   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
45   * 
46   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
47   * Delft, the Netherlands. All rights reserved.
48   * 
49   * See for project information <a href="http://www.simulation.tudelft.nl/">
50   * www.simulation.tudelft.nl </a>.
51   * 
52   * The source code and binary code of this software are proprietary information
53   * of Delft University of Technology.
54   * 
55   * @author <a
56   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
57   *         van Houten </a>
58   * @version $Revision: 1.2 $ $Date: 2005/08/09 15:43:41 $
59   * @since 1.1.10
60   */
61  public class PCShopOrderHandlerMake extends OrderHandler
62  {
63  	/*** the serial version uid */
64  	private static final long serialVersionUID = 10L;
65  
66  	/*** the production schedules */
67  	private ProductionSchedule[] schedules = null;
68  
69  	/***
70  	 * constructs a new PCShopOrderHandlerMake
71  	 * 
72  	 * @param trader the trader
73  	 * @param schedules the schedules
74  	 */
75  	public PCShopOrderHandlerMake(final Trader trader,
76  			final ProductionSchedule[] schedules)
77  	{
78  		super(trader, trader.getStock());
79  		this.schedules = schedules;
80  	}
81  
82  	/***
83  	 * @see nl.tudelft.simulation.content.HandlerInterface#handleContent(java.io.Serializable)
84  	 */
85  	public boolean handleContent(final Serializable content)
86  	{
87  		// get the order
88  		Order order = (Order) content;
89  
90  		// send out the confirmation
91  		OrderConfirmation orderConfirmation = new OrderConfirmation(
92  				super.owner, order.getSender(), order.getInternalDemandID(),
93  				order, OrderConfirmation.CONFIRMED);
94  		super.owner.sendContent(orderConfirmation, 0.00001);
95  
96  		// tell the stock that we claimed some amount
97  		this.stock.changeClaimedAmount(order.getProduct(), order.getAmount());
98  
99  		// wait till the right time to start shipping
100 		try
101 		{
102 			// TODO: get shipping times into all orders
103 			double shippingTime = Math.max(super.owner.getSimulatorTime(),
104 					((OrderBasedOnQuote) order).getQuote()
105 							.getProposedShippingDate());
106 			Object[] args = new Object[]{order};
107 			SimEventInterface simEvent = new SimEvent(shippingTime, this, this,
108 					"ship", args);
109 			super.owner.getSimulator().scheduleEvent(simEvent);
110 		} catch (Exception e)
111 		{
112 			Logger.severe(this, "handleContent", e);
113 			return false;
114 		}
115 		return true;
116 	}
117 
118 	/***
119 	 * @return returns the schedules
120 	 */
121 	public ProductionSchedule[] getSchedules()
122 	{
123 		return this.schedules;
124 	}
125 
126 	/***
127 	 * Pick and ship the goods.
128 	 * 
129 	 * @param order the order that should be handled
130 	 */
131 	protected void ship(final Order order)
132 	{
133 		Product product = order.getProduct();
134 		double amount = order.getAmount();
135 		try
136 		{
137 			double day = TimeUnit.convert(1.0, TimeUnitInterface.DAY,
138 					super.owner.getSimulator());
139 
140 			if (this.stock.getActualAmount(product) < amount)
141 			{
142 				boolean success = false;
143 				ProductionSchedule schedule = null;
144 				for (int i = 0; i < this.schedules.length; i++)
145 				{
146 					schedule = this.schedules[i];
147 					if (schedule.getProduct().getName().equals(
148 							product.getName()))
149 					{
150 						schedule.produce();
151 						success = true;
152 						break;
153 					}
154 				}
155 				if (success)
156 				{
157 					// try again after product time
158 					Object[] args = new Object[]{order};
159 					SimEventInterface simEvent = new SimEvent(super.owner
160 							.getSimulatorTime()
161 							+ schedule.getTime() + 0.0001, this, this, "ship",
162 							args);
163 					super.owner.getSimulator().scheduleEvent(simEvent);
164 				} else
165 				{
166 					Logger.severe(this, "ship",
167 							"Tried to produce for product: "
168 									+ product.getName()
169 									+ " but no schedule has been found.");
170 				}
171 			} else
172 			{
173 				// tell the stock that we got the claimed amount
174 				this.stock.changeClaimedAmount(order.getProduct(), -order
175 						.getAmount());
176 
177 				// available: make shipment and ship to customer
178 				Cargo cargo = this.stock.getStock(product, amount);
179 				Shipment shipment = new Shipment(super.owner,
180 						order.getSender(), order.getInternalDemandID(), order,
181 						cargo);
182 				shipment.setInTransit(true);
183 
184 				// TODO get the transportation mode from the shipment?
185 				super.owner.sendContent(shipment, TransportMode.PLANE
186 						.transportTime(shipment.getSender(), shipment
187 								.getReceiver()));
188 
189 				//
190 				// send a bill when the shipment leaves...
191 				Bill bill = new Bill(super.owner, order.getSender(), order
192 						.getInternalDemandID(), order, super.owner
193 						.getSimulatorTime()
194 						+ (14.0 * day), cargo.getValue(), "SALE");
195 				super.owner.sendContent(bill, 0.002);
196 			}
197 		} catch (Exception e)
198 		{
199 			Logger.severe(this, "ship", e);
200 			return;
201 		}
202 	}
203 }