View Javadoc

1   /*
2    * @(#) Boat.java Jan 19, 2004
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.section45;
11  
12  import java.rmi.RemoteException;
13  
14  import nl.tudelft.simulation.dsol.SimRuntimeException;
15  import nl.tudelft.simulation.dsol.formalisms.Resource;
16  import nl.tudelft.simulation.dsol.formalisms.ResourceRequestorInterface;
17  import nl.tudelft.simulation.dsol.formalisms.process.Process;
18  import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
19  
20  /***
21   * A Boat <br>
22   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl"> Delft
23   * University of Technology </a>, the Netherlands. <br>
24   * See for project information <a href="http://www.simulation.tudelft.nl">
25   * www.simulation.tudelft.nl </a> <br>
26   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
27   * License (GPL) </a>, no warranty <br>
28   * 
29   * @version 1.0 Jan 19, 2004 <br>
30   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
31   *         Jacobs </a>
32   */
33  public class Boat extends Process implements ResourceRequestorInterface
34  {
35  	/*** the port to enter */
36  	private Port port = null;
37  
38  	/*** boat number */
39  	private static int number = 1;
40  
41  	/*** the description of the boat */
42  	private String description = "Boat(";
43  
44  	/***
45  	 * constructs a new Boat
46  	 * 
47  	 * @param simulator the simulator to schedule on
48  	 * @param port the port to sail to
49  	 */
50  	public Boat(final DEVSSimulator simulator, final Port port)
51  	{
52  		super(simulator);
53  		this.port = port;
54  		this.description = this.description + (Boat.number++) + ") ";
55  	}
56  
57  	/***
58  	 * @see nl.tudelft.simulation.dsol.formalisms.process.Process#process()
59  	 */
60  	public void process() throws SimRuntimeException, RemoteException
61  	{
62  		double startTime = this.simulator.getSimulatorTime();
63  
64  		//We seize one jetty
65  		this.port.getJetties().requestCapacity(1.0, this);
66  		this.suspend();
67  
68  		//Now we request 2 tugs
69  		this.port.getTugs().requestCapacity(2.0, this);
70  		this.suspend();
71  
72  		//Now we dock which takes 2 minutes
73  		this.hold(2.0);
74  
75  		//We may now release two tugs
76  		this.port.getTugs().releaseCapacity(2.0);
77  
78  		//Now we unload
79  		this.hold(14);
80  
81  		//Now we claim a tug again
82  		this.port.getTugs().requestCapacity(1.0, this);
83  		this.suspend();
84  
85  		//We may leave now
86  		this.hold(2.0);
87  
88  		//We release both the jetty and the tug
89  		this.port.getTugs().releaseCapacity(1.0);
90  		this.port.getJetties().releaseCapacity(1.0);
91  		System.out.println(this.toString() + "arrived at time=" + startTime
92  				+ " and left at time=" + this.simulator.getSimulatorTime()
93  				+ ". ProcessTime = "
94  				+ (super.simulator.getSimulatorTime() - startTime));
95  	}
96  
97  	/***
98  	 * @see nl.tudelft.simulation.dsol.formalisms.ResourceRequestorInterface
99  	 *      #receiveRequestedResource(double,
100 	 *      nl.tudelft.simulation.dsol.formalisms.Resource)
101 	 */
102 	public void receiveRequestedResource(final double requestedCapacity,
103 			final Resource resource)
104 	{
105 		this.resume();
106 	}
107 
108 	/***
109 	 * @see java.lang.Object#toString()
110 	 */
111 	public String toString()
112 	{
113 		return this.description;
114 	}
115 }