View Javadoc

1   /*
2    * @(#)InteractiveOrderHandlerStock.java Apr 25, 2004
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 is proprietary information
11   * of Delft University of Technology.
12   */
13  
14  package org.gscg.singleuser.handlers;
15  
16  import java.io.Serializable;
17  
18  import org.gscg.common.geo.CalculateLatLonDistance;
19  
20  import nl.tudelft.simulation.dsol.experiment.TimeUnit;
21  import nl.tudelft.simulation.dsol.experiment.TimeUnitInterface;
22  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
23  import nl.tudelft.simulation.dsol.formalisms.devs.SimEventInterface;
24  import nl.tudelft.simulation.logger.Logger;
25  import nl.tudelft.simulation.supplychain.actor.SupplyChainActor;
26  import nl.tudelft.simulation.supplychain.content.Bill;
27  import nl.tudelft.simulation.supplychain.content.Order;
28  import nl.tudelft.simulation.supplychain.content.OrderBasedOnQuote;
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.stock.StockInterface;
34  import nl.tudelft.simulation.supplychain.transport.TransportMode;
35  
36  /***
37   * The most simple form of an OrderHandler that takes the orders from stock is
38   * one that sends out an OrderConfirmation right away, and waits till the
39   * delivery date (should be minus the expected transportation time), picks the
40   * order, and ships it out as a Shipment. When the order is not available: wait
41   * one day and try again till it is available. <br>
42   * <br>
43   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
44   * Delft, the Netherlands. All rights reserved.
45   * 
46   * See for project information <a href="http://www.simulation.tudelft.nl/">
47   * www.simulation.tudelft.nl </a>.
48   * 
49   * The source code and binary code of this software is proprietary information
50   * of Delft University of Technology.
51   * 
52   * @author <a
53   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
54   *         Verbraeck </a>
55   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:56 $
56   * @since 1.0.0
57   */
58  public class InteractiveOrderHandlerStock extends OrderHandler
59  {
60  	/*** the serial version uid */
61  	private static final long serialVersionUID = 11L;
62  
63  	/*** the lat lon distance calculator to use */
64  	private CalculateLatLonDistance latLonDistanceCalculator = null;
65  
66  	/***
67  	 * Construct a new InteractiveOrderHandler that takes the goods from stock
68  	 * when ordered.
69  	 * 
70  	 * @param owner the owner of the handler
71  	 * @param stock the stock to use to handle the incoming order
72  	 */
73  	public InteractiveOrderHandlerStock(final SupplyChainActor owner,
74  			final StockInterface stock)
75  	{
76  		super(owner, stock);
77  		this.latLonDistanceCalculator = new CalculateLatLonDistance();
78  	}
79  
80  	/***
81  	 * @see nl.tudelft.simulation.content.HandlerInterface#handleContent(java.io.Serializable)
82  	 */
83  	public boolean handleContent(final Serializable content)
84  	{
85  		return true;
86  	}
87  
88  	/***
89  	 * Pick and ship the goods.
90  	 * 
91  	 * @param order the order that should be handled
92  	 */
93  	protected void ship(final Order order)
94  	{
95  		Product product = order.getProduct();
96  		double amount = order.getAmount();
97  		try
98  		{
99  			double day = TimeUnit.convert(1.0, TimeUnitInterface.DAY,
100 					super.owner.getSimulator());
101 			if (this.stock.getActualAmount(product) < amount)
102 			{
103 				// try again in one day
104 				Object[] args = new Object[]{order};
105 				SimEventInterface simEvent = new SimEvent(super.owner
106 						.getSimulatorTime()
107 						+ day, this, this, "ship", args);
108 				super.owner.getSimulator().scheduleEvent(simEvent);
109 			} else
110 			{
111 				// tell the stock that we got the claimed amount
112 				this.stock.changeClaimedAmount(order.getProduct(), -order
113 						.getAmount());
114 				// available: make shipment and ship to customer
115 				Cargo cargo = this.stock.getStock(product, amount);
116 				Shipment shipment = new Shipment(super.owner,
117 						order.getSender(), order.getInternalDemandID(), order,
118 						cargo);
119 
120 				shipment.setInTransit(true);
121 				// for now: handle it as a message and "fax" or "email" it
122 				// delay is of course the shipment time
123 				super.owner.sendContent(shipment, ((OrderBasedOnQuote) order)
124 						.getQuote().getProposedDeliveryDate()
125 						- ((OrderBasedOnQuote) order).getQuote()
126 								.getProposedShippingDate());
127 
128 				//
129 				// we substract the transportation costs
130 				double value = cargo.getValue();
131 				double weight = shipment.getOrder().getAmount()
132 						* shipment.getOrder().getProduct()
133 								.getAverageUnitWeight();
134 
135 				// calculate the costs based on the the lat lon distance!
136 				double transportCosts = TransportMode.PLANE.transportCosts(
137 						this.latLonDistanceCalculator.getDistance(shipment
138 								.getSender(), shipment.getReceiver()), weight);
139 				value -= transportCosts;
140 
141 				//
142 				// send a bill
143 				Bill bill = new Bill(super.owner, order.getSender(), order
144 						.getInternalDemandID(), order, super.owner
145 						.getSimulatorTime()
146 						+ 14.0 * day, /* cargo.getValue() */value, "SALE");
147 				super.owner.sendContent(bill, 0.002);
148 			}
149 		} catch (Exception e)
150 		{
151 			Logger.severe(this, "ship", e);
152 			return;
153 		}
154 	}
155 
156 	/***
157 	 * method sendOrder is called when a user confirms an order
158 	 * 
159 	 * @param order the order to send
160 	 */
161 	public void handleOrder(final Order order)
162 	{
163 
164 		this.stock.changeClaimedAmount(order.getProduct(), order.getAmount());
165 		// wait till the right time to start shipping
166 		try
167 		{
168 			double shippingTime = Math.max(
169 					super.owner.getSimulatorTime() + 0.1,
170 					((OrderBasedOnQuote) order).getQuote()
171 							.getProposedShippingDate());
172 			Object[] args = new Object[]{order};
173 			SimEventInterface simEvent = new SimEvent(shippingTime, this, this,
174 					"ship", args);
175 			super.owner.getSimulator().scheduleEvent(simEvent);
176 		} catch (Exception e)
177 		{
178 			Logger.severe(this, "handleContent", e);
179 		}
180 	}
181 }